[PATCH] D157412: [VPlan] Pick optimal seed value for VPBlend to enable mask elimination.

Paul Walker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 8 09:41:19 PDT 2023


paulwalker-arm created this revision.
Herald added subscribers: tschuett, psnobl, rogfer01, bollu, hiraditya.
Herald added a project: All.
paulwalker-arm requested review of this revision.
Herald added subscribers: llvm-commits, wangpc, vkmr.
Herald added a project: LLVM.

The seed value for a VPBlend is not masked. By picking an incoming
value whose mask is only used by the VPBlend as the seed value, we
are able to dead code the value's mask.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157412

Files:
  llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp


Index: llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -887,6 +887,19 @@
   // optimizations will clean it up.
 
   unsigned NumIncoming = getNumIncomingValues();
+  unsigned StartInValIdx = 0;
+
+  // We might have single edge PHIs (blocks)
+  if (NumIncoming > 1) {
+    // Find an incoming value whose mask is only used by the blend. By seeding
+    // the blend with this value it's mask is not used and can be deadcoded.
+    for (unsigned In = 0; In < NumIncoming; ++In) {
+      if (getMask(In)->getNumUsers() == 1) {
+        StartInValIdx = In;
+        break;
+      }
+    }
+  }
 
   // Generate a sequence of selects of the form:
   // SELECT(Mask3, In3,
@@ -895,21 +908,24 @@
   //                      In0)))
   // Note that Mask0 is never used: lanes for which no path reaches this phi and
   // are essentially undef are taken from In0.
- VectorParts Entry(State.UF);
+  VectorParts Entry(State.UF);
+
+  // Initialize with the incoming value whose mask can be esily deadcoded.
+  for (unsigned Part = 0; Part < State.UF; ++Part) {
+    Entry[Part] = State.get(getIncomingValue(StartInValIdx), Part);
+  }
+
   for (unsigned In = 0; In < NumIncoming; ++In) {
+    if (In == StartInValIdx)
+      continue;
+
     for (unsigned Part = 0; Part < State.UF; ++Part) {
-      // We might have single edge PHIs (blocks) - use an identity
-      // 'select' for the first PHI operand.
       Value *In0 = State.get(getIncomingValue(In), Part);
-      if (In == 0)
-        Entry[Part] = In0; // Initialize with the first incoming value.
-      else {
-        // Select between the current value and the previous incoming edge
-        // based on the incoming mask.
-        Value *Cond = State.get(getMask(In), Part);
-        Entry[Part] =
-            State.Builder.CreateSelect(Cond, In0, Entry[Part], "predphi");
-      }
+      // Select between the current value and the previous incoming edge
+      // based on the incoming mask.
+      Value *Cond = State.get(getMask(In), Part);
+      Entry[Part] =
+          State.Builder.CreateSelect(Cond, In0, Entry[Part], "predphi");
     }
   }
   for (unsigned Part = 0; Part < State.UF; ++Part)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157412.548260.patch
Type: text/x-patch
Size: 2332 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230808/9882631f/attachment.bin>


More information about the llvm-commits mailing list