# Research: dev code quality (/docs/experiments/tier0/research)

Area: dev builds, where tier0 compiles the packages that were just edited (all of a one-package program on its first build). Goals (SPEC 3c): run time at least 1.2x faster than go build, size at most 0.8x, memory at most 0.7x, build time at most 0.8x.

## Gaps [#gaps]

Bench run on Linux x86-64 at cfc9a774 (bench/cmd/bench, go against marmot-dev with MIR on, MARMOT_TIER1=0, one-package programs, so all of the program is tier0 code):

| Program | go | marmot dev (MIR) | dev / go | goal |
| --- | --- | --- | --- | --- |
| spectralnorm | 5.98 s | 43.0 s | 7.19x | at most 0.83x |
| strconcat | 0.62 s | 2.54 s | 4.07x | |
| matmul | 15.9 s | 52.6 s | 3.30x | |
| iface | 0.72 s | 2.18 s | 3.03x | |
| jsondec | 0.93 s | 2.79 s | 2.99x | |
| fmtbuf | 1.53 s | 4.52 s | 2.96x | |
| fannkuch | 1.97 s | 5.09 s | 2.58x | |
| quicksort | 0.84 s | 1.42 s | 1.69x | |
| nbody | 16.2 s | 24.1 s | 1.49x | |
| mapstr | 1.16 s | 1.67 s | 1.44x | |
| bintrees | 1.22 s | 1.46 s | 1.20x | |
| pingpong | 0.87 s | 0.67 s | 0.77x | |

Size is not the gap on these programs (dev binaries are about 0.1x of go's). The same programs at -O 2 run close to go (spectralnorm 4.87 s, matmul 14.0 s in a perfcmp at 575eba2e). So the code from tier0 causes the gap, not the runtime.

1. Run time of loop code in dev, worst on float loops with calls (spectralnorm 7.2x go, matmul 3.3x). Cause seen in spectralnorm: the dev build does not inline a(i, j), a four-operation leaf, so its call sits in the inner loop. Across that call the float accumulator lives in a stack slot, because x86-64 has no callee saved XMM registers. The index and the length need two of the five callee saved integer registers, so they are spilled too.
2. Register allocation quality: MIR gives each value one register or one slot for its whole interval (linear scan without splitting, no loop weights). One use far away (a panic block) or one call inside the interval decides the location for the whole loop. Fixed for panic blocks (strconcat 0.522 in a not yet conclusive run). Calls remain.
3. Checks and calls in string and interface code (strconcat 4.1x, iface 3.0x, jsondec 3.0x). Some are bounds and nil checks that dominating checks already decide (DOMCHECKS: strconcat 0.486 in a perfcmp with exit 0). Others are runtime helper calls that gc inlines.

## Evidence from other compilers and papers [#evidence-from-other-compilers-and-papers]

- Inlining small functions in fast tiers. Go gc has inlined small leaf functions since its first releases. The mid-stack inlining proposal (Go issue 19348) measured a further 9% geomean on the Go1 benchmarks on amd64 and 9 to 10% on ppc64. The cost was about 11% more binary size (https://go.googlesource.com/proposal/+/master/design/19348-midstack-inlining.md). HotSpot's fast tier C1 inlines methods up to 35 bytecodes. Raising its limits was rolled back because C1 frames grow (JDK-8235673, JDK-8356389). V8's mid tier Maglev also inlines.
- Linear scan with interval splitting. Wimmer and Moessenboeck, VEE 2005: lifetime holes, splitting and spill placement made integer benchmarks about 15% faster than HotSpot's earlier local allocator. The compile speed was the same (https://ssw.jku.at/Research/Papers/Wimmer05). Wimmer and Franz, CGO 2010: linear scan on SSA form, equal or slightly better code, faster liveness (https://bernsteinbear.com/assets/img/wimmer-linear-scan-ssa.pdf). Poletto and Sarkar, TOPLAS 1999: linear scan within about 12% of graph coloring, several times faster to run.
- Spill choice by next use. Braun and Hack, CC 2009: furthest next use (Belady) extended to control flow graphs with loop aware next-use distances (https://i44vm2.informatik.kit.edu/uploads/publikationen/braun09cc.pdf). Go's own register allocator (cmd/compile/internal/ssa/regalloc.go) is of this kind. Values move into registers just before use, and the value whose next use is furthest is evicted. Spills are placed at the dominator of the restores.
- Splitting at the cost of a move, hoisted out of loops. Cranelift's regalloc2 (2022): about 20% faster compilation and up to 10 to 20% faster code on register pressure sensitive Wasmtime benchmarks against its previous allocator. Split points are moved out of loops (https://cfallin.org/blog/2022/06/09/cranelift-regalloc2/). SpiderMonkey weights spill costs by loop depth (Mozilla bugs 1689328 and 1896581).
- Single pass back ends. TPDE (Schwarz, Kamm, Engelke, 2025): instruction selection, register allocation and encoding in one pass after a liveness and loop analysis. 8 to 24x faster than LLVM -O0 at about the same run time as -O0 code (https://arxiv.org/abs/2505.22610). Winch, the Wasmtime baseline compiler, skips an IR and register allocation and runs slower than TPDE code by 1.14x. Copy-and-patch (Xu and Kjolstad, OOPSLA 2021): code generation is 100x faster than LLVM -O0, with code 14% faster than -O0. It is 4.9 to 6.5x faster than Liftoff, with 39 to 63% faster code (https://arxiv.org/abs/2011.13127).

## Candidates for marmot [#candidates-for-marmot]

| Candidate | Expected gain | Cost | Risk |
| --- | --- | --- | --- |
| A. Inline small leaf callees of the same module in MIR | spectralnorm several times (the call and all spills of its loop go), other programs with small helpers in loops 5 to 20%, size about neutral for tiny callees | medium: clone a callee's MIR into the caller with renamed values and blocks | tracebacks and line tables inside inlined code, restricting the first version to callees that cannot panic or call removes that |
| B. Split intervals at calls: a value lives in a caller saved register and is stored and reloaded around the calls in its interval, instead of one slot for all of it | loops with calls (float accumulators on x86-64 above all): 10 to 30% on such loops, by the VEE 2005 and regalloc2 numbers | medium to high: interval pieces, moves at split points, resolution at block edges | wrong moves on edges, needs the conform and fuzz matrix |
| C. Loop depth weights for the choice of what to spill | 5 to 15% on loops under pressure | low on top of B | heuristic regressions on some programs |
| D. Copy-and-patch or a TPDE-style single pass back end | compile time only, tier0 compile time is not the gap, and their code is at -O0 level | high | none needed now |

## Pick [#pick]

1. A, leaf inlining in MIR, comes first. It is the cause of the largest measured gap (spectralnorm 7.2x go). The change is local to the MIR builder, and it is opt-in until gated.
2. B with C next: it addresses the second cause (one location for a whole interval) where calls remain after inlining.

Dropped for now: D (it targets compile time, which meets the goal already).