[llvm] [SLP] De-duplicate costing of perfect-diamond gather spills (PR #220569)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 06:25:09 PDT 2026


================
@@ -19040,6 +19045,77 @@ InstructionCost BoUpSLP::getSpillCost() {
     }
     return nullptr;
   };
+  auto IsCoveredByMatchingVectorEntry = [&](const TreeEntry *Gather,
+                                            const Loop *SpillLoop) -> bool {
+    assert(Gather->isGather() && "Expected a Gather Entry!");
+
+    Value *LookupValue = nullptr;
+    if (Gather->hasState()) {
+      LookupValue = Gather->getMainOp();
+    } else {
+      auto *It = find_if_not(Gather->Scalars, IsaPred<UndefValue>);
+      if (It == Gather->Scalars.end())
+        return false;
+      LookupValue = *It;
+    }
+
+    // Find the real vector entry reused by this perfect-diamond gather.
+    const TreeEntry *SameTE =
+        getSameValuesTreeEntry(LookupValue, Gather->Scalars, /*SameVF=*/true);
+    if (!SameTE || SameTE == Gather || SameTE->State != TreeEntry::Vectorize ||
+        ScalarOrPseudoEntries.contains(SameTE) || !SameTE->UserTreeIndex)
+      return false;
+
+    // Only permit an ordinary vectorized user.
+    const TreeEntry *UserTE = SameTE->UserTreeIndex.UserTE;
+    assert(UserTE && "Expected a user tree entry.");
+    if (UserTE->State != TreeEntry::Vectorize ||
+        ScalarOrPseudoEntries.contains(UserTE) ||
+        UserTE->getOpcode() == Instruction::PHI)
+      return false;
+
+    // Different demotion state would make the two edge costs unequal.
+    if (MinBWs.contains(SameTE) != MinBWs.contains(Gather))
+      return false;
+
+    // The spill walk does not descend through gather entries; if any ancestor
+    // of the matching entry's user is a gather or a combined scalar/pseudo
+    // entry, the matching edge is never charged and de-duplicating would lose
+    // the spill cost entirely.
+    for (const TreeEntry *E = UserTE; E != Root;) {
+      if (!E->UserTreeIndex)
+        return false;
+      E = E->UserTreeIndex.UserTE;
+      if (E->isGather() || ScalarOrPseudoEntries.contains(SameTE))
+        return false;
+    }
+
+    Instruction *Def = EntriesToLastInstruction.lookup(SameTE);
+    Instruction *Use = EntriesToLastInstruction.lookup(UserTE);
+    if (!Def || !Use)
+      return false;
----------------
alexey-bataev wrote:

```suggestion
    Instruction *Def = EntriesToLastInstruction.at(SameTE);
    Instruction *Use = EntriesToLastInstruction.at(UserTE);
```

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


More information about the llvm-commits mailing list