Issue 243260
Summary [X86] '-march=native' is not always rejected when running on non-x86 CPU
Labels clang:driver
Assignees
Reporter nathanchance
    When cross compiling for x86 (like from an aarch64 machine), `-march=native` should never work on a non-x86 CPU but it is not always rejected:

```
$ uname -m
aarch64

$ clang --target=x86_64-linux-gnu -march=native -c -o /dev/null -x c /dev/null
'+crc' is not a recognized feature for this target (ignoring feature)
'+fp-armv8' is not a recognized feature for this target (ignoring feature)
'+lse' is not a recognized feature for this target (ignoring feature)
'+neon' is not a recognized feature for this target (ignoring feature)
'+sha2' is not a recognized feature for this target (ignoring feature)
'+sha3' is not a recognized feature for this target (ignoring feature)
'-rand' is not a recognized feature for this target (ignoring feature)
'-sve' is not a recognized feature for this target (ignoring feature)
```

In this case, `getX86TargetCPU()` calls `llvm::sys::getHostCPUName()`, which ultimately calls `getHostCPUNameForARMFromComponents()`, which happens to return `generic` for this environment, instead of rejecting its invalid use outright. Later, `getX86TargetFeatures()` calls `llvm::sys::getHostCPUFeatures()`, which expands the `aarch64` host features, which are obviously invalid.

If the CPU is properly resolved, there is a nice error:

```
$ clang --target=x86_64-linux-gnu -march=native -c -o /dev/null -x c /dev/null
error: unknown target CPU 'apple-m1'
note: valid target CPU values are: nocona, core2, penryn, bonnell, atom, silvermont, slm, goldmont, goldmont-plus, ...
```

This appears to be a known issue per `getX86TargetCPU()`:

```
// FIXME: Reject attempts to use -march=native unless the target matches
// the host.
```

This causes issues when using the `CONFIG_X86_NATIVE_CPU` option in the Linux kernel. If `-march=native` was outright rejected when not running on an x86 machine, the existing `CONFIG_CC_HAS_MARCH_NATIVE` check would prevent this option from being selected.

>From a brief survey, other architecture drivers in `clang/lib/Driver/ToolChains/Arch` might have similar problems but I have just mentioned x86 for now.

Cc @nickdesaulniers 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to