perf: bounds-check elimination in loop-indexed array access#506
Merged
Conversation
Contributor
Benchmark Results (Linux x86-64)
CLI Tool Benchmarks
|
cs01
added a commit
that referenced
this pull request
Apr 13, 2026
cs01
added a commit
that referenced
this pull request
Apr 14, 2026
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.
Why this matters
Every
arr[i]emits a runtime bounds check (icmp + branch + fail-path +llvm.assume). In loops where the index is provably in-bounds, those checks are pure overhead that bloat the generated IR and prevent LLVM from vectorizing cleanly. This PR teaches the compiler to recognize the most common safe loop pattern and emit a fast-path GEP with no bounds check at all.What users will see
while (i < arr.length)andfor (let i = 0; i < arr.length; i++)patterns. Fewer icmp/branch instructions inside the hot loop, fewer basic blocks per access, smaller.lloutput.array-bounds-elim-safe.tsfixture drops from 11 bounds-check labels to 2 when compiled with the patched compiler. Thestringsearchbenchmark drops from 8 to 5 bounds-check labels;stringopsdrops from 5 to 2.array-bounds-elim-unsafe-offset.ts,array-bounds-elim-unsafe-pop.ts) explicitly prove that offset indices (arr[i+5]insidei < arr.length) and array-mutating loop bodies (e.g.arr.pop()inside the loop) keep their runtime bounds checks.What's actually eliminated
The analyzer runs before loop body codegen and registers
(indexVar, arrayVar)pairs on a per-loop-scope stack. TheIndexAccessGeneratorconsults the stack when it sees a directarr[i]pattern and skipsemitBoundsCheckwhen the pair is proven safe.Supported patterns:
for (let i = 0; i < arr.length; i = i + 1) { ... arr[i] ... }(Pattern A)while (i < arr.length) { ... arr[i] ...; i = i + 1 }(Pattern B)i = i + kwherekis a non-negative numeric literal (Pattern C)Soundness bail-outs (conservative — keep the bounds check):
push/pop/shift/unshift/splice/sort/reverse/fill/copyWithinon the array inside the bodyCoverage limitations (honest call-outs)
a[row * N + k]inbenchmarks/matmul/chadscript.tsis not a directarr[i]access, so bounds checks remain. Matmul timings are unchanged by this PR (~109ms on my laptop, same as main)..lengthloop bounds are not optimized.while (i < N)orwhile (p * p <= LIMIT)(sieve, nbody, quicksort) do not match the pattern — the analyzer only recognizesi < arr.lengthwherearris the access target. A future PR could track fixed-length arrays (new Uint8Array(N+1)) and provei < N+1 == arr.length.arr[i] = x) still emit bounds checks. The primary win is read-heavy loops.Scope
Applied to four access paths in
src/codegen/expressions/access/index.ts:generateNumericArrayIndex(number[])generateStringArrayIndex(string[])generateObjectArrayIndex(object[])generateUint8ArrayIndex(Uint8Array reads)JSON array index paths and index-assignment paths are unchanged.
Test plan
npm test— 777/777 pass (3 new fixtures)bash scripts/self-hosting.sh --quick— Stage 0 + Stage 1 both greenarray-bounds-elim-safe.ts— exercises the pattern, asserts correct sumsarray-bounds-elim-unsafe-offset.ts— proves out-of-boundsarr[i+5]still exits 1array-bounds-elim-unsafe-pop.ts— proves mutating loop keeps its bounds checkarray-bounds-elim-safe.tsgoes from 11 to 2bounds_faillabels