Reputation: 48058
While I've seen docs on using rustc
directly to output assembly, having to manually extract commands used by Cargo and edit them to write assembly is tedious.
Is there a way to run Cargo that writes out assembly files?
Upvotes: 84
Views: 46097
Reputation: 100110
If you just want to look at the assembly output instead of saving it, e.g. to judge if it's well-optimized, then an easy option is to use:
(don't forget to add -O
to the Compiler options box)
Upvotes: 4
Reputation: 523294
You can use Cargo's cargo rustc
command to send arguments to rustc
directly:
cargo rustc -- --emit asm
ls target/debug/deps/<crate_name>-<hash>.s
For optimized assembly:
cargo rustc --release -- --emit asm
ls target/release/deps/<crate_name>-<hash>.s
If you see multiple <crate_name>-<hash>-<hash>.rcgu.s
files instead of a <crate_name>-<hash>.s
file, disable incremental compilation by setting the environment variable CARGO_INCREMENTAL=0
.
Upvotes: 115
Reputation: 88646
Both existing answers (using cargo rustc
and RUSTFLAGS
) are the best ways to obtain assembly with standard tools. If you find yourself trying to look at assembly fairly often, you might want to consider using the cargo asm
subcommand. After installing it with cargo install cargo-asm
, you can print assembly like:
cargo build --release
cargo asm my_crate::my_function
There are a few things to pay attention to, though:
cargo asm
and it will list all symbols you can inspect.cargo build --release
before trying to look at the assembly, because cargo asm
(apparently) only looks at the already existing build-artifactsUpvotes: 66
Reputation: 88646
In addition to kennytm's answer, you can also use the RUSTFLAGS
environment variable and use the standard cargo commands:
RUSTFLAGS="--emit asm" cargo build
cat target/debug/deps/project_name-hash.s
Or in release mode (with optimizations):
RUSTFLAGS="--emit asm" cargo build --release
cat target/release/deps/project_name-hash.s
You can pass different values to the --emit
parameter, including (but not limited to):
mir
(Rust intermediate representation)llvm-ir
(LLVM intermediate representation)llvm-bc
(LLVM byte code)asm
(assembly)Upvotes: 58