Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
faizanoor3001 wants to merge9 commits intopython:main
base:main
Choose a base branch
Loading
fromfaizanoor3001:add-bytecode-specialization-guide

Conversation

faizanoor3001
Copy link

Solves:python/devguide#1286

Description

  • Add a "How to Add a New Bytecode Specialization” section in the InternalDocs/interpreter.md file.
    • This guide explains how to add a new bytecode specialization, using
      CONTAINS_OP as an example.

PS: Initial PR for reference:python/devguide#1522


📚 Documentation preview 📚:https://cpython-devguide--1522.org.readthedocs.build/

@bedevere-appbedevere-appbot added docsDocumentation in the Doc dir skip news awaiting review labelsMar 4, 2025
@faizanoor3001faizanoor3001 changed the titlegh-1286 : Document how to add a bytecode specialization in Interpreter.md filepython/devguide#1286 : Document how to add a bytecode specialization in Interpreter.md fileMar 4, 2025
@faizanoor3001faizanoor3001 changed the titlepython/devguide#1286 : Document how to add a bytecode specialization in Interpreter.md filegh-1286 : Document how to add a bytecode specialization in Interpreter.md fileMar 4, 2025
Copy link
Member

@Fidget-SpinnerFidget-Spinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks. Look good mostly. Just a few minor edits.

@@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Needs a line spacing

Suggested change
##How to add a new bytecode specialization
##How to add a new bytecode specialization

faizanoor3001 reacted with thumbs up emoji

3. Write the specializing function itself in [Python/specialize.c ](../Python/specialize.c).
Refer to any other function in that file for the format.
4. Remember to update operation stats by calling add_stat_dict in

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
4. Remember to update operation stats by calling add_stat_dict in
4. Remember to update operation stats by calling`add_stat_dict` in

faizanoor3001 reacted with thumbs up emoji
@Fidget-SpinnerFidget-Spinner changed the titlegh-1286 : Document how to add a bytecode specialization in Interpreter.md fileDocument how to add a bytecode specialization in Interpreter.md fileMar 4, 2025
Copy link
Member

@iritkatrieliritkatriel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

A few tweak/suggestions

Comment on lines 528 to 529
- Add a uop that calls the specializing function `_SPECIALIZE_CONTAINS_OP`.
For example.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
- Add a uop that calls the specializing function`_SPECIALIZE_CONTAINS_OP`.
For example.
- Add a uop that calls the specializing function:

faizanoor3001 reacted with thumbs up emoji
Comment on lines 545 to 546
- The original `CONTAINS_OP` is now a new macro consisting of
`_SPECIALIZE_CONTAINS_OP` and `_CONTAINS_OP`.
Copy link
Member

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;

faizanoor3001 reacted with thumbs up emoji
Comment on lines 557 to 565
3. Write the specializing function itself in [Python/specialize.c ](../Python/specialize.c).
Refer to any other function in that file for the format.
4. Remember to update operation stats by calling `add_stat_dict` in
[Python/specialize.c ](../Python/specialize.c).
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.
6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
7. Run ``make regen-all`` on `*nix` or `build.bat --regen` on Windows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
3. Write the specializing function itself in[Python/specialize.c](../Python/specialize.c).
Refer to any other function in that file for the format.
4. Remember to update operation stats by calling`add_stat_dict` in
[Python/specialize.c](../Python/specialize.c).
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.
6. Bump magic number in[Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
7. Run``make regen-all`` on`*nix` or`build.bat --regen` on Windows.
3. Write the specializing function itself (_Py_Specialize_ContainsOp) in[Python/specialize.c](../Python/specialize.c).
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).
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.
6. Bump magic number in[Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
7. Run`make regen-all` on`*nix` or`build.bat --regen` on Windows.

faizanoor3001 reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This suggestion has some edits to the text as well, not just whitespace.

faizanoor3001 reacted with thumbs up emoji
@iritkatriel
Copy link
Member

Let's give@brandtbucher a chance to review as well.

faizanoor3001 reacted with thumbs up emoji

Copy link
Member

@brandtbucherbrandtbucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Looks correct. A couple of possible minor improvements, if we're aiming to be comprehensive here:

#if ENABLE_SPECIALIZATION
if (ADAPTIVE_COUNTER_IS_ZERO(counter)) {
next_instr = this_instr;
_Py_Specialize_ContainsOp(right, next_instr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Nit, presumably this would use both operands:

Suggested change
_Py_Specialize_ContainsOp(right, next_instr);
_Py_Specialize_ContainsOp(left,right, next_instr);

6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).

7. Run ``make regen-all`` on `*nix` or `build.bat --regen` on Windows.

Copy link
Member

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 callsPyUnicode_Contains or something.

ZeroIntensity reacted with thumbs up emoji
Copy link
Member

@ZeroIntensityZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Several grammar nitpicks--English is hard!

Comment on lines +511 to +512
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):
Copy link
Member

Choose a reason for hiding this comment

The 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
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):
Let's say you found an instruction that serves as a good specialization candidate, such as[`CONTAINS_OP`](../Doc/library/dis.rst#contains_op):

cliff688 reacted with thumbs up emoji
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):

1. Update below in [Python/bytecodes.c](../Python/bytecodes.c)
Copy link
Member

Choose a reason for hiding this comment

The 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
1.Update below in[Python/bytecodes.c](../Python/bytecodes.c)
1.Make necessary changes to the instruction in[Python/bytecodes.c](../Python/bytecodes.c)

cliff688 reacted with thumbs up emoji
Comment on lines +516 to +518
- Convert `CONTAINS_OP` to a micro-operation (uop) by renaming
it to `_CONTAINS_OP` and changing the instruction definition
from `inst` to `op`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

  • CONTAINS_OP is still an example here, we should try to be more general.
  • For clarity, we could explain why it's a "u."
  • Reduce indentation.
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`.
- 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
from`inst` to`op`.

}
```

- Create a macro for the original bytecode name:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

"intruction" reads better to me here.

Suggested change
- Create a macro for the originalbytecode name:
- Create a macro for the originalinstruction name:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It also might be worth mentioning herewhat should go in that macro, but it also seems pretty clear based on the example.

Comment on lines +550 to +551
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.
Copy link
Member

Choose a reason for hiding this comment

The 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
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.
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.

cliff688 reacted with thumbs up emoji
} _PyContainsOpCache;
```

3. Write the specializing function itself (`_Py_Specialize_ContainsOp`) in [Python/specialize.c ](../Python/specialize.c).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

_Py_Specialize_ContainsOp is an example.

Suggested change
3. Write the specializing function itself (`_Py_Specialize_ContainsOp`) in[Python/specialize.c](../Python/specialize.c).
3. Write the specializing function itself (e.g.,`_Py_Specialize_ContainsOp`) in[Python/specialize.c](../Python/specialize.c).

cliff688 reacted with thumbs up emoji
3. Write the specializing function itself (`_Py_Specialize_ContainsOp`) in [Python/specialize.c ](../Python/specialize.c).
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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Extra space.

Suggested change
4. Add a call to`add_stat_dict` in`_Py_GetSpecializationStats` which is in[Python/specialize.c](../Python/specialize.c).
4. Add a call to`add_stat_dict` in`_Py_GetSpecializationStats` which is in[Python/specialize.c](../Python/specialize.c).

Comment on lines +564 to +565
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

  • "to," not "in."
  • I think the context already implies that it's Python'sdis module :)
Suggested change
5. Add the cache layoutin[Lib/opcode.py](../Lib/opcode.py) so thatPython's
`dis` module will know how to represent it properly.
5. Add the cache layoutto[Lib/opcode.py](../Lib/opcode.py) so thatthe
`dis` module will know how to represent it properly.

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.

6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Extra space.

Suggested change
6. Bump magic number in[Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
6. Bump magic number in[Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@iritkatrieliritkatrieliritkatriel left review comments

@Fidget-SpinnerFidget-SpinnerFidget-Spinner left review comments

@ZeroIntensityZeroIntensityZeroIntensity left review comments

@brandtbucherbrandtbucherbrandtbucher approved these changes

Assignees
No one assigned
Labels
awaiting mergedocsDocumentation in the Doc dirskip issueskip news
Projects
Status: Todo
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@faizanoor3001@iritkatriel@Fidget-Spinner@brandtbucher@ZeroIntensity

[8]ページ先頭

©2009-2025 Movatter.jp