Skip to Content
marmot

Status: proposal. Area: verify (proofs that let the compiler drop checks). Research and numbers: verify-research.md. Reviews: the playground and tools area (critic for verify), and backend (go-bce is a backend pass).

Goal

Remove the index checks of loops whose index is the loop's induction variable plus a constant, when the loop bound is not the slice length. go-bce rule B3 already removes the check when the loop guard compares against the same length value. This covers the rest. One case is a constant or another variable bound (matmul's k < n against len(a[i]) and len(b)). Others are a non-unit step (nsieve's j += i), and several slices indexed in one loop (fannkuch). Instruction counts with every index check removed: matmul -54%, nsieve -57%, fannkuch -20% (research note, section 1b).

Transformation (iteration space splitting)

Take a loop with header H and induction variable i (a block argument of H). It has start s, step d > 0 (a constant, or a loop-invariant value), and guard i < m or i <= m (signed or unsigned). Take the index checks C1..Ck in the loop body that

  • check i + c_j < len_j with c_j a constant >= 0 and len_j loop-invariant (defined outside the loop, or an extractvalue of a loop-invariant slice), and
  • are dominated by the true successor of the guard. They run on every iteration that reaches the latch (they post-dominate the body entry within the loop), or they are conditional. Conditional checks are allowed: the split only removes a check, it never adds one.

Before the loop, compute the main bound

b = min(m', len_1 - c_1, ..., len_k - c_k)    (m' = m for <, m + 1 for <=, saturating)

with signed arithmetic. Every len_j is >= 0 and at most the address space (below 2^48), so len_j - c_j cannot overflow for c_j < 2^62.

Emit:

  1. A main loop: a copy of the loop whose header tests i < b and whose checks C1..Ck carry nocheck. All other checks and every side effect stay. Its exit goes to the original header with the current i (and the current values of every other header argument).
  2. The original loop, unchanged, entered from the main loop's exit (or directly, when the preconditions below fail).

Preconditions, tested before the loop at run time (cheap, once per loop entry):

  • s >= 0, and s >= c_j's negative offsets if offsets below 0 are ever allowed (not in version 1: c_j >= 0 only).
  • d <= 2^62 when d is not a constant. Then i + d cannot overflow while i < b < 2^48.

When a precondition fails, the original loop runs from the start. So the program does exactly what the original does.

Why this is exact: in the main loop, every value of i satisfies s <= i < b. So 0 <= i + c_j < len_j for every removed check. The checks cannot fail there, and the iterations run in the same order with the same side effects. The main loop stops at the first i >= b. From there the original loop runs with all its checks. If b < m', its first check that fails is exactly the check where the original program fails. It fails at the same iteration, after the same side effects.

Proof obligation and independent check

The pass writes a side condition per split loop to its report, as go-bce does. It holds the header, the induction variable, the step and the bound expression. It also holds the list of removed checks with their offsets and lengths. verify/check re-checks each split from the MLIR before and after:

  • the main loop is a copy of the original except for the header test and the nocheck flags.
  • each removed check has the form i + c < len with len among the min operands.
  • the exit edge enters the original header with all header arguments. The arithmetic lemma is proved in Lean next to the existing B3 proof. It says that s <= i < b implies 0 <= i + c < len, with no overflow under the bounds above.

Rules added after review

  1. Width: the induction variable and i + c_j are computed at int width (i64). A narrow signed i can wrap: an int8 i = 127 with c = 1 indexes -128, where the original check fails. Narrow induction variables are skipped. Unsigned narrow ones wrap to a value in range, but they are skipped too in version 1.
  2. Step: a constant step d > 0, or a loop-invariant d tested at run time for 0 < d <= 2^62.
  3. Loop-invariant lengths: len_j is an SSA value defined outside the loop, never a load. It is an argument, a value computed before the header, or an extractvalue of such a value. A slice read from memory in the body (a field, a global, through a pointer) can change by a store, an append or a call. The report check enforces this.
  4. Exits: only the header test's exit goes to the original header. A break, return, goto or labeled continue in the main loop keeps its own target. Every latch adds exactly d to i, and nothing else in the body defines i.
  5. Constants: c_j is tested at compile time (0 <= c_j < 2^62). Otherwise the loop is not split.
  6. Unsigned guards with a bound above 2^63: the signed min makes b negative, the main loop runs no iteration, and the original loop runs. That is safe. An adversarial program keeps it safe.
  7. The report check verifies the preconditions and the computation of b. The min operands must be exactly m' and len_j - c_j of the removed checks. The loop copy and the nocheck flags alone are not enough.

B4 notes: the length argument of makeslice, not the capacity. The add has int width at the make (an int32 x widened after the add is a different value). A negative x admits no i >= 0 under i <= x, so it is safe.

What perfcmp must show, in place of instruction counts: wall clock and cycles, code size (tuios -release), and release build time against the 0.8x goal. Also the number of loops split on the corpus, and the cost when the main loop runs 0 iterations (short loops, len < c).

Limits of version 1

  • Inner loops only, at most 256 operations, code size growth capped per function.
  • Offsets c_j >= 0, step > 0. Decreasing loops (i--) come later, mirrored.
  • Not for checks whose length changes in the loop (b[k][j] in matmul stays checked: 1 of 3).
  • go dialect only (release and -O 2 builds). Dev builds keep their checks.

Companion: lengths from make (T4)

After s := make([]T, e) with e = llvm.add x, c and c > 0 a constant, len(s) == x + c with no wraparound. The add can only wrap to a negative value, and makeslice panics on one. With a guard i <= x (sle) and i >= 0, that gives i < len(s). This is a new go-bce rule (B4) with a Lean lemma. It removes nsieve's checks without splitting.

Checks before a default flip (rule 11)

  • verify/check validation of every split on the conformance corpus and the bench programs (0 unjustified splits).
  • The adversarial programs are in verify/testdata/progs. One is a loop that panics at the bound with side effects before it. Others are a bound below the start, and a step near the overflow limit. Also a length that is 0, a conditional check, several slices with different lengths and an unsigned guard. Outputs equal to Go in every configuration.
  • Conformance matrix and fuzz with the switch forced on.
  • perfcmp in dev, O2 and release, exit 0. Expected: matmul, nsieve, fannkuch and quicksort better, the rest within noise. Code size growth reported.

Ownership

The pass sits next to go-bce in the go dialect, so it is backend code. Proposal: verify writes the pass behind MARMOT_RANGESPLIT=1, and backend reviews it. Or backend writes it, and verify writes the report check, the Lean lemma and the adversarial programs. This is still to be decided.

On this page

Was this page helpful?

Report an issueOpen issues