[llvm] 94545a7 - [SandboxVec][Legality][NFC] Outline differentBlock() and areUnique() (#190024)

via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 3 12:15:00 PDT 2026


Author: vporpo
Date: 2026-04-03T12:14:55-07:00
New Revision: 94545a7c638d080bfc7dcdcc8c73ad4b46bda08e

URL: https://github.com/llvm/llvm-project/commit/94545a7c638d080bfc7dcdcc8c73ad4b46bda08e
DIFF: https://github.com/llvm/llvm-project/commit/94545a7c638d080bfc7dcdcc8c73ad4b46bda08e.diff

LOG: [SandboxVec][Legality][NFC] Outline differentBlock() and areUnique() (#190024)

And reuse them in LoadStoreVec.

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h
    llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp
    llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h
index 3d76cdaad6240..b1fa6ed7742e8 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h
@@ -344,6 +344,22 @@ class LegalityAnalysis {
         std::unique_ptr<ResultT>(new ResultT(std::move(Args)...)));
     return cast<ResultT>(*ResultPool.back());
   }
+
+  /// \returns true if \p Instrs are in 
diff erent blocks.
+  template <typename ValueT>
+  static bool 
diff erentBlock(ArrayRef<ValueT *> Instrs) {
+    auto *BB0 = cast<Instruction>(Instrs[0])->getParent();
+    return any_of(drop_begin(Instrs), [BB0](auto *V) {
+      return cast<Instruction>(V)->getParent() != BB0;
+    });
+  }
+
+  /// \returns true if all values in \p Values are unique.
+  template <typename ValueT> static bool areUnique(ArrayRef<ValueT *> Values) {
+    SmallPtrSet<Value *, 8> Unique(llvm::from_range, Values);
+    return Unique.size() == Values.size();
+  }
+
   /// Checks if it's legal to vectorize the instructions in \p Bndl.
   /// \Returns a LegalityResult object owned by LegalityAnalysis.
   /// \p SkipScheduling skips the scheduler check and is only meant for testing.

diff  --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp
index cac43241f6095..9976aaf845705 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp
@@ -219,13 +219,10 @@ const LegalityResult &LegalityAnalysis::canVectorize(ArrayRef<Value *> Bndl,
   if (any_of(Bndl, [](auto *V) { return !isa<Instruction>(V); }))
     return createLegalityResult<Pack>(ResultReason::NotInstructions);
   // Pack if not in the same BB.
-  auto *BB = cast<Instruction>(Bndl[0])->getParent();
-  if (any_of(drop_begin(Bndl),
-             [BB](auto *V) { return cast<Instruction>(V)->getParent() != BB; }))
+  if (LegalityAnalysis::
diff erentBlock(Bndl))
     return createLegalityResult<Pack>(ResultReason::DiffBBs);
   // Pack if instructions repeat, i.e., require some sort of broadcast.
-  SmallPtrSet<Value *, 8> Unique(llvm::from_range, Bndl);
-  if (Unique.size() != Bndl.size())
+  if (!LegalityAnalysis::areUnique(Bndl))
     return createLegalityResult<Pack>(ResultReason::RepeatedInstrs);
 
   auto CollectDescrs = getHowToCollectValues(Bndl);

diff  --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp
index 61ae69f911842..827debb889b14 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp
@@ -19,18 +19,12 @@ namespace sandboxir {
 
 std::optional<Type *> LoadStoreVec::canVectorize(ArrayRef<Instruction *> Bndl,
                                                  Scheduler &Sched) {
-  // TODO: Most of these checks are already implemented in LegalityAnalysis, we
-  // should reuse them.
-
   // Check if in the same BB.
-  auto *BB = cast<Instruction>(Bndl[0])->getParent();
-  if (any_of(drop_begin(Bndl),
-             [BB](auto *V) { return cast<Instruction>(V)->getParent() != BB; }))
+  if (LegalityAnalysis::
diff erentBlock(Bndl))
     return std::nullopt;
 
   // Check if instructions repeat.
-  SmallPtrSet<Value *, 8> Unique(Bndl.begin(), Bndl.end());
-  if (Unique.size() != Bndl.size())
+  if (!LegalityAnalysis::areUnique(Bndl))
     return std::nullopt;
 
   // TODO: This is target-dependent.


        


More information about the llvm-commits mailing list