1# Lowering MIR to a Codegen IR23Now that we have a list of symbols to generate from the collector, we need to4generate some sort of codegen IR. In this chapter, we will assume LLVM IR,5since that's what rustc usually uses. The actual monomorphization is performed6as we go, while we do the translation.78Recall that the backend is started by9[`rustc_codegen_ssa::base::codegen_crate`][codegen1]. Eventually, this reaches10[`rustc_codegen_ssa::mir::codegen_mir`][codegen2], which does the lowering from11MIR to LLVM IR.1213[codegen1]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/base/fn.codegen_crate.html14[codegen2]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/mir/fn.codegen_mir.html1516The code is split into modules which handle particular MIR primitives:1718- [`rustc_codegen_ssa::mir::block`][mirblk] will deal with translating19 blocks and their terminators. The most complicated and also the most20 interesting thing this module does is generating code for function calls,21 including the necessary unwinding handling IR.22- [`rustc_codegen_ssa::mir::statement`][mirst] translates MIR statements.23- [`rustc_codegen_ssa::mir::operand`][mirop] translates MIR operands.24- [`rustc_codegen_ssa::mir::place`][mirpl] translates MIR place references.25- [`rustc_codegen_ssa::mir::rvalue`][mirrv] translates MIR r-values.2627[mirblk]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/mir/block/index.html28[mirst]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/mir/statement/index.html29[mirop]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/mir/operand/index.html30[mirpl]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/mir/place/index.html31[mirrv]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/mir/rvalue/index.html3233Before a function is translated a number of simple and primitive analysis34passes will run to help us generate simpler and more efficient LLVM IR. An35example of such an analysis pass would be figuring out which variables are36SSA-like, so that we can translate them to SSA directly rather than relying on37LLVM's `mem2reg` for those variables. The analysis can be found in38[`rustc_codegen_ssa::mir::analyze`][mirana].3940[mirana]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/mir/analyze/index.html4142Usually a single MIR basic block will map to a LLVM basic block, with very few43exceptions: intrinsic or function calls and less basic MIR statements like44`assert` can result in multiple basic blocks. This is a perfect lede into the45non-portable LLVM-specific part of the code generation. Intrinsic generation is46fairly easy to understand as it involves very few abstraction levels in between47and can be found in [`rustc_codegen_llvm::intrinsic`][llvmint].4849[llvmint]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_llvm/intrinsic/index.html5051Everything else will use the [builder interface][builder]. This is the code that gets52called in the [`rustc_codegen_ssa::mir::*`][ssamir] modules discussed above.5354[builder]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_llvm/builder/index.html55[ssamir]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/mir/index.html5657> TODO: discuss how constants are generated
Findings
✓ No findings reported for this file.