| Issue |
243646
|
| Summary |
[X86][RegAlloc] Removing never-taken bounds-check branches from a hot loop makes it ~1.4x slower (greedy reloads loop-invariant values inside the loop)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
handyhandymanadam
|
### Summary
One function, compiled with and without its bounds checks. Without the checks (10 compare-and-branch pairs into cold `panic_bounds_check` blocks that are never taken), its hot inner loop runs about 1.4x slower on Zen 4. The visible difference is in register allocation: without the checks, greedy reloads loop-invariant values from the stack inside the loop; with them it does not. It reproduces with llc 22.1.0 and 23.1.0. Trunk allocates somewhat differently, but its code measures just as slow.
### Reproducer
https://godbolt.org/z/1z49oojxf
Two IR files of the same function, a UTF-8 encoder loop from an iconv written in Rust (rustc 1.100 `-O3` output, the function extracted with `llvm-extract`):
- `check.ll` (first editor): as compiled, with the 10 bounds checks.
- `nocheck.ll` (second editor): the same source with those checks removed before LLVM runs, because they were proven never to fail. Both files went through the same `-O3` middle end.
```
llc -O3 -relocation-model=static -pass-remarks-missed=regalloc
```
Register-allocation remarks for the innermost loop (blocks `bb6.i.i` to `bb38.i.i`):
| llc | with checks | without checks |
|---|---|---|
| 23.1.5 (rustc 1.100's) | 2 reloads, cost 20.2 | 7 reloads, cost 91.4 |
| trunk (24.0.0git, 2026-11-17) | 2 reloads, cost 20.2 | 4 reloads, cost 50.7 |
llc 22.1.0 and 23.1.0 on Compiler Explorer give the same 7 reloads / 91.4 for `nocheck.ll`.
What the code does without the checks (23.1.x): the input's base pointer, which the with-checks build recomputes with a `lea` of two live registers, is stored before the loop and reloaded in the 2-, 3- and 4-byte paths, and the loop bound is reloaded on the way into the shared latch. Meanwhile several registers hold values that have no use inside the loop. Trunk no longer reloads the loop bound at the latch; it still reloads the base pointer in the 2-, 3- and 4-byte paths.
### Measurements
AMD Ryzen 7 7800X3D, Linux, one pinned core, ASLR off. The benchmark converts ISO-8859-1 text made of bytes >= 0x80 to UTF-8, so every character takes the loop's 2-byte path. Times are microseconds for the same input, the benchmark's best of 7.
| build of this function | time |
|---|---|
| with checks, LLVM 23.1.5 | 5,197 to 5,340 us |
| without checks, LLVM 23.1.5 | 7,602 us (7,223 to 8,385 over six sessions) |
| without checks, trunk's code for this function spliced into the same binary | 7,681 us |
| without checks, LLVM 23.1.5 with `-tail-dup-size=4` | 5,589 us |
- Across 16 different code layouts of the binary (link order and padding varied), the build without checks is slower at every one; the median ratio is 1.41. So it is not an alignment accident.
- Counters for the loop: instructions about +5 %, cycles about +45 % (IPC 5.2 down to 3.8), branch misses equal.
### Observations that may help
- Machine block frequencies are the same in both builds (`-print-machine-bfi`); the check branches only add cold edges.
- On the `nocheck.ll` input with 23.1.5, `-regalloc-enable-priority-advisor=dummy` gives a loop with no stack loads at all (not timed), so the allocator can find a better assignment for the same input.
- `-tail-dup-size=4` copies the latch into each path before allocation; the loop then has no stack loads and the time comes back (table above). It is not a general fix: it changes many other functions, and one other benchmark case measured slower with it (one run).
- Reading the MIR before and after greedy (not a debug log, so take it as a reading): in the build without checks the two operands of the base pointer's `lea` are split inside the loop, and after that the `lea` is not rematerialized but spilled and reloaded.
- The reload count alone is not the whole story: patching only the 2-byte path's reload in the 23.1.5 binary did not bring the time back. The variants that are fast have no stack load in the 1- and 2-byte paths and also have the latch duplicated.
### Caveats
- The trunk number comes from trunk `llc`'s assembly for this one function (from Compiler Explorer), assembled with 23.1.5's `llvm-mc` and spliced into the 23.1.5-built binary; the same splice of 23.1.5's own assembly reproduces the original binary (every section identical apart from a file-name string). It is not a build with a full trunk toolchain.
- Timings on trunk are one run at one layout; the 23.1.5 comparison is the one measured at 16 layouts.
This was found and analyzed with AI assistance (Claude); the measurements were run on the hardware described. Happy to provide the IR files directly or more data.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs