8000 docs: update readme with JIT (#75) · oraluben/cpython@5a26cb9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a26cb9

Browse files
docs: update readme with JIT (python#75)
Co-authored-by: Jules <julia.poo.poo.poo@gmail.com>
1 parent bf19b03 commit 5a26cb9

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

README.md

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Type propagation means we can take the type information gathered from a single b
3636

3737
Type check removal means removing type checks in dynamic Python. E.g. if you have ``a + b``, the fast path in Python has to check that these are `int` or `str` or `float`, then if all those fail, rely on a generic `+` function. These type checks incur overhead at runtime. With type information, if we know the types, that we don't need any type checks at all! This means we can eliminate type checks.
3838

39-
We had a rudimentary test script and Doxygen documentation for all our functions to follow common SWE practices.
39+
We have a rudimentary test script and Doxygen documentation for all our functions to follow common SWE practices.
4040

4141
#### Orbital
4242

@@ -45,7 +45,7 @@ This Orbital, we intend to refine pyLBBV. These include but are not limited to:
4545
- [X] Bug fixing
4646
- [X] A more advanced type propagator.
4747
- [X] A more comprehensive test suite with Continuous Integration testing.
48-
- [ ] A copy and patch JIT compiler. (Not yet implemented!)
48+
- [X] A copy and patch JIT compiler.
4949

5050
A JIT(Just-in-Time) compiler is just a program that generates native machine executable code at runtime. [Copy and Patch](https://arxiv.org/abs/2011.13127) is a new fast compilation technique developed rather recently. The general idea is that compilation normally requires multiple steps, thus making compilation slow (recall how many steps your SICP meta-circular evaluator needs to execute JS)! Copy and patch makes compilation faster by skipping all the intermediate steps, and just creating "templates" for
5151
the final code. These "templates" are called *stencils* and they contain *holes*, i.e. missing values. All you have to do for compilation now is to copy and template, and patch in the holes. Thus making it very fast!
@@ -81,8 +81,41 @@ We did a major refactor of our code generation machinery. This makes the code ea
8181
![image](./orbital/CI.png)
8282
- All PRs require review and an approval before merging is allowed. All tests in CI must also pass. This follows standard best practices.
8383

84+
##### Copy and Patch JIT Compiler
85+
86+
The copy-and-patch JIT compiler uses a stencil compiler provided by Brandt Bucher.
87+
88+
- At runtime, each basic block, except the branches (exits) are compiled to machine code.
< 10000 /td>89+
- If compilation is successful, execution jumps into the machine code rather than the interpreter bytecode.
90+
- The branches remain as CPython interpreter bytecode, to faciliatate easy branching.
91+
- Upon encountering a branch, the interpreter leaves the machine code to go back into bytecode.
92+
- Execution thus interleaves between machine code and the interpreter.
93+
8494
## Architecture Diagram and Design
8595

96+
### Compile-time
97+
98+
```mermaid
99+
flowchart TD
100+
dsl[bytecodes.c<br> Modified DSL <br> containing CPython <br> bytecode definitions]
101+
typeprop[tier2_typepropagator.c.h <br> Type propagator]
102+
instdef[generated_cases.c.h <br> CPython bytecode <br> interpreter <br> includes Tier 2 <br> bytecode]
103+
stencils[jit_stencil.h <br> JIT versions of <br> CPython bytecode]
104+
tier2[Tier2 Execution]
105+
instdef -- Used in --> tier2
106+
stencils -- Used in --> tier2
107+
typeprop -- Used in --> tier2
108+
tier2 -- Component of --> python.exe
109+
subgraph Our Project
110+
dsl -- Generates --> instdef
111+
dsl -- Generates --> typeprop
112+
end
113+
subgraph Brandt Bucher's Stencil Compiler
114+
instdef -- Compiles into --> stencils
115+
end
116+
```
117+
118+
### Runtime
86119
```mermaid
87120
sequenceDiagram
88121
autonumber
@@ -100,25 +133,29 @@ sequenceDiagram
100133
end
101134
Tier 1 ->> Tier 2: Code executed more <br> than 63 times and <br> Tier 1 instructions <br> present, triggers the <br> Tier 2 interpreter
102135
loop Until exit scope executed
103-
loop until Tier 2 encounters type-specialised tier 1 instruction
136+
loop until Tier 2 encounters type-specialised tier 1 instruction or a branch instruction
104137
Note over Tier 2: Tier 2 copies Tier 1 <br> instructions into a <br> buffer to be executed <br> according to runtime <br> type info
105138
Tier 2 ->> Type Propagator: Requests type propagator
106139
Type Propagator ->> Tier 2: Type propagator <br> updates runtime type <br> info based on <br>newly emitted code
107140
end
108-
Note over Tier 2: Emits a typeguard <br> and executes Tier 2 code <br> until typeguard is hit.
141+
Note over Tier 2: Emits a typeguard <br> or branch
142+
Note over Tier 2: JIT compiles current <br> basic block into <br> machine code
143+
Note over Tier 2: Execute machine code <br> until the end of the <br> basic block
144+
Note over Tier 2: Execute the Tier 2 <br> branch instruction <br> or type guard
109145
Tier 2 ->> Type Propagator: Requests type propagator
110146
Type Propagator ->> Tier 2: Type propagator updates <br> runtime type info <br> based on branch taken
111-
Note over Tier 2: Emits type specialised <br> branch according to <br> runtime type info
147+
Note over Tier 2: Starts emitting <br> type specialised <br> branch according to <br> runtime type info
112148
end
113149
```
114150

115151
## What's left for our project
116152

117-
- The Copy and Patch JIT compiler.
153+
- Just general bugfixing.
154+
- Evaluation and benchmarking
118155

119156
## Evaluation and benchmarks
120157

121-
We will run the [bm_nbody.py](./bm_nbody.py) script and the [bm_float_unboxed.py](./bm_float_unboxed.py) to gather results. For now we expect performance to have no improvement as we have yet to implement the copy and patch JIT compiler.
158+
We will run the [bm_nbody.py](./bm_nbody.py) script and the [bm_float_unboxed.py](./bm_float_unboxed.py) to gather results.
122159

123160
## Changelog over CS4215
124161

@@ -148,6 +185,8 @@ We will run the [bm_nbody.py](./bm_nbody.py) script and the [bm_float_unboxed.py
148185

149186
## Build instructions
150187

188+
You should install LLVM 16.0 for your system.
189+
151190
You should follow the official CPython build instructions for your platform.
152191
https://devguide.python.org/getting-started/setup-building/
153192

@@ -168,13 +207,16 @@ After building, run `python tier2_test.py` or `python.bat tier2_test.py` (on Win
168207

169208
## Debugging output
170209

171-
In `tier2.c`, two flags can be set to print debug messages:
210+
In `tier2.c`, three flags can be set to print debug messages:
172211
```c
173-
// Prints codegen debug messages
212+
// Prints Tier 2 intepreter codegen debug messages
174213
#define BB_DEBUG 0
175214

176215
// Prints typeprop debug messages
177216
#define TYPEPROP_DEBUG 0
217+
218+
// Prints JIT codegen debug messages
219+
#define JIT_DEBUG 0
178220
```
179221
180222
## Addendum

0 commit comments

Comments
 (0)
0