Trouble with target-cpu=native

I 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 activate the avx instructions unless I make a .cargo/config file.

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:

The --print target-features is 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 => avx, avx2 supported, avx512xxx - Not supported

--print target-features output:
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 my project needs target-cpu=native to set the flags for me.

.cargo/config setup that works:

[target.x86_64-unknown-linux-gnu]
rustflags = ["-Ctarget-cpu=native"]

I'm convinced this is a real bug.

System is Linux Mint.
rustc 1.92.0-nightly (4082d6a3f 2025-09-27)
CPU: Intel(R) Core(TM) i9-10900K CPU

IMHO, RUSTFLAGS works for cargo.

You may try cargo rustc -Z unstable-options --print cfg instead

Also just prints the short version which is missing avx.

Have you forgotten the leading cargo?

$ cargo rustc -Z unstable-options --print cfg
debug_assertions
fmt_debug="full"
overflow_checks
panic="unwind"
relocation_model="pic"
target_abi=""
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="adx"
target_feature="aes"
target_feature="avx"
target_feature="avx2"
target_feature="avx512bf16"
target_feature="avx512bitalg"
target_feature="avx512bw"
target_feature="avx512cd"
target_feature="avx512dq"
target_feature="avx512f"
target_feature="avx512ifma"
target_feature="avx512vbmi"
target_feature="avx512vbmi2"
target_feature="avx512vl"
target_feature="avx512vnni"
target_feature="avx512vp2intersect"
target_feature="avx512vpopcntdq"
target_feature="avxvnni"
target_feature="bmi1"
target_feature="bmi2"
target_feature="cmpxchg16b"
target_feature="f16c"
target_feature="fma"
target_feature="fxsr"
target_feature="gfni"
target_feature="lahfsahf"
target_feature="lzcnt"
target_feature="movbe"
target_feature="pclmulqdq"
target_feature="popcnt"
target_feature="prfchw"
target_feature="rdrand"
target_feature="rdseed"
target_feature="sha"
target_feature="sse"
target_feature="sse2"
target_feature="sse3"
target_feature="sse4.1"
target_feature="sse4.2"
target_feature="sse4a"
target_feature="ssse3"
target_feature="vaes"
target_feature="vpclmulqdq"
target_feature="x87"
target_feature="xsave"
target_feature="xsavec"
target_feature="xsaveopt"
target_feature="xsaves"
target_has_atomic
target_has_atomic="16"
target_has_atomic="32"
target_has_atomic="64"
target_has_atomic="8"
target_has_atomic="ptr"
target_has_atomic_equal_alignment="16"
target_has_atomic_equal_alignment="32"
target_has_atomic_equal_alignment="64"
target_has_atomic_equal_alignment="8"
target_has_atomic_equal_alignment="ptr"
target_has_atomic_load_store
target_has_atomic_load_store="16"
target_has_atomic_load_store="32"
target_has_atomic_load_store="64"
target_has_atomic_load_store="8"
target_has_atomic_load_store="ptr"
target_has_reliable_f128
target_has_reliable_f16
target_has_reliable_f16_math
target_os="linux"
target_pointer_width="64"
target_thread_local
target_vendor="unknown"
ub_checks
unix
$ rustc -Z unstable-options --print cfg
debug_assertions
fmt_debug="full"
overflow_checks
panic="unwind"
relocation_model="pic"
target_abi=""
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_feature="x87"
target_has_atomic
target_has_atomic="16"
target_has_atomic="32"
target_has_atomic="64"
target_has_atomic="8"
target_has_atomic="ptr"
target_has_atomic_equal_alignment="16"
target_has_atomic_equal_alignment="32"
target_has_atomic_equal_alignment="64"
target_has_atomic_equal_alignment="8"
target_has_atomic_equal_alignment="ptr"
target_has_atomic_load_store
target_has_atomic_load_store="16"
target_has_atomic_load_store="32"
target_has_atomic_load_store="64"
target_has_atomic_load_store="8"
target_has_atomic_load_store="ptr"
target_has_reliable_f128
target_has_reliable_f16
target_has_reliable_f16_math
target_os="linux"
target_pointer_width="64"
target_thread_local
target_vendor="unknown"
ub_checks
unix

leading cargo is necessary.

1 Like

I didn't, but I was testing in another location to avoid anything I did or didn't have in my Cargo.toml file from affecting the output... which didn't work, but just running rustc worked.

So, when I have a .cargo/config file as stated above,
cargo rustc ...
works. (Everything works actually)

If I remove the .cargo directory, and just have [profile.dev], and the build.rs println statements requesting target-cpu=native, then cargo rustc ... doesn't work, and I get the short list of features.

Bug Reported -> Deferred

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.