Backend
Experiments in the LLVM backend: build time, parallel code generation and the release LLVM.
This page records the backend experiments of the last days. Each section gives what was tried, why, how it was measured, the verdict and the next step. Measurements are on linux/amd64 (16 cores, builds capped at 8) unless a section says otherwise. "perfcmp" is tests/perfcmp. It builds hello, ten bench programs, sketch and tuios in dev, -O 2 and -release twice (A and B). Then it compares run time, CPU, memory and size. A change becomes the default only when perfcmp exits 0 in all three modes.
Split groups clone only what they reach (MARMOT_SPLIT_REACH)
What: a large module (more than -split-large instructions, 20000 by default) is cut into groups that are optimized in parallel. Before, every group got a copy of every linkonce, weak and available_externally definition of the module. With MARMOT_SPLIT_REACH=1 a group gets only the definitions it reaches from its own functions and imports. Weak definitions stay in group 0, and a comdat goes whole.
Why: those copies were optimizer input that every group paid for and the linker then threw away.
Measurement: tuios, cold builds, two runs each, the backend at main with and without the change, MARMOT_IRSTATS per build.
| -O 2 before | -O 2 after | -release before | -release after | |
|---|---|---|---|---|
| functions into the optimizer | 483,371 | 459,065 | 205,233 | 142,821 |
| instructions into the optimizer | 7.13 M | 6.69 M | 6.07 M | 4.94 M |
| optimizer time, summed (s) | 933, 905 | 883, 858 | 917, 884 | 888, 797 |
| build CPU (s) | 2078, 2087 | 2044, 1990 | 1566, 1526 | 1504, 1383 |
With the switch off, the output is byte-identical to before (hello, the bench programs and sketch in all three modes: 36 of 36 binaries). Conform in O2 and release with MARMOT_SPLIT=400 (so that many modules split): 1528 of 1550 with and without the switch. The tests that differed were timeouts, and a rerun of them passed both ways.
Verdict: landed opt-in. The perfcmp gate (-b-env MARMOT_SPLIT_REACH=1) is running.
Next: flip the default on exit 0.
Home instances (MARMOT_HOME_INSTANCES)
What: a frontend switch. A generic instance or wrapper of more than 12 SSA instructions is emitted once, in an instances module. Before, every module that used it had a linkonce copy. The backend side is the import of small home-owned bodies where inlining needs them.
Why: on tuios, copies were a large part of the optimizer's input in the biggest modules.
Measurement: tuios -release, cold, split reach on in both builds.
| off | on | |
|---|---|---|
| instructions into the optimizer | 4.94 M | 4.59 M |
| optimizer time, summed (s) | 747 | 687 |
| code generation time, summed (s) | 638 | 603 |
| build wall time (s) | 482 | 424 |
The first measurement did not link: undefined local symbols (.str.N, .box.N). That was a backend bug, described in the next section, and it is fixed.
Verdict: opt-in. The perfcmp gate (-b-env MARMOT_HOME_INSTANCES=1) is queued.
Next: flip on exit 0. Then look at which instances are too large for the import limit (40 LLVM instructions) and are still called from hot code.
Whole-program imports keep local constants
What: a -release build imports small bodies across modules. The import source turned every definition it did not import into a declaration, local constants (string data, boxed values) too. But linkonce bodies linked along with an import still used them.
Why: found when home instances did not link. The same bug made a wasip1 -release build of cmd/marmot fail with an undefined .box.10.
Measurement: a new backend test (wpo-import-local) fails before and passes after. The bench programs, hello and sketch are byte-identical before and after in all three modes (36 of 36). Only builds that failed to link change.
Verdict: landed.
Park clear (MARMOT_PARKCLEAR)
What: a machine pass zeroes spill slots that are dead at park calls (select, channel operations, sleeps, waits).
Why: a parked goroutine is scanned conservatively, and dead spill slots kept dead pointers alive.
Measurement: gcstress conform of the tests that had timed out (http/fileserver, http/https_selfsigned, world/embedfs). 3 of 3 rounds pass with and without the pass, so load caused the earlier timeouts. perfcmp with -b-env MARMOT_PARKCLEAR=1: verdict ok, exit 0. Geomean wall dev 0.998, -O 2 1.004, -release 1.010. tuios -release 0.960. Two earlier perfcmp runs had no verdict. In the first, a host tool removed the job's temporary directory. In the second, the tuios -release build hit the time limit under load. Their WORSE rows were on binaries that are byte-identical with and without the pass.
Verdict: on by default. MARMOT_PARKCLEAR=0 turns it off.
Static backend against the official LLVM release (LLVM_RELEASE)
What: build marmot-backend against the official LLVM 22.1.8 release, which has static libraries only, instead of a shared libLLVM built locally. The release also has the WebAssembly target, which the local build lacked.
Why: no LLVM build of our own to maintain, wasm support on Linux, and no dynamic loading of libLLVM and libMLIR on every backend run.
Measurement: objects are byte-identical between the two backends (tuios -O 2: 823 of 823, hello binary identical). Cold builds, 3 runs, medians:
| shared | static | |
|---|---|---|
| hello dev, wall (s) | 0.604 | 0.297 |
| tuios -O 2, wall (s) | 334.6 | 315.1 |
| tuios -O 2, CPU (s) | 1578 | 1423 |
| tuios -release, wall (s) | 260.2 | 245.6 |
| tuios -release, CPU (s) | 1124 | 1040 |
Two bugs appeared only in the static build, and both are fixed. marmot-opt did not link (MLIROptLib). Park clear did nothing because of the order of static initializers. It now registers on first use.
Verdict: opt-in with LLVM_RELEASE=1 (download, checksum against the release's own attestation, tool subset copied into the install). The builds on the shared Linux machine use it.
macOS arm64 (checked on 17 November): the static libraries of the release are ThinLTO bitcode. So the backend links them with the libLTO of the release. The libLTO of the system linker is older and cannot read them. The build takes 97 s at 4 jobs and gives a 66 MB marmot-backend linked only to system libraries. The release clang has no default SDK, so a clang.cfg beside it names the one xcrun finds. Backend suite 74 of 75 (one test is x86-64 only). The installed tools take 540 MB. An installed marmot builds and runs hello at -O 2 and dev with no other LLVM on the machine's path.
Next: the default, after a week without problems.
pc tables for wasm (marmot-pcln)
What: -wasm-final reads the function table from the final module (code section and name section). -emit-source-map writes the DWARF lines of the linked module as a source map for wasm-opt, and -source-map reads the output map back.
Why: wasm-opt rewrites the code, so the DWARF of the linked module does not fit the final one, and tracebacks named the wrong functions.
Measurement: 3000 sampled functions of a 38684 function module match llvm-nm's offsets and names. A first version turned segments without a source into line 0. A small fmt program then had a line for 7 of 2434 functions. Skipped now: 2221 of 2434 (dev) and 295 of 541 (-release), where the old DWARF path had 1744 of 2442 and 249 of 451.
Verdict: landed. The driver side is on the front end page.
pc tables without functions at address 0
What: the linker gives functions it removed (--gc-sections) DWARF address 0. In a -no-pie binary, 0 minus the image base wrapped to the top of the table, which broke the sorted table: FuncForPC and runtime.Caller found nothing.
Measurement: run/std/runtime/func_names with MARMOT_NOPIE=1 had 1563 such functions and printed "unknown". After the change it prints the right names. A first landing was reverted because it also changes PIE tables. perfcmp then passed (geomean dev 0.994, -O 2 0.999, -release 0.998) and it landed again.
Verdict: landed.
Optimizer profile (analysis)
What: per-pass time from MARMOT_IRSTATS of full tuios builds.
Findings: at -O 2, code generation (1271 s summed) costs more than the optimizer (858 s). InstCombine is 17% of optimizer time and runs about 8 times per function. Most passes leave 95 to 99% of the functions unchanged at -O 2 (SLP vectorizer 99.3%, dead store elimination 99.6%, GVN 95.9%).
Next: a per-pass breakdown of code generation on the largest units, then gated trials of a shorter pipeline for small functions at -O 2.
Playground backend in one module (MARMOT_PLAY_TOOLS)
What: the browser build of marmot-backend also runs wasm-ld, so the page loads one copy of LLVM for both. The tier0 MLIR reader throws and catches to fall back to the MLIR parser. The browser build is now linked with exception catching, so three standard library modules (encoding/json, encoding/json/v2, text/template) no longer abort.
Verdict: landed.
Scalar boxing in -release (MARMOT_BOXSCALAR)
What: the go dialect lowering of a heap box allocated for every type. With MARMOT_BOXSCALAR=1 it does what gc and the front end's dev and -O 2 path do: 1-byte values point into runtime.staticuint64s, and 2, 4 and 8-byte integers and floats go through runtime.convT16, convT32 and convT64.
Why: the runtime and the front end found the allocation gap in sortslice and fmt: -release boxed every small integer into a new object.
Measurement: a -release loop that boxes int, int8, float64, uint16 and bool: 5.00 mallocs per iteration before, 1.99 after (go 1.99). fmt.Sprint(n, i%200): 3.00 before, 2.00 after per call (go 1.00, the rest is in the frontend).
Verdict: opt-in, landing waits for approval. Then its perfcmp gate.
Darwin link nondeterminism with GC scan records (analysis, 2026-11-18)
What: darwin -O 2 and -release builds of std-heavy programs (fmtbuf, fmtlog, fmtout, jsondec, jsonenc, mapstruct) sometimes differ between two builds of the same source. Parallel builds made it more frequent.
Finding: the objects and the link command are identical. One saved ld64 command (ld-prime, ld-27106.1), run 12 or 24 times, gives two outputs, also when the links run one after another. The variants differ by one 24-byte record in __DATA,__marmot_gcvars, and the __DATA sections come out in a different order, so every later address moves.
- MARMOT_GCVARS=0: all links identical.
- The same records in a section without the live_support attribute: 24 of 24 links are identical for jsonenc, jsondec and fmtbuf. The binary size does not change.
- A first input object with bytes in __data, __marmot_gcvars and __noptrdata does not fix it. The order is not "first seen".
- -ld_classic, -no_deduplicate and LTO thread options give the same variants.
Soundness: a linker map of two variants (fmtbuf -release) shows that every variable in the binary has its record in both. Every variable without a record was dead-stripped. What varies is one 432-byte private literal from one object (kept in one link, stripped in the other), and its record with it. No GC root is missed.
Reading: ld-prime's dead stripping of live_support sections (a record stays only while its variable is live) does not always give the same result. The section order follows from it. __marmot_tlinks has the same flags and is stable. So the trigger is specific to these records. They appear in few objects and point at variables that may be dead.
Linux (ELF, SHF_LINK_ORDER) is not affected.
Next: records without live_support behind an opt-in switch, and the 12-link loop on the 6 programs. Then macOS conformance, a size table, one macOS perfcmp, and the default.
Dropped or parked
- Stack maps for aggregate values: parked as a design. LLVM statepoints track only scalar pointers, and Go strings, slices and interfaces are aggregates. So only 70 of 730 functions of a tuios test build got maps. Park clear removes the stale roots that this would have fixed, so the work waits for traces that show stale roots park clear cannot reach.
- A local LLVM build with the WebAssembly target: dropped, because the official release has it and its objects are byte-identical.
- Gating on CPU split timing: not used. It failed the A against A test.
Known problems found on the way
- The driver numbers the part jobs of split modules in a different order from run to run. So two builds of tuios with the same inputs link in a different order and differ in layout. Every object is identical. The front end page has the fix (MARMOT_PART_ORDER).