Skip to content

build: force ceval dispatch tail-duplication on affected Apple clangs (gh-158283) - #109

Merged
ronaldtse merged 1 commit into
mainfrom
fix/arm64-build-slowdown
Nov 25, 2026
Merged

ronaldtse merged 1 commit into
mainfrom
fix/arm64-build-slowdown

Conversation

@ronaldtse

@ronaldtse ronaldtse commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

The defect

Fresh macos-15 3.14.7/arm64 builds compute pure Python at ~1.6–2.1x the
same-leg on-system reference (the compute-floor ratio gate on
fix/compute-floor-ratio-gate), while the published v0.3.3 exe reads
1.0–1.33 on the same image against the same reference
(interleaved
three-arm probe, exp branch run 45377845849: fresh/v0.3.3 = 1.258 on
one instance). The recipe is comment-only-diff since v0.3.3, the
recorded CONFIGURE_ARGS/OPT/CFLAGS in the two shipped env images
are byte-identical, the source tarball is byte-identical
(sha256-verified), and otool -L shows the same five system dylibs —
yet the fresh exe is a slower interpreter.

The mechanism — named

Apple clang 1700.x merges the ceval loop's per-opcode computed-goto
dispatch jumps into one shared indirect branch.
This is
cpython#158283:
LLVM 19 limited tail duplication of blocks ending in an indirect branch
(llvm/llvm-project#78582).
Clang lowers each DISPATCH() to one shared indirectbr and relies on
the tail duplicator to copy it back into every opcode handler; the new
profitability limit stops that. Per-opcode branch prediction is the
entire point of computed gotos — a single shared dispatch site defeats
the BTB. Fully fixed in LLVM 20.1.1
(llvm/llvm-project#114990);
no 19.x release has the fix. Apple clang 1700.0.x (Xcode 16.3 and
16.4 — the macos-15 fleet's whole toolchain set) carries the same bug;
1700.3–1700.6 (Xcode 26.0–26.3) still merges most dispatch jumps.

Binary evidence (_PyEval_EvalFrameDefault dispatch-site counts, this
repo's own artifacts unless noted):

build arch compiler indirect dispatch branches
published v0.3.3 exe arm64 Apple clang 15.0.0 260 (healthy)
fresh CI exe (run 45323985389) arm64 Apple clang 17.0.0 1 (merged)
published v0.3.3 exe x86_64 Apple clang 17.0.0 1 (merged — the Intel legs have built with Xcode 16.3/16.4 all along)
this PR's exe (first push) arm64 Apple clang 17.0.0 278 (restored)
this PR's exe (first push) x86_64 Apple clang 17.0.0 260 (restored)
pristine 3.14.7 ceval.c, -O3, local arm64 official LLVM 19.1.7 1 base → 284 with the flag

The same shape flips with the compiler alone on one machine (pristine
3.14.7 source, -O3): Apple clang 15 → 260 dispatch branches, upstream
LLVM 22 → 186, official LLVM 19.1.7 → 1, LLVM 19 + the flag → 284.
The speed tracks the shape: same-machine fib(35) fresh/v0.3.3 ≈ 1.26x
(matching the fleet's interleaved 1.258), while C-dominated workloads
(static openssl sha256, memcpy) are at parity — the tax is
dispatch-shaped, not allocator/interposition (corroborated by
tebako#1095's x86_64 closure: the preload shim never fires in the loop).

Upstream's own measurement on Xcode 16.4 (Apple clang 1700.0.13): one
dispatch jump left in the built exe; restoring duplication → 356–357
jumps and −11.4% pyperformance geomean. fib's call/dispatch density
sits at the hot end of that distribution, which is why our gate reads
1.6–2.1x.

Earlier false leads, closed by the exp branches: the tail-calling
interpreter (3.14.7's configure has no auto-probe; this recipe never
enabled it — both exes are the classic computed-goto ceval) and the
Xcode 16.3-vs-16.4 pin (same Apple clang 17 in both).

The fix

CPython's own merged remedy is gh-158286:
a compile probe on the compiler's identity macros arms
-mllvm -tail-dup-pred-size=1000 for ceval.c only — upstream
measured regex_effbot 6–9% slower when the forced duplication reaches
the regex engine's own computed goto, so a build-wide flag would trade
one regression for another. The source configurations predate the
upstream patch, so this PR carries the same probe+flag at the factory
seam, per line:

  • 3.14+ — PythonBuild#dispatch_tail_dup_env: the macOS legs
    compile upstream's identity-macro probe verbatim with the build's own
    compiler; an affected compiler compiles it and
    CFLAGS_CEVAL=-mllvm -tail-dup-pred-size=1000 rides configure's env
    (Makefile.pre's ceval-only knob; configure substitutes it and appends
    its own -finline-max-stacksize=512 probe result to the same
    variable). Proven on the first push: the 3.14.7 legs restored 278
    (arm64) / 260 (x86_64) dispatch sites and went green.
  • pre-3.14 — PythonBuild#dispatch_tail_dup_append: Makefile.pre
    carries no CFLAGS_CEVAL before 3.14 (the first push's older legs
    failed the new gate named at 1 dispatch site — the env channel never
    reached their ceval.o, exactly the drift the arm exists to catch), so
    substitute_makefiles appends a GNU make target-specific binding,
    Python/ceval.o: CFLAGS_NODIST += -mllvm -tail-dup-pred-size=1000.
    ceval.o's recipe expands the target-local value through the recursive
    PY_CFLAGS_NODIST; every other TU (ceval_gil.o, _sre.o, the
    stdlib set) expands the empty global. Marker-verified end to end
    against a configured 3.13.15 tree: the token lands on
    Python/ceval.o's compile line alone.
  • The probe, both channels: unaffected compilers trip the probe's
    #error — older clangs reject the option outright, which is why the
    gate is a compile probe and never a version sniff; GCC legs never see
    it; the workaround self-retires when the fleet leaves the affected
    window.
  • ci/check_symbol_provenance.sh — a dispatch-shape arm (Mach-O
    legs): the shipped exe's _PyEval_EvalFrameDefault must carry ≥ 100
    indirect dispatch branches (a merged build carries 1; healthy shapes
    185–360). The count is arch-aware — arm64 br <reg>, x86_64
    jmpq *<operand> (the first push's x86_64 legs "failed" at 0 counted
    branches — an arch-blind br-only count reads zero on Intel whatever
    the shape; fixed and re-verified). Red/green-verified against the
    artifacts: v0.3.3 arm64 passes (260), the pre-fix CI build fails
    named (1), v0.3.3 x86_64 fails named (1 — the merged shape it
    shipped), this PR's x86_64 build passes (260). A probe miss on a
    future toolchain fails the leg by name, never ships a green slow exe.
  • spec/python_build_spec.rb: the probe's enablement semantics and
    the per-line channel split (env on 3.14+, append pre-3.14, never
    probed off-macOS), plus the probe text's verbatim mirror of
    gh-158286's windows.

Deliberately NOT in this PR: PGO (a separate, larger recipe decision —
v0.3.3 had none either), --with-tail-call-interp (3.14 marks it
experimental; the classic ceval is the shipped shape), a toolchain
switch (the fleet's clang is what it is; the flag makes it produce the
right bytes), and any re-baseline of the compute-floor gate (the gate
was right; the build was wrong).

What CI must prove

  1. macos 3.14.7 legs (arm64 + x86_64, plain + jit): the build log
    shows the probe firing (gh-158283: the build compiler merges the computed-goto dispatch), the provenance gate counts ≥ 100 dispatch
    branches, and the arm64 compute-floor fib(35) best-of-3 returns to
    the healthy class (~1.3–2.0s on the current image, from the
    defective 3.2–3.6s).
  2. macos pre-3.14 legs (3.13.15 ± jit, 3.12.14, 3.11.16): the
    target-specific append lands — same ≥ 100 gate on the shipped exe.
  3. Every GCC leg (linux-gnu/musl, windows): the probe never fires —
    no clang-only flag touches a GCC build; legs unchanged.
  4. lint green: contract check, matrix compute, ruby -c, rspec.

Side finding, out of scope here (owner attention): the v2.8.83 link-unit
exe fails to mount any env image on macOS 14.1.1 ("Failed to import
encodings module") where the v2.8.55 exe mounts both images fine and the
fresh exe boots with a PYTHONHOME override — a product-repo driver
question, not a factory one.

@ronaldtse
ronaldtse force-pushed the fix/arm64-build-slowdown branch from 0bc70d8 to 19d79ea Compare November 25, 2026 05:04
… (gh-158283)

Fresh macos-15 3.14.7 builds read fib-ratio 1.6-2.1 vs the same-leg
on-system reference where the published v0.3.3 exe reads 1.0-1.33 on
the same image (interleaved: fresh/v0.3.3 = 1.258 on one instance),
with byte-identical recorded CONFIGURE_ARGS/OPT/CFLAGS, a
byte-identical source tarball, and an unchanged dylib closure. The
exe's _PyEval_EvalFrameDefault tells the story: v0.3.3 (Apple clang
15) carries 260 per-opcode indirect dispatch branches; the fresh
build (Apple clang 17.0.0, Xcode 16.3 and 16.4 alike) carries ONE —
every opcode handler direct-branches into a single shared dispatch
site. gh-158283: LLVM 19 limited tail duplication of blocks ending
in an indirect branch (llvm/llvm-project#78582), so clang lowers the
computed-goto DISPATCH()es to one shared indirectbr that never gets
copied back into the handlers — defeating the per-opcode branch
prediction computed gotos exist for (upstream: ~9-13% pyperformance;
our dispatch-dense fib sits at the hot end). Fixed in LLVM 20.1.1;
Apple clang 1700.0.x carries the bug, 1700.3-1700.6 still merges
most jumps. The x86_64 legs are hit too: the published v0.3.3
macos-x86_64 exes carry the same merged shape (1 indirect dispatch
jump) — the Intel fleet has built with Xcode 16.3/16.4 all along.

The remedy is CPython's own merged gh-158286, carried at the factory
seam since the 3.14.7 configure predates it: the macOS legs compile
the upstream identity-macro probe verbatim with the build's
compiler, and an affected compiler arms
-mllvm -tail-dup-pred-size=1000 for ceval.o ONLY — upstream measured
regex_effbot 6-9% slower when the forced duplication reaches the
regex engine's own computed goto, so a build-wide append would trade
one regression for another. The channel is per-line:

- 3.14+: CFLAGS_CEVAL, Makefile.pre's ceval-only knob, via the
  configure env (configure substitutes it and appends its own
  -finline-max-stacksize=512 probe result to the same variable).
- pre-3.14: Makefile.pre carries no CFLAGS_CEVAL, so
  substitute_makefiles appends a GNU make target-specific binding
  (Python/ceval.o: CFLAGS_NODIST += ...) — ceval.o's recipe expands
  the target-local value through the recursive PY_CFLAGS_NODIST and
  every other TU (ceval_gil.o, _sre.o) expands the empty global.
  Marker-verified end to end against a configured 3.13.15 tree: the
  token lands on Python/ceval.o's compile line alone.

Older clangs reject the option outright, so the gate is a compile
probe, never a version sniff; the workaround self-retires when the
fleet's toolchain leaves the affected window.

ci/check_symbol_provenance.sh gains the artifact-level arm: the
shipped exe's eval loop must carry >= 100 dispatch branches (a
merged build carries 1). The count is arch-aware — arm64 `br <reg>`,
x86_64 `jmpq *<operand>`; an arch-blind count reads 0 on Intel
whatever the shape. Red/green-verified against the artifacts:
v0.3.3 arm64 passes (260), the pre-fix CI build fails named (1),
v0.3.3 x86_64 fails named (1 — the merged shape it shipped), the
fixed x86_64 build passes (260). A probe miss on a future toolchain
is now a named failure, never a green slow exe.

This branch was previously deployed

1 inactive deployment
windows-signing — ab1b2de2 Deployed Nov 25, 2026 by ronaldtse via windows / 3.14.7 / windows-x86_64 #255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants