Skip to content

Commit 52f3714

Browse files
committed
[VPlan] Add VPDef class.
This patch introduces a new VPDef class, which can be used to manage VPValues defined by recipes/VPInstructions. The idea here is to mirror VPUser for values defined by a recipe. A VPDef can produce either zero (e.g. a store recipe), one (most recipes) or multiple (VPInterleaveRecipe) result VPValues. To traverse the def-use chain from a VPDef to its users, one has to traverse the users of all values defined by a VPDef. VPValues now contain a pointer to their corresponding VPDef, if one exists. To traverse the def-use chain upwards from a VPValue, we first need to check if the VPValue is defined by a VPDef. If it does not have a VPDef, this means we have a VPValue that is not directly defined iniside the plan and we are done. If we have a VPDef, it is defined inside the region by a recipe, which is a VPUser, and the upwards def-use chain traversal continues by traversing all its operands. Note that we need to add an additional field to to VPVAlue to link them to their defs. The space increase is going to be offset by being able to remove the SubclassID field in future patches. Reviewed By: Ayal Differential Revision: https://reviews.llvm.org/D90558
1 parent 7e30989 commit 52f3714

File tree

4 files changed

+142
-7
lines changed

4 files changed

+142
-7
lines changed

llvm/docs/Proposals/VectorizationPlan.rst

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ The low-level design of VPlan comprises of the following classes.
154154
A VPUser represents an entity that uses a number of VPValues as operands.
155155
VPUser is similar in some aspects to LLVM's User class.
156156

157+
:VPDef:
158+
A VPDef represents an entity that defines zero, one or multiple VPValues.
159+
It is used to model the fact that recipes in VPlan can define multiple
160+
VPValues.
161+
157162
:VPInstruction:
158163
A VPInstruction is both a VPRecipe and a VPUser. It models a single
159164
VPlan-level instruction to be generated if the VPlan is executed, including

llvm/lib/Transforms/Vectorize/VPlan.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, const VPValue &V) {
5656
return OS;
5757
}
5858

59+
VPValue::VPValue(const unsigned char SC, Value *UV, VPDef *Def)
60+
: SubclassID(SC), UnderlyingVal(UV), Def(Def) {
61+
if (Def)
62+
Def->addDefinedValue(this);
63+
}
64+
65+
VPValue::~VPValue() {
66+
assert(Users.empty() && "trying to delete a VPValue with remaining users");
67+
if (Def)
68+
Def->removeDefinedValue(this);
69+
}
70+
5971
void VPValue::print(raw_ostream &OS, VPSlotTracker &SlotTracker) const {
6072
if (const VPInstruction *Instr = dyn_cast<VPInstruction>(this))
6173
Instr->print(OS, SlotTracker);

llvm/lib/Transforms/Vectorize/VPlanValue.h

+74-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/// This file contains the declarations of the entities induced by Vectorization
1111
/// Plans, e.g. the instructions the VPlan intends to generate if executed.
1212
/// VPlan models the following entities:
13-
/// VPValue VPUser
13+
/// VPValue VPUser VPDef
1414
/// | |
1515
/// VPInstruction
1616
/// These are documented in docs/VectorizationPlan.rst.
@@ -21,14 +21,17 @@
2121
#define LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
2222

2323
#include "llvm/ADT/DenseMap.h"
24+
#include "llvm/ADT/STLExtras.h"
2425
#include "llvm/ADT/SmallVector.h"
26+
#include "llvm/ADT/TinyPtrVector.h"
2527
#include "llvm/ADT/iterator_range.h"
2628

2729
namespace llvm {
2830

2931
// Forward declarations.
3032
class raw_ostream;
3133
class Value;
34+
class VPDef;
3235
class VPSlotTracker;
3336
class VPUser;
3437
class VPRecipeBase;
@@ -39,6 +42,7 @@ class VPRecipeBase;
3942
// and live-outs which the VPlan will need to fix accordingly.
4043
class VPValue {
4144
friend class VPBuilder;
45+
friend class VPDef;
4246
friend struct VPlanTransforms;
4347
friend class VPBasicBlock;
4448
friend class VPInterleavedAccessInfo;
@@ -53,8 +57,11 @@ class VPValue {
5357
// Hold the underlying Value, if any, attached to this VPValue.
5458
Value *UnderlyingVal;
5559

56-
VPValue(const unsigned char SC, Value *UV = nullptr)
57-
: SubclassID(SC), UnderlyingVal(UV) {}
60+
/// Pointer to the VPDef that defines this VPValue. If it is nullptr, the
61+
/// VPValue is not defined by any recipe modeled in VPlan.
62+
VPDef *Def;
63+
64+
VPValue(const unsigned char SC, Value *UV = nullptr, VPDef *Def = nullptr);
5865

5966
// DESIGN PRINCIPLE: Access to the underlying IR must be strictly limited to
6067
// the front-end and back-end of VPlan so that the middle-end is as
@@ -87,13 +94,12 @@ class VPValue {
8794
VPVWidenGEPSC
8895
};
8996

90-
VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV) {}
97+
VPValue(Value *UV = nullptr, VPDef *Def = nullptr)
98+
: VPValue(VPValueSC, UV, Def) {}
9199
VPValue(const VPValue &) = delete;
92100
VPValue &operator=(const VPValue &) = delete;
93101

94-
virtual ~VPValue() {
95-
assert(Users.empty() && "trying to delete a VPValue with remaining users");
96-
}
102+
virtual ~VPValue();
97103

98104
/// \return an ID for the concrete type of this object.
99105
/// This is used to implement the classof checks. This should not be used
@@ -152,6 +158,8 @@ class VPValue {
152158
}
153159

154160
void replaceAllUsesWith(VPValue *New);
161+
162+
VPDef *getDef() { return Def; }
155163
};
156164

157165
typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
@@ -223,6 +231,65 @@ class VPUser {
223231
/// Method to support type inquiry through isa, cast, and dyn_cast.
224232
static inline bool classof(const VPRecipeBase *Recipe);
225233
};
234+
235+
/// This class augments a recipe with a set of VPValues defined by the recipe.
236+
/// It allows recipes to define zero, one or multiple VPValues. A VPDef owns
237+
/// the VPValues it defines and is responsible for deleting its defined values.
238+
/// Single-value VPDefs that also inherit from VPValue must make sure to inherit
239+
/// from VPDef before VPValue.
240+
class VPDef {
241+
friend class VPValue;
242+
243+
/// The VPValues defined by this VPDef.
244+
TinyPtrVector<VPValue *> DefinedValues;
245+
246+
/// Add \p V as a defined value by this VPDef.
247+
void addDefinedValue(VPValue *V) {
248+
assert(V->getDef() == this &&
249+
"can only add VPValue already linked with this VPDef");
250+
DefinedValues.push_back(V);
251+
}
252+
253+
/// Remove \p V from the values defined by this VPDef. \p V must be a defined
254+
/// value of this VPDef.
255+
void removeDefinedValue(VPValue *V) {
256+
assert(V->getDef() == this &&
257+
"can only remove VPValue linked with this VPDef");
258+
assert(find(DefinedValues, V) != DefinedValues.end() &&
259+
"VPValue to remove must be in DefinedValues");
260+
erase_value(DefinedValues, V);
261+
V->Def = nullptr;
262+
}
263+
264+
public:
265+
virtual ~VPDef() {
266+
for (VPValue *D : make_early_inc_range(DefinedValues)) {
267+
assert(D->Def == this &&
268+
"all defined VPValues should point to the containing VPDef");
269+
assert(D->getNumUsers() == 0 &&
270+
"all defined VPValues should have no more users");
271+
D->Def = nullptr;
272+
delete D;
273+
}
274+
}
275+
276+
/// Returns the VPValue with index \p I defined by the VPDef.
277+
VPValue *getVPValue(unsigned I = 0) {
278+
assert(DefinedValues[I] && "defined value must be non-null");
279+
return DefinedValues[I];
280+
}
281+
const VPValue *getVPValue(unsigned I = 0) const {
282+
assert(DefinedValues[I] && "defined value must be non-null");
283+
return DefinedValues[I];
284+
}
285+
286+
/// Returns an ArrayRef of the values defined by the VPDef.
287+
ArrayRef<VPValue *> definedValues() { return DefinedValues; }
288+
289+
/// Returns the number of values defined by the VPDef.
290+
unsigned getNumDefinedValues() const { return DefinedValues.size(); }
291+
};
292+
226293
class VPlan;
227294
class VPBasicBlock;
228295
class VPRegionBlock;

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -521,5 +521,56 @@ TEST(VPRecipeTest, CastVPWidenMemoryInstructionRecipeToVPUser) {
521521
delete Load;
522522
}
523523

524+
struct VPDoubleValueDef : public VPUser, public VPDef {
525+
VPDoubleValueDef(ArrayRef<VPValue *> Operands) : VPUser(Operands), VPDef() {
526+
new VPValue(nullptr, this);
527+
new VPValue(nullptr, this);
528+
}
529+
};
530+
531+
TEST(VPDoubleValueDefTest, traverseUseLists) {
532+
// Check that the def-use chains of a multi-def can be traversed in both
533+
// directions.
534+
535+
// Create a new VPDef which defines 2 values and has 2 operands.
536+
VPInstruction Op0(20, {});
537+
VPInstruction Op1(30, {});
538+
VPDoubleValueDef DoubleValueDef({&Op0, &Op1});
539+
540+
// Create a new users of the defined values.
541+
VPInstruction I1(
542+
1, {DoubleValueDef.getVPValue(0), DoubleValueDef.getVPValue(1)});
543+
VPInstruction I2(2, {DoubleValueDef.getVPValue(0)});
544+
VPInstruction I3(3, {DoubleValueDef.getVPValue(1)});
545+
546+
// Check operands of the VPDef (traversing upwards).
547+
SmallVector<VPValue *, 4> DoubleOperands(DoubleValueDef.op_begin(),
548+
DoubleValueDef.op_end());
549+
EXPECT_EQ(2u, DoubleOperands.size());
550+
EXPECT_EQ(&Op0, DoubleOperands[0]);
551+
EXPECT_EQ(&Op1, DoubleOperands[1]);
552+
553+
// Check users of the defined values (traversing downwards).
554+
SmallVector<VPUser *, 4> DoubleValueDefV0Users(
555+
DoubleValueDef.getVPValue(0)->user_begin(),
556+
DoubleValueDef.getVPValue(0)->user_end());
557+
EXPECT_EQ(2u, DoubleValueDefV0Users.size());
558+
EXPECT_EQ(&I1, DoubleValueDefV0Users[0]);
559+
EXPECT_EQ(&I2, DoubleValueDefV0Users[1]);
560+
561+
SmallVector<VPUser *, 4> DoubleValueDefV1Users(
562+
DoubleValueDef.getVPValue(1)->user_begin(),
563+
DoubleValueDef.getVPValue(1)->user_end());
564+
EXPECT_EQ(2u, DoubleValueDefV1Users.size());
565+
EXPECT_EQ(&I1, DoubleValueDefV1Users[0]);
566+
EXPECT_EQ(&I3, DoubleValueDefV1Users[1]);
567+
568+
// Now check that we can get the right VPDef for each defined value.
569+
EXPECT_EQ(&DoubleValueDef, I1.getOperand(0)->getDef());
570+
EXPECT_EQ(&DoubleValueDef, I1.getOperand(1)->getDef());
571+
EXPECT_EQ(&DoubleValueDef, I2.getOperand(0)->getDef());
572+
EXPECT_EQ(&DoubleValueDef, I3.getOperand(0)->getDef());
573+
}
574+
524575
} // namespace
525576
} // namespace llvm

0 commit comments

Comments
 (0)