Skip to content

target-cpu = "native" doesn't work correctly #147176

@pup-tv

Description

@pup-tv

Most methods of configuring cargo / rustc to detect my avx instructions when setting target-cpu="native" do not work.

Specifically none of these options worked:

RUSTFLAGS='-C target-cpu=native' cargo test

build.rs flags:
println!("cargo:rustc-env=RUSTFLAGS=-C target-cpu=native");

And Cargo.toml flags:
[profile.dev]
target-cpu = "native"

Creating a project level .cargo directory with a config file DOES work.

A quick test:

While debugging the issue, I noticed that rustc --print cfg and --print target-features generated significantly different output lists depending on how I called rustc:

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. This is probably the intended output.

I've included environment flags:

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.

Again: creating the following file at: .cargo/config works:

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

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

https://users.rust-lang.org/t/trouble-with-target-cpu-native/134246


Finally, as an aside, the Support ___ or Enable ____ entries in the rustc --print target-features list are confusing:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions