October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Inspect LLVM IR and Assembly Generated by Rust

Use rustc or cargo rustc to emit LLVM IR and target assembly, then account for optimization, compiler version, and target when reading or comparing the output.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use rustc --emit=llvm-ir,asm main.rs to emit LLVM IR and target assembly from a standalone Rust file. For a Cargo target, use cargo rustc and pass the emit option after --. The output depends on compiler version, target, optimization settings, and other code-generation options, so set those deliberately when comparing results.

Emit LLVM IR and assembly from a standalone Rust file

From the directory containing main.rs, run:

rustc --emit=llvm-ir,asm main.rs

Rustc writes LLVM IR and assembly as separate files. By default, their names are based on the crate name and end in .ll and .s. To choose stable filenames, specify a path for each output:

As an Amazon Associate I earn from qualifying purchases.

rustc --emit=llvm-ir=out.ll,asm=out.s main.rs

The --emit option accepts comma-separated output kinds. Other available kinds include mir for Rust’s Mid-level Intermediate Representation, llvm-bc for LLVM bitcode, obj for a native object file, metadata, and link for the normal linked artifact. These are different stages or artifact types, not interchangeable names for assembly. See the rustc reference for --emit.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Send one text output to the terminal

Rustc can write a text output such as LLVM IR or assembly to standard output by using - as its path:

rustc --emit=llvm-ir=- main.rs

For assembly instead:

rustc --emit=asm=- main.rs

Only one output type can be sent to stdout per invocation. Run separate commands or write both outputs to files if you need both.

Inspect a Cargo package target

Use cargo rustc, select the target you want, and put rustc’s arguments after Cargo’s -- separator. For a binary named my_program:

cargo rustc --bin my_program -- --emit=llvm-ir,asm

For the package library target:

cargo rustc --lib -- --emit=llvm-ir,asm

To compile the binary with Cargo’s release profile:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cargo rustc --release --bin my_program -- --emit=llvm-ir,asm

When a package has multiple targets, specify which one to compile with an option such as --bin or --lib. Cargo forwards the extra rustc arguments to the final compiler invocation for the selected package target, not to its dependencies; as the Cargo cargo rustc reference explains, “The specified args will all be passed to the final compiler invocation, not any of the dependencies.” Cargo may still compile dependencies as part of building the target, but those dependencies do not automatically receive the forwarded emit arguments.

Choose settings that answer your question

Decide whether to inspect unoptimized or optimized output

Rustc’s default optimization level is 0. Use -O for optimization level 3 in a direct rustc invocation:

rustc -O --emit=llvm-ir,asm main.rs

Rustc also documents levels 1, 2, s, and z. In Cargo, --release selects the release profile, whose settings determine the build’s optimization behavior. The rustc code-generation options reference describes the available optimization options.

Optimization can inline, simplify, or remove code, so a function visible in one build may not appear as a distinct function in another. Choose the settings that match your question: an unoptimized build can make some source-to-output relationships easier to follow, while an optimized build shows output after optimization under the selected profile.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Request LLVM IR before LLVM’s usual optimization passes

For a specialized view of IR before LLVM’s usual pre-populated optimization passes, rustc documents this combination:

rustc -O -C no-prepopulate-passes --emit=llvm-ir main.rs

no-prepopulate-passes uses an empty pass list instead of the usual pre-populated list. This is not the same view as ordinary optimized backend output: the command uses -O, but asks LLVM not to run its usual pre-populated passes. Consult the code-generation options reference when interpreting this specialized output.

Account for the target

Assembly is emitted for the selected compilation target. It reflects that target’s instructions and conventions; assembly for one architecture is not a universal listing of what Rust emits. Target CPU and target-feature options can also influence generated code. Rustc’s code-generation options reference documents relevant target and code-generation settings.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Interpret and compare the output carefully

LLVM IR and assembly answer related but distinct questions. LLVM IR shows the representation at LLVM’s level; assembly shows target-specific instructions. Neither is a fixed, target-independent description of a Rust function. Compiler version, target triple, optimization level or Cargo profile, and code-generation flags can all change what appears.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a meaningful comparison between two listings, keep the source and these settings the same, and report them alongside the output:

  • Rust compiler version: check the installed toolchain with rustc --version.
  • Target triple: identify the target for which the output was generated.
  • Build profile or optimization level: record whether you used the default, -O, or a Cargo profile such as release.
  • Relevant code-generation flags: include CPU, target-feature, or pass-related options that affect the result.
  • IR pass context: distinguish ordinary emitted IR from IR requested with -C no-prepopulate-passes.

Option availability and output details can vary across toolchains. Use rustc --help and rustc -C help to check what your installed compiler supports; the official rustc command-line reference and code-generation options reference document the relevant behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.