[llvm] [LoopVectorize] Widen selected-base loads with masked loads (PR #213227)
David Sherwood via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 3 07:55:14 PDT 2026
================
@@ -57,6 +57,93 @@ static std::optional<int64_t> getConstantStride(VPValue *Addr, Type *AccessTy,
return getStrideFromAddRec(AddRec, L, AccessTy, /*Ptr=*/nullptr, PSE);
}
+namespace {
+
+struct SelectedBaseLoadPattern {
+ VPInstruction *Addr;
+ SmallVector<VPValue *> GEPOperands;
+ VPValue *Cond;
+ VPValue *TrueBase;
+ VPValue *FalseBase;
+};
+
+/// Match a load through a GEP whose base pointer is selected per iteration
+/// between two loop-invariant candidates:
+///
+/// load (gep (select Cond, TrueBase, FalseBase), Indices...)
+///
+/// Legality, stride and profitability are checked separately by the caller.
+static std::optional<SelectedBaseLoadPattern>
+matchSelectedBaseLoad(VPInstruction *Load) {
+ if (Load->getOpcode() != Instruction::Load)
+ return std::nullopt;
+
+ // exclude volatile load, atomic load, other uneditable memory operations
+ auto *LI = dyn_cast_or_null<LoadInst>(Load->getUnderlyingInstr());
+ if (!LI || !LI->isSimple())
+ return std::nullopt;
+
+ VPValue *Addr = Load->getOperand(0);
+ auto *AddrI = dyn_cast<VPInstruction>(Addr);
+ if (!AddrI)
+ return std::nullopt;
+
+ Type *SourceElementTy = nullptr;
+ ArrayRef<VPValue *> GEPOperands;
+ // pattern with 3 params : cond + trueBase + falseBase
+ if (!match(Addr, m_GetElementPtr(SourceElementTy, GEPOperands)) ||
+ GEPOperands.size() < 2)
+ return std::nullopt;
+
+ VPValue *Cond = nullptr;
+ VPValue *TrueBase = nullptr;
+ VPValue *FalseBase = nullptr;
+ if (!match(GEPOperands.front(), m_Select(m_VPValue(Cond), m_VPValue(TrueBase),
+ m_VPValue(FalseBase))))
+ return std::nullopt;
+
+ if (!Cond->getScalarType()->isIntegerTy(1) || TrueBase == FalseBase)
+ return std::nullopt;
+
+ return SelectedBaseLoadPattern{AddrI, SmallVector<VPValue *>(GEPOperands),
+ Cond, TrueBase, FalseBase};
+}
+
+static InstructionCost getBlendedLoadCost(const TargetTransformInfo &TTI,
+ Type *VecTy, Align Alignment,
+ unsigned AddressSpace,
+ bool HasActiveMask,
+ TTI::TargetCostKind CostKind) {
+ Type *MaskTy = CmpInst::makeCmpResultType(VecTy);
+ InstructionCost Cost =
+ 2 * TTI.getMemIntrinsicInstrCost(
+ MemIntrinsicCostAttributes(Intrinsic::masked_load, VecTy,
+ Alignment, AddressSpace),
+ CostKind) +
+ TTI.getArithmeticInstrCost(Instruction::Xor, MaskTy, CostKind) +
+ TTI.getCmpSelInstrCost(Instruction::Select, VecTy, MaskTy,
+ CmpInst::BAD_ICMP_PREDICATE, CostKind);
+ if (HasActiveMask)
+ Cost += 2 * TTI.getArithmeticInstrCost(Instruction::And, MaskTy, CostKind);
+ return Cost;
+}
+
+static InstructionCost getGatherLoadCost(const LoadInst &LI, ElementCount VF,
----------------
david-arm wrote:
I don't think we should be adding more ways of calculating the cost of gathers. We already have something like this in both the legacy cost model (LoopVectorizationCostModel) and in the VPlan cost model.
https://github.com/llvm/llvm-project/pull/213227
More information about the llvm-commits
mailing list