Skip to content

gh-152215: Add uop peepholes for folded stack shuffles and adjacent guards#153035

Open
cocolato wants to merge 3 commits into
python:mainfrom
cocolato:jit/uop_peepholes
Open

gh-152215: Add uop peepholes for folded stack shuffles and adjacent guards#153035
cocolato wants to merge 3 commits into
python:mainfrom
cocolato:jit/uop_peepholes

Conversation

@cocolato

@cocolato cocolato commented Jul 4, 2026

Copy link
Copy Markdown
Member

This PR adds some small peephole helper functions to the cleanup phase of the
uop optimizer.

It splits the existing push/pop cleanup operations into a helper function and
adds a specific helper function for the stack reordering operations left over
from constant folding.

It also adds a helper that combines matching adjacent TOS and NOS guards
with the same side exit.

On AArch64, this allows two float guards to reuse the loaded _PyFloat_Type pointer, removing these instructions from the second
guard:

    and     x8, x24, #0xfffffffffffffffe
    ldr     x8, [x8, #0x8]
    adrp    x9, _PyFloat_Type
    ldr     x9, [x9]
    cmp     x8, x9
    b.ne    exit

    and     x8, x25, #0xfffffffffffffffe
    ldr     x8, [x8, #0x8]
    adrp    x9, _PyFloat_Type    // removed by fusion
    ldr     x9, [x9]             // removed by fusion
    cmp     x8, x9
    b.ne    exit

This reduces the stencil from 12 instructions (48 bytes) to 10 instructions
(40 bytes).

A generic stack simulator could perform more peephole optimizations, but for
now there don’t seem to be many examples that could be optimized, and it would
increase maintenance costs.

@cocolato cocolato changed the title gh-152215: Add uop peepholes for folded stack shuffles gh-152215: Add uop peepholes for folded stack shuffles and adjacent guards Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant