-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
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 all commits
3bf2baf
2d2c7e4
9ec70df
a023a52
10a7144
cb03b20
d9783ca
852cbaf
50ea367
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -506,6 +506,68 @@ After the last `DEOPT_IF` has passed, a hit should be recorded with | |||||||||||
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): | ||||||||||||
Comment on lines
+511
to
+512
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence is a dependent clause, you either need a comma and then some more information, or remove the word "assuming." How about this?
Suggested change
|
||||||||||||
|
||||||||||||
1. Update below in [Python/bytecodes.c](../Python/bytecodes.c) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like the phrase "Update below in," it's kind of wordy and an incomplete sentence. You could try something like this:
Suggested change
|
||||||||||||
|
||||||||||||
- Convert `CONTAINS_OP` to a micro-operation (uop) by renaming | ||||||||||||
it to `_CONTAINS_OP` and changing the instruction definition | ||||||||||||
from `inst` to `op`. | ||||||||||||
Comment on lines
+516
to
+518
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```c | ||||||||||||
// Before | ||||||||||||
inst(CONTAINS_OP, ...); | ||||||||||||
|
||||||||||||
// After | ||||||||||||
op(_CONTAINS_OP, ...); | ||||||||||||
``` | ||||||||||||
|
||||||||||||
- Add a uop that calls the specializing function: | ||||||||||||
|
||||||||||||
```c | ||||||||||||
specializing op(_SPECIALIZE_CONTAINS_OP, (counter/1, left, right -- left, right)) { | ||||||||||||
#if ENABLE_SPECIALIZATION | ||||||||||||
if (ADAPTIVE_COUNTER_IS_ZERO(counter)) { | ||||||||||||
next_instr = this_instr; | ||||||||||||
_Py_Specialize_ContainsOp(right, next_instr); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, presumably this would use both operands:
Suggested change
|
||||||||||||
DISPATCH_SAME_OPARG(); | ||||||||||||
} | ||||||||||||
STAT_INC(CONTAINS_OP, deferred); | ||||||||||||
DECREMENT_ADAPTIVE_COUNTER(this_instr[1].cache); | ||||||||||||
#endif /* ENABLE_SPECIALIZATION */ | ||||||||||||
} | ||||||||||||
``` | ||||||||||||
|
||||||||||||
- Create a macro for the original bytecode name: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "intruction" reads better to me here.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also might be worth mentioning here what should go in that macro, but it also seems pretty clear based on the example. |
||||||||||||
|
||||||||||||
```c | ||||||||||||
macro(CONTAINS_OP) = _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP; | ||||||||||||
``` | ||||||||||||
|
||||||||||||
2. Define the cache structure in [Include/internal/pycore_code.h](../Include/internal/pycore_code.h), | ||||||||||||
at the very least, a 16-bit counter is needed. | ||||||||||||
Comment on lines
+550
to
+551
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The clause after the first comma is independent, so it can't be used there. Let's just turn it into its own sentence.
Suggested change
|
||||||||||||
|
||||||||||||
```c | ||||||||||||
typedef struct { | ||||||||||||
uint16_t counter; | ||||||||||||
} _PyContainsOpCache; | ||||||||||||
``` | ||||||||||||
|
||||||||||||
3. Write the specializing function itself (`_Py_Specialize_ContainsOp`) in [Python/specialize.c ](../Python/specialize.c). | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
Refer to other functions in that file for the pattern. | ||||||||||||
|
||||||||||||
4. Add a call to `add_stat_dict` in `_Py_GetSpecializationStats` which is in [Python/specialize.c ](../Python/specialize.c). | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra space.
Suggested change
|
||||||||||||
|
||||||||||||
5. Add the cache layout in [Lib/opcode.py](../Lib/opcode.py) so that Python's | ||||||||||||
`dis` module will know how to represent it properly. | ||||||||||||
Comment on lines
+564
to
+565
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h). | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra space.
Suggested change
|
||||||||||||
|
||||||||||||
7. Run ``make regen-all`` on `*nix` or `build.bat --regen` on Windows. | ||||||||||||
|
||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||
|
||||||||||||
Additional resources | ||||||||||||
-------------------- | ||||||||||||
|
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.
Needs a line spacing