From 0189dccd68d6d4c158387ca1531a9b18f53bd83b Mon Sep 17 00:00:00 2001 From: pierwill Date: Sat, 19 Feb 2022 17:35:21 -0600 Subject: [PATCH] Edit codegen --- src/overview.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/overview.md b/src/overview.md index 6156157f1..db4f34e1b 100644 --- a/src/overview.md +++ b/src/overview.md @@ -194,6 +194,33 @@ Along the way, we also construct the THIR, which is an even more desugared HIR. THIR is used for pattern and exhaustiveness checking. It is also more convenient to convert into MIR than HIR is. +We do [many optimizations on the MIR][mir-opt] because it is still +generic and that improves the code we generate later, improving compilation +speed too. +MIR is a higher level (and generic) representation, so it is easier to do +some optimizations at MIR level than at LLVM-IR level. For example LLVM +doesn't seem to be able to optimize the pattern the [`simplify_try`] mir +opt looks for. + +Rust code is _monomorphized_, which means making copies of all the generic +code with the type parameters replaced by concrete types. To do +this, we need to collect a list of what concrete types to generate code for. +This is called _monomorphization collection_ and it happens at the MIR level. + +### Code generation + +We then begin what is vaguely called _code generation_ or _codegen_. +The [code generation stage][codegen] is when higher level +representations of source are turned into an executable binary. `rustc` +uses LLVM for code generation. The first step is to convert the MIR +to LLVM Intermediate Representation (LLVM IR). This is where the MIR +is actually monomorphized, according to the list we created in the +previous step. +The LLVM IR is passed to LLVM, which does a lot more optimizations on it. +It then emits machine code. It is basically assembly code with additional +low-level types and annotations added (e.g. an ELF object or WASM). +The different libraries/binaries are then linked together to produce the final +binary. [String interning]: https://en.wikipedia.org/wiki/String_interning [`rustc_lexer`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lexer/index.html