Skip to content

Commit a782925

Browse files
author
git apple-llvm automerger
committed
Merge commit '4560dbd4ceea' from apple/main into swift/next
2 parents 063b391 + 4560dbd commit a782925

File tree

6 files changed

+82
-5
lines changed

6 files changed

+82
-5
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,15 @@ VPBlockBase *VPBlockBase::getEnclosingBlockWithPredecessors() {
181181

182182
void VPBlockBase::deleteCFG(VPBlockBase *Entry) {
183183
SmallVector<VPBlockBase *, 8> Blocks;
184-
for (VPBlockBase *Block : depth_first(Entry))
184+
185+
VPValue DummyValue;
186+
for (VPBlockBase *Block : depth_first(Entry)) {
187+
// Drop all references in VPBasicBlocks and replace all uses with
188+
// DummyValue.
189+
if (auto *VPBB = dyn_cast<VPBasicBlock>(Block))
190+
VPBB->dropAllReferences(&DummyValue);
185191
Blocks.push_back(Block);
192+
}
186193

187194
for (VPBlockBase *Block : Blocks)
188195
delete Block;
@@ -305,6 +312,17 @@ void VPBasicBlock::execute(VPTransformState *State) {
305312
LLVM_DEBUG(dbgs() << "LV: filled BB:" << *NewBB);
306313
}
307314

315+
void VPBasicBlock::dropAllReferences(VPValue *NewValue) {
316+
for (VPRecipeBase &R : Recipes) {
317+
if (auto *VPV = R.toVPValue())
318+
VPV->replaceAllUsesWith(NewValue);
319+
320+
if (auto *User = R.toVPUser())
321+
for (unsigned I = 0, E = User->getNumOperands(); I != E; I++)
322+
User->setOperand(I, NewValue);
323+
}
324+
}
325+
308326
void VPRegionBlock::execute(VPTransformState *State) {
309327
ReversePostOrderTraversal<VPBlockBase *> RPOT(Entry);
310328

@@ -376,6 +394,12 @@ void VPRecipeBase::removeFromParent() {
376394
Parent = nullptr;
377395
}
378396

397+
VPValue *VPRecipeBase::toVPValue() {
398+
if (auto *V = dyn_cast<VPInstruction>(this))
399+
return V;
400+
return nullptr;
401+
}
402+
379403
iplist<VPRecipeBase>::iterator VPRecipeBase::eraseFromParent() {
380404
assert(getParent() && "Recipe not in any VPBasicBlock");
381405
return getParent()->getRecipeList().erase(getIterator());

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@ class VPRecipeBase : public ilist_node_with_parent<VPRecipeBase, VPBasicBlock> {
680680
/// Returns a pointer to a VPUser, if the recipe inherits from VPUser or
681681
/// nullptr otherwise.
682682
VPUser *toVPUser();
683+
684+
/// Returns a pointer to a VPValue, if the recipe inherits from VPValue or
685+
/// nullptr otherwise.
686+
VPValue *toVPValue();
683687
};
684688

685689
inline bool VPUser::classof(const VPRecipeBase *Recipe) {
@@ -1362,6 +1366,10 @@ class VPBasicBlock : public VPBlockBase {
13621366
/// this VPBasicBlock, thereby "executing" the VPlan.
13631367
void execute(struct VPTransformState *State) override;
13641368

1369+
/// Replace all operands of VPUsers in the block with \p NewValue and also
1370+
/// replaces all uses of VPValues defined in the block with NewValue.
1371+
void dropAllReferences(VPValue *NewValue);
1372+
13651373
private:
13661374
/// Create an IR BasicBlock to hold the output instructions generated by this
13671375
/// VPBasicBlock, and return it. Update the CFGState accordingly.
@@ -2006,10 +2014,7 @@ class VPlanSlp {
20062014
public:
20072015
VPlanSlp(VPInterleavedAccessInfo &IAI, VPBasicBlock &BB) : IAI(IAI), BB(BB) {}
20082016

2009-
~VPlanSlp() {
2010-
for (auto &KV : BundleToCombined)
2011-
delete KV.second;
2012-
}
2017+
~VPlanSlp() = default;
20132018

20142019
/// Tries to build an SLP tree rooted at \p Operands and returns a
20152020
/// VPInstruction combining \p Operands, if they can be combined.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void VPlanTransforms::VPInstructionsToVPRecipes(
3535
Plan->addCBV(NCondBit);
3636
}
3737
}
38+
VPValue DummyValue;
3839
for (VPBlockBase *Base : RPOT) {
3940
// Do not widen instructions in pre-header and exit blocks.
4041
if (Base->getNumPredecessors() == 0 || Base->getNumSuccessors() == 0)
@@ -48,6 +49,7 @@ void VPlanTransforms::VPInstructionsToVPRecipes(
4849
VPInstruction *VPInst = cast<VPInstruction>(Ingredient);
4950
Instruction *Inst = cast<Instruction>(VPInst->getUnderlyingValue());
5051
if (DeadInstructions.count(Inst)) {
52+
VPInst->replaceAllUsesWith(&DummyValue);
5153
Ingredient->eraseFromParent();
5254
continue;
5355
}
@@ -77,6 +79,7 @@ void VPlanTransforms::VPInstructionsToVPRecipes(
7779
new VPWidenRecipe(*Inst, Plan->mapToVPValues(Inst->operands()));
7880

7981
NewRecipe->insertBefore(Ingredient);
82+
VPInst->replaceAllUsesWith(&DummyValue);
8083
Ingredient->eraseFromParent();
8184
}
8285
}

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ class VPUser {
168168

169169
VPUser(const VPUser &) = delete;
170170
VPUser &operator=(const VPUser &) = delete;
171+
virtual ~VPUser() {
172+
for (VPValue *Op : operands())
173+
Op->removeUser(*this);
174+
}
171175

172176
void addOperand(VPValue *Operand) {
173177
Operands.push_back(Operand);

llvm/unittests/Transforms/Vectorize/VPlanSlpTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ TEST_F(VPlanSlpTest, testSlpSimple_2) {
116116
auto *CombinedLoadB = cast<VPInstruction>(CombinedAdd->getOperand(1));
117117
EXPECT_EQ(VPInstruction::SLPLoad, CombinedLoadA->getOpcode());
118118
EXPECT_EQ(VPInstruction::SLPLoad, CombinedLoadB->getOpcode());
119+
120+
delete CombinedStore;
121+
delete CombinedAdd;
122+
delete CombinedLoadA;
123+
delete CombinedLoadB;
119124
}
120125

121126
TEST_F(VPlanSlpTest, testSlpSimple_3) {
@@ -190,6 +195,11 @@ TEST_F(VPlanSlpTest, testSlpSimple_3) {
190195
VPInstruction *GetB = cast<VPInstruction>(&*std::next(Body->begin(), 3));
191196
EXPECT_EQ(GetA, CombinedLoadA->getOperand(0));
192197
EXPECT_EQ(GetB, CombinedLoadB->getOperand(0));
198+
199+
delete CombinedStore;
200+
delete CombinedAdd;
201+
delete CombinedLoadA;
202+
delete CombinedLoadB;
193203
}
194204

195205
TEST_F(VPlanSlpTest, testSlpReuse_1) {
@@ -249,6 +259,10 @@ TEST_F(VPlanSlpTest, testSlpReuse_1) {
249259
auto *CombinedLoadA = cast<VPInstruction>(CombinedAdd->getOperand(0));
250260
EXPECT_EQ(CombinedLoadA, CombinedAdd->getOperand(1));
251261
EXPECT_EQ(VPInstruction::SLPLoad, CombinedLoadA->getOpcode());
262+
263+
delete CombinedStore;
264+
delete CombinedAdd;
265+
delete CombinedLoadA;
252266
}
253267

254268
TEST_F(VPlanSlpTest, testSlpReuse_2) {
@@ -355,6 +369,15 @@ static void checkReorderExample(VPInstruction *Store1, VPInstruction *Store2,
355369
VPInstruction *LoadvD1 = cast<VPInstruction>(&*std::next(Body->begin(), 19));
356370
EXPECT_EQ(LoadvD0->getOperand(0), CombinedLoadD->getOperand(0));
357371
EXPECT_EQ(LoadvD1->getOperand(0), CombinedLoadD->getOperand(1));
372+
373+
delete CombinedStore;
374+
delete CombinedAdd;
375+
delete CombinedMulAB;
376+
delete CombinedMulCD;
377+
delete CombinedLoadA;
378+
delete CombinedLoadB;
379+
delete CombinedLoadC;
380+
delete CombinedLoadD;
358381
}
359382

360383
TEST_F(VPlanSlpTest, testSlpReorder_1) {

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ TEST(VPInstructionTest, replaceAllUsesWith) {
179179
delete VPV3;
180180
}
181181

182+
TEST(VPInstructionTest, releaseOperandsAtDeletion) {
183+
VPValue *VPV1 = new VPValue();
184+
VPValue *VPV2 = new VPValue();
185+
VPInstruction *I1 = new VPInstruction(0, {VPV1, VPV2});
186+
187+
EXPECT_EQ(1u, VPV1->getNumUsers());
188+
EXPECT_EQ(I1, *VPV1->user_begin());
189+
EXPECT_EQ(1u, VPV2->getNumUsers());
190+
EXPECT_EQ(I1, *VPV2->user_begin());
191+
192+
delete I1;
193+
194+
EXPECT_EQ(0u, VPV1->getNumUsers());
195+
EXPECT_EQ(0u, VPV2->getNumUsers());
196+
197+
delete VPV1;
198+
delete VPV2;
199+
}
182200
TEST(VPBasicBlockTest, getPlan) {
183201
{
184202
VPBasicBlock *VPBB1 = new VPBasicBlock();

0 commit comments

Comments
 (0)