Skip to content

Poor quality stencils produced with Homebrew clang version 22.1.2 on Apple Silicon #148501

Open

Description

@corona10

While working on #148217, I found that the _fixup_constants optimizer in Tools/jit/_optimizers.py fails to convert adrp+ldr to movz when the two instructions are split by a label.

Example: _LOAD_FAST_r01

// Before (8 instructions):
adrp    x8, __JIT_OPARG_16@GOTPAGE
ldr     x8, [x8, __JIT_OPARG_16@GOTPAGEOFF]
add     x8, x21, w8, uxth #3
ldr     x24, [x8, #0x50]
tbnz    w24, #0x0, ...
ldr     w8, [x24]
add     w8, w8, #0x1
str     w8, [x24]

// After (7 instructions):
movz    x8, #0x0
add     x8, x21, w8, uxth #3
ldr     x24, [x8, #0x50]
tbnz    w24, #0x0, ...
ldr     w8, [x24]
add     w8, w8, #0x1
str     w8, [x24]

Example: _LOAD_FAST_BORROW_r01

// Before (5 instructions):
adrp    x8, __JIT_OPARG_16@GOTPAGE
ldr     x8, [x8, __JIT_OPARG_16@GOTPAGEOFF]
add     x8, x21, w8, uxth #3
ldr     x8, [x8, #0x50]
orr     x24, x8, #0x1

// After (4 instructions):
movz    x8, #0x0
add     x8, x21, w8, uxth #3
ldr     x8, [x8, #0x50]
orr     x24, x8, #0x1

Linked PRs

Activity

  1. self-assigned this
    on Apr 13, 2026
  2. added a commit that references this issue on Apr 13, 2026
  3. markshannon commented on Apr 13, 2026

    @markshannon
    Member

    I'm not seeing the same stencils as you on main.
    For emit__LOAD_FAST_BORROW_r01, I see this:

        // 0000000000000000 <_JIT_ENTRY>:
        // 0: d2800008      mov     x8, #0x0                // =0
        // 4: 8b282ea8      add     x8, x21, w8, uxth #3
        // 8: f9402908      ldr     x8, [x8, #0x50]
        // c: b2400118      orr     x24, x8, #0x1
    

    what version of Clang are you using?

    I'm not seeing __JIT_OPARG_16@GOTPAGE in emit__LOAD_FAST_r01 either.

  4. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    type-bugAn unexpected behavior, bug, or error
    pendingThe issue will be closed if no feedback is provided
    on Apr 13, 2026
  5. corona10 commented on Apr 13, 2026

    @corona10
    MemberAuthor
    Homebrew clang version 22.1.2
    Target: arm64-apple-darwin25.4.0
    Thread model: posix
    InstalledDir: /opt/homebrew/Cellar/llvm/22.1.2/bin
    Configuration file: /opt/homebrew/Cellar/llvm/22.1.2/etc/clang/arm64-apple-darwin25.cfg
    System configuration file directory: /opt/homebrew/Cellar/llvm/22.1.2/etc/clang
    User configuration file directory: /Users/dongheena/.config/clang
    

    I think this could be matter, which clang version are you using on macOS?

  6. markshannon commented on Apr 14, 2026

    @markshannon
    Member

    My stencil is from linux. Clang 21.1.6.

    Which clang is being used to generate your stencils?
    It might be different from your default clang.

    Can you paste the assembly file for emit__LOAD_FAST_BORROW_r01 here?
    You'll need to add delete=False to the line with tempfile.TemporaryDirectory() as tempdir: in _targets.py and then find it in your temp directory.
    That should give us some hints as to why things aren't working.

  7. changed the title [-]Fix cross-block small constant optimization on AArch64[/-] [+]Poor quality stencils produced with Homebrew clang version 22.1.2 on Apple Silicon[/+] on Jun 10, 2026
  8. lgeiger commented on Sep 6, 2026

    @lgeiger
    Contributor

    I also noticed this issue in the macOS arm64 stencils. This is actually caused by LLVM’s Mach-O linker optimisation hint (Lloh) labels which split the adrp/ldr pairs into separate optimiser blocks.

    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
    

    We can disable the linker hints with -mllvm -aarch64-enable-collect-loh=false. These hints are never used anyway since the stencils are not passed directly to the Mach-O linker, so we can safely remove them.

    This would allow the current optimisations to work like they do on Linux:

        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
    

    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

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedperformancePerformance or resource usagetopic-JITtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions