Research: pipeline, link and build time
Area: the LLVM pipeline, linking, code size and build time. Data: perfcmp and MARMOT_IRSTATS runs on linux/amd64 (8 CPUs per build) from 15 to 17 November, and the LLVM build profile of 15 November. Also the front end's per-function SSA counts of a tuios -release build.
1. Gaps
| Gap | Measurement | SPEC 3c goal | Distance |
|---|---|---|---|
| Cold -release build time | tuios: go 28.6 s wall and 133 s CPU, marmot 223 s wall and 1086 s CPU (15 Nov), with tonight's static backend and split reach, 245 s wall and 1040 s CPU at a busier time | at most 0.8x of go | about 8x away |
| Parallel use of the build machine | tuios -release: 1040 s CPU in 245.6 s wall on 8 CPUs, so 4.2 busy cores on average (53%), -O 2: 1423 s CPU in 315 s, 4.5 cores (56%) | (part of build time) | up to 1.9x of wall time is idle cores |
| Binary size | tuios dev 92.9 MB against go 70.1 MB (1.33x), -release text 21.2 MB | at most 0.8x | 1.66x short in dev |
| Peak memory from stale stack roots | bench jsondec peak RSS 2.16x go (68 against 31 MB). After a forced GC go keeps 713 objects, marmot 272,851: main's frame keeps dead locals and the previous result reachable for the conservative scan (the runtime's measurement). With the runtime register fix and a noinline split: 148,849 objects, 42 MB | at most 0.7x | 3x short on this row |
Run time is in good shape for my area (perfcmp geomeans within noise of 1.0 between backend changes). The run time gaps are allocation bound library code, which is the front end's and the runtime's.
2. Evidence
Build time and parallelism
- ThinLTO (Johnson, Amini, Li, CGO 2017, https://research.google/pubs/thinlto-scalable-and-incremental-lto/, https://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html): a serial "thin link" over per-module summaries decides imports, and each module is then optimized in parallel. Clean builds scale like a non-LTO build while getting most of full LTO's cross-module optimization. marmot's -release already works this way (summaries, imports, parallel units).
- Rust codegen units (https://blog.rust-lang.org/2023/11/09/parallel-rustc/, https://nnethercote.github.io/2023/07/25/how-to-speed-up-the-rust-compiler-data-analysis-assistance-requested.html): splitting a crate into codegen units gives a good speedup up to the core count, not a factor of N. The limit is the estimate of each unit's LLVM cost and the serial parts. A reported full build went from 61 to 28.5 minutes with 6 jobs and 10 units. The parallel front end cut compile times by up to 50% on real code.
- Scheduling theory: longest processing time first (Graham, 1969) is within 4/3 of the optimal makespan for independent jobs. The order matters most when a few jobs are large and arrive late.
- Go gc: compiles packages in parallel as soon as the export data of their dependencies exists. The compile of one package is itself concurrent (SSA backend per function in parallel). This is the main reason for its 4.6x CPU to wall ratio on tuios (133 s CPU in 28.6 s).
Size
- Identical code folding: lld --icf=all made a Chromium binary 5.7% smaller. Switching Chrome from all to safe ICF cost about 5 MB of 169 MB text (https://gn.googlesource.com/gn/+/d7111cb6877187d1f378bd231e14ffdd5fdd87ae%5E%21, https://lists.llvm.org/pipermail/llvm-dev/2021-March/149432.html). Lld folds 1.5 GB of output in under 2 s. Go does not let a program compare func values (only to nil), so folding Go functions does not break the language. It does merge names in tracebacks and FuncForPC.
- MachineOutliner (https://discourse.llvm.org/t/rfc-turn-the-machineoutliner-on-by-default-in-aarch64-under-oz/48369, https://llvm.org/devmtg/2020-02-23/slides/Kyungwoo-GlobalMachineOutlinerForThinLTO.pdf): arm64 -Oz text 4.4% smaller on CTMark (0.3% to 15.4%), up to 11% with full LTO and 8% with ThinLTO. It costs run time on hot code, so it fits -Oz and cold code.
- Function merging: F3M (Stirling et al., CGO 2022, doi 10.1109/CGO53902.2022.9741269) improves the size reduction of earlier merging by 6 percentage points and makes the pass 1.8x cheaper. HyFM (Rocha et al., LCTES 2021) is 4.5x faster than SalSSA with far less memory.
- MLGO inlining for size (Trofin et al., arXiv 2101.04808, Google blog 2022): 3% to 7% smaller than LLVM -Oz. The trained model ships in LLVM.
3. Candidates for marmot
| Candidate | Gap | Expected gain | Cost | Risk |
|---|---|---|---|---|
| A. Build schedule: measure the -release timeline (frontend, each unit's start and end), then largest units first, cut the critical path (finer split of the last large units, start LLVM units while the frontend still emits) | build wall time | from 53% toward 80% use of 8 cores: up to 1.5x less wall time, CPU unchanged | 2 to 4 days | low: output unchanged if units are the same, scheduling only |
| B. Less work per unit: module passes (GlobalOpt, GlobalDCE, IPSCCP, the CGSCC walk) cost about 20% of optimizer time and scale with the whole module, including the runtime bitcode and declarations each unit carries | build CPU | 5 to 10% of optimizer CPU | 2 days | medium: pass changes change output, needs perfcmp |
| C. Dedup of optimizer input (split reach, home instances): measured -6% (O2) and -19% (release) input with split reach, -7% more with home instances | build CPU | 5% to 14% optimizer time, measured | done, gates running | low |
| D. Identical code folding at link for -release (Go functions only, safe mode first) | size | 2% to 6% text by the Chromium numbers, more for generic instances that differ only in type descriptors they do not use | 1 to 2 days | medium: traceback names of folded functions, needs an address significance table |
| E. MachineOutliner on cold code (panic paths, init) at -release, arm64 first | size | 2% to 4% text | 1 day to try | run time if it touches hot code |
| F. strconcat's hot loop (total += int(t[i]) & 3) is vectorized with SSE2 at 2 bytes per iteration, zero-extended to 64-bit lanes | run time | narrower lanes (sum in 32 or 16 bits with a widening reduce at the end) or AVX2 for -release when the target allows: 2x to 8x more bytes per iteration in the loop | 1 to 2 days (loop vectorizer cost model, or a reduction width pass before it) | low with a gate, AVX2 needs a target feature decision |
| G. Precise stack maps for pointer locals (statepoints, the parked aggregate design) or, cheaper, zeroing dead pointer spill slots before allocation slow paths as park clear does before park calls | memory | jsondec peak RSS 68 MB toward about 36 MB (the runtime's estimate) | statepoints: 1 to 2 weeks with the frontend ABI part, slot zeroing: 2 days | statepoints change every object, MARMOT_GCPTR=1 fails to build -release today (ptr against ptr addrspace(1) at go.nil_check) |
4. Pick
- A, the build schedule. It is the largest lever on the 3c build time gap that does not change output: the machine is half idle during a -release build. The first step is a measurement (a per-unit timeline). The cause of the idle cores decides the fix: a serial front end phase, a long last unit, or an admission limit.
- D, identical code folding, for size, after A.
C is already measured and in its perfcmp gates.