Skip to content

Tier2 peephole optimization: remove extraneous _CHECK_STACK_SPACE ops #116168

Description

@lazorchakp

Implement a new peephole optimization for the tier2 optimizer that removes _CHECK_STACK_SPACE if we see that this check is already present

Linked PRs

Activity

  1. lazorchakp commented on Feb 29, 2024

    @lazorchakp
    ContributorAuthor

    cc @Fidget-Spinner

    This might need some labels. I also looked around for a parent ticket, but didn't find anything particularly obvious

  2. Fidget-Spinner commented on Mar 1, 2024

    @Fidget-Spinner
    Member

    I will link to #115419

  3. gvanrossum commented on Mar 3, 2024

    @gvanrossum
    Member

    I don't know anything about the plan here, so this may not be news. :-) Couldn't you check for the needed stack space once at the beginning of the executor? And if there's a loop, lift it out of there. (And if it fits within the stack space of the code object already, drop it altogether.)

  4. Fidget-Spinner commented on Mar 3, 2024

    @Fidget-Spinner
    Member

    Makes sense. Let's go with consolidating them first then another follow-up PR for lifting. Lifting stuff out of loops requires _JUMP_ABSOLUTE and I can just point to the code from my old PR to copy. (Though the heuristics need someone else to work on it).

  5. gvanrossum commented on Mar 20, 2024

    @gvanrossum
    Member

    Is anyone working on this? Otherwise I'd like to give it a go.

  6. Fidget-Spinner commented on Mar 20, 2024

    @Fidget-Spinner
    Member

    @gvanrossum I think @lazorchakp might be working on this, @lazorchakp do you mind if Guido takes over? If you have something with an implementation up but no tests that's okay, you can just submit a draft PR and Guido can probably take over if you don't mind.

  7. gvanrossum commented on Mar 20, 2024

    @gvanrossum
    Member

    I'd also be happy to mentor @lazorchakp !

  8. lazorchakp commented on Mar 20, 2024

    @lazorchakp
    ContributorAuthor

    Sorry everyone - I've been busier than anticipated and haven't made concrete progress on this yet. If it's alright though, I'd still like to try it, and I should be able to get a draft up tonight if that works. @Fidget-Spinner has given me some guidance over email, but I'd gladly accept as much mentoring as you both are willing to provide 🙂

    That being said, @gvanrossum if you'd rather go for it yourself, that's quite alright! Just let me know.

  9. gvanrossum commented on Mar 20, 2024

    @gvanrossum
    Member

    No, I'd love to have you do it!

    My guess is that the critical trick should be to change the uop that checks for stack space to take a number indicating the amount of stack space needed, rather than digging that value out of the code object (which must be retrieved from the function object, which must be dug out of the stack at position -2-oparg, IIRC).

    Maybe this number can be put in its operand field by the initial trace projection pass (which needs the code object anyway). Then a later dedicated pass can combine stack checks as long as they are within the same "scope" (i.e., _PUSH_FRAME/_POP_FRAME pair).

  10. lazorchakp commented on Mar 20, 2024

    @lazorchakp
    ContributorAuthor

    That sounds great. It makes sense to me that we'd now need _CHECK_STACK_SPACE to know up-front how much space it's looking for. I'll see if I can take your suggestion and modify the uop so that this information is available for the optimization pass.

  11. added a commit that references this issue on Mar 21, 2024
  12. lazorchakp commented on Mar 21, 2024

    @lazorchakp
    ContributorAuthor

    I'm at the point where I have the operand for each _CHECK_STACK_SPACE being correctly assigned to its corresponding function's framesize (pushed a rough branch). Before I implement the actual optimization though, I want to make sure I'm reasoning about this correctly - I'm making some heavy assumptions about the underlying machinery. Are the following cases correct? (other ops removed for simplicity):


    CASE 1 - sequential function calls:

    _CHECK_STACK_SPACE A
    _PUSH_FRAME
    _POP_FRAME
    _CHECK_STACK_SPACE B
    _PUSH_FRAME
    _POP_FRAME
    

    proposed optimization:

    _CHECK_STACK_SPACE max(A, B)
    _PUSH_FRAME
    _POP_FRAME
    _PUSH_FRAME
    _POP_FRAME
    

    CASE 2 - nested function calls:

    _CHECK_STACK_SPACE A
    _PUSH_FRAME
    _CHECK_STACK_SPACE B
    _PUSH_FRAME
    _POP_FRAME
    _POP_FRAME
    

    proposed optimization:

    _CHECK_STACK_SPACE A + B
    _PUSH_FRAME
    _PUSH_FRAME
    _POP_FRAME
    _POP_FRAME
    

    If these cases are correct, I should be able to combine everything into a single _CHECK_STACK_SPACE uop per trace, as @gvanrossum mentioned above.

  13. 13 remaining items

  14. lazorchakp commented on Mar 26, 2024

    @lazorchakp
    ContributorAuthor

    Still have to iterate with the pipeline a bit in my draft PR. I'll move it to Open and send an update here when it's officially ready for review.

  15. lazorchakp commented on Mar 28, 2024

    @lazorchakp
    ContributorAuthor

    Just opened #117242 for review

  16. added a commit that references this issue on Apr 3, 2024
  17. gvanrossum commented on Apr 4, 2024

    @gvanrossum
    Member

    This is done -- thanks again!

  18. added a commit that references this issue on Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions