[llvm] f95f02c - [VPlan] Thread scalar types through VPReplicateRecipe. (NFC) (#199379)
via llvm-commits
llvm-commits at lists.llvm.org
Wed May 27 13:05:26 PDT 2026
Author: Florian Hahn
Date: 2026-05-27T21:05:20+01:00
New Revision: f95f02c4b1ac931f8b6dcac55a0ad4ee59997883
URL: https://github.com/llvm/llvm-project/commit/f95f02c4b1ac931f8b6dcac55a0ad4ee59997883
DIFF: https://github.com/llvm/llvm-project/commit/f95f02c4b1ac931f8b6dcac55a0ad4ee59997883.diff
LOG: [VPlan] Thread scalar types through VPReplicateRecipe. (NFC) (#199379)
Update VPReplicateRecipe to populate VPSingleDefValue's scalar
type. For most opcodes, the scalar type is determine from the operands,
via computeScalarTypeForInstruction (from
https://github.com/llvm/llvm-project/pull/199378).
For some opcodes, like Loads and casts, the type must be
provided explicitly.
Depends on https://github.com/llvm/llvm-project/pull/199378.
PR: https://github.com/llvm/llvm-project/pull/199379
Added:
Modified:
llvm/lib/Transforms/Vectorize/VPlan.h
llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp
llvm/lib/Transforms/Vectorize/VPlanAnalysis.h
llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index df5f13613b20e..46c5e5bea5fce 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -3323,7 +3323,8 @@ class LLVM_ABI_FOR_TEST VPReplicateRecipe : public VPRecipeWithIRFlags,
bool IsSingleScalar, VPValue *Mask = nullptr,
const VPIRFlags &Flags = {}, VPIRMetadata Metadata = {},
DebugLoc DL = DebugLoc::getUnknown())
- : VPRecipeWithIRFlags(VPRecipeBase::VPReplicateSC, Operands, Flags, DL),
+ : VPRecipeWithIRFlags(VPRecipeBase::VPReplicateSC, Operands,
+ computeScalarType(I, Operands), Flags, DL),
VPIRMetadata(Metadata), IsSingleScalar(IsSingleScalar),
IsPredicated(Mask) {
setUnderlyingValue(I);
@@ -3333,6 +3334,11 @@ class LLVM_ABI_FOR_TEST VPReplicateRecipe : public VPRecipeWithIRFlags,
~VPReplicateRecipe() override = default;
+ /// Compute the scalar result type for a VPReplicateRecipe wrapping \p I with
+ /// \p Operands (excluding any predicate mask).
+ static Type *computeScalarType(const Instruction *I,
+ ArrayRef<VPValue *> Operands);
+
VPReplicateRecipe *clone() override { return cloneWithOperands(operands()); }
VPReplicateRecipe *cloneWithOperands(ArrayRef<VPValue *> NewOperands) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp b/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp
index 58ac22b955fb8..fe1747098fb73 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp
@@ -79,62 +79,6 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPWidenRecipe *R) {
llvm_unreachable("Unhandled opcode!");
}
-Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPReplicateRecipe *R) {
- unsigned Opcode = R->getUnderlyingInstr()->getOpcode();
-
- if (Instruction::isBinaryOp(Opcode) || Instruction::isShift(Opcode) ||
- Instruction::isBitwiseLogicOp(Opcode)) {
- Type *ResTy = inferScalarType(R->getOperand(0));
- assert(ResTy == inferScalarType(R->getOperand(1)) &&
- "inferred types for operands of binary op don't match");
- CachedTypes[R->getOperand(1)] = ResTy;
- return ResTy;
- }
-
- if (Instruction::isCast(Opcode))
- return R->getUnderlyingInstr()->getType();
-
- switch (Opcode) {
- case Instruction::Call: {
- unsigned CallIdx = R->getNumOperands() - (R->isPredicated() ? 2 : 1);
- return cast<Function>(R->getOperand(CallIdx)->getLiveInIRValue())
- ->getReturnType();
- }
- case Instruction::Select: {
- Type *ResTy = inferScalarType(R->getOperand(1));
- assert(ResTy == inferScalarType(R->getOperand(2)) &&
- "inferred types for operands of select op don't match");
- CachedTypes[R->getOperand(2)] = ResTy;
- return ResTy;
- }
- case Instruction::ICmp:
- case Instruction::FCmp:
- return IntegerType::get(Ctx, 1);
- case Instruction::Alloca:
- case Instruction::ExtractValue:
- return R->getUnderlyingInstr()->getType();
- case Instruction::Freeze:
- case Instruction::FNeg:
- case Instruction::GetElementPtr:
- return inferScalarType(R->getOperand(0));
- case Instruction::Load:
- return cast<LoadInst>(R->getUnderlyingInstr())->getType();
- case Instruction::Store:
- // FIXME: VPReplicateRecipes with store opcodes still define a result
- // VPValue, so we need to handle them here. Remove the code here once this
- // is modeled accurately in VPlan.
- return Type::getVoidTy(Ctx);
- default:
- break;
- }
- // Type inference not implemented for opcode.
- LLVM_DEBUG({
- dbgs() << "LV: Found unhandled opcode for: ";
- R->getVPSingleValue()->dump();
- });
- llvm_unreachable("Unhandled opcode");
-}
-
Type *VPTypeAnalysis::inferScalarType(const VPValue *V) {
if (Type *CachedTy = CachedTypes.lookup(V))
return CachedTy;
@@ -145,7 +89,7 @@ Type *VPTypeAnalysis::inferScalarType(const VPValue *V) {
VPWidenIntrinsicRecipe, VPWidenGEPRecipe, VPVectorPointerRecipe,
VPVectorEndPointerRecipe, VPWidenCallRecipe, VPWidenLoadRecipe,
VPWidenLoadEVLRecipe, VPDerivedIVRecipe, VPHeaderPHIRecipe,
- VPInstruction>(V)) {
+ VPInstruction, VPReplicateRecipe>(V)) {
Type *Ty = V->getScalarType();
assert(Ty && "Scalar type must be set by recipe construction");
return Ty;
@@ -153,7 +97,7 @@ Type *VPTypeAnalysis::inferScalarType(const VPValue *V) {
Type *ResultTy =
TypeSwitch<const VPRecipeBase *, Type *>(V->getDefiningRecipe())
- .Case<VPBlendRecipe, VPWidenRecipe, VPReplicateRecipe>(
+ .Case<VPBlendRecipe, VPWidenRecipe>(
[this](const auto *R) { return inferScalarTypeForRecipe(R); })
.Case([this](const VPReductionRecipe *R) {
return inferScalarType(R->getChainOp());
diff --git a/llvm/lib/Transforms/Vectorize/VPlanAnalysis.h b/llvm/lib/Transforms/Vectorize/VPlanAnalysis.h
index 8f4bb5219d0ca..57e438c1b03c0 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanAnalysis.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanAnalysis.h
@@ -23,7 +23,6 @@ class LLVMContext;
class VPValue;
class VPBlendRecipe;
class VPWidenRecipe;
-class VPReplicateRecipe;
class VPRecipeBase;
class VPlan;
class Value;
@@ -48,7 +47,6 @@ class VPTypeAnalysis {
Type *inferScalarTypeForRecipe(const VPBlendRecipe *R);
Type *inferScalarTypeForRecipe(const VPWidenRecipe *R);
- Type *inferScalarTypeForRecipe(const VPReplicateRecipe *R);
public:
VPTypeAnalysis(const VPlan &Plan)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 17d471631148f..c655eae381397 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -536,6 +536,17 @@ Type *llvm::computeScalarTypeForInstruction(unsigned Opcode,
return Op0Ty;
}
+Type *VPReplicateRecipe::computeScalarType(const Instruction *I,
+ ArrayRef<VPValue *> Operands) {
+ unsigned Opcode = I->getOpcode();
+ if (Instruction::isCast(Opcode) ||
+ is_contained(ArrayRef<unsigned>({Instruction::ExtractValue,
+ Instruction::Load, Instruction::Alloca}),
+ Opcode))
+ return I->getType();
+ return computeScalarTypeForInstruction(Opcode, Operands);
+}
+
VPInstruction::VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands,
const VPIRFlags &Flags, const VPIRMetadata &MD,
DebugLoc DL, const Twine &Name, Type *ResultTy)
@@ -3483,7 +3494,7 @@ void VPReplicateRecipe::execute(VPTransformState &State) {
"VPReplicateRecipes must be unrolled before ::execute");
auto *Instr = getUnderlyingInstr();
Instruction *Cloned = Instr->clone();
- Type *ResultTy = State.TypeAnalysis.inferScalarType(this);
+ Type *ResultTy = getScalarType();
if (!ResultTy->isVoidTy()) {
Cloned->setName(Instr->getName() + ".cloned");
// The operands of the replicate recipe may have been narrowed, resulting in
diff --git a/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp b/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
index f61d94e690598..ff1bd73edbc05 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
@@ -288,6 +288,15 @@ bool VPlanVerifier::verifyRecipeTypes(const VPRecipeBase &R) const {
return CheckScalarType(
computeScalarTypeForInstruction(VPI->getOpcode(), Ops));
}
+ case VPRecipeBase::VPReplicateSC: {
+ auto *RepR = cast<VPReplicateRecipe>(&R);
+ SmallVector<VPValue *, 4> Ops(RepR->operands());
+ if (RepR->isPredicated())
+ Ops.pop_back();
+ return CheckScalarType(
+ VPReplicateRecipe::computeScalarType(RepR->getUnderlyingInstr(), Ops));
+ }
+
default:
return true;
}
diff --git a/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp b/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
index b4ba5f8c50ae4..6846179d796d7 100644
--- a/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
+++ b/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
@@ -1238,19 +1238,22 @@ TEST_F(VPRecipeTest, CastVPInterleaveRecipeToVPUser) {
TEST_F(VPRecipeTest, CastVPReplicateRecipeToVPUser) {
VPlan &Plan = getPlan();
IntegerType *Int32 = IntegerType::get(C, 32);
+ FunctionType *FTy = FunctionType::get(Int32, false);
+ Function *Fn = Function::Create(FTy, GlobalValue::ExternalLinkage, 0);
+ auto *Call = CallInst::Create(FTy, Fn);
VPValue *Op1 = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
VPValue *Op2 = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 2));
+ VPValue *CalledFn = Plan.getOrAddLiveIn(Call->getCalledFunction());
SmallVector<VPValue *, 4> Args;
Args.push_back(Op1);
Args.push_back(Op2);
-
- FunctionType *FTy = FunctionType::get(Int32, false);
- auto *Call = CallInst::Create(FTy, PoisonValue::get(FTy));
+ Args.push_back(CalledFn);
VPReplicateRecipe Recipe(Call, make_range(Args.begin(), Args.end()), true);
checkVPRecipeCastImpl<VPReplicateRecipe, VPUser, VPIRMetadata>(&Recipe);
delete Call;
+ delete Fn;
}
TEST_F(VPRecipeTest, CastVPBranchOnMaskRecipeToVPUser) {
More information about the llvm-commits
mailing list