Skip to content

Document how to add a bytecode specialization #1286

Open

Description

@Fidget-Spinner

How to add a new bytecode specialization is knowledge only a few of us remember right now. We should document this somewhere (devguide or CPython, I don't mind). Hopefully a new contributor can pick this up!

There reference is this PR: https://github.com/python/cpython/pull/116385/files

Assuming you found an instruction that serves as a good specialization candidate. Let's use the example of CONTAINS_OP:

  1. Set the original CONTAINS_OP to a uop by changing it from CONTAINS_OP to _CONTAINS_OP in Python/bytecodes.c. The instruction definition should also change from inst to op.
  2. Add the uop that calls a specializing function. In this case _SPECIALIZE_CONTAINS_OP.
  3. The original CONTAINS_OP is now a new macro consisting of _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP.
  4. Define the cache structure in Include/internal/pycore_code.h. At the very least, a 16-bit counter is needed.
  5. Write the specializing function itself in Python/specialize.c. Refer to any other function in that file for the format.
  6. Remember to update operation stats by calling add_stat_dict in Python/specialize.c.
  7. Add the cache layout in Lib/opcode.py so that Python's dis module will know how to represent it properly.
  8. Bump magic number in Include/internal/pycore_magic_number.h.
  9. Run make regen-all on *nix or build.bat --regen on PC.

Activity

  1. AbudiMutamba commented on Oct 10, 2024

    @AbudiMutamba

    Hey I would love to work on this issue if you don't mind, thanks,

  2. willingc commented on Oct 10, 2024

    @willingc
    Collaborator

    Hi @AbudiMutamba. Welcome 👋 Before you get too far along on this. Let's check with @Fidget-Spinner if this is still something that is needed?

  3. Fidget-Spinner commented on Oct 10, 2024

    @Fidget-Spinner
    MemberAuthor

    Yes we still need this.

  4. willingc commented on Oct 10, 2024

    @willingc
    Collaborator

    Thanks @Fidget-Spinner. If you want to give this a try @AbudiMutamba, please do so.

  5. faizanoor3001 commented on Feb 20, 2025

    @faizanoor3001

    Hello, @willingc is it something still open ? Would like to contribute. :)

  6. Fidget-Spinner commented on Feb 20, 2025

    @Fidget-Spinner
    MemberAuthor

    @faizanoor3001 this is still open

  7. faizanoor3001 commented on Feb 24, 2025

    @faizanoor3001

    Hello, @Fidget-Spinner, I’ve opened a PR adding “How to Add a New Bytecode Specialization” to the How-To section. We can verify the text changes or if any updates are required, but I’m unsure about the best place for this new guide. Would it fit better in:

    1. Interpreter.md section - appended under the existing InternalDocs?

    2. CPython Internals — as its own subsection?

      Current:
      Image

      Potential Alternative:

      Image

    PS: This is my first contribution, so I appreciate your guidance. I’m also studying the Crafting Interpreters book, and your original PR helped me delve deeper into the interpreter’s workings. Thank you!

  8. Fidget-Spinner commented on Feb 24, 2025

    @Fidget-Spinner
    MemberAuthor

    @faizanoor3001 thanks for taking this up! I will review it soon.

  9. faizanoor3001 commented on Mar 4, 2025

    @faizanoor3001

    @Fidget-Spinner Raised a PR, but since the issue is in devguide project and the PR I have raised in the cpython project, the bot is unable to recognize the gh-NNNN issue number in this case gh-1286 as correct issue number, could you please help here, as I guess we would need a new issue number ? Or how to work around on this ?

  10. StanFromIreland commented on Feb 21, 2026

    @StanFromIreland
    Member

    In general we've removed the internals sections here, maybe this is better suited for InternalDocs?

  11. nayanraj864-cmyk commented on Sep 8, 2026

    @nayanraj864-cmyk

    @Fidget-Spinner
    Hello,

    My name is Nayan Raj, and I have recently started contributing to organizations that participate in Google Summer of Code (GSoC). This is my first experience contributing to a GSoC organization, although I have previously worked on other open-source projects and have some contribution experience.

    I would greatly appreciate any guidance, suggestions, or feedback that could help me improve my contributions and better understand the GSoC contribution process. I am eager to learn, enhance my skills, and become a valuable contributor to the community.

    Thank you for your time and support. I look forward to learning from your experience and contributing effectively to the project.
    please assign this issue to me

    Best regards,
    Nayan Raj

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions