Skip to content

Keep method-splitting drivers under HugeMethodLimit - #334

Draft
andreaTP wants to merge 2 commits into
bytecodealliance:mainfrom
andreaTP:split-trampoline-driver
Draft

andreaTP wants to merge 2 commits into
bytecodealliance:mainfrom
andreaTP:split-trampoline-driver

Conversation

@andreaTP

@andreaTP andreaTP commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Implements the three steps of #287, with method sizes measured instead of estimated.

  1. Every chunk case of the trampoline driver jumps to one shared "PC done?" check: about 15 bytes per chunk instead of 26.
  2. When the driver does not fit the limit, the chunks are split into the fewest even groups whose drivers fit. Each group gets a sub-driver driver<g>_<name> with the same loop, and the function's method dispatches on pc / groupSize. This also removes the 64 KB failure above ~2300 chunks.
  3. ChunkSizeTest also checks the drivers, and adds basic.dotnet.wasm.

The second commit measures sizes: the measuring pass, each planned chunk and the drivers are written into a throwaway class and their code length read. A chunk over the limit is planned again with its estimate raised by what it missed (up to 8 rounds, at most 4 in the modules below). Measured sizes match the generated classes, or are a few bytes high where an ldc gets its 2-byte form.

Sizes

Methods of split functions over 8000 bytes:

Module main first commit (estimated) second commit (measured)
basic.dotnet.wasm 3 2 0
main.go.wasm 11 10 0
CPython 3.14 12 10 0
QuickJS, Javy, Lua 0 0 0

The driver of CPython's _PyEval_EvalFrameDefault (300 chunks) is 8,154 bytes on main and never JIT-compiled. Here it is 4,596 bytes and compiled by C2.

Run time

AWFY on CPython 3.14, QuickJS and Lua built for WASI. JDK 25, a fresh JVM per run, steady state = median of the second half of the iterations, median of 6 runs, p from a permutation test.

CPython main (ms) this PR (ms) p
Richards 3116 1040 0.014
Json 930 354 0.015
Towers 236 84 0.014
Bounce 107 44 0.020
DeltaBlue 54 51 0.86
  • QuickJS and Lua, same five benchmarks: no significant change (all p > 0.05).
  • Estimated vs measured sizes: no significant difference (CPython, 10 runs of 20 iterations, all p ≥ 0.22).
  • Sub-drivers cost a call on a jump between groups. With -Dendive.hugeMethodLimit=4000, CPython's eval loop in 2 groups is 12% slower on Richards than one driver over the same chunks (18 runs, p = 0.03). With the default limit only functions over about 500 chunks get them: CPython's startup-only _PyUnicode_InitStaticStrings and .NET's func_4610.

Compile time

Within noise for .NET, Go, QuickJS and Lua. CPython is 3–7% slower (about 0.3 s) from the extra emission rounds.

Testing

  • compiler, compiler-tests and machine-tests pass.
  • With -Dendive.hugeMethodLimit=1500:
    • wabt, built with 270 split functions, passes its tests and compiler-tests. The only failures are 4 in SimdBoundaryTest and WideMultiValueReturnTest, which assume the default limit and fail on main too.
    • machine-tests passes, with 770 split functions in its build-time modules.

Closes #287

The chunk cases of a trampoline driver now share one return check, and a
function whose driver would go over the limit gets sub-drivers that each
run an even share of the chunks. ChunkSizeTest also checks the drivers.
@andreaTP
andreaTP marked this pull request as draft December 3, 2026 10:38
The planner sized chunks with CodeSizeCounter plus fixed costs, and
drivers with fixed per-chunk costs. Chunks with many exits went over
HugeMethodLimit (basic.dotnet.wasm, main.go.wasm, CPython), and
sub-drivers were added before they were needed.

Instruction sizes now come from writing the measuring pass into a
throwaway class. Each plan is emitted the same way and every chunk
measured: a chunk over the limit is planned again with its estimate
raised by what it missed, for up to 8 rounds. Drivers are measured too,
with the fewest sub-drivers that fit.
@Bernhard2000

Copy link
Copy Markdown
Contributor

Worked on this issue in parallel myself and seems like you have been a bit faster. I did find a minor oversight in your fix, a br_table can still exceed 8000 bytes by itself, fixable by splitting apart the br_table itself in such cases. Added a commit addressing this on top of this PR in a Fork (Bernhard2000@9c09de3), feel free to cherry pick or partially adapt it. Code isn't exactly the cleanest, but it works.

@andreaTP

andreaTP commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for looking into this! A br_table whose stubs alone go over the limit is a known limitation (it's under "Not splittable" in METHOD_SPLITTING.md). None of the methods still over the limit in the modules we measured (CPython, .NET, Go, QuickJS, Lua, Javy) has a br_table, so for now we'd rather not add another way of splitting. If you have a real module that hits it, please open an issue with it and we can look at it again with that module as the test case.

@sampokuokkanen

Copy link
Copy Markdown

Tested on libprism (ruby/prism compiled to wasm). This fixes the one oversized method we had.

On main, function 46 is estimated at 8,776 B and gets split, but its real size is 7,647 B. The split also leaves chunk0_func_46 at 8,967 B, over the limit. With this PR, function 46 isn't split and no method is over 8000 B. Output is identical, and warm speed is the same as on main (function 46 isn't hot for us).

Two small ideas:

  1. measure() now emits every function over 200 instructions to measure it, including functions that won't be split. A cheap upper-bound estimate first, with the exact pass only when the estimate is over the limit, could save most of that compile time.
  2. If all 8 planning rounds still leave a chunk over the limit, the last plan is kept. Keeping the round with the smallest overshoot would be cheap, since the sizes are already measured.

Thanks!

This branch has not been deployed

No deployments
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.

Method splitting: trampoline driver goes over HugeMethodLimit past ~300 chunks

3 participants