How to pass compiler flags for benchmarks?

Hi, I try to optimize my code using SIMD instructions, and I use criterion for benchmarking. However, for SIMD to be effective, I need to pass the target-cpu or target-feature flag to rustc. How do I do that for a benchmark?

Use RUSTFLAGS environment variable when building.

1 Like

Is it possible to permanently set the compiler flags somewhere, like in Cargo.toml? Typing them in the command line every time is pretty annoying. Or can I store RUSTFLAGS somewhere permanently?

You can also set build.rustflags in .cargo/config.toml.

2 Likes

I put the following in a .cargo/config.toml in my project directory:

[build]
rustflags = "-Ctarget-cpu=native"

but it doesn't work. Cargo still doesn't compile for AVX etc. When I put RUSTFLAGS="-Ctarget-cpu=native" in front of the compilation or benchmarking command, it works.

[build]
rustflags = ["-Ctarget-cpu=native"]

This does not work, too.

Are you running cargo in the directory that contains .cargo/config.toml? Cargo looks for .cargo/config.toml in the working directory and it's parent directories. It doesn't look for it in the project directory if that is not the working directory.

Yes, I am running cargo in the same directory as the .cargo/config, which is also the root directory of my project. What do you mean with working directory?

The directory in which you run the command.

That should work. Can you check if you happen to have some value set for RUSTFLAGS (even if empty) Or if it is unset entirely. If some value is set I believe it overwrites the value in .cargo/config.toml.

also, target.<triplet>.rustflags and target.<cfg>.rustflags will overwrite build.rustflags, and I think if you pass --target command line argument when running cargo build, cargo will not use build.rustflags at all (but will use target.<triplet>.rustflags and target.<cfg>.rustflags if set).

1 Like

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.