[llvm] [VectorCombine] New folding pattern for extract/binop/shuffle chains (PR #145232)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 26 07:23:21 PDT 2025


================
@@ -2910,6 +2911,192 @@ bool VectorCombine::foldShuffleFromReductions(Instruction &I) {
   return foldSelectShuffle(*Shuffle, true);
 }
 
+bool VectorCombine::foldShuffleChainsToReduce(Instruction &I) {
+  auto *EEI = dyn_cast<ExtractElementInst>(&I);
+  if (!EEI)
+    return false;
+
+  std::queue<Value *> InstWorklist;
+  Value *InitEEV = nullptr;
+  Intrinsic::ID CommonOp = 0;
+
+  bool IsFirstEEInst = true, IsFirstCallInst = true;
+  bool ShouldBeCallInst = true;
+
+  SmallVector<Value *, 3> PrevVecV(3, nullptr);
+  int64_t ShuffleMaskHalf = -1, ExpectedShuffleMaskHalf = 1;
+  int64_t VecSize = -1;
+
+  InstWorklist.push(EEI);
+
+  while (!InstWorklist.empty()) {
+    Value *V = InstWorklist.front();
+    InstWorklist.pop();
+
+    auto *CI = dyn_cast<Instruction>(V);
+    if (!CI)
+      return false;
+
+    if (auto *EEInst = dyn_cast<ExtractElementInst>(CI)) {
----------------
RKSimon wrote:

It doesn't feel like we should need to include ExtractElementInst handling inside the worklist loop - you should be able to do just this at the top:
```cpp
Value *VecOp;
if (!match(&I, m_ExtractElt(mValue(VecOp), m_Zero()))
  return false;

auto *FVT = dyn_cast<FixedVectorType>(VecOp->getType())
if (!FVT || FVT->getNumElements()< 2 || (FVT->getNumElements() % 2) != 0)
  return false;

InstWorklist.push_back(VecOp);

<SOME OTHER INITIALISATIONS>

while (!InstWorklist.empty()) {
  ....
```

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


More information about the llvm-commits mailing list