# Inconsistent "target-cpu" across cargo commands

**URL:** <https://users.rust-lang.org/t/inconsistent-target-cpu-across-cargo-commands/20880>\
**Category:** help\
**Created:** [October 1, 2018, 1:19am UTC](https://users.rust-lang.org/t/inconsistent-target-cpu-across-cargo-commands/20880 "2018-10-01T01:19:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bees](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bees/32/6117_2.png) [@Bees](https://users.rust-lang.org/u/Bees)\
**Post date:** [October 1, 2018, 1:19am UTC](https://users.rust-lang.org/t/inconsistent-target-cpu-across-cargo-commands/20880/1 "2018-10-01T01:19:17Z")

</div>

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 check`s 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.

---

<div class="post-metadata">

**Author:** ![G2P](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/g2p/32/7712_2.png) [@G2P](https://users.rust-lang.org/u/G2P)\
**Post date:** [October 1, 2018, 5:56am UTC](https://users.rust-lang.org/t/inconsistent-target-cpu-across-cargo-commands/20880/2 "2018-10-01T05:56:59Z")

</div>

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](https://github.com/rust-lang/cargo/issues/2535) to do it [for different profiles](https://github.com/rust-lang/cargo/issues/5298)), but you can simply use the `build.rustflags` `.cargo/config` setting:

```rust
# in .cargo/config 
[build]
rustflags = "-C target-cpu=native"

```

---

<div class="post-metadata">

**Author:** ![Bees](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bees/32/6117_2.png) [@Bees](https://users.rust-lang.org/u/Bees)\
**Post date:** [October 3, 2018, 9:56pm UTC](https://users.rust-lang.org/t/inconsistent-target-cpu-across-cargo-commands/20880/3 "2018-10-03T21:56:28Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:55am UTC](https://users.rust-lang.org/t/inconsistent-target-cpu-across-cargo-commands/20880/4 "2023-01-12T08:55:01Z")

</div>

Closed on January 12, 2023, 8:55am UTC


