# tier0 dev speed: causes and ranked plan (2026-11-18) (/docs/experiments/tier0/devspeed)

Goal (2026-11-18): dev build programs run at 0.46x of go on macOS and 0.41x on Linux. Find the top causes on the bench programs with MIR on and rank the fixes. Constraint: no change slows dev builds by more than 2%.

## Baseline facts [#baseline-facts]

- 0.46x: Mac night bench (arm64) at bd3fa968. 0.41x: Linux night bench (x86-64) at 961b5f85. Geomean of go time over marmot dev time, 45 programs.
- Config of those runs: MIR off (both commits are before the reflip 9660f5c9, and MIR was reverted again in b8e0a99c), MARMOT_TIER1=0 (bench/cmd/bench sets it unless the caller does). So they measure the older emitters. The new baseline (MIR on, MARMOT_TIER1=0, Linux, queue) is run after the reflip lands.
- With MARMOT_TIER1=0 a dev build compiles every Go package with tier0, the standard library included (driver tier0(): every non-release build without -O). Only the C runtime is precompiled at -O 2. So fmt, strconv, json, regexp and the Go part of maps run as tier0 code in the bench.

## Method (static, no timing) [#method-static-no-timing]

Setup: marmot 8a4a62e0 on macOS. Every bench program is built dev with MARMOT_TIER0_MIR=1 and with -O 2, with --nocache. The targets are arm64 native and linux/amd64 cross (objects only). Disassembly of the object that defines main.main. Counts over the 45 main objects, x86-64:

| Count | dev (MIR) | -O 2 |
| --- | --- | --- |
| instructions | 13,617 | 9,246 |
| instructions that touch (%rsp) | 3,022 | 34 |
| register to register movq | 2,618 | 961 |
| conditional jumps (signed and unsigned compares) | 383 | 122 |
| unconditional jmp | 275 | 165 |
| shl by 1 to 3 (index scaling) | 117 | 0 |
| div and idiv | 74 | 56 |
| movaps/movapd register copies | 188 | 75 |

-O 2 also inlines and vectorizes, so the totals overstate what tier0 can reach. The per-loop samples below compare against scalar code.

## Samples (x86-64, MIR on) [#samples-x86-64-mir-on]

- saxpy, y[i] += a*x[i]: 27 instructions per element (scalar code needs about 6). Of those: 9 build the three addresses (mov, shl, add per access instead of (base,index,8)). 3 compare and branch pairs check i against len(x) once and len(y) twice, after the loop test already checked len(x). 2 copy the loop counter between two registers (phi not coalesced). 1 jmp closes the loop because the test is at the top. The function is a leaf, yet it saves and restores rbx, r12, r13 and r14 (8 stack accesses per call). arm64 has the same loop shape (lsl, add, ldr, the same checks, mov x2, x5) but no callee saved spills.
- histogram, hist[b]++ over a byte slice: 26 instructions per byte. B comes from a byte load (zero extended), yet it is compared with 256 twice. buf[i] is compared with the length after the loop test did the same. The address of hist (a stack array) is rebuilt with leaq, mov and add for the load and again for the store.
- conv, s += in[i+j]*k[j]: 29 instructions per tap. The extra instructions: two bounds checks (k[j] with j &lt; 16 known), two address builds and two phi copies. Also two zero extension self moves (movl %r12d, %r12d) after a 32-bit multiply and add. Also mov then imul, because the two-address form is not planned for. The array k initialization is followed by 16 dead loads of its elements. i%23 and i%5 use idivq.
- fib: the n &lt; 2 path saves and restores two callee saved registers that only the other path uses (no shrink wrapping).

## Ranked plan [#ranked-plan]

Order by expected gain per cost. Each item is opt-in behind a MARMOT_TIER0_* switch until its perfcmp (dev, -b-env), conform and fuzz pass, as for the earlier MIR changes. Each one removes instructions from the output of tier0 and adds at most a linear pass. So build time should not move. Buildbench dev warm and cold before and after is part of each gate (limit 2%).

1. Addressing modes (both targets). Fold base + index &lt;&lt; scale + constant into the load or store operand (x86-64 (base,index,8) and disp(%rsp,index,8), arm64 [x0, x5, lsl #3]), also for stack arrays. This removes 3 instructions per indexed access. That is about a third of the loop body in saxpy, conv and histogram. The share is similar in prefixsum, summinmax, dot, matmul, stencil, transpose and bytecount. Cost low: a selection rule in the emitters on single-use address values.
2. Bounds and range checks. Flip DOMCHECKS (gated already: dev geomean 0.913, exit 0, at 575eba2e). Then add: (a) one check per (index, length) pair in a block and the blocks it dominates (y[i] load and store, hist[b] load and store). (b) ranges from zero extension (a byte is below 256), from x & (2^k - 1), and from constant indexes into fixed arrays. Removes 1 to 3 compare and branch pairs per iteration in the samples. Cost low to medium: MirBuild already has the dominator walk for DOMCHECKS.
3. Loop carried values and loop shape. Coalesce a phi with its incoming value in MirAlloc: i and i+1 get the same register when the old value dies. Also test loops at the bottom: copy the header test to the latch. Removes the per iteration copies and the jmp (2 to 4 instructions per iteration in every loop, register copies are 2.7x those of -O 2). Cost medium.
4. x86-64 register choice. (a) Use caller saved registers for intervals that cross no call. Calls followed by unreachable already clobber nothing. saxpy would save 8 stack accesses per call, and every small leaf gains. (b) Allocate the result of a two-address operation to its first operand when that operand dies (mov plus op becomes op). (c) Drop zero extension moves after 32-bit operations, which already zero the upper half. Cost low to medium.
5. Calls in loops: land the pending gates, inlining of small leaf callees (MARMOT_TIER0_INLINE) and values over calls in caller saved registers (MARMOT_TIER0_CALLSAVE). spectralnorm, the worst program (0.10x on Linux), has its four-operation callee in the inner loop.
6. Stack traffic in large functions: structcopy (72% of instructions touch the stack) and sha256 (68%) keep aggregates as pieces in slots. 64 pieces is the default on main. Next, aggregates copied as memory blocks instead of pieces. Shrink wrapping of the callee saved saves (fib).
7. Small items: division by a constant through multiplication (74 div instructions in the main objects), dead load removal (conv's 16 loads).
8. Not tier0, for a project decision: in the bench config the standard library is tier0 code. The profiles can show that fmt*, json*, regexp, parse and wordfreq spend most of their time inside std. Then the larger lever is -O 2 objects for packages outside the main module. These are prebuilt with the toolchain or built once into the cache. They cost no dev build time after the first build. With MARMOT_TIER1 on (the default) users get that after the background pass. The bench turns it off.

## Next measurements (Linux, queue, after the reflip) [#next-measurements-linux-queue-after-the-reflip]

1. Baseline: bench dev with MIR on and MARMOT_TIER1=0 (45 programs).
2. Perf record of each dev binary: the share of samples in main, in std packages and in the C runtime, and the hottest loops. This weights items 1 to 7 against item 8 and replaces the static counts.
3. Item 1 first (largest static share, lowest cost), then 2 and 3.