[llvm-dev] [RFC] New Clang target selection options for ARM/AArch64

Renato Golin via llvm-dev llvm-dev at lists.llvm.org
Mon Sep 24 13:51:01 PDT 2018


On Fri, 21 Sep 2018 at 11:06, David Spickett via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> Below is a document detailing changes we'd like to make to Clang/LLVM to improve the usability of the target options for ARM and AArch64.

Hi David,

This is *awesome*. Thanks for such a detailed analysis!


> In this RFC we propose changes to ARM and AArch64 target selection. With the top level goals to:
> - validate that given options make sense within architectural restrictions
> - make option discovery and documentation easier
> - unify the list of extensions that command lines and asm directives use
> - bring the options closer to GCC's where appropriate

One additional goal we had in the past, when we first wrote the
TargetParser was to use the *existing* target description table-gen
files to generate the parser tables.

This means new changes to cores, sub-arches, and fixes to existing
ones will *automatically* be translated to command line and assembly
parsing.



> Proposed solution
> ------------------
>
> ARM and AArch64:
> - Make the TargetParser the single source for extension names, removing the AsmParser tables.
> - Reject unknown extension names with a diagnostic that includes a list of valid extensions for that architecture/CPU.
> - Reject invalid combinations of architecture/CPU and extensions with an error diagnostic.
> - Add independent subtarget features for each extension so that v8.x+1-a extensions can be used individually with earlier v8.x-a architectures where allowed.

SGTM.

> - Emit a warning when a mandatory feature of the base architecture is enabled with '+extension', or disabled with '+noextension'. (and ignore the option)
> - Errors caused by the solution above should be able to be downgraded to warnings with the usual -W* options. This applies only to cases where there is a reasonable interpretation of the options chosen.

I'd be more comfortable if these weren't enabled by default, but were
present in -Wall.

Writing generic and precise build systems is a nightmare, which is the
biggest reason why compilers generally ignore nonsense options
silently.


> ARM:
> - Allow all possible ARM extensions in the '.arch_extension' directive, without the '+' syntax
> (allow them to be recognised, they could still be rejected for compatibility).
> - Reject invalid mfpu and march/mcpu combinations with an error diagnostic.
> - Reject invalid arch/cpu and extension combinations with an error diagnostic.

SGTM.

> - Add an 'auto' value for -mfpu and make it the default. Meaning that the FPU is implied by mcpu/march. If mfpu is not auto, it should override other options and a warning should be emitted.

I'd have assumed -mfpu is already "auto" by default. Or is this to
just override a previous option?

ex: clang -mcpu cortex-a8 -mfpu vfp4 -mfpu auto -> defaults back to VFP3.



> Optional features
> -----------------
>
> AArch64:
> - add the '.arch_extension' directive, with the same behaviour as ARM (no '+', one extension per directive). This brings Clang in line with GCC which has this directive for both architectures. Clang does however allow you to achieve the same thing by using '+' with '.arch'.
>
> ARM:
> - Allow '+' in '.arch' and '.cpu'. GCC does not allow this, but it would make ARM/AArch64 more consistent within Clang.

I see no reason to be inconsistent with GNU tools here. We can have
more, but we should not have less or different behaviour.


> Use of Table-gen
> ================
>
> We think the benefits outweigh the disadvantages in this case.

Agreed!


> To do this, we would need to move TargetParser to break the cyclic dependency of LLVMSupport -> llvm-tblgen -> LLVMSupport. There are 2 options for this:
> 1. create a new LLVMTargetParser library that contains all parsers for architectures that use it.
> 2. put the TargetParser for each backend in the library group for that backend. This requires one of:
>     * Relaxing the requirement that target parsers must be built even if the backend is not.
>     * Modifying the CMake scripts to build the target parsers even if the backend is not being built.
>
> Option 1 is simpler but option 2 would allow us to make use of the existing tablegen files in the backends so it is preferred.

Option 1 makes everyone pay the cost and can be a lot harder to make
it flexible and "zero-cost". This is the reason why it was changed
from a class-based model to a static function / table model.

I had a go at option 2 years ago and it works. You need to fiddle a
bit with the CMake file in lib/Targets (to prepare the inc files even
if targets aren't being built, because Clang needs to use it for all
supported targets regardless).

It wasn't upstreamed because the hard part is to re-use the existing
table-gen files for a new back-end, which would generate the tables.
Not so much writing the new back-end, but making sure the data we need
isn't redundant or contradictory (which it was both) across all
table-gen files. We also had to add new options to the targets (define
new classes, etc) which were solely used by the parser, so were harder
to justify on its own and needed a much more extensive validation than
we had bandwidth for.


> Consider this AArch64 march:
> -march=armv8.4-a+crypto+nosha2
>
> The base arch is armv8.4-a, the crypto extension turns on AES/SHA2/SHA3/SM4. The nosha2 disables SHA2/SHA3 (since SHA3 is dependant on SHA2). Each of these features has an ACLE feature test macro, so Clang needs to know that nosha2 also disables SHA3.

Is this complex logic done by GCC's front-end as well?

It would be pretty cool to have it smart like that, but we also have
to be careful to have a rock solid model before improving on GCC's
(potentially broken) functionality, and hopefully someone talking to
them on the side.

The amount of noise that comes every time we change the command line
options interpretation is non-trivial. :)


> Errors:
> - unknown extension in an assembly directive (currently fails silently)

IIRC, this is by design.

Imagine a macro that defines .cpu in an asm file to multiple things,
and the rest of the file has .fpu all over the place, with support for
all .cpu options, but with the guarantee that those functions will
only be compiled/executed if the .cpu is correct.

This may sound weird, but some libraries (ex. unwind) actually depend
on weird behaviour like that.


> - extension incompatible with base arch, message shows the base arch it requires.
> - extension requires another which is disabled later, message shows which one is required.
> - extension requires another which is not enabled, message shows requirements.
> - ARM mfpu option is not 'auto' and is incompatible with the base arch, message shows list of valid FPUs.

Define "incompatible". Older Arm cores could have new features that
wasn't even define in its own standard because manufacturers upgraded
the extra but not the core.

I'm happy to have errors for things that are impossible, like "ARMv5
AArch64" or enabling and disabling intersecting groups that cannot be
represented in the compiler.

I'm happy to have warnings, possibly only under -Wall, for nonsense
options like "ARMv5 VFP4" or "ARMv8A IWMMX".


> Warnings:
> - ARM mfpu option is not auto and another option implies a different FPU than the mfpu value. The mfpu value will be used, and the message will show what was overridden.

This is nice.

> - mandatory feature of the base arch is enabled with '+' (option is redundant so is ignored)

Maybe under -Wall?

> - mandatory feature of a base arch is disabled with '+no<feature>' (option makes no sense so the extension remains enabled)

Arm is a flexible architecture, and build systems are crazy. This will
likely confuse a lot of builds in the wild.

I'd avoid it unless in -Wall.


> .arch_extension Directive
> =========================
>
> We can handle this in a few of ways:
> - Remove .arch_extension in favour of .arch. This conflicts with the option above to add it to AArch64 to bring us in line with GCC, and will break a lot of code written for older versions of Clang.

.arch_extension was implemented because GCC does it. I'm not sure what
you mean by that, but I'm not happy with removing it, as it will break
scores of assembly files out there.

> - Track the current base target, as implied by the command line or the last .arch/.cpu directive. This makes the directives as similar to the command lines as they can be without breaking backwards compatibility.

This makes sense, but will likely require changes in a lot of existing
low-level assembly files, which choose a generic .cpu and vary
.fpu/.arch_extension to implement independent functionality (like
unwinders).

If you read the GNU manuals, the assembly directives is more to allow
the assembler to relax checks than enforce them more.

I personally like strong checks, but the problems we have with inline
assembly will come crashing in assembly files if we start tightening
the checks there, too.

It's a worthy long goal, but it's a loooong goal and you don't want
your current TargetParser work to depend on that.


> $ ./clang --target=arm-arm-none-eabi -march=armv7-m -mfpu=neon-fp16 -c /tmp/test.c -o /tmp/test.o
> (should be invalid but is allowed)
>
> $ ./arm-eabi-gcc -march=armv7-m -mfpu=neon-fp16 -c /tmp/test.c -o /tmp/test.o
> (same example given for Clang above, should be invalid)

If both are allowed, I'd recommend you not to change it in this
current pass. Let's get the parser fixed before changing overall
behaviour.


> Dependencies within extensions are not checked. For example crypto requires simd, but it can be disabled in the same march option.
>
> $ ./clang --target=aarch64-arm-none-eabi -march=armv8-a+crypto+nosimd -c /tmp/test.c -o /tmp/test.o
>
> Extensions are rejected if not recognised but not checked for compatibility. Hence the Clang crypto/simd example above is allowed with GCC too.
>
> $ ./aarch64-elf-gcc -march=armv8-a+crypto+nosimd -c /tmp/test.c -o /tmp/test.o
> (should not be allowed)

This is unlikely to change, let alone in the time frame of your work.

I strongly recommend that you do not change *any* user-facing
behaviour until the underlying parser changes are done and released
upstream.

-- 
cheers,
--renato


More information about the llvm-dev mailing list