[llvm] [VectorCombine][AMDGPU] Narrow Phi of Shuffles. (PR #140188)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 5 01:28:57 PDT 2025
================
@@ -3519,6 +3520,95 @@ 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) {
----------------
RKSimon wrote:
```
for (auto [M0,M1] : zip(Mask0, Mask1)) {
if (M0 >= 0 && M1 >= 0)
NewMask.push_back(M0 - M1);
else if (M0 == -1 && M1 == -1)
continue;
else
return false;
}
```
https://github.com/llvm/llvm-project/pull/140188
More information about the llvm-commits
mailing list