Skip to content

Commit c9a37a4

Browse files
committed
[VPlan] Assign custom opcodes to recipes not mapping to IR opcodes.
We can perform CSE on recipes that do not directly map to Instruction opcodes. One example is VPVectorPointerRecipe. Currently this is handled by supporting them in ::canHandle, but currently that means that we return std::nullopt from getOpcodeOrIntrinsicID() for it. This currently only works, because the only case we return std::nullopt and perform CSE is VPVectorPointerRecipe. But that does not work if we support more such recipes, like VPPredInstPHIRecipe (#162110). To fix this, return a custom opcode from getOpcodeOrIntrinsicID for recipes like VPVectorPointerRecipe, using the VPDefID after all regular instruction opcodes.
1 parent 3477eb9 commit c9a37a4

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,7 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
10641064
ResumeForEpilogue,
10651065
/// Returns the value for vscale.
10661066
VScale,
1067+
OpsEnd = VScale,
10671068
};
10681069

10691070
/// Returns true if this VPInstruction generates scalar values for all lanes.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,13 @@ struct VPCSEDenseMapInfo : public DenseMapInfo<VPSingleDefRecipe *> {
19881988
.Case<VPWidenIntrinsicRecipe>([](auto *I) {
19891989
return std::make_pair(true, I->getVectorIntrinsicID());
19901990
})
1991+
.Case<VPVectorPointerRecipe>([](auto *I) {
1992+
// For recipes that do not directly map to LLVM IR instructions,
1993+
// assign opcodes after the last VPInstruction opcode (which is also
1994+
// after the last IR Instruction opcode), based on the VPDefID.
1995+
return std::make_pair(false,
1996+
VPInstruction::OpsEnd + 1 + I->getVPDefID());
1997+
})
19911998
.Default([](auto *) { return std::nullopt; });
19921999
}
19932000

@@ -2011,12 +2018,9 @@ struct VPCSEDenseMapInfo : public DenseMapInfo<VPSingleDefRecipe *> {
20112018
static bool canHandle(const VPSingleDefRecipe *Def) {
20122019
// We can extend the list of handled recipes in the future,
20132020
// provided we account for the data embedded in them while checking for
2014-
// equality or hashing. We assign VPVectorEndPointerRecipe the GEP opcode,
2015-
// as it is essentially a GEP with different semantics.
2016-
auto C = isa<VPVectorPointerRecipe>(Def)
2017-
? std::make_pair(false, Instruction::GetElementPtr)
2018-
: getOpcodeOrIntrinsicID(Def);
2021+
// equality or hashing.
20192022

2023+
auto C = getOpcodeOrIntrinsicID(Def);
20202024
// The issue with (Insert|Extract)Value is that the index of the
20212025
// insert/extract is not a proper operand in LLVM IR, and hence also not in
20222026
// VPlan.
@@ -2054,6 +2058,8 @@ struct VPCSEDenseMapInfo : public DenseMapInfo<VPSingleDefRecipe *> {
20542058
vputils::isSingleScalar(L) != vputils::isSingleScalar(R) ||
20552059
!equal(L->operands(), R->operands()))
20562060
return false;
2061+
assert(getOpcodeOrIntrinsicID(L) && getOpcodeOrIntrinsicID(R) &&
2062+
"must have valid opcode info for both recipes");
20572063
if (auto *LFlags = dyn_cast<VPRecipeWithIRFlags>(L))
20582064
if (LFlags->hasPredicate() &&
20592065
LFlags->getPredicate() !=

0 commit comments

Comments
 (0)