Inconsistent "target-cpu" across cargo commands

I recently added some SIMD code paths to a library. I put the code paths behind some #[cfg(all( target_cpu = "x86_64", target_feature = "avx", target_feature = "sse4.1" ))] gates, which my development computer supports, but I’m finding that I have to jump through some hoops to get those code paths recognized when I run tests. I wonder what is the expected or documented behavior for tests/benchmarks that depend on conditional compilation.

Some details:

cargo test runs test without the SIMD code paths compiled

RUSTFLAGS='-C target-cpu=native' cargo test runs tests with the SIMD code paths compiled

cargo bench runs benchmarks with the SIMD code paths compiled, without needing the RUSTFLAGS env var

cargo bench on Windows runs benchmarks with the SIMD code paths compiled, but the avx/sse intrinsic functions panic

RUSTFLAGS='-C target-cpu=native' cargo bench on Windows (that’s not the windows syntax for env vars though) runs benchmarks with SIMD code paths compiled, and functioning correctly

cargo test and cargo check behave the same; the #[cfg] attributes never turn on compilation unless compiled with target-cpu=native

The tests are in their own test modules with #[cfg(test)] followed by the cpu feature #[cfg]s. Removing them lets them get run in tests without forcing target-cpu=native, but I figure that would make it impossible for a person on a computer without those CPU features to run tests.

It’s especially frustrating because my editor cargo checks on save, as many do, and it does so without any RUSTFLAGS env var, so modifying a file immediately after running a test, or vice versa, results in a lot of time spent recompiling criterion and the rest of my dev dependencies for a different target.

Tests run in the default, debug mode, whereas benches run in --release mode.

I don’t know what’s setting target-cpu = native in release mode (there currently isn’t a way to do it for different profiles), but you can simply use the build.rustflags .cargo/config setting:

# in .cargo/config 
rustflags = "-C target-cpu=native"
1 Like

Thanks that’s handy.

I’m still curious, though, what causes the x86_64, avx and sse4.1 features to be detected without explicitly compiling for the native arch when I cargo run, cargo build, cargo bench, but not when I cargo test.