Repository navigation
gh-119786: Add jit.md. Move adaptive.md to a section of interpreter.md. #127175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2fb97e3
gh-119786: add intro to adaptive.md. Update index headings
iritkatriel 2414871
add content to tier2
iritkatriel e040267
review comments
iritkatriel e5802d7
add jit
iritkatriel 0c96eb2
merge adaptive.md into interpreter.md
iritkatriel 43aa0df
cache layouts
iritkatriel e7c3363
tier 2 --> jit
iritkatriel 4731d94
remove adaptive.md
iritkatriel c4b288b
mike's comments
iritkatriel aced06a
Apply suggestions from code review
iritkatriel bc8a50b
interpreter jit is for debugging
iritkatriel 3bbf6ee
Update InternalDocs/jit.md
iritkatriel acaef71
Merge branch 'main' into adaptive
iritkatriel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add content to tier2
- Loading branch information
commit 24148714e28c8d1750f56ba4faa4f045e2755e84
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,67 @@ | ||
| # The Tier 2 Interpreter | ||
|
|
||
| Coming soon. | ||
| The [basic interpreter](interpreter.md), also referred to as the `tier 1` | ||
| interpreter, consists of a main loop that executes the bytecode instructions | ||
| generated by the [bytecode compiler](compiler.md) and their | ||
| [specializations](adaptive.md). Runtime optimization in tier 1 can only be | ||
| done for one instruction at a time. The `tier 2` interpreter is based on a | ||
| mechanism to replace an entire sequence of bytecode instructions, and this | ||
| enables optimizations that span multiple instructions. | ||
|
|
||
| ## The Optimizer and Executors | ||
|
|
||
| The program begins running in tier 1, until a `JUMP_BACKWARD` instruction | ||
| determines that it is `hot` because the counter in its | ||
|
iritkatriel marked this conversation as resolved.
Outdated
|
||
| [inline cache](interpreter.md#inline-cache-entries) indicates that is | ||
| executed more than some threshold number of times. It then calls the | ||
|
iritkatriel marked this conversation as resolved.
Outdated
|
||
| function `_PyOptimizer_Optimize()` in | ||
| [`Python/optimizer.c`](../Python/optimizer.c), passing it the current | ||
| [frame](frames.md) and instruction pointer. `_PyOptimizer_Optimize()` | ||
| constructs an object of type | ||
| [`_PyExecutorObject`](Include/internal/pycore_optimizer.h) which implements | ||
| an optimized version of the instruction trace beginning at this jump. | ||
|
|
||
| The optimizer determines where the trace ends, and the executor is set up | ||
| to either return to `tier 1` and resume execution, or transfer control | ||
| to another executor (see `_PyExitData` in Include/internal/pycore_optimizer.h). | ||
|
|
||
| The executor is stored on the [`code object`](code_objects.md) of the frame, | ||
| in the `co_executors` field which is an array of executors. The start | ||
| instruction of the trace (the `JUMP_BACKWARD`) is replaced by an | ||
| `ENTER_EXECUTOR` instruction whose `oparg` is equal to the index of the | ||
| executor in `co_executors`. | ||
|
|
||
| ## The uop optimizer | ||
|
iritkatriel marked this conversation as resolved.
Outdated
|
||
|
|
||
| The optimizer that `_PyOptimizer_Optimize()` runs is configurable | ||
| via the `_Py_SetTier2Optimizer()` function (this is used in test | ||
| via `_testinternalcapi.set_optimizer()`.) | ||
|
iritkatriel marked this conversation as resolved.
Outdated
|
||
|
|
||
| The tier 2 optimizer, `_PyUOpOptimizer_Type`, is defined in | ||
| [`Python/optimizer.c`](../Python/optimizer.c). It translates | ||
| an instruction trace into a sequence of micro-ops by replacing | ||
| each bytecode by an equivalent sequence of micro-ops | ||
| (see `_PyOpcode_macro_expansion` in | ||
| [pycore_opcode_metadata.h](../Include/internal/pycore_opcode_metadata.h) | ||
| which is generated from [`Python/bytecodes.c`](../Python/bytecodes.c)). | ||
| The micro-op sequence is then optimized by | ||
| `_Py_uop_analyze_and_optimize` in | ||
| [`Python/optimizer_analysis.c`](../Python/optimizer_analysis.c). | ||
|
|
||
| ## Running a uop executor | ||
|
|
||
| After a tier 1 `JUMP_BACKWARD` instruction invokes the uop optimizer | ||
| to create a tier 2 uop executor, it transfers control to this executor | ||
| via the `GOTO_TIER_TWO` macro, which jumps to `tier2_dispatch:` in | ||
| [`Python/ceval.c`](../Python/ceval.c), where there is a loops that | ||
|
iritkatriel marked this conversation as resolved.
Outdated
|
||
| executes the micro-ops which are defined in | ||
| [`Python/executor_cases.c.h`](../Python/executor_cases.c.h). | ||
| This loop exits when an `_EXIT_TRACE` or `_DEOPT` uop is reached. | ||
|
iritkatriel marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Invalidating Executors | ||
|
|
||
| In addition to being stored on the code object, each executor is also | ||
| inserted into a list of all executors which is stored in the interpreter | ||
| state's `executor_list_head` field. This list is used when it is necessary | ||
| to invalidate executors because values that their construction depended | ||
| on may have changed. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is potentially confusing. The tier 2 interpreter is, in my mind, only used to debug tier 2 code. It is the jit-compiled machine code that executes tier 2 normally.
Also, I think we want to move away from "tier 2" and use "JIT". See faster-cpython/ideas#614 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm making this change, but the code does refer to tier 2 in various places so I think it needs to be mentioned, otherwise future devs will be confused.