[llvm] [VPlan] Compute blend masks from minimum set of edge masks (PR #184838)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 10 02:28:31 PDT 2026


================
@@ -205,10 +210,81 @@ void VPPredicator::createSwitchEdgeMasks(const VPInstruction *SI) {
   setEdgeMask(Src, DefaultDst, DefaultMask);
 }
 
+DenseMap<const VPBasicBlock *, VPValue *>
+VPPredicator::computeBlendMasks(VPBasicBlock *VPBB) {
+  // First compute the set of ancestors which are reachable from multiple
+  // incoming blocks. This is where we can no longer determine the unique
+  // incoming edge.
+  SmallPtrSet<VPBlockBase *, 8> NonUnique;
+  DenseMap<VPBlockBase *, unsigned> Freq;
+  for (VPBlockBase *InVPBB : VPBB->predecessors()) {
+    for (VPBlockBase *VPBB : vp_inverse_depth_first_shallow(InVPBB)) {
+      Freq[VPBB]++;
+      if (Freq[VPBB] > 1)
+        NonUnique.insert(VPBB);
+    }
+  }
+
+  auto IsNonUnique = [&NonUnique](VPBlockBase *VPBB) {
+    return NonUnique.contains(VPBB);
+  };
+
+  // Then for each incoming block, compute the disjunction of the incoming edges
+  // to its "unique" subgraph.
+  DenseMap<const VPBasicBlock *, VPValue *> Masks;
+  for (VPBlockBase *InVPBBBase : VPBB->predecessors()) {
+    auto *InVPBB = cast<VPBasicBlock>(InVPBBBase);
+
+    // If the incoming block isn't unique, we need to use the incoming edge
+    // mask.
+    if (NonUnique.contains(InVPBB)) {
+      Masks[InVPBB] = getEdgeMask(InVPBB, VPBB);
+      continue;
+    }
+
+    // Traverse upwards and find the edges where the path is no longer unique to
+    // that incoming edge.
+    VPValue *Mask = nullptr;
+    SmallVector<VPBasicBlock *> Worklist = {InVPBB};
+    SmallPtrSet<VPBasicBlock *, 8> Visited;
+    while (!Worklist.empty()) {
+      VPBasicBlock *Unique = Worklist.pop_back_val();
+      if (!Visited.insert(Unique).second)
+        continue;
+
+      // If all predecessors aren't unique, just use the block mask.
----------------
fhahn wrote:

```suggestion
      // If no predecessor is unique, just use the block mask.
```

May also be worth IsNonUnique -> IsUnique

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


More information about the llvm-commits mailing list