Skip to content

Commit 6b671a7

Browse files
committed
!fixup address latest comments, thanks
1 parent 8c359c7 commit 6b671a7

File tree

4 files changed

+70
-39
lines changed

4 files changed

+70
-39
lines changed

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,23 @@ m_VPInstruction(const Op0_t &Op0, const Op1_t &Op1) {
251251
return BinaryVPInstruction_match<Op0_t, Op1_t, Opcode>(Op0, Op1);
252252
}
253253

254+
template <typename Op0_t, typename Op1_t, typename Op2_t, unsigned Opcode,
255+
bool Commutative, typename... RecipeTys>
256+
using TernaryRecipe_match = Recipe_match<std::tuple<Op0_t, Op1_t, Op2_t>,
257+
Opcode, Commutative, RecipeTys...>;
258+
259+
template <typename Op0_t, typename Op1_t, typename Op2_t, unsigned Opcode>
260+
using TernaryVPInstruction_match =
261+
TernaryRecipe_match<Op0_t, Op1_t, Op2_t, Opcode, /*Commutative*/ false,
262+
VPInstruction>;
263+
264+
template <unsigned Opcode, typename Op0_t, typename Op1_t, typename Op2_t>
265+
inline TernaryVPInstruction_match<Op0_t, Op1_t, Op2_t, Opcode>
266+
m_VPInstruction(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
267+
return TernaryVPInstruction_match<Op0_t, Op1_t, Op2_t, Opcode>(
268+
{Op0, Op1, Op2});
269+
}
270+
254271
template <typename Op0_t>
255272
inline UnaryVPInstruction_match<Op0_t, VPInstruction::Not>
256273
m_Not(const Op0_t &Op0) {

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ bool VPInstruction::opcodeMayReadOrWriteFromMemory() const {
773773
case VPInstruction::LogicalAnd:
774774
case VPInstruction::Not:
775775
case VPInstruction::PtrAdd:
776+
case VPInstruction::WideIVStep:
776777
return false;
777778
default:
778779
return true;

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,16 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
968968
TypeInfo.inferScalarType(R.getOperand(1)) ==
969969
TypeInfo.inferScalarType(R.getVPSingleValue()))
970970
return R.getVPSingleValue()->replaceAllUsesWith(R.getOperand(1));
971+
972+
if (match(&R, m_VPInstruction<VPInstruction::WideIVStep>(
973+
m_VPValue(X), m_SpecificInt(1), m_VPValue(Y)))) {
974+
if (TypeInfo.inferScalarType(X) != TypeInfo.inferScalarType(Y)) {
975+
X = new VPWidenCastRecipe(Instruction::Trunc, X,
976+
TypeInfo.inferScalarType(Y));
977+
X->getDefiningRecipe()->insertBefore(&R);
978+
}
979+
R.getVPSingleValue()->replaceAllUsesWith(X);
980+
}
971981
}
972982

973983
/// Try to simplify the recipes in \p Plan. Use \p CanonicalIVTy as type for all
@@ -2050,9 +2060,10 @@ void VPlanTransforms::createInterleaveGroups(
20502060
}
20512061
}
20522062

2053-
void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
2054-
Type *CanonicalIVType = Plan.getCanonicalIV()->getScalarType();
2055-
VPTypeAnalysis TypeInfo(CanonicalIVType);
2063+
void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan,
2064+
Type *CanonicalIVTy) {
2065+
using namespace llvm::VPlanPatternMatch;
2066+
VPTypeAnalysis TypeInfo(CanonicalIVTy);
20562067

20572068
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
20582069
vp_depth_first_deep(Plan.getEntry()))) {
@@ -2070,42 +2081,44 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
20702081
continue;
20712082
}
20722083

2073-
auto *VPI = dyn_cast<VPInstruction>(&R);
2074-
if (VPI && VPI->getOpcode() == VPInstruction::WideIVStep) {
2075-
VPBuilder Builder(VPI->getParent(), VPI->getIterator());
2076-
VPValue *VectorStep = VPI->getOperand(0);
2077-
Type *IVTy = TypeInfo.inferScalarType(VPI->getOperand(2));
2078-
if (TypeInfo.inferScalarType(VectorStep) != IVTy) {
2079-
Instruction::CastOps CastOp = IVTy->isFloatingPointTy()
2080-
? Instruction::UIToFP
2081-
: Instruction::Trunc;
2082-
VectorStep = Builder.createWidenCast(CastOp, VectorStep, IVTy);
2083-
}
2084-
2085-
VPValue *ScalarStep = VPI->getOperand(1);
2086-
auto *ConstStep =
2087-
ScalarStep->isLiveIn()
2088-
? dyn_cast<ConstantInt>(ScalarStep->getLiveInIRValue())
2089-
: nullptr;
2090-
if (!ConstStep || ConstStep->getValue() != 1) {
2091-
if (TypeInfo.inferScalarType(ScalarStep) != IVTy) {
2092-
ScalarStep =
2093-
Builder.createWidenCast(Instruction::Trunc, ScalarStep, IVTy);
2094-
}
2095-
2096-
std::optional<FastMathFlags> FMFs;
2097-
if (IVTy->isFloatingPointTy())
2098-
FMFs = VPI->getFastMathFlags();
2084+
VPValue *VectorStep;
2085+
VPValue *ScalarStep;
2086+
VPValue *IVTyOp;
2087+
if (!match(&R, m_VPInstruction<VPInstruction::WideIVStep>(
2088+
m_VPValue(VectorStep), m_VPValue(ScalarStep),
2089+
m_VPValue(IVTyOp))))
2090+
continue;
2091+
auto *VPI = cast<VPInstruction>(&R);
2092+
VPBuilder Builder(VPI->getParent(), VPI->getIterator());
2093+
Type *IVTy = TypeInfo.inferScalarType(IVTyOp);
2094+
if (TypeInfo.inferScalarType(VectorStep) != IVTy) {
2095+
Instruction::CastOps CastOp = IVTy->isFloatingPointTy()
2096+
? Instruction::UIToFP
2097+
: Instruction::Trunc;
2098+
VectorStep = Builder.createWidenCast(CastOp, VectorStep, IVTy);
2099+
}
20992100

2100-
unsigned MulOpc =
2101-
IVTy->isFloatingPointTy() ? Instruction::FMul : Instruction::Mul;
2102-
VPInstruction *Mul = Builder.createNaryOp(
2103-
MulOpc, {VectorStep, ScalarStep}, FMFs, R.getDebugLoc());
2104-
VectorStep = Mul;
2105-
}
2106-
VPI->replaceAllUsesWith(VectorStep);
2107-
VPI->eraseFromParent();
2101+
auto *ConstStep =
2102+
ScalarStep->isLiveIn()
2103+
? dyn_cast<ConstantInt>(ScalarStep->getLiveInIRValue())
2104+
: nullptr;
2105+
assert(!ConstStep || ConstStep->getValue() != 1);
2106+
if (TypeInfo.inferScalarType(ScalarStep) != IVTy) {
2107+
ScalarStep =
2108+
Builder.createWidenCast(Instruction::Trunc, ScalarStep, IVTy);
21082109
}
2110+
2111+
std::optional<FastMathFlags> FMFs;
2112+
if (IVTy->isFloatingPointTy())
2113+
FMFs = VPI->getFastMathFlags();
2114+
2115+
unsigned MulOpc =
2116+
IVTy->isFloatingPointTy() ? Instruction::FMul : Instruction::Mul;
2117+
VPInstruction *Mul = Builder.createNaryOp(
2118+
MulOpc, {VectorStep, ScalarStep}, FMFs, R.getDebugLoc());
2119+
VectorStep = Mul;
2120+
VPI->replaceAllUsesWith(VectorStep);
2121+
VPI->eraseFromParent();
21092122
}
21102123
}
21112124
}

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ struct VPlanTransforms {
136136
VPRecipeBuilder &RecipeBuilder);
137137

138138
/// Lower abstract recipes to concrete ones, that can be codegen'd.
139-
static void convertToConcreteRecipes(VPlan &Plan);
139+
static void convertToConcreteRecipes(VPlan &Plan, Type *CanonicalIVTy);
140140

141-
static void simplifyRecipes(VPlan &Plan, Type *CanonicalIVTy);
141+
static void simplifyRecipes(VPlan &Plan, Type *CanonicalIVTy);
142142

143143
/// If there's a single exit block, optimize its phi recipes that use exiting
144144
/// IV values by feeding them precomputed end values instead, possibly taken

0 commit comments

Comments
 (0)