[PATCH] riscv: Fix alignment-ignorant memcpy implementation

Vineet Gupta vineetg@rivosinc.com
Mon Mar 4 19:30:28 GMT 2024



On 3/4/24 11:07, Adhemerval Zanella Netto wrote:
>
> On 04/03/24 16:04, Vineet Gupta wrote:
>>
>> On 3/4/24 10:30, Adhemerval Zanella Netto wrote:
>>> On 04/03/24 15:28, Adhemerval Zanella Netto wrote:
>>>> On 04/03/24 15:24, Andreas Schwab wrote:
>>>>> On Mär 04 2024, Adhemerval Zanella wrote:
>>>>>
>>>>>> Also, there is no reason why the implementation can't be coded in C,
>>>>>> since it uses only normal registers and the compiler is able to
>>>>>> generate code as good as the assembly implementation.  I have not
>>>>>> checked the performance, but the C implementation uses the same
>>>>>> strategies, the generate code with gcc 13 seems straightforward, and
>>>>>> the tail code also avoid byte-operations.
>>>>> RISC-V is a strict-alignment target, and there is a non-zero chance that
>>>>> the C compiler messes this up.
>>>>>
>>>> I am aware, but there is other project that successfully provides C 
>>>> implementation (musl, for instance, where it optimizes for aligned
>>>> access) so I think if this implementation does have any mistake this 
>>>> should not be considered a blocked for this change.
>>> Maybe we should force -mno-strict-align for the unaligned implementation.
>> That alone might not be sufficient. RISC-V gcc gates alignment with an
>> additional cpu tune param and latter overrides the usual
>> -m[no-]strict-align: meaning you could have more strict alignment but
>> not any less. The default -mtune is rocket which penalizes unaligned access.
> How does gcc optimization and strict-align plays with __may_alias__ it
> would still tries to generated aligned access in this case?

I'm not sure about the semantics of this attribute. However gcc backend
effectively enforces -mstrict-align for the default -mtune/-mcpu
overriding -mno-strict-align.

  riscv_slow_unaligned_access_p = (cpu->tune_param->slow_unaligned_access
                   || TARGET_STRICT_ALIGN);

/* Implement TARGET_SLOW_UNALIGNED_ACCESS.  */

static bool
riscv_slow_unaligned_access (machine_mode, unsigned int)
{
  return riscv_slow_unaligned_access_p;
}


More information about the Libc-alpha mailing list