[llvm] [NFCI][VPlan] Split initial mem-widening into a separate transformation (PR #182592)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 28 21:12:44 PDT 2026


================
@@ -6333,3 +6373,57 @@ void VPlanTransforms::createPartialReductions(VPlan &Plan,
     for (const VPPartialReductionChain &Chain : Chains)
       transformToPartialReduction(Chain, CostCtx.Types, Plan, Phi);
 }
+
+void VPlanTransforms::makeMemOpWideningDecisions(
+    VPlan &Plan, VFRange &Range, VPRecipeBuilder &RecipeBuilder) {
+  // Collect all loads/stores first. We will start with ones having simpler
+  // decisions followed by more complex ones that are potentially
+  // guided/dependent on the simpler ones.
+  SmallVector<VPInstruction *> MemOps;
+  for (VPBasicBlock *VPBB :
+       VPBlockUtils::blocksOnly<VPBasicBlock>(vp_depth_first_shallow(
+           Plan.getVectorLoopRegion()->getEntryBasicBlock()))) {
+    for (VPRecipeBase &R : *VPBB) {
+      auto *VPI = dyn_cast<VPInstruction>(&R);
+      if (VPI && VPI->getUnderlyingValue() &&
+          is_contained({Instruction::Load, Instruction::Store},
+                       VPI->getOpcode()))
+        MemOps.push_back(VPI);
+    }
+  }
+
+  auto *MiddleVPBB = Plan.getMiddleBlock();
+  VPBuilder FinalRedStoresBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
+
+  for (VPInstruction *VPI : MemOps) {
+    auto ReplaceWith = [&](VPRecipeBase *New) {
+      RecipeBuilder.setRecipe(cast<Instruction>(VPI->getUnderlyingValue()),
+                              New);
+      New->insertBefore(VPI);
+      if (VPI->getOpcode() == Instruction::Load)
+        VPI->replaceAllUsesWith(New->getVPSingleValue());
+      VPI->eraseFromParent();
+    };
----------------
lukel97 wrote:

We can avoid the lambda if we just refactor the below code to:

```c++
VPRecipeBase *New;
if (VPHistogramRecipe *Histogram = RecipeBuilder.widenIfHistogram(VPI))
  New = Histogram;
else if (VPRecipeBase *WidenMem = RecipeBuilder.tryToWidenMemory(VPI, Range))
  New = WidenMem;
else
  New = RecipeBuilder.handleReplication(VPI, Range);
RecipeBuilder.setRecipe(cast<Instruction>(VPI->getUnderlyingValue()),
                              New);
New->insertBefore(VPI);
if (VPI->getOpcode() == Instruction::Load)
  VPI->replaceAllUsesWith(New->getVPSingleValue());
VPI->eraseFromParent();
```

Since it looks like the lambda gets shuffled about in #182593 anyway, so it would be good to just do the refactoring bit in that PR

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


More information about the llvm-commits mailing list