Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

target-cpu=native should also check all the target-features #54688

Closed
GabrielMajeri opened this issue Sep 30, 2018 · 3 comments · Fixed by #80749
Closed

target-cpu=native should also check all the target-features #54688

GabrielMajeri opened this issue Sep 30, 2018 · 3 comments · Fixed by #80749
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@GabrielMajeri
Copy link
Contributor

Because LLVM doesn't know what target-cpu=native is, we currently convert this to an actual CPU family name, relying on the llvm::sys::getHostCPUName function. So if the current CPU's family is Haswell, we convert target-cpu=native to target-cpu=haswell.

As @EFanZh noticed here and here, some Intel Pentiums belong to the Haswell microarch, but they lack AVX.


Here are the flags Clang generates for -march=native:

$ clang -E - -march=native -###
-target-cpu skylake -target-feature +sse2 -target-feature +cx16 -target-feature +sahf
...
-target-feature -avx512ifma -target-feature -avx512dq

Not only does it set the right target-cpu for optimization purposes, it actually manually enables / disables all the features available / not available on the host. This ensures no matter what the microarch is, only available features will be used.


We should probably use the llvm::sys::getHostCPUFeatures to manually get a list of supported target features. This is what Clang does.

I think this issue will supersede both #38218 and #48464. If we can fix this, we will never generate code with unavailable target features.

Reproducing these errors is pretty hard, unless you have a machine with a Haswell Intel Pentium.
I think it might be possible to reproduce this in QEMU with -cpu Haswell,-avx

@nikic nikic added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Dec 9, 2018
@gnzlbg
Copy link
Contributor

gnzlbg commented Apr 23, 2019

We should probably use the llvm::sys::getHostCPUFeatures to manually get a list of supported target features. This is what Clang does.

Alternatively, we could also use the is_{arch}_feature_detected! macros to do this. I'll work on this.

@gendx
Copy link

gendx commented Jan 3, 2020

To add another example, target-cpu=native doesn't detect the aes feature on broadwell (see #67836).

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 3, 2020
@as-com
Copy link
Contributor

as-com commented Jan 6, 2021

I submitted a PR for a fix: #80749

@bors bors closed this as completed in c87ef0a Jan 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants