Trouble with target-cpu=native
⚓ Rust 📅 2025-09-28 👤 surdeus 👁️ 8I can't get cargo / rustc to detect my avx instructions when using target-cpu=native.
Depending on how I invoke rustc I can sometimes see avx instructions, but I can't get target-cpu=native to ever activate the avx instructions.
Here's what I'm seeing:
RUSTFLAGS='-C target-cpu=native' rustc --print cfg
This generates a short list of features missing avx:
rustc -C target-cpu=native --print cfg
Generates a longer list of features: (includes avx which is supported on my cpu)
rustc --print target-features
Generates a LONG list of supported features:
This last entry seems a little misleading, as I believe it includes all of the features that rustc could cross-compile, but includes things not supported by my cpu even though it says: "Features supported by rustc for this target:"
In the list, it describes items as either Support ___, or Enable ___. The Supported items are not supported by my cpu. Many of the things labeled Enable are also not supported according to
cat /proc/cpuinfo
avx10.1                         - Support AVX10.1 up to 512-bit instruction.
avx10.2                         - Support AVX10.2 up to 512-bit instruction.
avx2                            - Enable AVX2 instructions.
avx512bf16                      - Support bfloat16 floating point.
avx512bitalg                    - Enable AVX-512 Bit Algorithms.
Anyway, back to the issue at hand...
I've included environment flags:
RUSTFLAGS='-C target-cpu=native'
build.rs flags:
println!("cargo:rustc-env=RUSTFLAGS=-C target-cpu=native");
And Cargo.toml flags:
[profile.dev]
target-cpu = "native"
And still my attempts to use target-cpu=native to set the avx and avx2 features isn't working.
#[cfg(target_feature = "avx2")]
I'm pretty sure I could set the flags I need manually, but I'd really rather get target-cpu=native to set the flags for me.
But .cargo/config in the project directory works:
[target.x86_64-unknown-linux-gnu]
rustflags = ["-Ctarget-cpu=native"]
Thoughts? A real bug?
System is Linux Mint.
rustc 1.92.0-nightly (4082d6a3f 2025-09-27)
4 posts - 2 participants
🏷️ Rust_feed