GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Move const folding to the peephole optimizer · Issue #126835 · python/cpython

Move const folding to the peephole optimizer #126835

New issue
Copy link
New issue
Copy link
Open
Open
Move const folding to the peephole optimizer#126835
Copy link
Assignees
tomasr8Eclips4
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Description

@Eclips4
Eclips4
opened on Nov 14, 2024
Issue body actions

Feature or enhancement

Proposal:

For additional context see #126830 (comment).

Flow graph optimizer has more information and can do better job.

The problem is that we need to convert from UNARY_OP(-, CONST(1)) to CONST(-1), still before the code generation phase, because this leads to a few problems, one of which is shown below.

x = 1

match x:
    case -0:
        y = 0
eclips4@nixos ~/p/p/cpython (remove-ast-optimizer)> ./python example.py
  File "/home/eclips4/programming/programming-languages/cpython/example.py", line 4
    case -0:
         ^^
SyntaxError: patterns may only match literals and attribute lookups

cc @markshannon

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-126835: Disable tuple folding in the AST optimizer #128802
  • gh-126835: Move optimization of constant sequence creation from codegen to CFG #129426
  • gh-126835: Move constant unaryop & binop folding to CFG #129550
  • gh-126835: Move constant subscript folding to CFG #129568
  • gh-126835: Fix optimize_if_const_subscr refleaks #129634
  • gh-126835: make CFG optimizer skip over NOP's when looking for const sequence construction #129703
  • gh-126835: Fold unary & binary complex constant expressions during code generation. #129963
  • gh-126835: Remove unused docstring const #130016
  • gh-126835: Move const list & set folding in for_iter & contains from ast_opt.c to flowgraph.c #130032
  • gh-126835: Set location for noped out instructions after constant folding in CFG. #130109
  • gh-126835: Move constant tuple folding to CFG #130769
  • gh-126835: Avoid creating unnecessary tuple when looking for constant sequence during constant folding #131054
  • gh-126835: Refine constant folding tests in test_peepholer.py::TestTranforms #131826
  • gh-126835: Rename AST optimization related stuff after moving const folding to the peephole optimizier #131830
  • gh-126835: Fix _PY_IS_SMALL_INT() macro #146631
  • [3.14] gh-126835: Fix _PY_IS_SMALL_INT() macro (GH-146631) #147187
Reacted by Tomas R., Xuanteng Huang, W1thLuv, Mikhail Efimov, Michael Kashirin, Yan Yanchii and Tony

Activity

Next
  1. Eclips4
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Nov 14, 2024
  2. Eclips4
    self-assigned this
    on Nov 15, 2024
  3. Eclips4 commented on Dec 21, 2024

    @Eclips4
    Eclips4
    on Dec 21, 2024
    MemberAuthor
    More actions

    We had a discussion with @tomasr8 and decided that splitting this into parts would be good. Initially, I tried to do it in one PR, but realized that it would be impossible to review, as it would do a lot of unrelated things.

    To begin, we decided to start by moving tuple folding from AST optimizer to CFG.

    Problem: Currently compiler emits a warning if is/is not operators are used with constants1 (because it can evaluate them to True which is an implementation detail of CPython, other implementations might evaluate const is const to False).
    So, the compiler (especially the codegen part) assumes that tuples are already folded, and has (in AST terms) Constant_kind. This breaks when we fold tuples later and they have a Tuple_kind at the codegen phase.

    Possible solution: Perform this in a CFG after folding. This will be slower (I have no information on how slower it will be, but I assume it will) than performing this in codegen phase.

    Footnotes

    1. https://github.com/python/cpython/blob/2a66dd33dfc0b845042da9bb54aaa4e890733f54/Python/codegen.c#L1678-L1691 ↩

    Reacted by Tomas R., Mikhail Efimov and Michael Kashirin
  4. Eclips4
    assigned
    tomasr8
    on Dec 21, 2024
  5. WolframAlph commented on Feb 1, 2025

    @WolframAlph
    WolframAlph
    on Feb 1, 2025
    Contributor
    More actions

    Is there a plan what parts/in what order should be moved from ast optimizer to cfg? Does it make sense to create sub issues to make it more granular (instead of just PRs linked to this issue)? This issue Remove the AST optimizer seems too general and broad IMO.

  6. Eclips4
    added a commit that references this issue on Feb 1, 2025

    gh-126835: Move optimization of constant sequence creation from codeg…

    7d0521d
  7. tomasr8 commented on Feb 1, 2025

    @tomasr8
    tomasr8
    on Feb 1, 2025
    Member
    More actions

    I'm currently looking into moving unaryop/binop folding (though I'm at Fosdem atm so It might take a bit longer) Anything else is up for grabs I'd say, unless Kirill is working on something currently?

  8. WolframAlph commented on Feb 1, 2025

    @WolframAlph
    WolframAlph
    on Feb 1, 2025
    Contributor
    More actions

    I am looking into binop folding as well (almost done). Do you want to pass it or should I take something else?

  9. WolframAlph commented on Feb 1, 2025

    @WolframAlph
    WolframAlph
    on Feb 1, 2025
    Contributor
    More actions

    Problem with binop I discovered is that after moving it to cfg, we, for instance, run into problem with case matching which @Eclips4 mentioned in issue description. For example:

    match x:
        case 0 + 0j:
            pass

    raises

    File "/Users/yyanchii/Desktop/cpython/internal/t.py", line 2
        case 0 + 0j:
             ^^^^^^
    SyntaxError: patterns may only match literals and attribute lookups

    It is raised from:

    cpython/Python/codegen.c

    Lines 6022 to 6025 in 89fe067

    if (!MATCH_VALUE_EXPR(value)) {
    const char *e = "patterns may only match literals and attribute lookups";
    return _PyCompile_Error(c, LOC(p), e);
    }

    So at codegen stage, it already expects (in this case) binops to be folded. @tomasr8 did you run into this already? We would need to add more logic in here which doesn't look good to me. On the other hand, how would we move binop folding to CFG then?

  10. Eclips4 commented on Feb 2, 2025

    @Eclips4
    Eclips4
    on Feb 2, 2025
    MemberAuthor
    More actions

    @tomasr8 @WolframAlph

    I think the current priority is to move unaryop folding from AST optimizer to codegen phase.
    I'm looking into it.

    Then we should have to think about binaryop folding :)

    Reacted by Tomas R.
  11. WolframAlph commented on Feb 2, 2025

    @WolframAlph
    WolframAlph
    on Feb 2, 2025
    Contributor
    More actions

    Placing this here, not to forget in future: #129568 (comment). Maybe in future we get rid of emitting LOAD_SMALL_INT in codegen and let CFG do it instead

  12. Eclips4 commented on Feb 3, 2025

    @Eclips4
    Eclips4
    on Feb 3, 2025
    MemberAuthor
    More actions

    cc @iritkatriel
    We need to decide on what to do with the optimize parameter of ast.parse and the PyCF_OPTIMIZED_AST flag.
    If we remove the AST optimizer, these two will become obsolete.
    Should we deprecate them in 3.14 (after the AST optimizer is removed) and remove them in 3.19?

  13. WolframAlph commented on Feb 3, 2025

    @WolframAlph
    WolframAlph
    on Feb 3, 2025
    Contributor
    More actions

    @Eclips4 as I am reading https://docs.python.org/3/library/ast.html#ast.parse, it is not clear to me whether feature_version also works for constant folding case.

  14. WolframAlph commented on Feb 3, 2025

    @WolframAlph
    WolframAlph
    on Feb 3, 2025
    Contributor
    More actions

    But I guess Best-effort part makes it clear that one cannot rely on the consistent output.

  15. 130 remaining items

  16. Eclips4 commented on May 5, 2025

    @Eclips4
    Eclips4
    on May 5, 2025
    MemberAuthor
    More actions

    @Eclips4 does this change deserve having "What's New" entry? Seems like not very interesting to the end user. Or am I wrong?

    I guess some users may have been relying on the behavior of the previous ast optimizer, but now it's gone.
    There was no explanation of exactly what ast.parse(optimize=1) does, but even with that some users may still have used it.
    So, I think we should at least "warn" them in "What's new".

    Reacted by Mikhail Efimov and Irit Katriel
  17. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-126835: Rename `ast_opt.c` to `ast_preprocess.c` and related…

    53ed8c0
  18. zSirius
    added 3 commits that reference this issue on Mar 20, 2026

    Fix O(N²) in add_const() after constant folding moved to CFG

    120e518

    Fix O(N²) in add_const() after constant folding moved to CFG

    24c8355

    pythongh-146455: Fix O(N²) in add_const() after constant folding move…

    2a67136
  19. vstinner
    added 2 commits that reference this issue on Mar 30, 2026

    pythongh-126835: Fix _PY_IS_SMALL_INT() macro

    850832e

    gh-126835: Fix _PY_IS_SMALL_INT() macro (#146631)

    adf2c47
  20. maurycy
    added a commit that references this issue on Mar 30, 2026

    Merge remote-tracking branch 'upstream/main' into tachyon-ndjson-kole…

    6a0ea81
  21. miss-islington
    added a commit that references this issue on Mar 31, 2026

    pythongh-126835: Fix _PY_IS_SMALL_INT() macro (pythonGH-146631)

    1fd1eb5
  22. vstinner
    added a commit that references this issue on Mar 31, 2026

    [3.14] gh-126835: Fix _PY_IS_SMALL_INT() macro (GH-146631) (#147187)

    e387bac
  23. clin1234
    added a commit that references this issue on Apr 16, 2026

    pythongh-126835: Fix _PY_IS_SMALL_INT() macro (python#146631)

    aa04a23
  24. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-126835: Fix _PY_IS_SMALL_INT() macro (python#146631)

    2ccc9bb
  25. Eclips4
    added 2 commits that reference this issue on Apr 26, 2026

    gh-146455: Fix O(N²) in add_const() after constant folding moved to C…

    5d41632

    [3.14] gh-146455: Fix O(N²) in add_const() after constant folding mov…

    78c5e54
  26. flameastro commented on Jul 11, 2026

    @flameastro
    flameastro
    on Jul 11, 2026 · Hidden as spam
    show commentMore actions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @Eclips4
    Eclips4
  • @tomasr8
    tomasr8

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.