Skip to Content
marmot

Area: bounds, nil and overflow checks that a proof can remove. Goal from SPEC 3c: run time and CPU at least 1.2x faster than Go on every scoreboard program. To remove a check that gc keeps is one of the few ways to be strictly faster than Go on array code. gc's own prove pass sets the bar.

1. Gaps

1a. Checks left in the main package of each bench program

Static count of bounds checks left after optimization, main package only. gc: the number of Found IsInBounds and Found IsSliceInBounds lines of -gcflags=-d=ssa/check_bce/debug=1. marmot: branch edges into blocks that call runtime.panicBounds in the -release LLVM output. marmot revision 14d3417b97, Go 1.27.4, linux/amd64. Programs with no check in either are left out.

Programgcmarmot
dot11
fannkuch66
generic74
heapsort610
mapstr21
matmul67
nbody02
nsieve42
parse01
quicksort911
regexpbench12
saxpy11
sortslice34
stencil40
stringsearch11
summinmax11
transpose30
wordfreq33

Reading:

  • marmot keeps more checks than gc in heapsort (10 against 6), quicksort (11 against 9), matmul (7 against 6) and nbody (2 against 0). It also keeps more in sortslice, regexpbench and parse. These are the gaps where marmot is not yet at gc's level. Heapsort and quicksort are two of the 12 programs where marmot is slower than Go (quicksort 0.78x).
  • marmot removes more than gc in stencil (0 against 4), transpose (0 against 3), nsieve and generic.
  • The counts are small. Most of the time in these programs goes to loops with one or two checks per iteration. So the real question is what those checks cost at run time (section 1b).

1b. The ceiling: run time with no index checks at all

A private build of the compiler (never landed) emits no index bounds checks when an environment variable is set. The same driver builds both variants of all 45 bench programs with -release, and the two run interleaved, 7 times each, outputs compared. Slice expression checks stay, because the backend lowers those.

Instruction counts (valgrind callgrind, deterministic, no timing lock needed) for the 18 programs that have checks, both variants. The wall clock timing run is still queued behind tonight's gates. Instructions overstate the time saved, because a well-predicted check is cheap, but they show where the checks are and how much work they cost.

Programinstructions, default (G)no index checks (G)fewer
nsieve37.1715.9257.2%
matmul133.3061.5753.8%
fannkuch13.5410.8719.7%
quicksort3.102.5318.5%
heapsort3.663.0716.2%
generic7.006.1012.9%
regexpbench7.206.3412.0%
sortslice11.1610.664.5%
nbody31.0930.103.2%
wordfreq12.1311.931.6%
parse16.7916.640.9%
transpose3.043.040.1%
stringsearch2.322.320.0%
saxpy85.0485.040.0%
summinmax120.92120.92-0.0%
stencil73.0473.04-0.0%
dot142.56142.56-0.0%
mapstr3.893.91-0.5%

Reading:

  • Four programs spend a large share of their work on index checks: matmul (54%), nsieve (57%), fannkuch (20%) and quicksort (18%). Correction: the nsieve number is confounded. Without index ops, go-bool-pack does not apply, so the no-check build of nsieve used a byte array instead of bit operations. B4 later removed nsieve's checks with no change in the instruction count. Heapsort, generic and regexpbench follow at 12 to 16%. Matmul runs at 1.00x of Go and quicksort at 0.78x today, against a 1.2x goal.
  • Where the count does not move (dot, saxpy, stencil, summinmax, transpose), go-bce and LLVM already removed the checks from the hot loop.
  • What the checks are:
    • matmul's inner loop s += a[i][k] * b[k][j] checks k against len(a[i]) and len(b), and j against len(b[k]). The first two have a loop-invariant length and an index that steps by 1.
    • nsieve's flags[j] with j <= n and flags := make([]bool, n+1) is in range. No pass proves it, because n+1 might overflow. But a make that succeeded already rules out the overflow: n+1 >= 0 holds after it, and n+1 can only wrap to a negative value.
    • quicksort and heapsort index with data-dependent values (scan loops that stop at a pivot, sift-down by computed child). These need versioning on the loop's exit or stay.
  • Engine A on the same programs: it proves none of nsieve's index sites and 11 of 21 of matmul's obligations. So feeding today's engine A proofs into codegen (T2) would not reach these checks.

1c. Proofs that exist but do not reach the compiler

Engine A (marmot verify) already proves index and slice sites that gc does not. On 9 std packages: both prove 619 sites, only engine A proves 60, only gc proves 30. Engine B (z3) brings the advantage of gc down to 16. None of these proofs is used by code generation today. Each proved site still has its check.

2. Evidence from research and other compilers

The gains are large in tight array loops where the check adds a compare and branch per iteration. They are largest where the check blocks vectorization. On general code the reported gains are 1 to 3%.

3. Candidate techniques

T1. Loop versioning for checks on induction variables

Take a loop whose index is is + c, with a loop-invariant stride and offset. Examples are matmul's a[in+k] and b[k*n+j], and fannkuch's permutation arrays. Test once before the loop that the first and last index are in range. On success, run a copy of the loop without those checks. On failure, run the original loop. Go semantics are kept exactly: the slow copy panics at the same iteration with the same message.

  • Evidence: HotSpot (SciMark +40%), .NET and Jikes. In Rust kernels the checks block vectorization, and removing them gives 26%.
  • Expected gain for marmot: on matmul, transpose, saxpy, dot, stencil and fannkuch, the inner loops lose a compare and branch per access and may vectorize. The ceiling (section 1b) bounds it.
  • Cost: code size, because each versioned loop is duplicated (keep it to inner loops under a size cap). Also a pass in the go dialect after go-bce.
  • Risk: medium. The proof obligation is that first and last index in range implies every index in range. verify can check this kind independently with the existing report validation.

T2. Engine A proofs as nocheck facts in code generation

Run engine A in release builds and mark every index and slice site it proves, as go-bce marks the sites it proves.

  • Evidence: Pentagons (83% of accesses) and ABCD (45% of dynamic checks). On std packages engine A proves 60 sites that gc keeps.
  • Expected gain: small on the bench programs. The main-package counts in 1a are low, and most remaining checks are data dependent (heapsort, quicksort), which engine A does not prove either. Larger on library code with guard-heavy helpers.
  • Cost: build time. Engine A costs about 1.17x of marmot check on an edit. That is against the 0.8x build time goal, unless it runs only in release builds and is cached per function.
  • Risk: soundness is the whole risk. A wrong proof becomes memory corruption instead of a wrong report. It needs the soundness fuzzer clean over a longer run, and a re-check of the proofs used (the go-bce report validation pattern).

T3. Cheaper checks instead of fewer

Where a check cannot be removed (data-dependent indexes in heapsort and quicksort), make it cheaper. Merge the two compares of a 0 <= i < n check into one unsigned compare (already done). Move the cold panic block out of the loop. Combine adjacent checks on the same slice: check a[j] and a[j+1] once, as j+1 < n.

  • Evidence: standard in gc and LLVM. It is unlikely to give more than a few percent.

3b. Correction: marmot already versions loops (Rule V)

backend/lib/Loops/Versioning.cpp (rule V, proved in verify/lean/RuleV.lean) versions loops for bounds checks at the LLVM level. It uses ScalarEvolution ranges and wrap limits. Correction (2026-11-17): it runs only inside the parallelize pass, which Register.cpp adds only with -par (opts.loops.parallel). Default release builds version no loops at all. It is all-or-nothing by design: the fast copy must have a single exit, so that the vectorizer and loop-parallelize can take it. A loop with one check it cannot cover is not versioned at all. matmul's inner loop has b[k][j], whose length is loaded in the loop. So T1 is an extension of rule V, not a new pass. Steps:

  1. Read the loops remarks for matmul, nsieve and fannkuch to learn why each loop is missed.
  2. Partial versioning: remove the covered checks and keep the others, accepting a fast copy with several exits, which is then not vectorized.
  3. Whatever the nsieve miss is (a loop-invariant step, the j <= n guard against len n+1, or the wrap limits).

4. Pick

  1. T1, now as an extension of rule V (section 3b). It targets matmul (2 of 3 checks per inner iteration), nsieve and fannkuch, where the ceiling is 20 to 57% of the instructions. It has the strongest published evidence (HotSpot, .NET, Jikes). It beats gc, which does not version loops. Each versioning decision has a simple side condition that verify can check again from a report, as it already does for go-bce.
  2. A small companion, T4: no-overflow facts from a successful make (and from other operations that panic on a negative length). After s := make([]T, n+c) with c > 0, len(s) == n+c exactly. This is go-bce rule work: one new fact, proved in the existing Lean or Bend model before it lands. It removes nsieve's checks without versioning.

T2 (engine A proofs into codegen) is not picked for now. The ceiling programs' checks are not among engine A's proofs, it costs build time, and a wrong proof becomes memory corruption. It stays a later step for library code.

Design note: verify-design-loopversion.md.

On this page

Was this page helpful?

Report an issueOpen issues