| 1 |
# Borrow-checker IR (BIR) design notes |
| 2 |
|
| 3 |
## Design goals |
| 4 |
|
| 5 |
Rust GCC project aims to use the [Polonius project](https://github.com/rust-lang/polonius) as its borrow-checker. |
| 6 |
Polonius operates on a set of [facts](https://github.com/rust-lang/polonius/blob/master/polonius-engine/src/facts.rs) about the program to determine |
| 7 |
the actual lifetimes of borrows. |
| 8 |
As Polonius's primary analysis is location sensitive, the facts are tied to the program's control flow graph (CFG). |
| 9 |
Unlike rustc, which has |
| 10 |
its |
| 11 |
own three address language specific representation called [MIR](https://rustc-dev-guide.rust-lang.org/mir/index.html), gccrs uses gcc's AST based |
| 12 |
representation GENERIC. |
| 13 |
GIMPLE (the three address IR) is generated from GENERIC inside GCC and can carry no language-specific information. |
| 14 |
Therefore, we |
| 15 |
need to generate our own representation of the program's CFG. |
| 16 |
Since gccrs has no intention of having a MIR-like IR, the BIR is not to be used for |
| 17 |
code generation. |
| 18 |
Therefore, BIR carries only minimal information that is necessary for the borrow-checker and for BIR debugging. |
| 19 |
|
| 20 |
Since BIR is in fact a dead branch of the compilation pipeline, the only way to verify its generations is through manual inspection. |
| 21 |
To have some frame of reference for testing, BIR build and dump are carefully designed to resemble the textual dump of rustc's MIR as much as |
| 22 |
possible. |
| 23 |
This includes the style of the output, numbering of locals and order of basic blocks (when possible). |
| 24 |
|
| 25 |
## BIR Dump Example |
| 26 |
|
| 27 |
An example program calculating the i-th fibonacci number: |
| 28 |
|
| 29 |
```rust |
| 30 |
|
| 31 |
fn fib(i: usize) -> i32 { |
| 32 |
if i == 0 || i == 1 { |
| 33 |
1 |
| 34 |
} else { |
| 35 |
fib(i - 1) + fib(i - 2) |
| 36 |
} |
| 37 |
} |
| 38 |
``` |
| 39 |
|
| 40 |
Here is an example of BIR dump (note: this needs to be updated regularly): |
| 41 |
|
| 42 |
``` |
| 43 |
fn fib(_1: usize) -> i32 { |
| 44 |
let _0: i32; |
| 45 |
let _2: i32; |
| 46 |
let _3: bool; |
| 47 |
let _4: bool; |
| 48 |
let _5: bool; |
| 49 |
let _6: usize; |
| 50 |
let _7: i32; |
| 51 |
let _8: usize; |
| 52 |
let _9: i32; |
| 53 |
let _10: i32; |
| 54 |
|
| 55 |
bb0: { |
| 56 |
_4 = Operator(_1, const usize); |
| 57 |
switchInt(_4) -> [bb1, bb2]; |
| 58 |
} |
| 59 |
|
| 60 |
bb1: { |
| 61 |
_3 = const bool; |
| 62 |
goto -> bb3; |
| 63 |
} |
| 64 |
|
| 65 |
bb2: { |
| 66 |
_5 = Operator(_1, const usize); |
| 67 |
_3 = _5; |
| 68 |
goto -> bb3; |
| 69 |
} |
| 70 |
|
| 71 |
bb3: { |
| 72 |
switchInt(_3) -> [bb4, bb7]; |
| 73 |
} |
| 74 |
|
| 75 |
bb4: { |
| 76 |
_2 = const i32; |
| 77 |
goto -> bb8; |
| 78 |
} |
| 79 |
|
| 80 |
bb5: { |
| 81 |
_6 = Operator(_1, const usize); |
| 82 |
_7 = Call(fib)(_6, ) -> [bb6]; |
| 83 |
} |
| 84 |
|
| 85 |
bb6: { |
| 86 |
_8 = Operator(_1, const usize); |
| 87 |
_9 = Call(fib)(_8, ) -> [bb7]; |
| 88 |
} |
| 89 |
|
| 90 |
bb7: { |
| 91 |
_10 = Operator(_7, _9); |
| 92 |
_2 = _10; |
| 93 |
goto -> bb8; |
| 94 |
} |
| 95 |
|
| 96 |
bb8: { |
| 97 |
_0 = _2; |
| 98 |
return; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
``` |
| 104 |
|
| 105 |
The dump consists of: |
| 106 |
|
| 107 |
- A function header with arguments: `fn fib(_1: usize) -> i32 { ... }`. |
| 108 |
- Declaration of locals: `let _0: i32;`, where `_0` is the return value (even if it is of the unit type). Arguments are not listed here, they are |
| 109 |
listed in the function header. |
| 110 |
- A list of basic blocks: `bb0: { ... }`. The basic block name is the `bb` prefix followed by a number. |
| 111 |
- Each basic block consists of a list of BIR statements. Instruction can be either assigned to a local (place) or be a statement. |
| 112 |
Instructions take locals (places) as arguments. |
| 113 |
- Each basic block is terminated with a control flow instruction followed by a list of destinations: |
| 114 |
- `goto -> bb3;` - a goto instruction with a single destination. |
| 115 |
- `switchInt(_3) -> [bb4, bb7];` - a switch instruction with multiple destinations. |
| 116 |
- `return;` - a return instruction with no destinations. |
| 117 |
- `Call(fib)(_6, ) -> [bb6];` - a call instruction with a single destination. This section is prepared for panic handling. |
| 118 |
|
| 119 |
## BIR Structure |
| 120 |
|
| 121 |
BIR structure is defined in `gcc/rust/checks/errors/borrowck/rust-bir.h`. It is heavily inspired by rustc's MIR. The main difference is that BIR |
| 122 |
drastically reduces the amount of information carried to only borrow-checking relevant information. |
| 123 |
|
| 124 |
As borrow-checking is performed on each function independently, BIR represents a single function (`struct Function`). A `Function` consists of a list |
| 125 |
of basic blocks, list of arguments (for dump only) and place database, which keeps track of locals. |
| 126 |
|
| 127 |
### Basic Blocks |
| 128 |
|
| 129 |
A basic block is identified by its index in the function's basic block list. |
| 130 |
It contains a list of BIR statements and a list of successor |
| 131 |
basic block indices in CFG. |
| 132 |
|
| 133 |
### BIR Statements |
| 134 |
|
| 135 |
BIR statements are of three categories: |
| 136 |
|
| 137 |
- An assignment of an expression to a local (place). |
| 138 |
- A control flow operation (switch, return). |
| 139 |
- A special statement (not executable), which carries additional information for borrow-checking (`StorageDead`, `StorageLive`). |
| 140 |
|
| 141 |
#### Expressions |
| 142 |
|
| 143 |
Expressions represent the executable parts of the rust code. Many different Rust contracts are represented by a single expression, as only data (and |
| 144 |
lifetime) flow needs to be tracked. |
| 145 |
|
| 146 |
- `InitializerExpr` represents any kind of struct initialization. It can be either explicit (struct expression) or implicit (range expression, |
| 147 |
e.g. `0..=5`). |
| 148 |
- `Operator<ARITY>` represents any kind of operation, except the following, where special information is needed either for borrow-checking or for |
| 149 |
better debugging. |
| 150 |
- `BorrowExpr` represents a borrow operation. |
| 151 |
- `AssignmentExpr` holds a place for an assignment statement (i.e., no operation is done on the place, it is just assigned). |
| 152 |
- `CallExpr` represents a function call. |
| 153 |
- For functions, the callable is represented by a constant place (see below). (E.i. all calls use the same constant place.) |
| 154 |
- For closures and function pointers, the callable is represented by a (non-constant) place. |
| 155 |
|
| 156 |
### Places |
| 157 |
|
| 158 |
Places are defined in `gcc/rust/checks/errors/borrowck/rust-bir-place.h`. |
| 159 |
|
| 160 |
Places represent locals (variables), their field, and constants. They are identified by their index (`PlaceId`) in the function's place database. For |
| 161 |
better dump correspondence to MIR, constants use a different index range. |
| 162 |
|
| 163 |
Non-constant places are created according to Polonius path [documentation](https://rust-lang.github.io/polonius/rules/atoms.html). The following |
| 164 |
grammar describes |
| 165 |
possible path elements: |
| 166 |
|
| 167 |
``` |
| 168 |
Path = Variable |
| 169 |
| Path "." Field // field access |
| 170 |
| Path "[" "]" // index |
| 171 |
| "*" Path |
| 172 |
``` |
| 173 |
|
| 174 |
It is important to highlight that different fields are assigned to different places; however, all indices are assigned to the same place. |
| 175 |
Also, to match the output of rustc. |
| 176 |
In dump, paths contain at most one dereference and are split otherwise. |
| 177 |
Same paths always result in the same place. |
| 178 |
|
| 179 |
Variables are identified by `AST` `NodeId`. Fields indexes are taken from `TyTy` types. |
| 180 |
|
| 181 |
Each place holds indices to its next relatives (in the path tree), `TyTy` type, lifetime and information whether the type can be copies or it needs to |
| 182 |
be moved. Not that unlike rustc, we copy any time we can (for simplicity), while rustc prefers to move if possible (only a single copy is held). |
| 183 |
|
| 184 |
## BIR Builders |
| 185 |
|
| 186 |
There are multiple builders (visitor classes) for BIR based on what context is needed in them. |
| 187 |
provides the entry point that handles function parameters and return values, and it creates the BIR main unit `Function`. |
| 188 |
`rust-bir-internal.h` provides abstract builder classes with common helper methods for all builder and for expression builders. |
| 189 |
Specific builders are then defined for expressions+statements, lazy boolean expressions, patterns, and struct initialization. |