| 1 |
========================= |
| 2 |
PyPy's assembler backends |
| 3 |
========================= |
| 4 |
|
| 5 |
Draft notes about the organization of assembler backends in the PyPy JIT, in 2016 |
| 6 |
================================================================================= |
| 7 |
|
| 8 |
|
| 9 |
input: linear sequence of instructions, called a "trace". |
| 10 |
|
| 11 |
A trace is a sequence of instructions in SSA form. Most instructions |
| 12 |
correspond to one or a few CPU-level instructions. There are a few |
| 13 |
meta-instructions like `label` and debugging stuff. All branching is |
| 14 |
done with guards, which are instructions that check that a condition is |
| 15 |
true and exit the trace if not. A failing guard can have a new trace |
| 16 |
added to it later, called a "bridge". A patched guard becomes a direct |
| 17 |
`Jcond` instruction going to the bridge, with no indirection, no |
| 18 |
register spilling, etc. |
| 19 |
|
| 20 |
A trace ends with either a `return` or a `jump to label`. The target |
| 21 |
label is either inside the same trace, or in some older one. For |
| 22 |
historical reasons we call a "loop" a trace that is not a bridge. The |
| 23 |
machine code that we generate is organized as a forest of trees; the |
| 24 |
trunk of the tree is a "loop", and the branches are all bridges |
| 25 |
(branching off the trunk or off another branch). |
| 26 |
|
| 27 |
* every trunk or branch that ends in a `jump to label` can target a |
| 28 |
label from a different tree, too. |
| 29 |
|
| 30 |
* the whole process of assembling a loop or a branch is basically |
| 31 |
single-threaded, so no synchronization issue there (including to patch |
| 32 |
older generated instructions). |
| 33 |
|
| 34 |
* the generated assembler has got a "frame" in %rbp, which is actually |
| 35 |
not on the stack at all, but is a GC object (called a "jitframe"). |
| 36 |
Spilling goes there. |
| 37 |
|
| 38 |
* the guards are `Jcond` to a very small piece of generated code, which |
| 39 |
is basically pushing a couple of constants on the stack and then |
| 40 |
jumping to the general guard-recovery code. That code will save the |
| 41 |
registers into the jitframe and then exit the whole generated |
| 42 |
function. The caller of that generated function checks how it |
| 43 |
finished: if it finished by hitting a guard, then the caller is |
| 44 |
responsible for calling the "blackhole interpreter". This is the part |
| 45 |
of the front-end that recovers from failing guards and finishes |
| 46 |
running the frame (including, possibly, by jumping again into |
| 47 |
generated assembler). |
| 48 |
|
| 49 |
|
| 50 |
Details about the JITting process: |
| 51 |
|
| 52 |
* front-end and optimization pass |
| 53 |
|
| 54 |
* rewrite (includes gc related transformation as well as simplifactions) |
| 55 |
|
| 56 |
* assembler generation |
| 57 |
|
| 58 |
|
| 59 |
Front-end and optimization pass |
| 60 |
------------------------------- |
| 61 |
|
| 62 |
Not discussed here in detail. This produces loops and bridges using an |
| 63 |
instruction set that is "high-level" in some sense: it contains |
| 64 |
instructions like "new"/"new_array", and |
| 65 |
"setfield"/"setarrayitem"/"setinteriorfield" which describe the action |
| 66 |
of storing a value in a precise field of the structure or array. For |
| 67 |
example, the "setfield" action might require implicitly a GC write |
| 68 |
barrier. This is the high-level trace that we send to the following |
| 69 |
step. |
| 70 |
|
| 71 |
|
| 72 |
Rewrite |
| 73 |
------- |
| 74 |
|
| 75 |
A mostly but not completely CPU-independent phase: lowers some |
| 76 |
instructions. For example, the variants of "new" are lowered to |
| 77 |
"malloc" and a few "gc_store": it bumps the pointer of the GC and then |
| 78 |
sets a few fields explicitly in the newly allocated structure. The |
| 79 |
"setfield" is replaced with a "cond_gc_wb_call" (conditional call to the |
| 80 |
write barrier) if needed, followed by a "gc_store". |
| 81 |
|
| 82 |
The "gc_store" instruction can be encoded in a single MOV assembler |
| 83 |
instruction, but is not as flexible as a MOV. The address is always |
| 84 |
specified as "some GC pointer + an offset". We don't have the notion of |
| 85 |
interior pointer for GC objects. |
| 86 |
|
| 87 |
A different instruction, "gc_store_indexed", offers additional operands, |
| 88 |
which can be mapped to a single MOV instruction using forms like |
| 89 |
`[rax+8*rcx+24]`. |
| 90 |
|
| 91 |
Some other complex instructions pass through to the backend, which must |
| 92 |
deal with them: for example, "card marking" in the GC. (Writing an |
| 93 |
object pointer inside an array would require walking the whole array |
| 94 |
later to find "young" references. Instead of that, we flip a bit for |
| 95 |
every range of 128 entries. This is a common GC optimization.) Setting |
| 96 |
the card bit of a GC object requires a sequence of assembler |
| 97 |
instructions that depends too much on the target CPU to be expressed |
| 98 |
explicitly here (moreover, it contains a few branches, which are hard to |
| 99 |
express at this level). |
| 100 |
|
| 101 |
|
| 102 |
Assembly |
| 103 |
-------- |
| 104 |
|
| 105 |
No fancy code generation technique, but greedy forward pass that tries |
| 106 |
to avoid some pitfalls |
| 107 |
|
| 108 |
|
| 109 |
Handling instructions |
| 110 |
~~~~~~~~~~~~~~~~~~~~~ |
| 111 |
|
| 112 |
* One by one (forward direction). Each instruction asks the register |
| 113 |
allocator to ensure that some arguments are in registers (not in the |
| 114 |
jitframe); asks for a register to put its result into; and asks for |
| 115 |
additional scratch registers that will be freed at the end of the |
| 116 |
instruction. There is a special case for boolean variables: they are |
| 117 |
stored in the condition code flags instead of being materialized as a |
| 118 |
0/1 value. (They are materialized later, except in the common case |
| 119 |
where they are only used by the next `guard_false` or `guard_true` and |
| 120 |
then forgotten.) |
| 121 |
|
| 122 |
* Instruction arguments are loaded into a register on demand. This |
| 123 |
makes the backend quite easy to write, but leads do some bad |
| 124 |
decisions. |
| 125 |
|
| 126 |
|
| 127 |
Linear scan register allocation |
| 128 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 129 |
|
| 130 |
Although it's always a linear trace that we consider, we don't use |
| 131 |
advanced techniques for register allocation: we do forward, on-demand |
| 132 |
allocation as the backend produces the assembler. When it asks for a |
| 133 |
register to put some value into, we give it any free register, without |
| 134 |
consideration for what will be done with it later. We compute the |
| 135 |
longevity of all variables, but only use it when choosing which register |
| 136 |
to spill (we spill the variable with the longest longevity). |
| 137 |
|
| 138 |
This works to some extend because it is well integrated with the earlier |
| 139 |
optimization pass. Loops are unrolled once by the optimization pass to |
| 140 |
allow more powerful optimizations---the optimization pass itself is the |
| 141 |
place that benefits the most, but it also has benefits here in the |
| 142 |
assembly pass. These are: |
| 143 |
|
| 144 |
* The first peeling initializes the register binding on the first use. |
| 145 |
|
| 146 |
* This leads to an already allocated register of the trace loop. |
| 147 |
|
| 148 |
* As well as allocated registers when exiting bridges |
| 149 |
|
| 150 |
[Try to better allocate registers to match the ABI (minor to non benefit |
| 151 |
in the current state)] |
| 152 |
|
| 153 |
|
| 154 |
More complex mappings |
| 155 |
~~~~~~~~~~~~~~~~~~~~~ |
| 156 |
|
| 157 |
Some instructions generate more complex code. These are either or both of: |
| 158 |
|
| 159 |
* complex instructions generating some local control flow, like |
| 160 |
"cond_gc_wb_call" (for write barriers), "call_assembler" (a call |
| 161 |
followed by a few checks). |
| 162 |
|
| 163 |
* instructions that invoke custom assembler helpers, like the slow-path |
| 164 |
of write barriers or the slow-path of allocations. These slow-paths |
| 165 |
are typically generated too, so that we are not constrained by the |
| 166 |
usual calling conventions. |
| 167 |
|
| 168 |
|
| 169 |
GC pointers |
| 170 |
~~~~~~~~~~~ |
| 171 |
|
| 172 |
Around most CALL instructions, we need to record a description of where |
| 173 |
the GC pointers are (registers and stack frame). This is needed in case |
| 174 |
the CALL invokes a garbage collection. The GC pointers can move; the |
| 175 |
pointers in the registers and stack frame are updated by the GC. That's |
| 176 |
a reason for why we don't have explicit interior pointers. |
| 177 |
|
| 178 |
GC pointers can appear as constants in the trace. We are busy changing |
| 179 |
that to use a constant table and `MOV REG, (%RIP+offset)`. The |
| 180 |
"constant" in the table is actually updated by the GC if the object |
| 181 |
move. |
| 182 |
|
| 183 |
|
| 184 |
Vectorization |
| 185 |
~~~~~~~~~~~~~ |
| 186 |
|
| 187 |
Optimization developed to use SIMD instructions for trace loops. Primary |
| 188 |
idea was to use it as an optimization of micro numpy. It has several |
| 189 |
passes on the already optimized trace. |
| 190 |
|
| 191 |
Shortly explained: It builds dependencies for an unrolled trace loop, |
| 192 |
gathering pairs/packs of operations that could be executed in parallel |
| 193 |
and finally schedules the operations. |
| 194 |
|
| 195 |
What did it add to the code base: |
| 196 |
|
| 197 |
* Dependencies can be constructed |
| 198 |
|
| 199 |
* Code motion of guards to relax dependencies |
| 200 |
|
| 201 |
* Scheduler to reorder trace |
| 202 |
|
| 203 |
* Array bound check removal (especially for unrolled traces) |
| 204 |
|
| 205 |
What can it do: |
| 206 |
|
| 207 |
* Transform vector loops (element wise operations) |
| 208 |
|
| 209 |
* Accumulation (`reduce([...],operator,0)`). Requires Operation to be |
| 210 |
associative and commutative |
| 211 |
|
| 212 |
* SSE 4.1 as "vector backend" |
| 213 |
|
| 214 |
|
| 215 |
We do not |
| 216 |
~~~~~~~~~ |
| 217 |
|
| 218 |
* Keep tracing data around to reoptimize the trace tree. (Once a trace |
| 219 |
is compiled, minimal data is kept.) This is one reason (there are |
| 220 |
others in the front-end) for the following result: JIT-compiling a |
| 221 |
small loop with two common paths ends up as one "loop" and one bridge |
| 222 |
assembled, and the bridge-following path is slightly less efficient. |
| 223 |
This is notably because this bridge is assembled with two constraints: |
| 224 |
the input registers are fixed (from the guard), and the output |
| 225 |
registers are fixed (from the jump target); usually these two sets of |
| 226 |
fixed registers are different, and copying around is needed. |
| 227 |
|
| 228 |
* We don't join trace tails: we only assemble *trees*. |
| 229 |
|
| 230 |
* We don't do any reordering (neither of trace instructions nor of |
| 231 |
individual assembler instructions) |
| 232 |
|
| 233 |
* We don't do any cross-instruction optimization that makes sense only |
| 234 |
for the backend and can't easily be expressed at a higher level. I'm |
| 235 |
sure there are tons of examples of that, but e.g. loading a large |
| 236 |
constant in a register that will survive for several instructions; |
| 237 |
moving out of loops *parts* of some instruction like the address |
| 238 |
calculation; etc. etc. |
| 239 |
|
| 240 |
* Other optimization opportunities I can think about: look at the |
| 241 |
function prologue/epilogue; look at the overhead (small but not zero) |
| 242 |
at the start of a bridge. Also check if the way guards are |
| 243 |
implemented makes sense. Also, we generate large-ish sequences of |
| 244 |
assembler instructions with tons of `Jcond` that are almost never |
| 245 |
followed; any optimization opportunity there? (They all go forward, |
| 246 |
if it changes anything.) In theory we could also replace some of |
| 247 |
these with a signal handler on segfault (e.g. `guard_nonnull_class`). |
| 248 |
|
| 249 |
|
| 250 |
a GCC or LLVM backend? |
| 251 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 252 |
|
| 253 |
At least for comparison we'd like a JIT backend that emits its code |
| 254 |
using GCC or LLVM (irrespective of the time it would take). But it's |
| 255 |
hard to map reasonably well the guards to the C language or to LLVM IR. |
| 256 |
The problems are: (1) we have many guards, we would like to avoid having |
| 257 |
many paths that each do a full |
| 258 |
saving-all-local-variables-that-are-still-alive; (2) it's hard to patch |
| 259 |
a guard when a bridge is compiled from it; (3) instructions like a CALL |
| 260 |
need to expose the local variables that are GC pointers; CALL_MAY_FORCE |
| 261 |
need to expose *all* local variables for optional off-line |
| 262 |
reconstruction of the interpreter state. |
| 263 |
|