llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

armhf: defaults to emitting NEON instructions

Open sylvestre opened this issue 10 years ago • 20 comments

Bugzilla Link 30842
Version trunk
OS Linux
CC @jmolloy,@rengolin

Extended Description

Reported here: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=842143

To quote:

LLVM 3.8/3.9 default to emitting NEON instructions on armhf but it is optional in ARMv7 so LLVM is producing binaries that do not work on ARMv7 CPUs that lack NEON. This is causing build failures in packages that use LLVM on Debian buildds that lack NEON.

sylvestre avatar Oct 31 '16 12:10 sylvestre

clang/lib/Driver/Tools.cpp and clang/lib/Basics/Targets.cpp are for target setting parsing, validation and defaults. My guess is that gnutools::Assembler::ConstructJob sets fpu defaults for armv7. I browsed that file when I created patch to changed x86 default from pentium4 to i686. But I haven't read arm code carefully.

We have a TargetParser, which has the default FPU and extensions for each Arch/CPU. Those tables are the "source of truth" to make sure we have the same behaviour across all tools.

ARM devs don't usually understand why generic distributions would have to compile for least features supported. They think more in terms of project targeting specific board.

That's not true. Most ARM devs I know have been bitten by too-much-support problems on their highly modified cores. This is not about "knowing what's wrong", just having a different opinion.

But generic distributions need base code that runs with minimum features and then runtime optimization selection that can be done using functions specific attributes pretty easily (it can compile multiple versions from same function). Of course developers need to work more to support good performance when targeting heterogeneous processor targets.

Again, the problem here is not what distributions do or do not, or even why they do it. It's about the "default" case, and how harder it is to override it on a per-distro basis.

With GCC, you can build with --with-flags=foo and the default behaviour is overridden, but LLVM has no such facility. This has caused a wide-spread culture of overriding all defaults, for better or worse, and allowed distros to tailor the toolchain to their own benefits. But that also cause confusion to the user, that don't see the same code on different distros.

LLVM takes a different approach, where we hard-code the default on a toolchain level, so everyone gets the same code with the same flags. The side effect is that distros and other users have to use more flags by default.

If you consider how hard it is to interact with multiple GNU toolchains and their infinite number of default configurations, you can imagine why we will always have to have the flags anyway. If we had multiple defaults, then the complexity of the flag-space would be much worse.

On the particular case of VFP vs NEON, I have a slight preference towards VFP being the default on Linux, for mostly legacy reasons, but I certainly don't speak for the entire community.

I also understand why LLVM has chosen NEON, which is in line with the other default option policies (most relevant/useful/common cases), and I'm certainly not ready to make a proposal on my personal (weak) preference. Having to use extra flags is not a strong enough case for me to take on, as we already have to do that anyway.

cheers, --renato

rengolin avatar Nov 07 '16 20:11 rengolin

Comment from Pauli (he doesn't have an account yet)

clang/lib/Driver/Tools.cpp and clang/lib/Basics/Targets.cpp are for target setting parsing, validation and defaults. My guess is that gnutools::Assembler::ConstructJob sets fpu defaults for armv7. I browsed that file when I created patch to changed x86 default from pentium4 to i686. But I haven't read arm code carefully.

ARM devs don't usually understand why generic distributions would have to compile for least features supported. They think more in terms of project targeting specific board.

But generic distributions need base code that runs with minimum features and then runtime optimization selection that can be done using functions specific attributes pretty easily (it can compile multiple versions from same function). Of course developers need to work more to support good performance when targeting heterogeneous processor targets.

===

sylvestre avatar Nov 04 '16 13:11 sylvestre

My guess is that using the -mfpu args would override the default

sylvestre avatar Nov 04 '16 12:11 sylvestre

Ouch! :(

But surely you don't want to turn it off in the compiler source, because then people (like me) who use the compiler package in debian would get weird results when they tried to use -mfpu=neon ?

James

jmolloy avatar Nov 04 '16 12:11 jmolloy

I know it would suck but Debian focus more on portability than performance :/

sylvestre avatar Nov 04 '16 12:11 sylvestre

.. I mean debian of course.

jmolloy avatar Nov 04 '16 12:11 jmolloy

You want to disable NEON unconditionally? This will make a lot of code very pessimistic indeed - is that what you want for the FreeBSD base system? :/

jmolloy avatar Nov 04 '16 12:11 jmolloy

I was more thinking in the code to make sure that it is disabled for every packages :)

sylvestre avatar Nov 04 '16 12:11 sylvestre

Hi Sylvestre,

Use -mfpu=vfp3. -mfpu=neon is the default.

Cheers,

James

jmolloy avatar Nov 04 '16 12:11 jmolloy

How can we disable NEON instructions? thanks

sylvestre avatar Nov 04 '16 12:11 sylvestre

Hi,

The principle of least surprise also comes down in favour of LLVM's current behaviour - most chips have NEON, therefore enable NEON. If I target Cortex-A15, I expect NEON even though it doesn't have to have it.

This argument is even more poignant for UDIV/SDIV. This one is much more difficult to diagnose because it involves profiling your code, looking for calls to __aeabi_uidiv or the libgcc equivalent and realising that actually ARM has a divide instruction, it's just not mandatory in ARMv7!

This is why it's on by default, because diagnosing the performance degradation is much harder than looking at a SIGILL and seeing the instruction staring you in the face (which is what happens if you run on a pandaboard without compiling with -mcpu=cortex-a9).

James

jmolloy avatar Oct 31 '16 13:10 jmolloy

I disagree - performant should be the default because conformance issues due to a default are easier to find and diagnose than performance issues.

Defaulting to non-NEON will end up with users leaving performance on the floor and without domain-specific knowledge will really wonder why and make ARM/LLVM look bad.

The same thing happens to GCC already, so it'll look bad for both (or none).

The first argument we used in the past was more pragmatic than this (and why I haven't bothered to change): The number of non-NEON ARMv7 boards available in the wild is... Tegra3.

Admittedly, there are a good number of them still in use, but a tiny fraction of the overall number.

The second argument was even more pragmatic: The ARM manuals recommend implementations to have NEON. They don't go as far as AArch64, but NEON shows up in the core list of instructions and considerations, and is an integral part of the ISA. So, the burden of change should be in those that are different.

Another difference between GCC and LLVM: GCC defaults to Thumb2 for ARMv7 because of ARMv7M. LLVM interprets "ARMv7" as "ARMv7A" because that's what people normally mean by "ARMv7".

Now, the other pragmatic argument against LLVM's behaviour is the principle of least surprise. Linux environments are dominated by GNU and GCC, so however their tools behave is "the standard". From that point of view, if LLVM behaves differently, it will surprise users.

For example, just using "CC=clang" for a Tegra3 or an M4 boards will break many things.

rengolin avatar Oct 31 '16 13:10 rengolin

I disagree - performant should be the default because conformance issues due to a default are easier to find and diagnose than performance issues.

Defaulting to non-NEON will end up with users leaving performance on the floor and without domain-specific knowledge will really wonder why and make ARM/LLVM look bad.

jmolloy avatar Oct 31 '16 12:10 jmolloy

This is just a default. I don't understand why it would be considered a bug for the default options to not be as conservative as possible.

Clang Bug [noun]: Something we do that GCC doesn't. A fault in the compiler.

rengolin avatar Oct 31 '16 12:10 rengolin

Hi Sylvestre,

This decision was consciously taken many many years ago (2.x or earlier) before my time.

I agree with the assessment that VFP needs to be the default, but so far had no pressure to do that from anyone, even on Linux.

I'll assess the situation, talk to the people involved and reply back. But whatever we do, bear in mind that this will be a slow move, so very unlikely to be present in 4.0 (though I will try).

cheers, --renato

PS: The bug report is incorrect that 3.7 doesn't default to NEON. What is probably happening is that 3.8 can vectorise some loop that 3.7 can't, thus some NEON instructions are created where none existed.

PS2: Until this becomes the rule of law, Debian should always build with "-mfpu=vfpv3" for ARMv7.

rengolin avatar Oct 31 '16 12:10 rengolin

Hi,

This is just a default. I don't understand why it would be considered a bug for the default options to not be as conservative as possible.

Yes, NEON is optional in ARMv7. The default target for ARMv7 is Cortex-A8, which may or may not have NEON (but most often does).

If you want to target a core where NEON is optional and the core happens not to have NEON, you should use the -mfpu=vfp3 flag to explicitly tell the compiler.

Cheers,

James

jmolloy avatar Oct 31 '16 12:10 jmolloy

https://godbolt.org/z/MWahnWf5s

GCC 10.5.0 seem default to emit NEON also.

CC @pinskia

hstk30-hw avatar Mar 06 '25 06:03 hstk30-hw

https://godbolt.org/z/MWahnWf5s

GCC 10.5.0 seem default to emit NEON also.

Nope, that compiler was configured with --with-fpu=neon which enables neon.

Note that compiler was also configured to default to thumb2: --with-mode=thumb

Full configure line:

Configured with: /opt/.build/arm-unknown-linux-gnueabihf/src/gcc/configure --build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu --target=arm-unknown-linux-gnueabihf --prefix=/opt/compiler-explorer/arm/gcc-10.5.0/arm-unknown-linux-gnueabihf --exec_prefix=/opt/compiler-explorer/arm/gcc-10.5.0/arm-unknown-linux-gnueabihf --with-sysroot=/opt/compiler-explorer/arm/gcc-10.5.0/arm-unknown-linux-gnueabihf/arm-unknown-linux-gnueabihf/sysroot --enable-languages=c,c++,fortran --with-arch=armv7-a --with-fpu=neon --with-float=hard --enable-__cxa_atexit --disable-libmudflap --enable-libgomp --enable-libssp --enable-libquadmath --enable-libquadmath-support --enable-libsanitizer --disable-libmpx --with-gmp=/opt/.build/arm-unknown-linux-gnueabihf/buildtools --with-mpfr=/opt/.build/arm-unknown-linux-gnueabihf/buildtools --with-mpc=/opt/.build/arm-unknown-linux-gnueabihf/buildtools --with-isl=/opt/.build/arm-unknown-linux-gnueabihf/buildtools --disable-lto --without-zstd --enable-threads=posix --enable-target-optspace --disable-plugin --disable-nls --disable-multilib --with-local-prefix=/opt/compiler-explorer/arm/gcc-10.5.0/arm-unknown-linux-gnueabihf/arm-unknown-linux-gnueabihf/sysroot --enable-long-long --with-mode=thumb

pinskia avatar Mar 06 '25 06:03 pinskia

clang/lib/Driver/Tools.cpp and clang/lib/Basics/Targets.cpp are for target setting parsing, validation and defaults. My guess is that gnutools::Assembler::ConstructJob sets fpu defaults for armv7. I browsed that file when I created patch to changed x86 default from pentium4 to i686. But I haven't read arm code carefully.

We have a TargetParser, which has the default FPU and extensions for each Arch/CPU. Those tables are the "source of truth" to make sure we have the same behaviour across all tools.

ARM devs don't usually understand why generic distributions would have to compile for least features supported. They think more in terms of project targeting specific board.

That's not true. Most ARM devs I know have been bitten by too-much-support problems on their highly modified cores. This is not about "knowing what's wrong", just having a different opinion.

But generic distributions need base code that runs with minimum features and then runtime optimization selection that can be done using functions specific attributes pretty easily (it can compile multiple versions from same function). Of course developers need to work more to support good performance when targeting heterogeneous processor targets.

Again, the problem here is not what distributions do or do not, or even why they do it. It's about the "default" case, and how harder it is to override it on a per-distro basis.

With GCC, you can build with --with-flags=foo and the default behaviour is overridden, but LLVM has no such facility. This has caused a wide-spread culture of overriding all defaults, for better or worse, and allowed distros to tailor the toolchain to their own benefits. But that also cause confusion to the user, that don't see the same code on different distros.

LLVM takes a different approach, where we hard-code the default on a toolchain level, so everyone gets the same code with the same flags. The side effect is that distros and other users have to use more flags by default.

If you consider how hard it is to interact with multiple GNU toolchains and their infinite number of default configurations, you can imagine why we will always have to have the flags anyway. If we had multiple defaults, then the complexity of the flag-space would be much worse.

On the particular case of VFP vs NEON, I have a slight preference towards VFP being the default on Linux, for mostly legacy reasons, but I certainly don't speak for the entire community.

I also understand why LLVM has chosen NEON, which is in line with the other default option policies (most relevant/useful/common cases), and I'm certainly not ready to make a proposal on my personal (weak) preference. Having to use extra flags is not a strong enough case for me to take on, as we already have to do that anyway.

cheers, --renato

Can we give a warning on llvm if neon is enabled by default and give a hint to developers?

Zhenhang1213 avatar Mar 18 '25 02:03 Zhenhang1213

clang/lib/Driver/Tools.cpp and clang/lib/Basics/Targets.cpp are for target setting parsing, validation and defaults. My guess is that gnutools::Assembler::ConstructJob sets fpu defaults for armv7. I browsed that file when I created patch to changed x86 default from pentium4 to i686. But I haven't read arm code carefully.

We have a TargetParser, which has the default FPU and extensions for each Arch/CPU. Those tables are the "source of truth" to make sure we have the same behaviour across all tools.

ARM devs don't usually understand why generic distributions would have to compile for least features supported. They think more in terms of project targeting specific board.

That's not true. Most ARM devs I know have been bitten by too-much-support problems on their highly modified cores. This is not about "knowing what's wrong", just having a different opinion.

But generic distributions need base code that runs with minimum features and then runtime optimization selection that can be done using functions specific attributes pretty easily (it can compile multiple versions from same function). Of course developers need to work more to support good performance when targeting heterogeneous processor targets.

Again, the problem here is not what distributions do or do not, or even why they do it. It's about the "default" case, and how harder it is to override it on a per-distro basis. With GCC, you can build with --with-flags=foo and the default behaviour is overridden, but LLVM has no such facility. This has caused a wide-spread culture of overriding all defaults, for better or worse, and allowed distros to tailor the toolchain to their own benefits. But that also cause confusion to the user, that don't see the same code on different distros. LLVM takes a different approach, where we hard-code the default on a toolchain level, so everyone gets the same code with the same flags. The side effect is that distros and other users have to use more flags by default. If you consider how hard it is to interact with multiple GNU toolchains and their infinite number of default configurations, you can imagine why we will always have to have the flags anyway. If we had multiple defaults, then the complexity of the flag-space would be much worse. On the particular case of VFP vs NEON, I have a slight preference towards VFP being the default on Linux, for mostly legacy reasons, but I certainly don't speak for the entire community. I also understand why LLVM has chosen NEON, which is in line with the other default option policies (most relevant/useful/common cases), and I'm certainly not ready to make a proposal on my personal (weak) preference. Having to use extra flags is not a strong enough case for me to take on, as we already have to do that anyway. cheers, --renato

Can we give a warning on llvm if neon is enabled by default and give a hint to developers without setting fpu

Zhenhang1213 avatar Mar 18 '25 03:03 Zhenhang1213