Skip to content

Commit 710aceb

Browse files
committed
Revert "[VPlan] Use VPValue def for VPMemoryInstructionRecipe."
It introduced a memory leak. This reverts commit 525b085.
1 parent c87bd2d commit 710aceb

File tree

4 files changed

+35
-123
lines changed

4 files changed

+35
-123
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,6 @@ class InnerLoopVectorizer {
531531
/// value into a vector.
532532
Value *getOrCreateVectorValue(Value *V, unsigned Part);
533533

534-
void setVectorValue(Value *Scalar, unsigned Part, Value *Vector) {
535-
VectorLoopValueMap.setVectorValue(Scalar, Part, Vector);
536-
}
537-
538534
/// Return a value in the new loop corresponding to \p V from the original
539535
/// loop at unroll and vector indices \p Instance. If the value has been
540536
/// vectorized but not scalarized, the necessary extractelement instruction
@@ -557,8 +553,8 @@ class InnerLoopVectorizer {
557553
/// non-null. Use \p State to translate given VPValues to IR values in the
558554
/// vectorized loop.
559555
void vectorizeMemoryInstruction(Instruction *Instr, VPTransformState &State,
560-
VPValue *Def, VPValue *Addr,
561-
VPValue *StoredValue, VPValue *BlockInMask);
556+
VPValue *Addr, VPValue *StoredValue,
557+
VPValue *BlockInMask);
562558

563559
/// Set the debug location in the builder using the debug location in
564560
/// the instruction.
@@ -2507,9 +2503,11 @@ void InnerLoopVectorizer::vectorizeInterleaveGroup(
25072503
}
25082504
}
25092505

2510-
void InnerLoopVectorizer::vectorizeMemoryInstruction(
2511-
Instruction *Instr, VPTransformState &State, VPValue *Def, VPValue *Addr,
2512-
VPValue *StoredValue, VPValue *BlockInMask) {
2506+
void InnerLoopVectorizer::vectorizeMemoryInstruction(Instruction *Instr,
2507+
VPTransformState &State,
2508+
VPValue *Addr,
2509+
VPValue *StoredValue,
2510+
VPValue *BlockInMask) {
25132511
// Attempt to issue a wide load.
25142512
LoadInst *LI = dyn_cast<LoadInst>(Instr);
25152513
StoreInst *SI = dyn_cast<StoreInst>(Instr);
@@ -2638,8 +2636,7 @@ void InnerLoopVectorizer::vectorizeMemoryInstruction(
26382636
if (Reverse)
26392637
NewLI = reverseVector(NewLI);
26402638
}
2641-
2642-
State.set(Def, Instr, NewLI, Part);
2639+
VectorLoopValueMap.setVectorValue(Instr, Part, NewLI);
26432640
}
26442641
}
26452642

@@ -7757,16 +7754,6 @@ VPlanPtr LoopVectorizationPlanner::buildVPlanWithVPRecipes(
77577754

77587755
if (auto Recipe =
77597756
RecipeBuilder.tryToCreateWidenRecipe(Instr, Range, Plan)) {
7760-
// Check if the recipe can be converted to a VPValue. We need the extra
7761-
// down-casting step until VPRecipeBase inherits from VPValue.
7762-
VPValue *MaybeVPValue = Recipe->toVPValue();
7763-
if (!Instr->getType()->isVoidTy() && MaybeVPValue) {
7764-
if (NeedDef.contains(Instr))
7765-
Plan->addOrReplaceVPValue(Instr, MaybeVPValue);
7766-
else
7767-
Plan->addVPValue(Instr, MaybeVPValue);
7768-
}
7769-
77707757
RecipeBuilder.setRecipe(Instr, Recipe);
77717758
VPBB->appendRecipe(Recipe);
77727759
continue;
@@ -7816,14 +7803,7 @@ VPlanPtr LoopVectorizationPlanner::buildVPlanWithVPRecipes(
78167803

78177804
for (unsigned i = 0; i < IG->getFactor(); ++i)
78187805
if (Instruction *Member = IG->getMember(i)) {
7819-
VPValue *NewVPV = nullptr;
7820-
if (!Member->getType()->isVoidTy()) {
7821-
NewVPV = new VPValue(Member);
7822-
Plan->getVPValue(Member)->replaceAllUsesWith(NewVPV);
7823-
}
78247806
RecipeBuilder.getRecipe(Member)->eraseFromParent();
7825-
if (NewVPV)
7826-
Plan->addVPValue(Member, NewVPV);
78277807
}
78287808
}
78297809

@@ -8165,11 +8145,9 @@ void VPPredInstPHIRecipe::execute(VPTransformState &State) {
81658145
}
81668146

81678147
void VPWidenMemoryInstructionRecipe::execute(VPTransformState &State) {
8168-
Instruction *Instr = getUnderlyingInstr();
81698148
VPValue *StoredValue = isa<StoreInst>(Instr) ? getStoredValue() : nullptr;
8170-
State.ILV->vectorizeMemoryInstruction(Instr, State,
8171-
StoredValue ? nullptr : this, getAddr(),
8172-
StoredValue, getMask());
8149+
State.ILV->vectorizeMemoryInstruction(&Instr, State, getAddr(), StoredValue,
8150+
getMask());
81738151
}
81748152

81758153
// Determine how to lower the scalar epilogue, which depends on 1) optimising
@@ -8215,12 +8193,6 @@ static ScalarEpilogueLowering getScalarEpilogueLowering(
82158193
return CM_ScalarEpilogueAllowed;
82168194
}
82178195

8218-
void VPTransformState::set(VPValue *Def, Value *IRDef, Value *V,
8219-
unsigned Part) {
8220-
set(Def, V, Part);
8221-
ILV->setVectorValue(IRDef, Part, V);
8222-
}
8223-
82248196
// Process the loop in the VPlan-native vectorization path. This path builds
82258197
// VPlan upfront in the vectorization pipeline, which allows to apply
82268198
// VPlan-to-VPlan transformations from the very beginning without modifying the

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,6 @@ VPUser *VPRecipeBase::toVPUser() {
101101
return nullptr;
102102
}
103103

104-
VPValue *VPRecipeBase::toVPValue() {
105-
if (auto *V = dyn_cast<VPInstruction>(this))
106-
return V;
107-
if (auto *V = dyn_cast<VPWidenMemoryInstructionRecipe>(this))
108-
return V;
109-
return nullptr;
110-
}
111-
112-
const VPValue *VPRecipeBase::toVPValue() const {
113-
if (auto *V = dyn_cast<VPInstruction>(this))
114-
return V;
115-
if (auto *V = dyn_cast<VPWidenMemoryInstructionRecipe>(this))
116-
return V;
117-
return nullptr;
118-
}
119-
120104
// Get the top-most entry block of \p Start. This is the entry block of the
121105
// containing VPlan. This function is templated to support both const and non-const blocks
122106
template <typename T> static T *getPlanEntry(T *Start) {
@@ -421,15 +405,14 @@ void VPRecipeBase::removeFromParent() {
421405
Parent = nullptr;
422406
}
423407

408+
VPValue *VPRecipeBase::toVPValue() {
409+
if (auto *V = dyn_cast<VPInstruction>(this))
410+
return V;
411+
return nullptr;
412+
}
413+
424414
iplist<VPRecipeBase>::iterator VPRecipeBase::eraseFromParent() {
425415
assert(getParent() && "Recipe not in any VPBasicBlock");
426-
// If the recipe is a VPValue and has been added to the containing VPlan,
427-
// remove the mapping.
428-
if (Value *UV = getUnderlyingInstr())
429-
if (!UV->getType()->isVoidTy())
430-
if (auto *Plan = getParent()->getPlan())
431-
Plan->removeVPValueFor(UV);
432-
433416
return getParent()->getRecipeList().erase(getIterator());
434417
}
435418

@@ -920,8 +903,7 @@ void VPPredInstPHIRecipe::print(raw_ostream &O, const Twine &Indent,
920903

921904
void VPWidenMemoryInstructionRecipe::print(raw_ostream &O, const Twine &Indent,
922905
VPSlotTracker &SlotTracker) const {
923-
O << "\"WIDEN "
924-
<< Instruction::getOpcodeName(getUnderlyingInstr()->getOpcode()) << " ";
906+
O << "\"WIDEN " << Instruction::getOpcodeName(Instr.getOpcode()) << " ";
925907

926908
bool First = true;
927909
for (VPValue *Op : operands()) {

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 17 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,6 @@ struct VPTransformState {
282282
// delegates the call to ILV below.
283283
if (Data.PerPartOutput.count(Def)) {
284284
auto *VecPart = Data.PerPartOutput[Def][Instance.Part];
285-
if (!VecPart->getType()->isVectorTy()) {
286-
assert(Instance.Lane == 0 && "cannot get lane > 0 for scalar");
287-
return VecPart;
288-
}
289285
// TODO: Cache created scalar values.
290286
return Builder.CreateExtractElement(VecPart,
291287
Builder.getInt32(Instance.Lane));
@@ -302,7 +298,6 @@ struct VPTransformState {
302298
}
303299
Data.PerPartOutput[Def][Part] = V;
304300
}
305-
void set(VPValue *Def, Value *IRDef, Value *V, unsigned Part);
306301

307302
/// Hold state information used when constructing the CFG of the output IR,
308303
/// traversing the VPBasicBlocks and generating corresponding IR BasicBlocks.
@@ -689,20 +684,6 @@ class VPRecipeBase : public ilist_node_with_parent<VPRecipeBase, VPBasicBlock> {
689684
/// Returns a pointer to a VPValue, if the recipe inherits from VPValue or
690685
/// nullptr otherwise.
691686
VPValue *toVPValue();
692-
const VPValue *toVPValue() const;
693-
694-
/// Returns the underlying instruction, if the recipe is a VPValue or nullptr
695-
/// otherwise.
696-
Instruction *getUnderlyingInstr() {
697-
if (auto *VPV = toVPValue())
698-
return cast_or_null<Instruction>(VPV->getUnderlyingValue());
699-
return nullptr;
700-
}
701-
const Instruction *getUnderlyingInstr() const {
702-
if (auto *VPV = toVPValue())
703-
return cast_or_null<Instruction>(VPV->getUnderlyingValue());
704-
return nullptr;
705-
}
706687
};
707688

708689
inline bool VPUser::classof(const VPRecipeBase *Recipe) {
@@ -744,6 +725,10 @@ class VPInstruction : public VPUser, public VPValue, public VPRecipeBase {
744725
void generateInstruction(VPTransformState &State, unsigned Part);
745726

746727
protected:
728+
Instruction *getUnderlyingInstr() {
729+
return cast_or_null<Instruction>(getUnderlyingValue());
730+
}
731+
747732
void setUnderlyingInstr(Instruction *I) { setUnderlyingValue(I); }
748733

749734
public:
@@ -1222,9 +1207,8 @@ class VPPredInstPHIRecipe : public VPRecipeBase {
12221207
/// - For store: Address, stored value, optional mask
12231208
/// TODO: We currently execute only per-part unless a specific instance is
12241209
/// provided.
1225-
class VPWidenMemoryInstructionRecipe : public VPRecipeBase,
1226-
public VPValue,
1227-
public VPUser {
1210+
class VPWidenMemoryInstructionRecipe : public VPRecipeBase, public VPUser {
1211+
Instruction &Instr;
12281212

12291213
void setMask(VPValue *Mask) {
12301214
if (!Mask)
@@ -1233,22 +1217,20 @@ class VPWidenMemoryInstructionRecipe : public VPRecipeBase,
12331217
}
12341218

12351219
bool isMasked() const {
1236-
return (isa<LoadInst>(getUnderlyingInstr()) && getNumOperands() == 2) ||
1237-
(isa<StoreInst>(getUnderlyingInstr()) && getNumOperands() == 3);
1220+
return (isa<LoadInst>(Instr) && getNumOperands() == 2) ||
1221+
(isa<StoreInst>(Instr) && getNumOperands() == 3);
12381222
}
12391223

12401224
public:
12411225
VPWidenMemoryInstructionRecipe(LoadInst &Load, VPValue *Addr, VPValue *Mask)
1242-
: VPRecipeBase(VPWidenMemoryInstructionSC),
1243-
VPValue(VPValue::VPMemoryInstructionSC, &Load), VPUser({Addr}) {
1226+
: VPRecipeBase(VPWidenMemoryInstructionSC), VPUser({Addr}), Instr(Load) {
12441227
setMask(Mask);
12451228
}
12461229

12471230
VPWidenMemoryInstructionRecipe(StoreInst &Store, VPValue *Addr,
12481231
VPValue *StoredValue, VPValue *Mask)
1249-
: VPRecipeBase(VPWidenMemoryInstructionSC),
1250-
VPValue(VPValue::VPMemoryInstructionSC, &Store),
1251-
VPUser({Addr, StoredValue}) {
1232+
: VPRecipeBase(VPWidenMemoryInstructionSC), VPUser({Addr, StoredValue}),
1233+
Instr(Store) {
12521234
setMask(Mask);
12531235
}
12541236

@@ -1271,7 +1253,7 @@ class VPWidenMemoryInstructionRecipe : public VPRecipeBase,
12711253

12721254
/// Return the address accessed by this recipe.
12731255
VPValue *getStoredValue() const {
1274-
assert(isa<StoreInst>(getUnderlyingInstr()) &&
1256+
assert(isa<StoreInst>(Instr) &&
12751257
"Stored value only available for store instructions");
12761258
return getOperand(1); // Stored value is the 2nd, mandatory operand.
12771259
}
@@ -1637,10 +1619,6 @@ class VPlan {
16371619
/// VPlan.
16381620
Value2VPValueTy Value2VPValue;
16391621

1640-
/// Contains all VPValues that been allocated by addVPValue directly and need
1641-
/// to be free when the plan's destructor is called.
1642-
SmallVector<VPValue *, 16> VPValuesToFree;
1643-
16441622
/// Holds the VPLoopInfo analysis for this VPlan.
16451623
VPLoopInfo VPLInfo;
16461624

@@ -1656,8 +1634,8 @@ class VPlan {
16561634
~VPlan() {
16571635
if (Entry)
16581636
VPBlockBase::deleteCFG(Entry);
1659-
for (VPValue *VPV : VPValuesToFree)
1660-
delete VPV;
1637+
for (auto &MapEntry : Value2VPValue)
1638+
delete MapEntry.second;
16611639
if (BackedgeTakenCount)
16621640
delete BackedgeTakenCount;
16631641
for (VPValue *Def : VPExternalDefs)
@@ -1707,24 +1685,7 @@ class VPlan {
17071685
void addVPValue(Value *V) {
17081686
assert(V && "Trying to add a null Value to VPlan");
17091687
assert(!Value2VPValue.count(V) && "Value already exists in VPlan");
1710-
VPValue *VPV = new VPValue(V);
1711-
Value2VPValue[V] = VPV;
1712-
VPValuesToFree.push_back(VPV);
1713-
}
1714-
1715-
void addVPValue(Value *V, VPValue *VPV) {
1716-
assert(V && "Trying to add a null Value to VPlan");
1717-
assert(!Value2VPValue.count(V) && "Value already exists in VPlan");
1718-
Value2VPValue[V] = VPV;
1719-
}
1720-
1721-
void addOrReplaceVPValue(Value *V, VPValue *VPV) {
1722-
assert(V && "Trying to add a null Value to VPlan");
1723-
auto I = Value2VPValue.find(V);
1724-
if (I == Value2VPValue.end())
1725-
Value2VPValue[V] = VPV;
1726-
else
1727-
I->second = VPV;
1688+
Value2VPValue[V] = new VPValue(V);
17281689
}
17291690

17301691
VPValue *getVPValue(Value *V) {
@@ -1740,8 +1701,6 @@ class VPlan {
17401701
return getVPValue(V);
17411702
}
17421703

1743-
void removeVPValueFor(Value *V) { Value2VPValue.erase(V); }
1744-
17451704
/// Return the VPLoopInfo analysis for this VPlan.
17461705
VPLoopInfo &getVPLoopInfo() { return VPLInfo; }
17471706
const VPLoopInfo &getVPLoopInfo() const { return VPLInfo; }
@@ -1823,9 +1782,9 @@ class VPlanPrinter {
18231782
};
18241783

18251784
struct VPlanIngredient {
1826-
const Value *V;
1785+
Value *V;
18271786

1828-
VPlanIngredient(const Value *V) : V(V) {}
1787+
VPlanIngredient(Value *V) : V(V) {}
18291788
};
18301789

18311790
inline raw_ostream &operator<<(raw_ostream &OS, const VPlanIngredient &I) {

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class VPValue {
4343
friend class VPBasicBlock;
4444
friend class VPInterleavedAccessInfo;
4545
friend class VPSlotTracker;
46-
friend class VPRecipeBase;
4746

4847
const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
4948

@@ -78,7 +77,7 @@ class VPValue {
7877
/// are actually instantiated. Values of this enumeration are kept in the
7978
/// SubclassID field of the VPValue objects. They are used for concrete
8079
/// type identification.
81-
enum { VPValueSC, VPInstructionSC, VPMemoryInstructionSC };
80+
enum { VPValueSC, VPInstructionSC };
8281

8382
VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV) {}
8483
VPValue(const VPValue &) = delete;

0 commit comments

Comments
 (0)