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

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 13 09:59:59 PDT 2026


================
@@ -205,10 +214,78 @@ 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.
+  DenseMap<VPBlockBase *, SmallPtrSet<VPBlockBase *, 8>> NonUnique;
+  for (VPBlockBase *InVPBB : VPBB->predecessors()) {
+    NonUnique[InVPBB].insert(VPBB);
+    for (VPBlockBase *Ancestor : vp_inverse_depth_first_shallow(InVPBB))
+      for (VPBlockBase *OtherInVPBB : VPBB->predecessors())
+        if (OtherInVPBB != InVPBB)
+          NonUnique[OtherInVPBB].insert(Ancestor);
+  }
+
+  // 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[InVPBB].contains(InVPBB)) {
+      Masks[InVPBB] = getEdgeMask(InVPBB, VPBB);
+      continue;
+    }
+
+    // Traverse the post dominator frontier and find the edges where the path is
+    // no longer unique to that incoming edge.
+    SmallVector<VPBlockBase *> Worklist = {InVPBB};
+    MapVector<VPBlockBase *, SmallSetVector<VPBlockBase *, 8>> Edges;
+    while (!Worklist.empty()) {
+      auto *X = cast<VPBasicBlock>(Worklist.pop_back_val());
+      if (!NonUnique[InVPBB].contains(X)) {
+        append_range(Worklist, VPPDF.find(X)->second);
+        continue;
+      }
+      // Find edges from non-unique to unique blocks and add them to the mask.
----------------
lukel97 wrote:

Added an ascii diagram in 597dc53feaa2 to hopefully explain better why we skip blocks in the PDF 

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


More information about the llvm-commits mailing list