Repository navigation
Fix super-linear LLVM compile time for counted_for with large loop state in the JIT. - #5545
Draft
copybara-service[bot] wants to merge 1 commit into
Draft
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
…ate in the JIT. `HandleCountedFor` loaded and stored the entire loop state as an LLVM first-class aggregate value: once when initializing the state buffer and once per iteration when copying the next state back into the state buffer. LLVM scalarizes such aggregate loads/stores element-by-element (InstCombine unpacks every struct and any array of up to 1024 elements, recursively; SROA unpacks aggregate loads/stores of allocas with no size limit), producing one instruction (or `insertvalue`) per array element. Subsequent passes (SLPVectorizer, InstCombine, GVN) and the backend then run in super-linear time and memory on the result. For a `counted_for` whose state is e.g. `(token, bits[1], bits[64][20480])` this made `aot_compiler_main` take minutes and tens of GiB of memory on otherwise trivial unoptimized IR. Hold the loop state in two buffers that are ping-ponged between iterations instead: the body reads from one buffer and writes the next state into the other, with the two pointers carried across iterations as loop-carried phis and swapped on every iteration. The state is only ever moved with `memcpy` (into the first buffer before the loop and out of the final buffer after it). This also removes the full copy of the state (and, for states larger than the stack alloca limit, a heap allocation and free) on every iteration, resolving a long-standing TODO. Measured with `aot_compiler_main` on a proc whose `counted_for` state is a tuple wrapping a `bits[64][4096]` array updated at a dynamic index on each of 15 iterations: 8.05 s / 1.48 GB before, 0.04 s / 53 MB after. A `bits[64][1024][9]` state updated at a dynamic row index: 20.4 s before, 4.1 s after (the remainder is the per-element packed-ABI wrapper, which is unrelated to `counted_for`). Add an evaluator test with a large (heap-buffered), tuple-wrapped array loop state updated at a dynamic index, covering trip counts of zero, one, and both parities, so that both orientations of the ping-pong buffers and the zero-trip path are exercised under both the interpreter and the JIT. PiperOrigin-RevId: 1016292121
copybara-service
Bot
force-pushed
the
test_1016292121
branch
from
November 24, 2026 19:48
2bdb274 to
a079be6
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix super-linear LLVM compile time for counted_for with large loop state in the JIT.
HandleCountedForloaded and stored the entire loop state as an LLVMfirst-class aggregate value: once when initializing the state buffer and
once per iteration when copying the next state back into the state buffer.
LLVM scalarizes such aggregate loads/stores element-by-element (InstCombine
unpacks every struct and any array of up to 1024 elements, recursively; SROA
unpacks aggregate loads/stores of allocas with no size limit), producing one
instruction (or
insertvalue) per array element. Subsequent passes(SLPVectorizer, InstCombine, GVN) and the backend then run in super-linear
time and memory on the result. For a
counted_forwhose state is e.g.(token, bits[1], bits[64][20480])this madeaot_compiler_maintakeminutes and tens of GiB of memory on otherwise trivial unoptimized IR.
Hold the loop state in two buffers that are ping-ponged between iterations
instead: the body reads from one buffer and writes the next state into the
other, with the two pointers carried across iterations as loop-carried phis
and swapped on every iteration. The state is only ever moved with
memcpy(into the first buffer before the loop and out of the final buffer after
it). This also removes the full copy of the state (and, for states larger
than the stack alloca limit, a heap allocation and free) on every iteration,
resolving a long-standing TODO.
Measured with
aot_compiler_mainon a proc whosecounted_forstate is atuple wrapping a
bits[64][4096]array updated at a dynamic index on eachof 15 iterations: 8.05 s / 1.48 GB before, 0.04 s / 53 MB after. A
bits[64][1024][9]state updated at a dynamic row index: 20.4 s before,4.1 s after (the remainder is the per-element packed-ABI wrapper, which is
unrelated to
counted_for).Add an evaluator test with a large (heap-buffered), tuple-wrapped array loop
state updated at a dynamic index, covering trip counts of zero, one, and
both parities, so that both orientations of the ping-pong buffers and the
zero-trip path are exercised under both the interpreter and the JIT.