[llvm] [SLP]Vectorize operand chains of non-vectorizable instructions (PR #194248)
Alexey Bataev via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 30 04:06:51 PDT 2026
================
@@ -30098,6 +30098,221 @@ bool SLPVectorizerPass::vectorizeCmpInsts(iterator_range<ItT> CmpInsts,
return Changed;
}
+/// Returns true if \p I is an instruction whose result the SLP vectorizer
+/// cannot turn into a vector instruction directly, but whose operand chains
+/// may still be worth vectorizing as bundle seeds.
+static bool isNonVectorizableInst(const Instruction *I,
+ const TargetLibraryInfo *TLI) {
+ if (const auto *CB = dyn_cast<CallBase>(I)) {
+ if (CB->isInlineAsm())
+ return false;
+ if (const auto *II = dyn_cast<IntrinsicInst>(CB)) {
+ if (II->isAssumeLikeIntrinsic())
+ return false;
+ if (isa<AnyMemIntrinsic>(II))
+ return false;
+ }
+ if (const auto *CI = dyn_cast<CallInst>(CB)) {
+ Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
+ if (isTriviallyVectorizable(ID))
+ return false;
+ if (!VFDatabase::getMappings(*CI).empty())
+ return false;
+ }
+ // Skip vector-returning calls in non-revec mode - we cannot turn their
+ // results into wider vectors here.
+ return SLPReVec || !CB->getType()->isVectorTy();
+ }
+ if (isa<AtomicRMWInst, AtomicCmpXchgInst>(I))
+ return true;
+ if (const auto *RI = dyn_cast<ReturnInst>(I))
+ return RI->getNumOperands() > 0;
+ return false;
+}
+
+/// Visits the value operands of \p I that are candidates for operand-chain
+/// vectorization.
+template <typename Func>
+static void forEachOperandChainCandidate(Instruction *I, Func F) {
+ if (auto *CB = dyn_cast<CallBase>(I)) {
+ for (auto [Idx, U] : enumerate(CB->args()))
+ F(U.get(), Idx);
+ return;
+ }
+ if (auto *SI = dyn_cast<StoreInst>(I)) {
+ F(SI->getValueOperand(), 0);
+ return;
+ }
+ if (auto *AI = dyn_cast<AtomicRMWInst>(I)) {
+ F(AI->getValOperand(), 0);
+ return;
+ }
+ if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I)) {
+ F(AI->getCompareOperand(), 0);
+ F(AI->getNewValOperand(), 1);
+ return;
+ }
+ if (auto *RI = dyn_cast<ReturnInst>(I)) {
+ if (RI->getNumOperands() > 0)
+ F(RI->getReturnValue(), 0);
+ return;
+ }
+ llvm_unreachable("Unexpected instruction kind for operand-chain seeding");
+}
+
+template <typename ItT>
+bool SLPVectorizerPass::vectorizeNonVectorizableInsts(
+ iterator_range<ItT> InstRange, BasicBlock *BB, BoUpSLP &R) {
+ SmallVector<Instruction *> Insts(InstRange);
+ if (Insts.empty())
+ return false;
+ stable_sort(Insts, [](const Instruction *A, const Instruction *B) {
+ return A->getOpcode() < B->getOpcode();
+ });
+
+ bool Changed = false;
+ // Pass 1 - try to find horizontal reductions feeding the root operands.
+ SmallPtrSet<Value *, 8> RootSeen;
+ for (Instruction *I : Insts) {
+ if (R.isDeleted(I))
+ continue;
+ bool RootDeleted = false;
+ forEachOperandChainCandidate(I, [&](Value *Op, unsigned /*Position*/) {
+ if (RootDeleted)
+ return;
+ auto *RootOp = dyn_cast<Instruction>(Op);
+ if (!RootOp || RootOp->getParent() != BB || R.isDeleted(RootOp) ||
+ isa<ShuffleVectorInst>(RootOp) ||
+ !isValidElementType(RootOp->getType()))
+ return;
+ if (!RootSeen.insert(RootOp).second)
+ return;
+ Changed |= vectorizeRootInstruction(nullptr, RootOp, BB, R);
+ if (R.isDeleted(I))
+ RootDeleted = true;
+ });
+ }
+ // Pass 2 - collect the operand instructions across all roots and try to
+ // vectorize them as bundles.
+ struct OperandGroupKey {
+ enum class Kind : unsigned {
+ NonCall = 0,
+ Intrinsic,
+ NamedFunction,
+ IndirectCall,
+ };
+ Kind RootKind;
+ unsigned KindID; // Intrinsic ID for Intrinsic, opcode for NonCall,
+ // callee value-kind for IndirectCall, 0 for
+ // NamedFunction.
+ unsigned SubOp; // AtomicRMW operation; 0 otherwise.
+ StringRef FuncName; // Non-empty only for NamedFunction.
+ unsigned Position; // Operand slot within the root.
+
+ bool operator==(const OperandGroupKey &O) const {
+ return RootKind == O.RootKind && KindID == O.KindID && SubOp == O.SubOp &&
+ FuncName == O.FuncName && Position == O.Position;
+ }
+ bool operator!=(const OperandGroupKey &O) const { return !(*this == O); }
+ bool less(const OperandGroupKey &O) const {
+ if (RootKind != O.RootKind)
+ return static_cast<unsigned>(RootKind) <
+ static_cast<unsigned>(O.RootKind);
+ if (KindID != O.KindID)
+ return KindID < O.KindID;
+ if (SubOp != O.SubOp)
+ return SubOp < O.SubOp;
+ if (int C = FuncName.compare(O.FuncName))
+ return C < 0;
+ return Position < O.Position;
+ }
+ };
+ SmallDenseMap<Value *, OperandGroupKey> OpKeys;
+
+ auto BuildKey = [](Instruction *I, unsigned Position) -> OperandGroupKey {
+ if (auto *CB = dyn_cast<CallBase>(I)) {
+ if (auto *II = dyn_cast<IntrinsicInst>(CB))
+ return {OperandGroupKey::Kind::Intrinsic,
+ II->getIntrinsicID(),
+ 0,
+ {},
+ Position};
+ if (Function *F = CB->getCalledFunction())
+ return {OperandGroupKey::Kind::NamedFunction, 0, 0, F->getName(),
+ Position};
+ return {OperandGroupKey::Kind::IndirectCall,
+ CB->getCalledOperand()->getValueID(),
+ 0,
+ {},
+ Position};
+ }
+ unsigned SubOp = 0;
+ if (auto *AI = dyn_cast<AtomicRMWInst>(I))
+ SubOp = static_cast<unsigned>(AI->getOperation());
+ return {
+ OperandGroupKey::Kind::NonCall, I->getOpcode(), SubOp, {}, Position};
+ };
+
+ auto OperandSorter = [&OpKeys](Value *V1, Value *V2) -> bool {
+ if (V1 == V2)
+ return false;
+ const OperandGroupKey &K1 = OpKeys.at(V1);
+ const OperandGroupKey &K2 = OpKeys.at(V2);
+ if (K1 != K2)
+ return K1.less(K2);
+ auto *I1 = cast<Instruction>(V1);
+ auto *I2 = cast<Instruction>(V2);
+ if (I1->getType()->getTypeID() != I2->getType()->getTypeID())
+ return I1->getType()->getTypeID() < I2->getType()->getTypeID();
+ if (I1->getType()->getScalarSizeInBits() !=
+ I2->getType()->getScalarSizeInBits())
+ return I1->getType()->getScalarSizeInBits() <
+ I2->getType()->getScalarSizeInBits();
+ if (I1->getOpcode() != I2->getOpcode())
+ return I1->getOpcode() < I2->getOpcode();
+ return I1->comesBefore(I2);
+ };
+
+ auto AreCompatibleOperands = [&OpKeys](ArrayRef<Value *> VL,
+ Value *V) -> bool {
+ if (VL.empty() || VL.back() == V)
+ return true;
+ const OperandGroupKey &KBack = OpKeys.at(VL.back());
+ const OperandGroupKey &K = OpKeys.at(V);
+ if (KBack != K)
+ return false;
+ auto *I1 = cast<Instruction>(VL.back());
+ auto *I2 = cast<Instruction>(V);
+ return I1->getType() == I2->getType() && I1->getOpcode() == I2->getOpcode();
----------------
alexey-bataev wrote:
Because it does not store the actual instruction, it just does not have access to this data
https://github.com/llvm/llvm-project/pull/194248
More information about the llvm-commits
mailing list