11# Backend Agnostic Codegen
22
3- [ ` rustc_codegen_ssa ` ]
4- provides an abstract interface for all backends to implement,
3+ [ ` rustc_codegen_ssa ` ] provides an abstract interface for all backends to implement,
54namely LLVM, [ Cranelift] , and [ GCC] .
65
76[ Cranelift ] : https://github.com/rust-lang/rustc_codegen_cranelift
87[ GCC ] : https://github.com/rust-lang/rustc_codegen_gcc
98[ `rustc_codegen_ssa` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/index.html
109
11- Below is some background information on the refactoring that created this
12- abstract interface.
10+ Below is some background information on the refactoring that created this abstract interface.
1311
1412## Refactoring of ` rustc_codegen_llvm `
1513by Denis Merigoux, October 23rd 2018
@@ -22,12 +20,10 @@ Here is the breakdown of the most important elements:
2220* the ` back ` folder (7,800 LOC) implements the mechanisms for creating the
2321 different object files and archive through LLVM, but also the communication
2422 mechanisms for parallel code generation;
25- * the ` debuginfo ` (3,200 LOC) folder contains all code that passes debug
26- information down to LLVM;
23+ * the ` debuginfo ` (3,200 LOC) folder contains all code that passes debug information down to LLVM;
2724* the ` llvm ` (2,200 LOC) folder defines the FFI necessary to communicate with
2825 LLVM using the C++ API;
29- * the ` mir ` (4,300 LOC) folder implements the actual lowering from MIR to LLVM
30- IR;
26+ * the ` mir ` (4,300 LOC) folder implements the actual lowering from MIR to LLVM IR;
3127* the ` base.rs ` (1,300 LOC) file contains some helper functions but also the
3228 high-level code that launches the code generation and distributes the work.
3329* the ` builder.rs ` (1,200 LOC) file contains all the functions generating
@@ -53,19 +49,16 @@ have to be done at the same time for the resulting code to compile:
5349 will define the interface between backend-agnostic code and the backend
5450
5551While LLVM-specific code will be left in ` rustc_codegen_llvm ` , all the new
56- traits and backend-agnostic code will be moved in ` rustc_codegen_ssa ` (name
57- suggestion by @eddyb ).
52+ traits and backend-agnostic code will be moved in ` rustc_codegen_ssa ` (name suggestion by @eddyb ).
5853
5954### Generic types and structures
6055
6156@irinagpopa started to parametrize the types of ` rustc_codegen_llvm ` by a
6257generic ` Value ` type, implemented in LLVM by a reference ` &'ll Value ` .
63- This
64- work has been extended to all structures inside the ` mir ` folder and elsewhere,
58+ This work has been extended to all structures inside the ` mir ` folder and elsewhere,
6559as well as for LLVM's ` BasicBlock ` and ` Type ` types.
6660
67- The two most important structures for the LLVM codegen are ` CodegenCx ` and
68- ` Builder ` .
61+ The two most important structures for the LLVM codegen are ` CodegenCx ` and ` Builder ` .
6962They are parametrized by multiple lifetime parameters and the type for ` Value ` .
7063
7164``` rust,ignore
@@ -86,10 +79,8 @@ The code in `rustc_codegen_llvm` has to deal with multiple explicit lifetime
8679parameters, that correspond to the following:
8780* ` 'tcx ` is the longest lifetime, that corresponds to the original ` TyCtxt `
8881 containing the program's information;
89- * ` 'a ` is a short-lived reference of a ` CodegenCx ` or another object inside a
90- struct;
91- * ` 'll ` is the lifetime of references to LLVM objects such as ` Value ` or
92- ` Type ` .
82+ * ` 'a ` is a short-lived reference of a ` CodegenCx ` or another object inside a struct;
83+ * ` 'll ` is the lifetime of references to LLVM objects such as ` Value ` or ` Type ` .
9384
9485Although there are already many lifetime parameters in the code, making it
9586generic uncovered situations where the borrow-checker was passing only due to
@@ -108,8 +99,7 @@ However, the two most important structures,
10899are not defined in the backend-agnostic code.
109100Indeed, their content is highly specific to the backend,
110101and it makes more sense to leave their definition to the backend
111- implementor than to allow just a narrow spot via a generic field for the
112- backend's context.
102+ implementor than to allow just a narrow spot via a generic field for the backend's context.
113103
114104### Traits and interface
115105
@@ -167,16 +157,13 @@ Finally, a master structure implementing the `ExtraBackendMethods` trait is
167157used for high-level codegen-driving functions like ` codegen_crate ` in ` base.rs ` .
168158For LLVM, it is the empty ` LlvmCodegenBackend ` .
169159` ExtraBackendMethods ` should be implemented by the same structure that
170- implements the ` CodegenBackend ` defined in
171- ` rustc_codegen_ssa/src/traits/backend.rs ` .
160+ implements the ` CodegenBackend ` defined in ` rustc_codegen_ssa/src/traits/backend.rs ` .
172161
173162During the traitification process, certain functions have been converted from
174163methods of a local structure to methods of ` CodegenCx ` or ` Builder ` and a
175164corresponding ` self ` parameter has been added.
176- Indeed, LLVM stores information
177- internally that it can access when called through its API.
178- This information
179- does not show up in a Rust data structure carried around when these methods are
165+ Indeed, LLVM stores information internally that it can access when called through its API.
166+ This information does not show up in a Rust data structure carried around when these methods are
180167called.
181168However, when implementing a Rust backend for ` rustc ` , these methods
182169will need information from ` CodegenCx ` , hence the additional parameter (unused
@@ -200,15 +187,13 @@ most important elements:
200187* ` builder.rs ` : 1,400 (BA) vs 0 (LLVM);
201188* ` common.rs ` : 350 (BA) vs 350 (LLVM);
202189
203- The ` debuginfo ` folder has been left almost untouched by the splitting and is
204- specific to LLVM.
190+ The ` debuginfo ` folder has been left almost untouched by the splitting and is specific to LLVM.
205191Only its high-level features have been traitified.
206192
207193The new ` traits ` folder has 1500 LOC only for trait definitions.
208194Overall,
209195the 27,000 LOC-sized old ` rustc_codegen_llvm ` code has been split into the new
210- 18,500 LOC-sized new ` rustc_codegen_llvm ` and the 12,000 LOC-sized
211- ` rustc_codegen_ssa ` .
196+ 18,500 LOC-sized new ` rustc_codegen_llvm ` and the 12,000 LOC-sized ` rustc_codegen_ssa ` .
212197We can say that this refactoring allowed the reuse of
213198approximately 10,000 LOC that would otherwise have had to be duplicated between
214199the multiple backends of ` rustc ` .
0 commit comments