[llvm] [SLP] Refine loop-aware gather cost and admit sibling-loop subtrees (PR #192801)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Fri May 1 09:28:31 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.
----------------
alexey-bataev wrote:

`!CurrentLoopNest.empty()` checks that there is at least one outer loop here. If it is empty (no outer loops), the condition is false and no early exit.

https://github.com/llvm/llvm-project/pull/192801


More information about the llvm-commits mailing list