Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

What Code Does Rust Pass to LLVM? Generics and Codegen Units

Rustc’s LLVM backend receives LLVM IR generated from MIR. See how monomorphization, codegen units, LLVM, and linking fit together—and how to inspect the output.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Rustc’s LLVM backend passes LLVM IR to LLVM—not generic Rust source. Rustc first determines which concrete generic instances the program needs, then monomorphizes them as it translates MIR into LLVM IR. It groups generated items into codegen units (CGUs), which LLVM processes as modules before object files are linked into the final output.

What LLVM receives from rustc

Rustc does not send LLVM the original Rust source or its reusable generic definitions. The path described by the Rust Compiler Development Guide begins with MIR, Rust’s mid-level intermediate representation, and lowers it to LLVM IR for the LLVM backend. Rust supports other codegen backends, so this explanation applies specifically to builds using LLVM.

Generic MIR can remain parameterized while rustc performs earlier compiler analysis. Before code generation, rustc determines which concrete instances are needed. During translation to codegen IR, it substitutes concrete types and emits code for those instances. As the guide puts it, “The actual monomorphization is performed as we go, while we do the translation.”

How generic functions become concrete code

1. Collect the required instances

Before lowering MIR for code generation, rustc collects the concrete instances of generic functions and other monomorphized items that the program needs. The compiler guide identifies collect_and_partition_mono_items as the process that collects these items and partitions them into CGUs.

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

2. Translate and monomorphize

Rustc then translates the collected items. For a generic function or type, the compiler substitutes the concrete type arguments used by the program and generates code specialized for that instantiation. For example, using Vec<u64> and Vec<String> can require generated code for both concrete types.

This specialization lets generated code work with specific types, but producing concrete instances can increase compile time and binary size. It is useful to distinguish the collection step from the translation step: rustc identifies what must be generated first, while the actual monomorphization happens during translation.

What codegen units are for

A codegen unit is a group of code-generation items that rustc compiles as an LLVM module. The compiler guide’s codegen discussion describes LLVM modules being processed independently, which allows work to happen in parallel. CGUs also matter to incremental compilation because they provide units that may be reused.

The guide describes a default partitioning approach that creates two CGUs per source-level module: a relatively stable unit for non-generic code and a more volatile unit for monomorphized or specialized instances. This is an implementation description, not a guarantee that every build, compiler version, or configuration will have the same boundaries.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Dependency items are not all handled alike. Generic instances may be generated in the consuming crate’s CGU, while ordinary non-generic functions from a dependency are not simply copied into every downstream CGU. Inline and generic-inline items have their own behavior in the guide’s partitioning description.

From LLVM IR to the final artifact

  1. Rustc lowers MIR to LLVM IR. For the LLVM backend, the translated, concrete code is represented as LLVM IR and grouped into CGUs.
  2. LLVM processes the modules. It applies code-generation and optimization work to the modules and emits object files. The particular IR and optimization work depend on compiler settings.
  3. The linker combines outputs. The linker brings object files together with relevant metadata or archives to produce the requested executable or other output. With some forms of link-time optimization (LTO), optimization can also take place during linking.

Consequently, “what rustc passes to LLVM” can refer to more than one point in the build: LLVM IR at the backend boundary, or IR after LLVM has run optimization passes. The answer depends on which point you mean and on settings such as optimization, LTO, and CGU count.

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

How to inspect the LLVM IR

The compiler guide documents --emit=llvm-ir for emitting LLVM IR. With Cargo, the guide shows passing the option through RUSTFLAGS. To preserve intermediate bitcode, it documents -C save-temps; llvm-dis can convert bitcode into readable .ll text.

  1. Emit LLVM IR with Cargo: run RUSTFLAGS='--emit=llvm-ir' cargo build. This asks rustc to emit LLVM IR while Cargo builds the package.
  2. Preserve intermediate files: add -C save-temps to the rustc options passed through RUSTFLAGS when you want intermediate bitcode retained.
  3. Convert bitcode to text if needed: use llvm-dis on a bitcode file to produce readable LLVM IR text.
  4. Reduce CGU interleaving in pass output: the guide illustrates -C codegen-units=1 for clearer LLVM pass output, since output from multiple CGUs can interleave.

The emitted IR can differ with optimization settings, and the precise files and behavior are version-sensitive. The guide documents these options; they are not a promise of one universal IR snapshot across rustc versions or build configurations. Rust’s codegen tests inspect emitted LLVM IR, while codegen-unit tests examine mono-item collection and CGU partitioning.

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

What can change between builds

  • Backend: this pipeline describes rustc’s LLVM backend; Rust also supports other codegen backends.
  • Optimization and LTO: they affect the IR and can move some optimization work to link time.
  • CGU configuration and partitioning: these influence how code is grouped into modules and processed.
  • Inspection point: IR emitted by rustc is not necessarily the same as IR after LLVM passes.

The online compiler-guide pages do not state a single rustc release version or publication date for these implementation details. Check the documentation and options for the rustc version you are using when exact behavior matters.

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. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver 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.