Can't specify target_feature AVX2

I can't set AVX2 and then read it through. This command just doesn't work.

#[cfg(target_feature = "+avx2")]
const CHUNK_SIZE: usize = 4;

Here is an example of my Cargo.toml file:

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
target-feature = "+avx2"
target-cpu = "x86-64-v4"
RUSTFLAGS = "-C target-cpu=x86-64-v4 -C target-feature=+avx2"

[build]
rustflags = ["target-feature", "+avx2"]

The compilation looks like this:

cargo bench --verbose
rustc --crate-name tsrs --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=154 --emit=dep-info,link -C opt-level=3 -C lto -C codegen-units=1 --test -C metadata=d359a2008d8c28b7 -C extra-filename=-d359a2008d8c28b7 --out-dir /home/hexen/Projects/tsrs/target/release/deps -C strip=symbols -L dependency=/home/hexen/Projects/tsrs/target/release/deps --extern rand=/home/hexen/Projects/tsrs/target/release/deps/librand-1b509dcebceec914.rlib
rustc 1.77.1-nightly (7cf61ebde 2024-03-27) (gentoo)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.77.1-nightly
LLVM version: 17.0.6

Should the rust source have "avx2", not "+avx2"?

2 Likes

I tried to write "avx2" - to no avail.

The [build] section is only part of .cargo/config.toml, not part of Cargo.toml. And profile.release.RUSTFLAGS doesn't work either. Profile settings in Cargo.toml can't set rustflags. You either have to use the RUSTFLAGS env var or the build.rustflags option in .cargo/config.toml.

Also as @user16251 noted, the correct syntax in the rust source is #[cfg(target_feature = "avx2")] without +.

2 Likes