Research: SSA-level optimizations
Area: the code generator's SSA-level work (escape analysis, allocation, devirtualization, bounds checks, inlining, profile use). Data: bench/TARGETS-linux-amd64.md (2026-11-12, release mode), bench/reasons/*.md, and perfcmp runs on a shared Linux machine on 15 and 16 November. Also the per-function SSA statistics of a cold tuios -release build (MARMOT_SSASTATS, 52897 functions, 3.77 M SSA instructions).
1. Gaps
| Gap | Measurement | SPEC 3c goal | Distance |
|---|---|---|---|
| Allocation-bound library code (class B: fmtout, jsondec, jsonenc, fmtbuf, parse, wordfreq) | release wall speedup against go 0.84x to 1.17x, jsondec: allocation and GC of decoded values are about 50% of the profile, Go allocates the same objects | at least 1.2x faster | 1.4x short at the worst row |
| Dev binary size | tuios dev 92.9 MB against go 70.1 MB (1.33x) | at most 0.8x | 1.66x short |
| Code expansion per SSA instruction (affects size and build time) | append 18.4 MLIR values per instruction, defer 16.8, RunDefers 11.9, range Next 23.6, == and != 4.9, generic instances are only 3.5% of all SSA instructions | (size and build time goals) | large single sources |
Not a gap any more: quicksort. The 0.78x row came from a loaded run. Quiet runs give 1.05x on linux/amd64, and on darwin/arm64 marmot needs 7% fewer instructions and 4% fewer cycles than go (bench/reasons/quicksort.md).
2. Evidence
Stack allocation of small slices (Go).
- Go 1.25 gives make with a non-constant size a 32-byte stack buffer, used when the size fits. Go 1.26 does the same for append on a slice that starts nil. It puts a small speculative stack backing store at the append site. Where the slice escapes, runtime.move2heap makes one exactly sized heap copy, only when the slice is still on the stack. The article reports allocation counts (one to zero), not time. Source: https://go.dev/blog/allocation-optimizations
- marmot already has the Go 1.25 part (stackMakeSlice, 32-byte buffer). It does not have the append part or the move at escape points.
Partial escape analysis (GraalVM).
- Stadler, Wuerthinger, Moessenboeck, "Partial Escape Analysis and Scalar Replacement for Java", CGO 2014 (https://doi.org/10.1145/2544137.2544157). Flow-sensitive: an object is scalar replaced on the paths where it does not escape and materialized only on the paths where it does. The thesis reports up to 58.5% less allocated memory and up to 33% shorter run time on DaCapo, ScalaDaCapo and SPECjbb2005. The gain is best with abstraction layers (https://oberon.ssw.jku.at/Teaching/PhDTheses/Stadler/Thesis_Stadler_14.pdf).
- HotSpot C2 uses flow-insensitive escape analysis after Choi et al. (OOPSLA 1999) with interprocedural summaries after Kotzmann and Moessenboeck (https://cr.openjdk.org/~cslucas/escape-analysis/EscapeAnalysis.html). Its known weakness is the all-or-nothing case that partial escape analysis fixes.
Profile-guided inlining and devirtualization.
- Go: PGO inlining since 1.20, PGO devirtualization of interface calls since 1.21. Most programs of a representative set gain 2 to 14% in Go 1.22 (https://go.dev/doc/pgo). Uber reports 12% on its go-json benchmark suite from PGO inlining, with L1 instruction cache misses -10.8% and iTLB misses -34.6% (https://www.uber.com/blog/automating-efficiency-of-go-programs-with-pgo).
- .NET guarded devirtualization (Dynamic PGO, default since .NET 8): a type check and an inlined fast path at hot interface call sites. Public numbers mix several effects.
- marmot already passes -pgo to LLVM (instrumented profiles), so LLVM's indirect call promotion covers part of this. The frontend does not use the profile for its own decisions (escape, inlining across interface calls, devirtualization before escape analysis).
Bounds check elimination.
- ABCD (Bodik, Gupta, Sarkar, PLDI 2000, https://homes.cs.washington.edu/~bodik/ucb/research/pldi00.html): about 45% of dynamic bounds checks removed on average, fewer than 10 analysis steps per check.
- Go's prove pass and loopbce cover induction variables. An open proposal replaces it with an eSSA range analysis (https://github.com/golang/go/issues/80146).
- marmot has bounds check removal in the backend (go-bce) and the checker's proofs. The measured programs do not show bounds checks as the bottleneck now (quicksort: as tight as gc).
Generic code sharing.
- Go 1.18 GC shape stenciling with dictionaries: one body per GC shape (https://go.googlesource.com/proposal/+/master/design/generics-implementation-gcshape.md).
- For marmot this addresses only 3.5% of tuios's SSA instructions, so the size gain is small.
3. Candidates
| Candidate | Gap | Expected gain for marmot | Cost | Risk |
|---|---|---|---|---|
| A. Speculative stack buffer for append from nil, heap move at escape points (Go 1.26) | allocation-bound programs | removes the first allocations of short local slices, programs that build small slices per item (fmt arguments, parse tokens, wordfreq fields) lose one heap allocation per slice, jsondec's decoded slices escape into the result, so they gain only where a temporary slice is built | medium: codegen for append sites, a move at each escape point (store, return, call argument that escapes, closure capture), the existing escape analysis decides | memory lifetime transform: rule 11 sign-off (verify), poison mode tests, stack size per frame grows by 32 bytes per site |
| B. Partial escape analysis (materialize on the escaping path only) | allocation-bound programs | objects that escape only on error or rare paths stay on the stack or in registers on the hot path, Graal reports up to 58.5% less allocated memory | high: a flow-sensitive pass over SSA with virtual object states at merges | lifetime transform, large surface for wrong code |
| C. Frontend use of the profile (devirtualize hot interface calls before escape analysis, inline through them) | class B interface-heavy code | 2 to 14% from Go's experience, more here, because devirtualizing before escape analysis can turn escaping arguments into non-escaping ones | medium: profile reader in the driver, call site counts from LLVM profiles or a sampled pprof | profile staleness, build keys must include the profile (already do) |
| D. Smaller expansions of append, defer and range-over-func | size, build time | append is 18.4 values per instruction, an out-of-line slow path for append growth would cut most of it | low to medium | none for semantics, perf must be measured |
4. Pick
Pick A.
- A is the proven, contained form of the allocation work, and Go ships it. It fits the escape analysis that marmot already has, and it targets the gap with the largest distance. It needs the rule 11 path: opt-in switch, written rule, verify sign-off, poison tests.
- D was examined and dropped (16 November). The append growth path is already a cold out-of-line block with one runtime call. Most of the 18.4 values per append are slice field extracts and inserts and two block arguments, which LLVM folds. A smaller expansion would cut MLIR text but little machine code.
- B stays the longer-term step after A shows how often slices escape only on cold paths (the allocation report can count that).
Next step: a design note for A, with reviews of allocation and stack scanning (runtime) and of lifetime soundness (verify). Then an opt-in switch MARMOT_APPENDSTACK=1.