Amanieu opened PR #15161 from Amanieu:group_loops to bytecodealliance:main:
This significantly helps register allocators that only split live ranges linearly since it avoids live range splits in the middle of a loop when this is not necessary.
Sightglass benchmarks on my system are quite noisy, but there is a geomean 2-3% speedup in compilation time due to reduced time spent in regalloc. Execution time speedup is around geomean 1%, with some benchmarks getting over 10% speedup. The actual time spent in
group_loopsis tiny, only 0.1% of total compilation time (measured using a sampling profiler).
Amanieu requested wasmtime-compiler-s390x-reviewers for a review on PR #15161.
Amanieu removed the review request for wasmtime-compiler-reviewers from PR #15161.
Amanieu requested cfallin for a review on PR #15161.
Amanieu requested wasmtime-compiler-reviewers for a review on PR #15161.
fitzgen commented on PR #15161:
Thanks!
Sightglass benchmarks on my system are quite noisy, but there is a geomean 2-3% speedup in compilation time due to reduced time spent in regalloc. Execution time speedup is around geomean 1%, with some benchmarks getting over 10% speedup. The actual time spent in
group_loopsis tiny, only 0.1% of total compilation time (measured using a sampling profiler).Clarification: is this when running
default.suite?
Amanieu commented on PR #15161:
Yes, this was with default.suite. I can re-run some specific benchmarks if you want particular numbers.
:memo: fitzgen submitted PR review:
LGTM modulo a few nitpicks below -- thanks!
:speech_balloon: fitzgen created PR review comment:
This should already be available if we changed the above
let r = loop_index(loops.innermost_loop(block));to
let lp = loops.innermost_loop(block); let r = loop_index(lp);which maybe LLVM could figure out on its own, but additionally lets readers not have to double check whether they should be concerned about subtraction overflow.
:speech_balloon: fitzgen created PR review comment:
To check my understanding: this is not a
SecondaryMapbecause we want to effectively key onOption<Loop>rather than theLoopentity itself?
:speech_balloon: fitzgen created PR review comment:
Shouldn't this be provided already by
Context::clear? This seems like something we should be able to assert, because the only way this wouldn't be the case was if someone reused a context without clearing it first, which is an API contract violation.
:speech_balloon: fitzgen created PR review comment:
Would it be possible to make this something like
let mut stack: SmallVec<[_; 8]> = smallvec![(0usize, loop_blocks[0].iter())];or even just
let mut stack: SmallVec<[_; 8]> = smallvec![loop_blocks[0].iter()];?
I think that would make this a little easier on code readers (and also same nitpick about the
rvariable name, but I think the iterator version makes it go away completely).
:speech_balloon: fitzgen created PR review comment:
nitpick: can we name this
loop_idxor something instead ofr?
fitzgen commented on PR #15161:
Yes, this was with default.suite. I can re-run some specific benchmarks if you want particular numbers.
Nope,
default.suiteis fine! Was just curious
fitzgen commented on PR #15161:
Ah, well, also double checking: was this a recent-ish checkout of Sightglass? Like within the last ~month? Specifically, is
default.suitea symlink topca-20-min.suitein your checkout? If so then all good. If not, then pulling and rebenching would be appreciated.
:memo: cfallin submitted PR review:
Some thoughts below -- thanks for this!
Also a high-level thought/question: did you consider approaches that worked in a more "inline" way, by altering the choice order of the initial DFS that computes the block lowering order? I ask because it seems to me that at least in principle, visiting edges to successors that have the same inner loop first-in-RPO (so last-in-DFS-computing-PO I think?) should give a similar effect, and would be cheaper still. To be clear I'm not overly concerned about your measured average overhead, but I am concerned about (i) pathological worst-case behavior when we are allocating based on CFG shape, and (ii) complexity of the code, where, all else equal, it'd be great to avoid a pass with a subtle design. If not that, at least it'd be good to understand the worst-case memory behavior here: it should be linear because blocks only accumulate into the list for their innermost loop, not all containing loops, right? And the stack has depth at most O(blocks)?
:speech_balloon: cfallin created PR review comment:
Let's add a clarification here (at least this addresses a concern I had initially): while CLIF may have irreducible loops (in the sense of cycles in the CFG), the loop analysis does not classify these as loops, because it only finds natural loops (those with a single entry); and by definition, the DFS will find the header first, so this statement is true.
:speech_balloon: cfallin created PR review comment:
regalloc2 doesn't have any requirements on block order; perhaps you're thinking of the lowering algorithm, which does require seeing uses before defs?
:speech_balloon: cfallin created PR review comment:
regalloc2 doesn't have any requirements on block order; perhaps you're thinking of the lowering algorithm, which does require seeing uses before defs?
:memo: Amanieu submitted PR review.
:speech_balloon: Amanieu created PR review comment:
Yes. We could also do this with a
SecondaryMapand a separateVecfor the non-loop blocks, but I felt a singleVecwas cleaner.
:memo: Amanieu submitted PR review.
:speech_balloon: Amanieu created PR review comment:
I wasn't sure about the exact API contract. If we can safely assume that the loop analysis results are never stale then it should be safe to remove this.
Amanieu commented on PR #15161:
Regarding the DFS idea: this is actually something that I used to do in my compiler. However it turns out that this doesn't work in all cases.
Consider the test case in blockorder.rs: the only DFS choice here is whether you visit
2or3first. This means that the only 2 RPOs that it can produce are:
- 0, 1, 3, 5, 2, 4, 6
- 0, 1, 2, 4, 3, 5, 6
Regarding complexity, this pass is linear and uses O(blocks) time and memory. The stack size is the maximum loop depth, and the total set of
Items are one for each block and one for each loop.
cfallin commented on PR #15161:
Regarding the DFS idea: this is actually something that I used to do in my compiler. However it turns out that this doesn't work in all cases.
Consider the test case in blockorder.rs: the only DFS choice here is whether you visit
2or3first. This means that the only 2 RPOs that it can produce are:* 0, 1, 3, 5, 2, 4, 6 * 0, 1, 2, 4, 3, 5, 6(assuming you're referring to this CFG)
Ah, I see, yeah -- the fundamental issue is that we would need to interleave the DFS subtrees because each contains "in-loop" and "out-of-loop" blocks. I guess one could somehow "defer" blocks but at that point the complexity tradeoff becomes a lot less clear. Separate from-the-top sort pass as you've done seems fine, then. Thanks for clarifying!
:thumbs_up: cfallin submitted PR review:
LGTM from me, once my nits above and Nick's comments are addressed too.
github-actions[bot] added the label cranelift:area:machinst on PR #15161.
github-actions[bot] added the label cranelift on PR #15161.
github-actions[bot] added the label cranelift:area:aarch64 on PR #15161.
github-actions[bot] added the label cranelift:area:x64 on PR #15161.
Amanieu updated PR #15161.
:thumbs_up: fitzgen submitted PR review:
Thanks!
fitzgen added PR #15161 cranelift: Group blocks belonging to the same loop together during lowering to the merge queue.
:check: fitzgen merged PR #15161.
fitzgen removed PR #15161 cranelift: Group blocks belonging to the same loop together during lowering from the merge queue.
fitzgen commented on PR #15161:
Some nice compilation speedups from (I'm assuming) this PR: https://github.com/bytecodealliance/are-we-fast-yet/actions/runs/42120691141
(No execution difference though)
Last updated: Nov 19 2026 at 20:05 UTC