Re: [PATCH] LoongArch: Drop -ffreestanding from CFLAGS

From: Nick Desaulniers
Date: Fri Aug 04 2023 - 12:16:12 EST


On Fri, Aug 4, 2023 at 8:56 AM Xi Ruoyao <xry111@xxxxxxxxxxx> wrote:
>
> On Fri, 2023-08-04 at 08:46 -0700, Nick Desaulniers wrote:
> > > Not all processors support unaligned access, so we need the
> > > alternative mechanism to select memset/memcpy/memmove implementations.
> > > If remove -ffreestanding, the builtin implementation cannot be used on
> > > all hardware.
> >
> > That sounds like a compiler bug in that compiler's implementation of
> > string.h builtins then; it should default to the safest implementation
> > (aligned accesses) until instructed otherwise. Have you filed a bug
> > against your compiler vendor for which compiler you observe that
> > behavior from?
>
> AFAIK there is no such bug in GCC.
>
> But GCC indeed has a bug about builtin expansion: it generates really
> stupid code for __builtin_memcpy and friends. See
> https://gcc.gnu.org/PR109465. The bug is fixed for GCC 14, but GCC 14
> won't be released soon.

Perhaps -fno-builtin-* flags may be of help here, and more precise an
incision than the blunt -ffreestanding.

>
> > At the very least, there should be a comment above the addition of
> > -ffreestanding justifying why it's being used, probably with a link to
> > the above bug report.
> >
> > I would expect either -mcpu or perhaps some other -m flag to guide the
> > compiler when it is safe to emit memcpy (and friends) in terms of
> > unaligned access or not.
>
> It's controlled by -m{no-,}strict-align. LoongArch kernel defaults to -
> mstrict-align, with this the compiler should not generate unaligned
> access. -mno-strict-align is hidden behind CONFIG_EXPERT (FWIW I
> personally dislike the decision to hide it).

If GCC generates unaligned accesses in its implementation of
__builtin_memcpy when -mstrict-align is set, then that is a bug in GCC
and should be reported.

Clang users should not have to pay for such mistakes.

>
> --
> Xi Ruoyao <xry111@xxxxxxxxxxx>
> School of Aerospace Science and Technology, Xidian University



--
Thanks,
~Nick Desaulniers