Repository navigation
Description
Findings from code-alignment experiments in October and November 2026. With identical instructions, evmone benchmarks swing by 50% and more from code placement alone. Nothing is applied on master; #1815 (GCC -falign-labels=32) is an open draft.
Summary
- Baseline interpreter: aligning the computed-goto handlers removes the layout lottery, mostly by locking in the best default layout. It beats every default layout only with clang 21 at x86-64-v3 on CircleCI's Skylake-SP, Cascade Lake and Ice Lake machines.
- GCC:
-falign-labels=32is the only flag that aligns the handlers (cmake: Align the Baseline interpreter's labels to 32 bytes with GCC #1815). It matches the best default layout on every CPU tested except EPYC 7763, and growsdispatch_cgotofrom 29 to 49 KB. - clang:
-falign-loops=32aligns them. Geomean −2..−5% on those three Intel machines at v3, within ±0.8% elsewhere (v2, clang 18 on GitHub's runners, Neoverse N2), and regressions of up to +8.5% at v2. - Proposed, not decided: GCC
-falign-labels=32and clang-falign-loops=32forbaseline_execution.cpp.
- GCC:
- JUMPDEST analysis loop: no alignment flag is worth shipping. Placement explains the pointer-walk variants' x86 outliers (up to +133%), but at its best placement
sziskis still +4..+14% against master's loop with GCC (Ice Lake −2%), and −8..+5% with clang.
Why placement matters
- Inlining (GCC): code growth elsewhere in the file can change what GCC inlines into
dispatch_cgoto, because its inline budget scales with the file size. Specialize the Knuth division for 3- and 4-word divisors chfast/intx#397 grewudivrem<512,256>by 0.7 KB, and GCC then inlinedextcodehashintodispatch_cgoto(+49 instructions, stack frame 0x3b8 → 0x408).arithmetic-ADDMOD-xgot 42–47% slower with the same instruction count and byte-identical ADDMOD code; branch misses went from 40K to 4.3M. cmake: Raise GCC's inline budget for the Baseline interpreter #1814 raised the budget, which pins these decisions. - Chained MUL (GCC, v3): builds run it in one of two states with identical instructions: 0.44M or 6.41M branch misses per 3 executions, 183M or 295M cycles. The mispredicting branch is PUSH32's
jmp *, which alternates between ADD and MUL. The state follows the low 4 bits of the MUL handler'sjmpaddress: padding the MUL handler's start by a few bytes flips it, moving the whole function by 16–2048 bytes doesn't. - Jump-heavy micro benchmarks (
JUMPDEST_n0,loop_with_many_jumpdests): spreads of 50% and more (up to ~200% on EPYC 7763) when an unrelated handler (SMOD) is padded.
What the compilers align on x86-64
| GCC 16 | clang 23 | |
|---|---|---|
| functions | 16 B | 16 B |
| loop headers | 16 B (GCC 15 put the JUMPDEST loop at 32 B, see below) | 16 B, except loops it considers cold |
| other jump targets | 16 B if at most 11 B of padding, else 8 B; not computed-goto targets | none |
-falign-labels |
off by default | ignored ("not supported"), as is -falign-jumps |
The 155 handler entries of dispatch_cgoto:
- GCC: 9–12 start on a 16-byte boundary, by chance.
-falign-jumps,-falign-functionsand-falign-loopsdon't move them;-falign-loops=64leavesdispatch_cgotobyte-identical.-falign-labels=32aligns all 155, but also every other label in the file:dispatch_cgotogrows from 29 to 49 KB, 6.4 KB of the padding lies on paths that execute, and libevmone grows from 181 to 237 KB (GCC 16, v3). - clang: every handler is a loop header, because its
jmp *can return to it, so 121 start on a 16-byte boundary by default.-falign-loops=Naligns 149 of them, all but the cold STOP, RETURN, REVERT, INVALID, SELFDESTRUCT and undefined-opcode handlers, and changes no instructions (clang 21, v2):=32: +1.4 KB, with 54 B of executed padding.=64:dispatch_cgotogrows from 20.3 to 25.3 KB; executed padding grows from 22 to 150 B, in EXP (51 B), SIGNEXTEND (58 B) and CLZ (41 B).
- clang
-mllvm -align-all-nofallthru-blocks=Naligns none of the handlers. It skips a block that is a CFG successor of the block before it, and the indirect jumps make every handler one. It aligned about 600 other, mostly cold blocks. cmake: Align the Baseline interpreter's labels to 32 bytes with GCC #1815's clang measurements used it, so they don't measure handler alignment.
Baseline measurements
Each variant differs only in how baseline_execution.cpp is compiled. To sample layouts, each is built with the SMOD handler padded: by 0/4/8/12 B for #1815, and by 0/16/32/48 B for the rerun, since clang's 16-byte alignment absorbs 4 and 8. One process loads all variants and times 17 programs (evm-benchmarks main and micro, and 3 MUL contracts) in rotating rounds, taking the minimum over the rounds. Changes are against the default build's mean over the paddings; the spread is the slowest padding against the fastest.
#1815, 2026-11-16
GCC 14 on GitHub runners, 18 jobs at x86-64-v2 and v3. The per-CPU table is in #1815 (comment).
- Jump-heavy at v3: 3.4–23% faster; the spread drops from 7–76% to at most 0.7%, except
loop_with_many_jumpdestson EPYC 9V74 (8.8%). Measured on Xeon 6975P-C and 8573C and EPYC 9V74. - Chained MUL: 14–16% faster at v2 on AMD, +1.5% at v3 on EPYC 7763. On a local Zen 3 (GCC 16, v3), indirect-branch mispredicts drop from 6.4M to 0.46M per run, 32% faster.
- Geomean: between −8% and +0.1%. The other programs move by −1.4..+1.0%.
- Not clean on EPYC 7763:
loop_with_many_jumpdestsstays bimodal (spread ~140%), and in one v2 jobJUMPDEST_n0became stable at 17.5% above master's mean.signextendwas 8.7% slower in one EPYC 9V74 v2 job. - Other values:
=16left a 7–35% spread on the jump-heavy programs;=64measured like=32with more code. - CodSpeed reports −0.89% (15 benchmarks −1..−2.5%, 5 up to +1.8%), but flagged that base and head of the changed benchmarks ran on different hardware and toolchains.
Rerun with clang -falign-loops, 2026-11-22
22 jobs: GitHub x86 runners and Neoverse N2, and CircleCI's Skylake-SP, Cascade Lake and Ice Lake machines. Branch bench/baseline-align (67c2c78), https://github.com/ipsilon/evmone/actions/runs/45052026885.
GCC:
-falign-labels=32matches the best default layout on every CPU and removes the spread. On CircleCI's Intel machines, the jump-heavy programs are 11% faster at v2 and 20% at v3, and the geomean is −2.7..−3.3%. On GitHub: EPYC 9V74 v3 −2.9%, Xeon 6975P-C −0.1..−0.7%. The outlier isloop_with_many_jumpdestson EPYC 7763 v3, which already flips between two speeds from one process to the next.-falign-loops=64leaves the handlers unaligned but moves the whole function, so it is another layout sample rather than a control: the jump-heavy programs land 12–25% behind the best default layout, andstructarray_allocis −2..−4%.
clang:
| Runner (clang, level) | geomean =32 / =64 |
JUMPDEST_n0, loop_with_many_jumpdests |
|---|---|---|
| CircleCI Skylake-SP, Cascade Lake (21, v3), 3 jobs | −4.9% / −4.3% | −20%, spread 50% → 0.1% |
| CircleCI Ice Lake (21, v3) | −2.9% / −2.3% | −20%, spread 49% → 0.1% |
| CircleCI, same machines at v2 | −0.8..+0.4% | no change; the default is stable |
| GitHub EPYC 9V45, Xeon 6975P-C, Xeon 8573C (18, v2 and v3) | −0.7..+0.5% | no change |
| Neoverse N2 | −0.1% / −0.3% | no change |
- Faster than every default layout only at v3 on CircleCI's Intel machines:
signextend−15%, MUL-c −4..−6%. - Slower than the best default layout: Skylake-SP v2
signextend+8.5% (=32) and +5.9% (=64); EPYC 9V45 v2 with=64,JUMPDEST_n0+9.5% and MUL-c +3.9%; Xeon 6975P-C v2 MUL-c +5.8%. =32and=64measure the same, and=32costs 1.4 KB instead of 5 KB.
JUMPDEST analysis loop
The variants of analyze_jumpdests() come from the pointer walk in ZisK's evmone patches (0xPolygonHermez/ziskethone, patch 04):
index: master's loop.zisk: the patch, with the pointer advanced in each branch.ptr: master's loop shape with a pointer.szisk:ziskoverconst int8_t*.
The harness (test/internal_benchmarks/jumpdest_pointer_bench.cpp on bench/jumpdest-pointer) runs each loop at 4 code shifts, using an asm jump over .p2align 6 and .skip K. GCC 15 aligns this loop to 32 B, so for it the shifts give only 2 positions in the 64 B line.
- Rounds 1 and 2 (2026-11-20): the Skylake JCC erratum (the back edge's
cmp+jbcrossing or ending on a 32 B boundary) matches the all-STOP slowdowns on Skylake-SP per shift (clang 21: +100% at the affected shifts), but not the mainnet contracts. - Round 3 (2026-11-22, 6ff8fb2, 23 x86 jobs, https://github.com/ipsilon/evmone/actions/runs/45007830563): each loop is compiled 3 ways in one binary: the default,
+al(-falign-loops=64) and+alj(+aland-mbranches-within-32B-boundaries).- clang honours
-falign-loops=64:index's spread over the shifts drops from 5–40% to under 0.5%. Pinned,sziskagainstindexon the mainnet contracts: Coffee Lake −2.4%, Skylake-SP +3.1%, Cascade Lake +5.4%, Ice Lake −7.8%, Granite Rapids +2.4%, Zen 3 −3.5% (clang 21) and −6.6% (clang 18). +aljmakesszisk5–12% faster on Skylake-SP and Cascade Lake, so the erratum costs there.- GCC 13 and 15 ignore
-falign-loops=64for this loop, which stays at 16 B and 32 B, so for GCC+alis the default build again (they agree within 1–2%). - The GCC
sziskoutliers are placement. On Ice Lake with GCC 13 it is +125% and +133% at the two shifts wherecmp $0x5f,%clstraddles a 32 B boundary. On Granite Rapids the 33–35 B loop is fast only when it starts a 64 B line: GCC 15 +2..+8% at line offset 0 and +60..+65% at 32; clang 21 +2% at 0 and +24%, +44%, +43% at 16, 32, 48.szisk'smovsbqis one byte longer thanzisk'smovzbl. - GCC
sziskat line offset 0, againstindexat its best shift: Ice Lake −1.8% and −2.1% (GCC 13 and 15), Skylake-SP +4.1%, Granite Rapids +4.8% and +9.6% (GCC 15 and 13), Cascade Lake +6.3%, Zen 4 +8.4% and +9.9% (GCC 15 and 13), Zen 3 +12.0% and +14.4% (GCC 15 and 13). ptris within ±1% ofindexon every CPU and shift, except with GCC 11 (+6..+8%).- master's
indexloop varies by up to 40% with placement on Granite Rapids with clang, and by ±2% with GCC.-falign-loops=64onbaseline_analysis.cppfor clang would make it stable.
- clang honours
- Aligning this loop: with GCC,
-falign-jumps=64aligns it, because the top of the rotated loop is a jump target; so does__attribute__((optimize("align-jumps=64"))), which GCC's documentation advises against in production code.[[clang::code_align(64)]]is emitted as loop metadata but ignored by the x86 backend for this loop. Inline-asm.p2align 6doesn't work: before the loop, the compiler re-aligns after its own setup code; inside it, the padding lands on the PUSH path. - Round 4 (0242328, 30 jobs, https://github.com/ipsilon/evmone/actions/runs/45014884558) adds Neoverse N2 and V1, Apple M1 and M4 Pro, C910 and Emerald Rapids.
tsziskandtsptrload the opcode asunsigned charon x86 and assigned charelsewhere. Againstindex: ARM GCCszisk−25..−27% andtsptr−19..−21%; Appleszisk−9..−10% andtsptr~0; C910 about −6% for all. On x86 with GCC,sziskis +108% on Granite Rapids with GCC 13 andtszisk+18..+20% on Skylake-SP and Cascade Lake, both from layout;ptrandtsptrare within ±2% everywhere.
Compiler issues (not reported upstream yet)
- GCC:
-falign-jumpsdoesn't apply to computed-goto targets;-falign-labels, the only way to align the handlers, pads every label, fall-through ones included. On x86,-falign-loops=64doesn't move the JUMPDEST loop;-falign-jumps=64does, because the top of the rotated loop is a jump target. - LLVM:
-align-all-nofallthru-blockstests whether the previous block has the block as a CFG successor, not whether it falls through into it, so it skips every computed-goto handler. - LLVM:
[[clang::code_align(64)]]is ignored for a rotated loop on x86.
Measurement notes
- With GCC,
-falign-functions,-falign-jumpsand-falign-loopsdon't move the handlers insidedispatch_cgoto, so builds that differ only in these flags are one sample of the dispatch layout. Sample layouts by padding a handler instead. - Trust an A/B effect only if it has the same sign in every layout and lies outside the range of null tests from the same run: programs that run no changed code measured −0.5..+2.0% with GCC.
- Count branch misses (on Zen 3,
ex_ret_brn_ind_misp) before explaining a fixture change: the MUL-c and jump-heavy swings are mispredict states with identical instructions. - Placement matters beyond 64 B alignment too. Two clang 21 builds with instruction-identical
modexp, shifted by 0x1f0, differed by 10% on 1024-byte inputs, also when both were built with 64 B function, loop and block alignment. Fresh copies of one binary (same bytes and virtual addresses, new physical pages) differed by up to 13%. Take medians over several fresh copies, and call a delta codegen only if it holds in both a default and an aligned build. - Not tried: pinning the dispatcher's address with its own section and a link order file (lld
--symbol-ordering-file, GNU ld ≥ 2.43--section-ordering-file).
Activity
chfast commentedon Nov 25, 2026
Another case, from the intx 0.15 → 0.16 upgrade (#1834), and a note on
-mllvm -align-all-nofallthru-blocks=5with clang 23.Case. clang 23, x86-64-v2, Release, local Zen 3 (5950X),
evmone run --bench, 16M gas, pinned, best of 5. A loop ofPUSH32 b, PUSH32 a, LT, POPruns at half the speed with intx 0.16, with about one branch miss per LT. perf attributes the misses to the PUSH32 and LT handler entries, i.e. the dispatchjmp *. The operands are fixed, so the data plays no role, and the LT code itself is not the cause:Builds of intx 0.16 (whole-build flags except the last row):
jmp *address mod 32-falign-loops=32-falign-loops=64-mllvm -align-all-nofallthru-blocks=5-mllvm -align-all-nofallthru-blocks=6-mllvm -align-all-blocks=5-mllvm -align-all-nofallthru-blocks=5,baseline_execution.cpponly-falign-labels=32(#1815)With intx 0.15, the per-file
-align-all-nofallthru-blocks=5build gives LT pair 1582, SLT pair 1639, SGT pair 1585, so it does not slow the old layout down on these loops.Notes.
-align-all-nofallthru-blocks=5does align handler entries: 87 at 32 B against 47 by default. This differs from the clang 21 observation above that it aligns none of them.-falign-loops=32aligns more handlers (111) and stays slow. What differs is the address of PUSH32's dispatchjmp *: 27 mod 32 in all the slow builds and 11 in the fast one (the low 4 bits, 0xb, are the same in both). That matches the chained-MUL finding that the state follows thejmpaddress, so this build may be another layout sample rather than a fix. It is worth adding to thebench/baseline-alignsweep with the SMOD paddings before drawing a conclusion.COMPILE_OPTIONSproperty the flag has to be a list,"-mllvm;-align-all-nofallthru-blocks=5"; aSHELL:prefix is passed to the compiler verbatim there. I have a local commit with that change; I have not run the full benchmark set on it.