[PATCH] riscv: Fix alignment-ignorant memcpy implementation

Alexander Monakov amonakov@ispras.ru
Mon Mar 4 20:13:22 GMT 2024


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;
}

Alexander


More information about the Libc-alpha mailing list