[PATCH] riscv: Fix alignment-ignorant memcpy implementation

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Mon Mar 4 20:22:57 GMT 2024



On 04/03/24 17:13, Alexander Monakov wrote:
> 
> On Mon, 4 Mar 2024, Adhemerval Zanella Netto wrote:
> 
>>>> How does gcc optimization and strict-align plays with __may_alias__ it
>>>> would still tries to generated aligned access in this case?
> 
> may_alias does not imply reduced alignment: this is necessary to avoid
> penalizing correct code that forms properly aligned aliased pointers.
> 
> Like with may_alias, you can typedef a reduced-alignment type:
> 
> typedef op_t misal_op_t __attribute__((aligned(1)));
> 
>> Right, so the question is whether compiler will really mess the unaligned
>> code using __may_alias__ and if we really should keep the implementation as
>> a assembly one.
> 
> As always, if you lie to the compiler, you get to keep the pieces.
> 
> The solution here, if you know that a particular instruction works fine
> to perform a misaligned load, and the compiler doesn't expose it, is
> to make a simple asm wrapper:
> 
> static inline
> op_t load(void *p)
> {
>     typedef op_t misal_op_t __attribute__((aligned(1)));
>     op_t r;
>     asm("lw %0, %1" : "=r"(r) : "m"(*(misal_op_t *)p));
>     return r;
> }
> 

Sigh, these kind of extra hackery that lead to just keep the implementation
as assembly.


More information about the Libc-alpha mailing list