You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: InternalDocs/interpreter.md
+13-15Lines changed: 13 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -506,16 +506,15 @@ After the last `DEOPT_IF` has passed, a hit should be recorded with
506
506
After an optimization has been deferred in the adaptive instruction,
507
507
that should be recorded with `STAT_INC(BASE_INSTRUCTION, deferred)`.
508
508
509
+
509
510
## How to add a new bytecode specialization
510
511
511
-
Assuming you found an instruction that serves as a good specialization candidate.
512
-
Let's use the example of [`CONTAINS_OP`](../Doc/library/dis.rst#contains_op):
512
+
Let's say you found an instruction that serves as a good specialization candidate, such as [`CONTAINS_OP`](../Doc/library/dis.rst#contains_op):
513
513
514
-
1.Update below in [Python/bytecodes.c](../Python/bytecodes.c)
514
+
1.Make necessary changes to the instruction in [Python/bytecodes.c](../Python/bytecodes.c)
515
515
516
-
- Convert `CONTAINS_OP` to a micro-operation (uop) by renaming
517
-
it to `_CONTAINS_OP` and changing the instruction definition
518
-
from `inst` to `op`.
516
+
- Convert the instruction (`CONTAINS_OP`, in our example) to a micro-operation (uop, formally μop) by renaming it to `_INSTRUCTION_NAME` (e.g., `_CONTAINS_OP`) and changing the instruction definition
517
+
from `inst` to `op`.
519
518
520
519
```c
521
520
// Before
@@ -532,7 +531,7 @@ Let's use the example of [`CONTAINS_OP`](../Doc/library/dis.rst#contains_op):
2. Define the cache structure in [Include/internal/pycore_code.h](../Include/internal/pycore_code.h),
551
-
at the very least, a 16-bit counter is needed.
549
+
2. Define the cache structure in [Include/internal/pycore_code.h](../Include/internal/pycore_code.h). It needs to have at least a 16-bit counter field.
552
550
553
551
```c
554
552
typedef struct {
555
553
uint16_t counter;
556
554
} _PyContainsOpCache;
557
555
```
558
556
559
-
3. Write the specializing function itself (`_Py_Specialize_ContainsOp`) in [Python/specialize.c ](../Python/specialize.c).
557
+
3. Write the specializing function itself (e.g., `_Py_Specialize_ContainsOp`) in [Python/specialize.c ](../Python/specialize.c).
560
558
Refer to other functions in that file for the pattern.
561
559
562
-
4. Add a call to `add_stat_dict` in `_Py_GetSpecializationStats` which is in [Python/specialize.c ](../Python/specialize.c).
560
+
4. Add a call to `add_stat_dict` in `_Py_GetSpecializationStats` which is in [Python/specialize.c ](../Python/specialize.c).
563
561
564
-
5. Add the cache layout in [Lib/opcode.py](../Lib/opcode.py) so that Python's
565
-
`dis` module will know how to represent it properly.
562
+
5. Add the cache layout to [Lib/opcode.py](../Lib/opcode.py) so that the
563
+
`dis` module will know how to represent it properly.
566
564
567
-
6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
565
+
6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
568
566
569
567
7. Run ``make regen-all`` on `*nix` or `build.bat --regen` on Windows.
0 commit comments