Skip to content

Commit 7ba864b

Browse files
[SandboxVectorizer] Register erase callback for seed collection (llvm#115951)
1 parent b0a4e95 commit 7ba864b

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/SeedCollector.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ class SeedContainer {
223223
/// Note that the bundles themselves may have additional ordering, created
224224
/// by the subclasses by insertAt. The bundles themselves may also have used
225225
/// instructions.
226+
227+
// TODO: Range_size counts fully used-bundles. Further, iterating over
228+
// anything other than the Bundles in a SeedContainer includes used
229+
// seeds. Rework the iterator logic to clean this up.
226230
iterator(BundleMapT &Map, BundleMapT::iterator MapIt, ValT *Vec, int VecIdx)
227231
: Map(&Map), MapIt(MapIt), Vec(Vec), VecIdx(VecIdx) {}
228232
value_type &operator*() {
@@ -288,7 +292,7 @@ class SeedCollector {
288292
SeedContainer StoreSeeds;
289293
SeedContainer LoadSeeds;
290294
Context &Ctx;
291-
295+
Context::CallbackID EraseCallbackID;
292296
/// \Returns the number of SeedBundle groups for all seed types.
293297
/// This is to be used for limiting compilation time.
294298
unsigned totalNumSeedGroups() const {

llvm/lib/Transforms/Vectorize/SandboxVectorizer/SeedCollector.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,19 @@ template bool isValidMemSeed<StoreInst>(StoreInst *LSI);
159159

160160
SeedCollector::SeedCollector(BasicBlock *BB, ScalarEvolution &SE)
161161
: StoreSeeds(SE), LoadSeeds(SE), Ctx(BB->getContext()) {
162-
// TODO: Register a callback for updating the Collector data structures upon
163-
// instr removal
164162

165163
bool CollectStores = CollectSeeds.find(StoreSeedsDef) != std::string::npos;
166164
bool CollectLoads = CollectSeeds.find(LoadSeedsDef) != std::string::npos;
167165
if (!CollectStores && !CollectLoads)
168166
return;
167+
168+
EraseCallbackID = Ctx.registerEraseInstrCallback([this](Instruction *I) {
169+
if (auto SI = dyn_cast<StoreInst>(I))
170+
StoreSeeds.erase(SI);
171+
else if (auto LI = dyn_cast<LoadInst>(I))
172+
LoadSeeds.erase(LI);
173+
});
174+
169175
// Actually collect the seeds.
170176
for (auto &I : *BB) {
171177
if (StoreInst *SI = dyn_cast<StoreInst>(&I))
@@ -181,8 +187,7 @@ SeedCollector::SeedCollector(BasicBlock *BB, ScalarEvolution &SE)
181187
}
182188

183189
SeedCollector::~SeedCollector() {
184-
// TODO: Unregister the callback for updating the seed datastructures upon
185-
// instr removal
190+
Ctx.unregisterEraseInstrCallback(EraseCallbackID);
186191
}
187192

188193
#ifndef NDEBUG

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/SeedCollectorTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,22 @@ define void @foo(ptr noalias %ptr, float %val) {
374374
// Expect just one vector of store seeds
375375
EXPECT_EQ(range_size(StoreSeedsRange), 1u);
376376
ExpectThatElementsAre(SB, {St0, St1, St2, St3});
377+
// Check that the EraseInstr callback works.
378+
379+
// TODO: Range_size counts fully used-bundles even though the iterator skips
380+
// them. Further, iterating over anything other than the Bundles in a
381+
// SeedContainer includes used seeds. So for now just check that removing all
382+
// the seeds from a bundle also empties the bundle.
383+
St0->eraseFromParent();
384+
St1->eraseFromParent();
385+
St2->eraseFromParent();
386+
St3->eraseFromParent();
387+
size_t nonEmptyBundleCount = 0;
388+
for (auto &B : SC.getStoreSeeds()) {
389+
(void)B;
390+
nonEmptyBundleCount++;
391+
}
392+
EXPECT_EQ(nonEmptyBundleCount, 0u);
377393
}
378394

379395
TEST_F(SeedBundleTest, VectorStores) {

0 commit comments

Comments
 (0)