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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 01:50:44 PDT 2026


================
@@ -6405,3 +6405,53 @@ void VPlanTransforms::makeMemOpWideningDecisions(
     ReplaceWith(Recipe);
   }
 }
+
+void VPlanTransforms::makeScalarizationDecisions(VPlan &Plan, VFRange &Range) {
+  if (LoopVectorizationPlanner::getDecisionAndClampRange(
+          [&](ElementCount VF) { return VF.isScalar(); }, Range))
+    return;
+
+  PostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> POT(
+      Plan.getEntry());
+  for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(POT)) {
+    for (VPRecipeBase &R : make_early_inc_range(reverse(*VPBB))) {
+      auto *VPI = dyn_cast<VPInstruction>(&R);
+      if (!VPI)
+        continue;
+
+      // For now we (effectively) only scalarize to first-lane-only address
+      // computation chain for the memory operations such that
+      // MemOp->usesFirstLaneOnly(MemOpAddressOperand).
+
+      auto *I = cast_or_null<Instruction>(VPI->getUnderlyingValue());
+      if (!I)
+        // Wouldn't be able to create a `VPReplicateRecipe` anyway.
+        continue;
+
+      // If "executing" other lanes produces side-effects we can't avoid them.
+      if (VPI->mayHaveSideEffects())
+        continue;
+
+      // We want to drop the mask operand, verify we can safely do that.
+      if (VPI->isMasked() && !VPI->isSafeToSpeculativelyExecute())
+        continue;
+
+      // Avoid rewriting IV increment as that interferes with
+      // `removeRedundantCanonicalIVs`.
+      if (VPI->getOpcode() == Instruction::Add &&
+          any_of(VPI->operands(), IsaPred<VPWidenIntOrFpInductionRecipe>))
+        continue;
+
+      // Other lanes are needed - can't drop them.
+      if (!vputils::onlyFirstLaneUsed(VPI))
+        continue;
+
+      auto *Recipe =
+          new VPReplicateRecipe(I, VPI->operandsWithoutMask(), true, nullptr,
----------------
fhahn wrote:

```suggestion
          new VPReplicateRecipe(I, VPI->operandsWithoutMask(), /*IsSingleScalar=*/true, nullptr,
```

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


More information about the llvm-commits mailing list