[llvm] [SLP] Refine loop-aware gather cost and admit sibling-loop subtrees (PR #192801)
Ryan Buchner via llvm-commits
llvm-commits at lists.llvm.org
Fri May 1 09:39:15 PDT 2026
================
@@ -12618,21 +12648,61 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
S.getMainOp()->getParent()) {
BasicBlock *Parent = S.getMainOp()->getParent();
if (const Loop *L = LI->getLoopFor(Parent)) {
- // Check that the new loop nest is not involved.
- // Otherwise, mark it as a gather node.
+ // Check that the new loop nest shares the same outer structure as the
+ // tree's current loop nest. Completely disjoint nests (different
+ // outermost loops) are forced to gather because their scales cannot be
+ // meaningfully combined. Sibling inner loops inside a common outer
+ // loop are allowed: the cost model scales each entry by its own loop
+ // via getScaleToLoopIterations(), so a tree that spans sibling inner
+ // loops (e.g. a PHI at their merge block) can still be costed
+ // correctly. Contract CurrentLoopNest to the longest common prefix
+ // with the new entry's nest so subsequent entries in yet another
+ // sibling can also be admitted.
----------------
bababuck wrote:
For the case of:
```
for Y in Ys:
inst A
for Z in Zs:
inst B
```
First, when vectorizing `inst B`, `CurrentLoopNest` is empty, so we will assign:
```
if (CurrentLoopNest.empty()) {
CurrentLoopNest.assign(NewLoopNest);
```
so `CurrLoopNest = {Loop Z};`
Then, when we build the node for `inst A`, we will see that there is no common prefix (`CommonLen == 0`) so we will abort.
However, if we had the case of
```
for i : 1: (always runs once)
for Y in Ys:
inst A
for Z in Zs:
inst B
```
The first call we would instead have `CurrLoopNest = {Loop Z, Loop 1};` and on the second call we would instead get and `CommonLen == 1` since there is a common outer `Loop 1`, and we wouldn't exit early.
https://github.com/llvm/llvm-project/pull/192801
More information about the llvm-commits
mailing list