Skip to content

Commit 947a2d4

Browse files
hugovkFidget-SpinnerZeroIntensitybrandtbucher
authored
Apply suggestions from code review
Co-authored-by: Ken Jin <[email protected]> Co-authored-by: Peter Bierma <[email protected]> Co-authored-by: Brandt Bucher <[email protected]>
1 parent 50ea367 commit 947a2d4

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

InternalDocs/interpreter.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,15 @@ After the last `DEOPT_IF` has passed, a hit should be recorded with
506506
After an optimization has been deferred in the adaptive instruction,
507507
that should be recorded with `STAT_INC(BASE_INSTRUCTION, deferred)`.
508508

509+
509510
## How to add a new bytecode specialization
510511

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):
513513

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)
515515

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`.
519518

520519
```c
521520
// Before
@@ -532,7 +531,7 @@ Let's use the example of [`CONTAINS_OP`](../Doc/library/dis.rst#contains_op):
532531
#if ENABLE_SPECIALIZATION
533532
if (ADAPTIVE_COUNTER_IS_ZERO(counter)) {
534533
next_instr = this_instr;
535-
_Py_Specialize_ContainsOp(right, next_instr);
534+
_Py_Specialize_ContainsOp(left, right, next_instr);
536535
DISPATCH_SAME_OPARG();
537536
}
538537
STAT_INC(CONTAINS_OP, deferred);
@@ -541,30 +540,29 @@ Let's use the example of [`CONTAINS_OP`](../Doc/library/dis.rst#contains_op):
541540
}
542541
```
543542

544-
- Create a macro for the original bytecode name:
543+
- Create a macro for the original instruction name:
545544

546545
```c
547546
macro(CONTAINS_OP) = _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP;
548547
```
549548
550-
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.
552550
553551
```c
554552
typedef struct {
555553
uint16_t counter;
556554
} _PyContainsOpCache;
557555
```
558556
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).
560558
Refer to other functions in that file for the pattern.
561559
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).
563561
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.
566564
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).
568566
569567
7. Run ``make regen-all`` on `*nix` or `build.bat --regen` on Windows.
570568

0 commit comments

Comments
 (0)