Skip to content

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
mainfrom
test_1016292121
Draft

copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1016292121

Conversation

@copybara-service

Copy link
Copy Markdown

Fix super-linear LLVM compile time for counted_for with large loop state 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.

…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

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.

1 participant