| 1 |
PyJitPl5 |
| 2 |
======== |
| 3 |
|
| 4 |
This document describes the fifth generation of the RPython JIT generator. |
| 5 |
|
| 6 |
|
| 7 |
Implementation of the JIT |
| 8 |
------------------------- |
| 9 |
|
| 10 |
The JIT's :doc:`theory <overview>` is great in principle, but the actual code is a different |
| 11 |
story. This section tries to give a high level overview of how RPython's JIT is |
| 12 |
implemented. It's helpful to have an understanding of how the :doc:`RPython translation |
| 13 |
toolchain <../translation>` works before digging into the sources. |
| 14 |
|
| 15 |
Almost all JIT specific code is found in rpython/jit subdirectories. Translation |
| 16 |
time code is in the codewriter directory. The metainterp directory holds |
| 17 |
platform independent code including the the tracer and the optimizer. Code in |
| 18 |
the backend directory is responsible for generating machine code. |
| 19 |
|
| 20 |
|
| 21 |
JIT hints |
| 22 |
~~~~~~~~~ |
| 23 |
|
| 24 |
To add a JIT to an interpreter, RPython only requires two hints to be added to |
| 25 |
the target interpreter. These are jit_merge_point and can_enter_jit. |
| 26 |
jit_merge_point is supposed to go at the start of opcode dispatch. It allows |
| 27 |
the JIT to bail back to the interpreter in case running machine code is no |
| 28 |
longer suitable. can_enter_jit goes at the end of a application level loop. In |
| 29 |
the Python interpreter, this is the JUMP_ABSOLUTE bytecode. The Python |
| 30 |
interpreter defines its hints in pypy/module/pypyjit/interp_jit.py in a few |
| 31 |
overridden methods of the default interpreter loop. |
| 32 |
|
| 33 |
An interpreter wishing to use the RPython JIT generator must define a list of *green* |
| 34 |
variables and a list of *red* variables. The *green* variables are loop |
| 35 |
constants. They are used to identify the current loop. Red variables are for |
| 36 |
everything else used in the execution loop. For example, the Python interpreter |
| 37 |
passes the code object and the instruction pointer as greens and the frame |
| 38 |
object and execution context as reds. These objects are passed to the JIT at |
| 39 |
the location of the JIT hints. |
| 40 |
|
| 41 |
|
| 42 |
JIT Generation |
| 43 |
~~~~~~~~~~~~~~ |
| 44 |
|
| 45 |
After the RTyping phase of translation, where high level Python operations are |
| 46 |
turned into low-level ones for the backend, the translation driver calls |
| 47 |
apply_jit() in metainterp/warmspot.py to add a JIT compiler to the currently |
| 48 |
translating interpreter. apply_jit() decides what assembler backend to use then |
| 49 |
delegates the rest of the work to the WarmRunnerDesc class. WarmRunnerDesc |
| 50 |
finds the two JIT hints in the function graphs. It rewrites the graph |
| 51 |
containing the jit_merge_point hint, called the portal graph, to be able to |
| 52 |
handle special JIT exceptions, which indicate special conditions to the |
| 53 |
interpreter upon exiting from the JIT. The location of the can_enter_jit hint |
| 54 |
is replaced with a call to a function, maybe_compile_and_run in warmstate.py, |
| 55 |
that checks if current loop is "hot" and should be compiled. |
| 56 |
|
| 57 |
Next, starting with the portal graph, codewriter/\*.py converts the graphs of the |
| 58 |
interpreter into JIT bytecode. Since this bytecode is stored in the final |
| 59 |
binary, it's designed to be concise rather than fast. The bytecode codewriter |
| 60 |
doesn't "see" (what it sees is defined by the JIT's policy) every part of the |
| 61 |
interpreter. In these cases, it simply inserts an opaque call. |
| 62 |
|
| 63 |
Finally, translation finishes, including the bytecode of the interpreter in the |
| 64 |
final binary, and interpreter is ready to use the runtime component of the JIT. |
| 65 |
|
| 66 |
|
| 67 |
Tracing |
| 68 |
~~~~~~~ |
| 69 |
|
| 70 |
Application code running on the JIT-enabled interpreter starts normally; it is |
| 71 |
interpreted on top of the usual evaluation loop. When an application loop is |
| 72 |
closed (where the can_enter_jit hint was), the interpreter calls the |
| 73 |
maybe_compile_and_run() method of WarmEnterState. This method increments a |
| 74 |
counter associated with the current green variables. When this counter reaches |
| 75 |
a certain level, usually indicating the application loop has been run many |
| 76 |
times, the JIT enters tracing mode. |
| 77 |
|
| 78 |
*Tracing* is where JIT interprets the bytecode, generated at translation time, |
| 79 |
of the interpreter interpreting the application level code. This allows it to |
| 80 |
see the exact operations that make up the application level loop. Tracing is |
| 81 |
performed by MetaInterp and MIFrame classes in metainterp/pyjitpl.py. |
| 82 |
maybe_compile_and_run() creates a MetaInterp and calls its |
| 83 |
compile_and_run_once() method. This initializes the MIFrame for the input |
| 84 |
arguments of the loop, the red and green variables passed from the |
| 85 |
jit_merge_point hint, and sets it to start interpreting the bytecode of the |
| 86 |
portal graph. |
| 87 |
|
| 88 |
Before starting the interpretation, the loop input arguments are wrapped in a |
| 89 |
*box*. Boxes (defined in metainterp/history.py) wrap the value and type of a |
| 90 |
value in the program the JIT is interpreting. There are two main varieties of |
| 91 |
boxes: constant boxes and normal boxes. Constant boxes are used for values |
| 92 |
assumed to be known during tracing. These are not necessarily compile time |
| 93 |
constants. All values which are "promoted", assumed to be constant by the JIT |
| 94 |
for optimization purposes, are also stored in constant boxes. Normal boxes |
| 95 |
contain values that may change during the running of a loop. There are three |
| 96 |
kinds of normal boxes: BoxInt, BoxPtr, and BoxFloat, and four kinds of constant |
| 97 |
boxes: ConstInt, ConstPtr, ConstFloat, and ConstAddr. (ConstAddr is only used |
| 98 |
to get around a limitation in the translation toolchain.) |
| 99 |
|
| 100 |
The meta-interpreter starts interpreting the JIT bytecode. Each operation is |
| 101 |
executed and then recorded in a list of operations, called the trace. |
| 102 |
Operations can have a list of boxes they operate on, arguments. Some operations |
| 103 |
(like GETFIELD and GETARRAYITEM) also have special objects that describe how |
| 104 |
their arguments are laid out in memory. All possible operations generated by |
| 105 |
tracing are listed in metainterp/resoperation.py. When a (interpreter-level) |
| 106 |
call to a function the JIT has bytecode for occurs during tracing, another |
| 107 |
MIFrame is added to the stack and the tracing continues with the same history. |
| 108 |
This flattens the list of operations over calls. Most importantly, it unrolls |
| 109 |
the opcode dispatch loop. Interpretation continues until the can_enter_jit hint |
| 110 |
is seen. At this point, a whole iteration of the application level loop has |
| 111 |
been seen and recorded. |
| 112 |
|
| 113 |
Because only one iteration has been recorded the JIT only knows about one |
| 114 |
codepath in the loop. For example, if there's a if statement construct like |
| 115 |
this:: |
| 116 |
|
| 117 |
if x: |
| 118 |
do_something_exciting() |
| 119 |
else: |
| 120 |
do_something_else() |
| 121 |
|
| 122 |
and ``x`` is true when the JIT does tracing, only the codepath |
| 123 |
``do_something_exciting`` will be added to the trace. In future runs, to ensure |
| 124 |
that this path is still valid, a special operation called a *guard operation* is |
| 125 |
added to the trace. A guard is a small test that checks if assumptions the JIT |
| 126 |
makes during tracing are still true. In the example above, a GUARD_TRUE guard |
| 127 |
will be generated for ``x`` before running ``do_something_exciting``. |
| 128 |
|
| 129 |
Once the meta-interpreter has verified that it has traced a loop, it decides how |
| 130 |
to compile what it has. There is an optional optimization phase between these |
| 131 |
actions which is covered future down this page. The backend converts the trace |
| 132 |
operations into assembly for the particular machine. It then hands the compiled |
| 133 |
loop back to the frontend. The next time the loop is seen in application code, |
| 134 |
the optimized assembly can be run instead of the normal interpreter. |
| 135 |
|
| 136 |
|
| 137 |
Optimizations |
| 138 |
~~~~~~~~~~~~~ |
| 139 |
|
| 140 |
The JIT employs several techniques, old and new, to make machine code run |
| 141 |
faster. |
| 142 |
|
| 143 |
Virtuals and Virtualizables |
| 144 |
""""""""""""""""""""""""""" |
| 145 |
|
| 146 |
A *virtual* value is an array, struct, or RPython level instance that is created |
| 147 |
during the loop and does not escape from it via calls or longevity past the |
| 148 |
loop. Since it is only used by the JIT, it can be "optimized out"; the value |
| 149 |
doesn't have to be allocated at all and its fields can be stored as first class |
| 150 |
values instead of dereferencing them in memory. Virtuals allow temporary objects |
| 151 |
in the interpreter to be unwrapped. For example, a W_IntObject in the PyPy interpreter can |
| 152 |
be unwrapped to just be its integer value as long as the object is known not to |
| 153 |
escape the machine code. |
| 154 |
|
| 155 |
A *virtualizable* is similar to a virtual in that its structure is optimized out |
| 156 |
in the machine code. Virtualizables, however, can escape from JIT controlled |
| 157 |
code. |
| 158 |
|
| 159 |
Other optimizations |
| 160 |
""""""""""""""""""" |
| 161 |
|
| 162 |
Most of the JIT's optimizer is contained in the subdirectory |
| 163 |
``metainterp/optimizeopt/``. Refer to it for more details. |
| 164 |
|
| 165 |
|
| 166 |
More resources |
| 167 |
-------------- |
| 168 |
|
| 169 |
More documentation about the current JIT is available as a first published |
| 170 |
article: |
| 171 |
|
| 172 |
* `Tracing the Meta-Level: PyPy's Tracing JIT Compiler`__ |
| 173 |
|
| 174 |
.. __: https://foss.heptapod.net/pypy/extradoc/-/tree/branch/extradoc/icooolps2009/bolz-tracing-jit-final.pdf |
| 175 |
|
| 176 |
Chapters 5 and 6 of `Antonio Cuni's PhD thesis`__ contain an overview of how |
| 177 |
Tracing JITs work in general and more information about the concrete case of |
| 178 |
PyPy's JIT. |
| 179 |
|
| 180 |
.. __: http://antocuni.eu/download/antocuni-phd-thesis.pdf |
| 181 |
|
| 182 |
The `blog posts with the JIT tag`__ might also contain additional information. |
| 183 |
|
| 184 |
.. __: https://www.pypy.org/categories/jit.html |