[llvm] [VectorCombine][AMDGPU] Narrow Phi of Shuffles. (PR #140188)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue May 27 02:03:11 PDT 2025
================
@@ -3519,6 +3520,97 @@ bool VectorCombine::foldInterleaveIntrinsics(Instruction &I) {
return true;
}
+// Attempt to narrow a phi of shufflevector instructions where the two incoming
+// values have the same operands but different masks. If the two shuffle masks
+// are offsets of one another we can use one branch to rotate the incoming
+// vector and perform one larger shuffle after the phi.
+bool VectorCombine::shrinkPhiOfShuffles(Instruction &I) {
+ auto *Phi = dyn_cast<PHINode>(&I);
+ if (!Phi || Phi->getNumIncomingValues() != 2u)
+ return false;
+
+ Value *Op = nullptr;
+ ArrayRef<int> Mask0;
+ ArrayRef<int> Mask1;
+
+ if (!match(Phi->getOperand(0u),
+ m_OneUse(m_Shuffle(m_Value(Op), m_Poison(), m_Mask(Mask0)))) ||
+ !match(Phi->getOperand(1u),
+ m_OneUse(m_Shuffle(m_Specific(Op), m_Poison(), m_Mask(Mask1)))))
+ return false;
+
+ auto *Shuf = cast<ShuffleVectorInst>(Phi->getOperand(0u));
+
+ // Ensure result vectors are wider than the argument vector.
+ auto *InputVT = cast<FixedVectorType>(Op->getType());
+ auto *ResultVT = cast<FixedVectorType>(Shuf->getType());
+ auto const InputNumElements = InputVT->getNumElements();
+
+ if (InputNumElements >= ResultVT->getNumElements())
+ return false;
+
+ // Take the difference of the two shuffle masks at each index. Ignore poison
+ // values at the same index in both masks.
+ SmallVector<int, 16> NewMask;
+ NewMask.reserve(Mask0.size());
+
+ for (auto I = 0u; I < Mask0.size(); ++I) {
+ if (Mask0[I] >= 0 && Mask1[I] >= 0)
+ NewMask.push_back(Mask0[I] - Mask1[I]);
+ else if (Mask0[I] == -1 && Mask1[I] == -1)
+ continue;
+ else
+ return false;
+ }
+
+ // Ensure all elements of the new mask are equal. If the difference between
+ // the incoming mask elements is the same, the two must be constant offsets
+ // of one another.
+ if (NewMask.empty() ||
+ !std::equal(NewMask.begin() + 1u, NewMask.end(), NewMask.begin()))
----------------
dtcxzyw wrote:
```suggestion
!all_equal(NewMask))
```
https://github.com/llvm/llvm-project/pull/140188
More information about the llvm-commits
mailing list