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

rustc does not detect CPU features correctly when target-cpu=native on Zen 3 #80633

Closed
as-com opened this issue Jan 3, 2021 · 2 comments
Closed
Labels
C-bug Category: This is a bug.

Comments

@as-com
Copy link
Contributor

as-com commented Jan 3, 2021

I (actually, @tykosa, who managed to get a Ryzen 5 5600X) tried this code:

#[cfg(target_feature = "avx")]
fn foo() {
    println!("hi");
}

fn main() {
    foo();
}

I expected to see this happen: the compiler detects that the CPU has AVX support and compiles the code, like it does on other CPUs

Instead, this happened:

$ rustc -C target-cpu=native test.rs
error[E0425]: cannot find function foo in this scope
 --> test.rs:7:9
  |
7 |         foo();
  |         ^^^ not found in this scope

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (17eec1433 2021-01-01)
binary: rustc
commit-hash: 17eec1433c69972844dd228b5fe801f218e118c3
commit-date: 2021-01-01
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly

rustc --print=target-cpus

Available CPUs for this target:
    native         - Select the CPU of the current host (currently generic).
    amdfam10
    athlon
    athlon-4
...
...
  yonah
    znver1
    znver2

Snippet of /proc/cpuinfo:
image

@as-com as-com added the C-bug Category: This is a bug. label Jan 3, 2021
@ehuss
Copy link
Contributor

ehuss commented Jan 3, 2021

I believe zen 3 support will be included with LLVM 12, and rustc should pick it up when it upgrades to that release (upstream change). I would guess that the 12 release is still a few months out.

@nagisa
Copy link
Member

nagisa commented Jan 5, 2021

target-cpu=native is a best effort kind of feature and will not be able to detect 100% accurately in all instances and for all versions. As thus target-cpu=native not detecting znver3 is not an implementation bug.

The only question is how can we better manage user's expectations wrt -Ctarget-cpu=native? This is not the first time this comes up.

I'll mark this as a duplicate of #54688 for now as that improvement would resolve this specific issue.

@nagisa nagisa closed this as completed Jan 5, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 9, 2021
…nagisa

Make target-cpu=native detect individual features

This PR makes target-cpu=native check for and enable/disable individual features instead of detecting and targeting a CPU by name. This brings the flag's behavior more in line with clang and gcc and ensures that the host actually supports each feature that we are compiling for.

This should resolve issues with miscompilations on e.g. "Haswell" Pentiums and Celerons that lack support for AVX, and also enable support for `aes` on Broadwell processors that support it. It should also resolve issues with failing to detect feature support in newer CPUs that aren't yet known by LLVM (see: rust-lang#80633).

Fixes rust-lang#54688
Fixes rust-lang#48464
Fixes rust-lang#38218
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants