Skip to content

gh-148501: Disable AArch64 Mach-O LOH for JIT stencils - #157040

Open
lgeiger wants to merge 2 commits into
python:mainfrom
lgeiger:macho-loh-jit
Open

gh-148501: Disable AArch64 Mach-O LOH for JIT stencils#157040
lgeiger wants to merge 2 commits into
python:mainfrom
lgeiger:macho-loh-jit

Conversation

@lgeiger

@lgeiger lgeiger commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Currently, LLVM’s AArch64 Mach-O linker optimisation hints prevent the JIT optimiser from folding adrp/ldr constant loads into movz. These hints are actually unused because stencils are not directly passed to the Mach-O linker.

This PR disables the linker hints on macOS Aarch64 with -aarch64-enable-collect-loh=false.

E.g. the stencil for _LOAD_SMALL_INT_r01 on macOS currently looks something like this:

Lloh0:
jit_temp_0_x8:
    adrp    x8, __JIT_OPARG_16@GOTPAGE
Lloh1:
    ldr     x8, [x8, __JIT_OPARG_16@GOTPAGEOFF]
    and     x8, x8, #0xffff

Lloh2:
    adrp    x9, __PyRuntime@GOTPAGE
Lloh3:
    ldr     x9, [x9, __PyRuntime@GOTPAGEOFF]

    add     x8, x9, x8, lsl #5
    mov     w9, #13889
    add     x24, x8, x9

    .loh AdrpLdrGot Lloh2, Lloh3
    .loh AdrpLdrGot Lloh0, Lloh1

After this PR, the linker optimisation hints are removed and the JIT optimiser can correctly fold adrp/ldr constant loads.

    movz    x8, 0

    adrp    x9, __PyRuntime@GOTPAGE
    ldr     x9, [x9, __PyRuntime@GOTPAGEOFF]

    add     x8, x9, x8, lsl #5
    mov     w9, #13889
    add     x24, x8, x9

Overall, this reduces the size of all stencils on macOS by 4.5%:

Metric Before After Reduction
Machine code 56,340 bytes 55,468 bytes 872 bytes (1.55%)
Instructions 14,085 13,867 218 instructions
Stencil data 3,632 bytes 1,784 bytes 1,848 bytes (50.88%)
Combined 59,972 bytes 57,252 bytes 2,720 bytes (4.54%)

I ran the pyperformance suite before and after on a Apple M5 Pro which suggests a geometric mean improvement of 0.57% and up to 7.3% faster on scimark_sparse_mat_mult.

Expand for full benchmarking results

full-suite

Benchmarks and code size analysis was run by GPT 5.6 Sol. Code changes are authored by myself.

I realised too late that work on the JIT is currently paused. I'm still opening the PR since #148501 is labeled as a bug, but feel free to ignore it until there's an official decision on the PEP by the steering council.

A follow up to this would be to recognise the @GOTPAGE/@GOTPAGEOFF Darwin syntax similar to what's done for the :got:/:got_l12: ELF syntax so patch_aarch64_33rx can deal with it.

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