Do Rust compilers have optimization flags like C compilers have?

For Gentoo, with their portage package managing system, you are able to use certain flags to optimize the code specifically for your CPU. I believe C compilers such as gcc offer such features as well. What about the Rust compiler, does it offer such flags?

To compile with your current CPU, use this:

rustc -C target-cpu=native test.rs

To get a list of all available target CPU architectures, use this:

rustc --print target-cpus

To do this with Cargo, set the RUSTFLAGS environment variable:

export RUSTFLAGS="-C target-cpu=native"
cargo build --release
3 Likes

At the moment there is really only one Rust compiler, and it is a frontend for LLVM, like Clang. So if Clang supports a given option (for codegen, that is, not language options), there's probably a way to turn it on for Rust.

rustc's codegen options usually start with -C. You can run rustc -C help to see a list of supported ones. I would assume you can pass others straight to LLVM with the -C llvm-args option.

2 Likes

Thanks mate :slight_smile:

I know that with C you can specify 02, 03 flags or soemthing, is this possible with Rust as well?

Yes, -C opt-level=3.

3 Likes

When compiling Rust with cargo build --release it is using opt-level=3 by default, but when compiling in debug mode I'm pretty sure it is opt-level=1, but you can change the settings for both in your Cargo toml like this:

[profile.dev]
opt-level = 3

That, for instance, would make your debug builds run with full optimizations. See the cargo profile docs for more info.

I see, but doesn't O3 breaks code sometimes as I heard it does with some C programs?

Is it possible to build it without opt-level at all for debug mode so that it builds a lot quicker?

Not in safe Rust, at least, it doesn't. It's the default mode for every Rust program you download so I don't think so! :slight_smile:

Is it possible to build it without opt-level at all for debug mode so that it builds a lot quicker?

I think that opt-level 01 is the minimum which is as close to "no optimizations" as you get for the best speed, but it can make it even faster if you disable debug symbols like this:

[profile.dev]
debug = false

You just won't get good backtraces ( I think ) or other debugger support like that.

Only if the C code violates at least some part of the C specification. Compilers optimizes code based on the assumption that it satisfies all the invariants requested by the C spec. And yeah, violating invariants is what we call UB.

Safe Rust is a safe language. Writing non-safe language is hard, since it's you who have responsibility to satisfy all those invariants by your hand. When you write safe Rust, all those invariants are checked and enforced by the compiler.

1 Like

Yeah, a great blog post about that and how tons of C programs were broken because of the C standard and undefined behavior. This post really helped me understand what UB actually is:

1 Like

Oh cool.

I see. thanks :slight_smile: