Compilation Pipeline Overview
Early Passes
Lexing and Parsing
parse - Source Code Parsing
parse - Source Code Parsing
Location:
rustc_interface::passes::parseThe first major pass that converts source code into an Abstract Syntax Tree (AST).Process:- Creates a parser from file or string input
- Tokenizes the source code (lexing)
- Parses tokens into AST nodes
- Injects command-line attributes into the crate
Input::File or Input::Str)Output: ast::CrateError Handling: Parse errors are emitted immediately and cause compilation to abort.pre_expansion_lint - Early Lint Checks
pre_expansion_lint - Early Lint Checks
Location:
rustc_interface::passes::pre_expansion_lintRuns lints on the AST before macro expansion. These lints check for issues that should be caught early.Timing: Before macro expansionInput: AST nodes, attributes, featuresLints Checked: Built-in pre-expansion lints from rustc_lint::BuiltinCombinedPreExpansionLintPassMacro Processing
configure_and_expand - Macro Expansion
configure_and_expand - Macro Expansion
Location:
rustc_interface::passes::configure_and_expandRuns the “early phases” of compilation: cfg processing, syntax expansion, and name resolution.Major Steps:- Crate Injection - Injects standard library imports
- Macro Expansion - Expands all macros recursively
- Test Harness - Injects test harness if
--testis specified - AST Validation - Validates the expanded AST
- Proc Macro Harness - Injects procedural macro runtime
- Name Resolution - Resolves all names and paths
- Respects recursion limits
- Handles
cfgattributes - Processes feature gates
ast::Crate, ResolverOutput: Fully expanded ast::Crate with all macros expandedexpand_crate - Macro Expansion Core
expand_crate - Macro Expansion Core
Timing: Within
configure_and_expandThe core macro expansion pass that:- Expands declarative macros (
macro_rules!) - Calls procedural macros
- Expands built-in macros like
println!,derive, etc. - Handles macro imports and exports
#![recursion_limit] attributeAnalysis Passes
HIR Construction and Analysis
resolver_for_lowering_raw - Resolution for Lowering
resolver_for_lowering_raw - Resolution for Lowering
Location:
rustc_interface::passes::resolver_for_lowering_rawQuery: resolver_for_lowering_rawPrepares resolution data needed for AST lowering to HIR.Output:ResolverAstLowering- Resolution tables for lowering- Expanded
ast::Crate ResolverGlobalCtxt- Global resolution context
hir_crate - HIR Construction
hir_crate - HIR Construction
Query:
hir_crateLowers the expanded AST into High-Level Intermediate Representation (HIR).Performed by: rustc_ast_loweringTransformations:- Desugars complex syntax into simpler forms
- Converts
forloops toloop+match - Expands question mark operator (
?) - Normalizes closure syntax
Crate<'tcx> - The HIR crateearly_lint_checks - Early HIR Lints
early_lint_checks - Early HIR Lints
Location:
rustc_interface::passes::early_lint_checksQuery: early_lint_checksRuns lints that operate on the fully expanded AST and early HIR.Timing: After expansion, before type checkingType Checking and Inference
Type Collection
Type Collection
Queries:
type_of, generics_of, predicates_ofCollects type signatures from all items in the crate.Process:- Collect types from item signatures
- Build generic parameter lists
- Collect trait bounds and where clauses
- Validate type well-formedness
Type Checking (Typeck)
Type Checking (Typeck)
Primary Crate:
rustc_hir_typeckQueries: Per-function type checking queriesPerforms type inference and checking for function bodies.Major Components:- Expression type checking
- Pattern type checking
- Method resolution
- Operator overload resolution
- Type coercion
- Closure signature inference
Trait Resolution
Trait Resolution
Primary Crate:
rustc_trait_selectionResolves trait implementations and validates trait bounds.Operations:- Trait selection (finding applicable impls)
- Projection (resolving associated types)
- Method candidate resolution
- Impl overlap checking
- Coherence checking
MIR Passes
mir_built - MIR Construction
mir_built - MIR Construction
Query:
mir_builtPrimary Crate: rustc_mir_buildBuilds MIR (Mid-level Intermediate Representation) from THIR (Typed HIR).Steps:- Lower HIR to THIR (adds type information)
- Build MIR control flow graph
- Generate MIR for match expressions
- Insert drop glue
mir::Body<'tcx> - Unoptimized MIRBorrow Checking
Borrow Checking
Primary Crate:
rustc_borrowckQueries: mir_borrowckValidates memory safety through borrow checking.Checks:- Validates borrows don’t outlive referenced data
- Ensures no simultaneous mutable and immutable borrows
- Prevents use-after-move
- Validates initialization before use
MIR Optimization Passes
MIR Optimization Passes
Primary Crate:
rustc_mir_transformPerforms optimization passes on MIR.Major Optimizations:- Inlining - Inline small functions
- Constant Propagation - Evaluate constants at compile time
- Dead Code Elimination - Remove unreachable code
- Copy Propagation - Eliminate unnecessary copies
- Destination Propagation - Optimize memory destinations
- Simplify CFG - Simplify control flow graph
- InstCombine - Combine instructions
Additional Analysis Passes
Reachability Analysis
Reachability Analysis
Module:
rustc_passes::reachableDetermines which items are reachable from entry points.Used For:- Dead code detection
- Determining what to include in metadata
- Link-time optimization decisions
Dead Code Detection
Dead Code Detection
Module:
rustc_passes::deadQuery: Provided through rustc_passes::provideDetects unused code that can be removed or warned about.Checks:- Unused functions
- Unused imports
- Unused variables
- Unused struct fields (private only)
Privacy Checking
Privacy Checking
Primary Crate:
rustc_privacyValidates privacy rules (pub/private).Checks:- Private types in public signatures
- Access to private items
- Computes effective visibilities
Stability Checking
Stability Checking
Module:
rustc_passes::stabilityValidates use of stable/unstable features.Checks:- Unstable feature gates
- Deprecated item usage
- Stability attribute consistency
Entry Point Detection
Entry Point Detection
Module:
rustc_passes::entryFinds the entry point for the program (main function or custom entry point).Validates:- Entry point signature
- Only one entry point exists
- Entry point visibility
Code Generation Passes
Monomorphization
Monomorphization
Primary Crate:
rustc_monomorphizeCreates concrete versions of generic functions for each type they’re used with.Process:- Collect all generic items used
- Generate concrete versions with type parameters substituted
- Partition into codegen units
- Handle dynamic dispatch (trait objects)
MonoItems - Concrete items ready for codegenCodegen Unit Partitioning
Codegen Unit Partitioning
Query:
collect_and_partition_mono_itemsDivides monomorphized items into compilation units for parallel codegen.Strategies:- Per-module partitioning (faster compilation)
- Single unit (better optimization, slower compilation)
LLVM Code Generation
LLVM Code Generation
Primary Crate:
rustc_codegen_llvmGenerates LLVM IR from MIR.Steps:- Convert MIR to LLVM IR
- Apply LLVM optimizations
- Generate object files
Linking
Linking
Final Phase: Links object files into final binaryProcess:
- Collect all object files
- Link with runtime libraries
- Apply LTO (Link-Time Optimization) if enabled
- Produce final executable/library
Pass Organization in Code
rustc_passes Crate
Therustc_passes crate (compiler/rustc_passes/) contains various analysis passes:
Analysis Pass
The mainanalysis query coordinates type checking and other analyses:
Location: rustc_interface::passes::analysis
Query: analysis
Runs:
- Parallel type checking of all items
- Privacy checking
- Liveness analysis
- Intrinsic checking
- Variance inference
- Other whole-crate analyses
Pass Timing and Profiling
All major passes are instrumented with timing information:rustc -Z time-passes to see timing information for each pass.
Related Documentation
Query System
Learn how passes are implemented as queries
Compiler Crates
See which crates implement which passes
Many passes are implemented as queries in the demand-driven query system. See the Query System documentation for details.