[llvm] [VPlan] Scalarize to first-lane-only directly on VPlan (PR #184267)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 19 02:18:57 PDT 2026


================
@@ -6541,3 +6541,60 @@ void VPlanTransforms::makeMemOpWideningDecisions(
     ReplaceWith(Recipe);
   }
 }
+
+void VPlanTransforms::makeScalarizationDecisions(VPlan &Plan, VFRange &Range) {
+  if (LoopVectorizationPlanner::getDecisionAndClampRange(
+          [&](ElementCount VF) { return VF.isScalar(); }, Range))
+    return;
+
+  // Extend lifetime per `llvm::PostOrderTraversal` documentation:
+  auto PO =
+      vp_post_order_shallow(Plan.getVectorLoopRegion()->getEntryBasicBlock());
+
+  for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(PO)) {
+    for (VPRecipeBase &R : make_early_inc_range(reverse(*VPBB))) {
+      auto *VPI = dyn_cast<VPInstruction>(&R);
+      if (!VPI)
+        continue;
+
+      auto *I = cast_or_null<Instruction>(VPI->getUnderlyingValue());
+      if (!I)
+        // Wouldn't be able to create a `VPReplicateRecipe` anyway.
+        continue;
+
+      bool CanTransformToFirstLaneOnly = [&]() {
+        if (VPI->mayHaveSideEffects())
+          return false;
+
+        // We want to drop the mask operand, doing that for integer division
+        // isn't safe.
+        if (is_contained({Instruction::SDiv, Instruction::UDiv,
+                          Instruction::SRem, Instruction::URem},
+                         VPI->getOpcode()) &&
+            VPI->getMask())
+          return false;
+
+        // Avoid rewriting IV increment as that interferes with
+        // `removeRedundantCanonicalIVs`.
+        if (VPI->getOpcode() == Instruction::Add &&
+            any_of(VPI->operands(), IsaPred<VPWidenInductionRecipe>))
+          return false;
----------------
artagnon wrote:

Sorry, I got confused: the `IsaPred<VPWidenInductionRecipe>` should be a VPWidenIntOrFpInduction that's canonical?

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


More information about the llvm-commits mailing list