-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
Document how to add a bytecode specialization in Interpreter.md file #130831
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
3bf2baf
2d2c7e4
9ec70df
a023a52
10a7144
cb03b20
d9783ca
852cbaf
50ea367
947a2d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -505,6 +505,62 @@ After the last `DEOPT_IF` has passed, a hit should be recorded with | |||||||
| `STAT_INC(BASE_INSTRUCTION, hit)`. | ||||||||
| After an optimization has been deferred in the adaptive instruction, | ||||||||
| that should be recorded with `STAT_INC(BASE_INSTRUCTION, deferred)`. | ||||||||
| ## How to add a new bytecode specialization | ||||||||
|
|
||||||||
| Assuming you found an instruction that serves as a good specialization candidate. | ||||||||
| Let's use the example of [`CONTAINS_OP`](../Doc/library/dis.rst#contains_op): | ||||||||
hugovk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| 1. Update below in [Python/bytecodes.c](../Python/bytecodes.c) | ||||||||
hugovk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| - Convert `CONTAINS_OP` to a micro-operation (uop) by renaming | ||||||||
| it to `_CONTAINS_OP` and changing the instruction definition | ||||||||
| from `inst` to `op`. | ||||||||
hugovk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| ```c | ||||||||
| // Before | ||||||||
| inst(CONTAINS_OP, ...); | ||||||||
|
|
||||||||
| // After | ||||||||
| op(_CONTAINS_OP, ...); | ||||||||
| ``` | ||||||||
|
|
||||||||
| - Add a uop that calls the specializing function `_SPECIALIZE_CONTAINS_OP`. | ||||||||
| For example. | ||||||||
|
||||||||
| - Add a uop that calls the specializing function `_SPECIALIZE_CONTAINS_OP`. | |
| For example. | |
| - Add a uop that calls the specializing function: |
hugovk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
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.
Create a macro for the original bytecode name:
macro(CONTAINS_OP) = _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP;
hugovk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
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.
| 4. Remember to update operation stats by calling add_stat_dict in | |
| 4. Remember to update operation stats by calling `add_stat_dict` in |
hugovk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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.
Seems like the only thing missing is adding an actual specialized variant. Maybe that's implied/obvious, but it wouldn't hurt to provide a dumb example of _Py_Specialize_ContainsOp and _CONTAINS_OP_UNICODE_UNICODE that just guards and calls PyUnicode_Contains or something.
Uh oh!
There was an error while loading. Please reload this page.