[llvm] [SandboxVectorizer] Extract vectorizeStores from runOnRegion (PR #215860)

Anshil Gandhi via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 12 11:15:27 PDT 2026


https://github.com/gandhi56 created https://github.com/llvm/llvm-project/pull/215860

Move runOnRegion()'s body -- the store-chain legality checks, operand classification, vector value construction, and profitability decision -- into a new vectorizeStores(Bndl, Rgn, Sched, A) method. runOnRegion() now only builds the initial bundle from the region's Aux and calls vectorizeStores() once. Also extract the cost-check tail into acceptIfProfitable(), a small helper worth having on its own. NFC.

>From 27a40cfecfabeae34473df4eee9a4906f2648f1d Mon Sep 17 00:00:00 2001
From: Anshil Gandhi <gandhi21299 at gmail.com>
Date: Wed, 12 Aug 2026 02:29:50 -0400
Subject: [PATCH] [SandboxVectorizer] Extract LoadStoreVec::vectorizeStores
 from runOnRegion

Move runOnRegion()'s body -- the store-chain legality checks, operand
classification, vector value construction, and profitability decision
-- into a new vectorizeStores(Bndl, Rgn, Sched, A) method. runOnRegion()
now only builds the initial bundle from the region's Aux and calls
vectorizeStores() once. Also extract the cost-check tail into
acceptIfProfitable(), a small helper worth having on its own. NFC.
---
 .../SandboxVectorizer/Passes/LoadStoreVec.h   |  5 ++
 .../SandboxVectorizer/Passes/LoadStoreVec.cpp | 48 ++++++++++++-------
 2 files changed, 35 insertions(+), 18 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.h
index 7d771bf49f897..8dcea27f0cc3c 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.h
@@ -38,6 +38,11 @@ class LLVM_ABI LoadStoreVec final : public RegionPass {
   void tryEraseDeadInstrs(ArrayRef<Instruction *> Stores,
                           ArrayRef<Value *> Operands);
 
+  /// Tries to vectorize the store chain \p Bndl into a single vector store.
+  /// \Returns whether it succeeded.
+  bool vectorizeStores(ArrayRef<Instruction *> Bndl, Region &Rgn,
+                       Scheduler &Sched, const Analyses &A);
+
 public:
   LoadStoreVec(StringRef AuxArg) : RegionPass("load-store-vec") {
     assert(AuxArg.empty() && "This pass ignores aux arg!");
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp
index adf9ee32a3533..73c0fcdd1bbb8 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp
@@ -64,14 +64,29 @@ void LoadStoreVec::tryEraseDeadInstrs(ArrayRef<Instruction *> Stores,
       PtrI->eraseFromParent();
 }
 
-bool LoadStoreVec::runOnRegion(Region &Rgn, const Analyses &A) {
-  SmallVector<Instruction *, 8> Bndl(Rgn.getAux().begin(), Rgn.getAux().end());
-  if (Bndl.size() < 2)
+/// Accepts the transaction if vectorizing was profitable, reverts it otherwise.
+/// \Returns true if the transaction was accepted.
+static bool acceptIfProfitable(Context &Ctx, const ScoreBoard &SB,
+                               InstructionCost CostBefore) {
+  InstructionCost CostAfter = SB.getAfterCost() - SB.getBeforeCost();
+  InstructionCost CostGain = CostAfter - CostBefore;
+  LLVM_DEBUG(dbgs() << DEBUG_PREFIX_LOCAL << "CostGain=" << CostGain
+                    << " (After=" << CostAfter << " Before=" << CostBefore
+                    << ")\n");
+  if (CostGain > CostThreshold) {
+    LLVM_DEBUG(dbgs() << DEBUG_PREFIX_LOCAL << "Not profitable, reverting.\n");
+    Ctx.revert();
     return false;
+  }
+  LLVM_DEBUG(dbgs() << DEBUG_PREFIX_LOCAL << "Profitable accepting.\n");
+  Ctx.accept();
+  return true;
+}
+
+bool LoadStoreVec::vectorizeStores(ArrayRef<Instruction *> Bndl, Region &Rgn,
+                                   Scheduler &Sched, const Analyses &A) {
   Function &F = *Bndl[0]->getParent()->getParent();
-  DL = &F.getParent()->getDataLayout();
   auto &Ctx = F.getContext();
-  Scheduler Sched(A.getAA(), Ctx, SchedDirection::BottomUp);
   if (!VecUtils::areConsecutive<StoreInst, Instruction>(
           Bndl, A.getScalarEvolution(), *DL))
     return false;
@@ -184,20 +199,17 @@ bool LoadStoreVec::runOnRegion(Region &Rgn, const Analyses &A) {
 
   tryEraseDeadInstrs(Bndl, Operands);
 
-  // Check the cost.
-  InstructionCost CostAfter = SB.getAfterCost() - SB.getBeforeCost();
-  InstructionCost CostGain = CostAfter - CostBefore;
-  LLVM_DEBUG(dbgs() << DEBUG_PREFIX_LOCAL << "CostGain=" << CostGain
-                    << " (After=" << CostAfter << " Before=" << CostBefore
-                    << ")\n");
-  if (CostGain > CostThreshold) {
-    LLVM_DEBUG(dbgs() << DEBUG_PREFIX_LOCAL << "Not profitable, reverting.\n");
-    Ctx.revert();
+  return acceptIfProfitable(Ctx, SB, CostBefore);
+}
+
+bool LoadStoreVec::runOnRegion(Region &Rgn, const Analyses &A) {
+  SmallVector<Instruction *, 8> Bndl(Rgn.getAux().begin(), Rgn.getAux().end());
+  if (Bndl.size() < 2)
     return false;
-  }
-  LLVM_DEBUG(dbgs() << DEBUG_PREFIX_LOCAL << "Profitable accepting.\n");
-  Ctx.accept();
-  return true;
+  Function &F = *Bndl[0]->getParent()->getParent();
+  DL = &F.getParent()->getDataLayout();
+  Scheduler Sched(A.getAA(), F.getContext(), SchedDirection::BottomUp);
+  return vectorizeStores(Bndl, Rgn, Sched, A);
 }
 
 } // namespace sandboxir



More information about the llvm-commits mailing list