Skip to content

Commit 2ff80d2

Browse files
committed
[SandboxVec][Scheduler] Fix reassignment of SchedBundle to DGNode
When assigning a bundle to a DAG Node that is already assigned to a SchedBundle we need to remove the node from the old bundle.
1 parent 1ca93b1 commit 2ff80d2

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class DGNode {
101101
/// The scheduler bundle that this node belongs to.
102102
SchedBundle *SB = nullptr;
103103

104-
void setSchedBundle(SchedBundle &SB) { this->SB = &SB; }
104+
void setSchedBundle(SchedBundle &SB);
105105
void clearSchedBundle() { this->SB = nullptr; }
106106
friend class SchedBundle; // For setSchedBundle(), clearSchedBundle().
107107

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ class SchedBundle {
115115
void eraseFromBundle(DGNode *N) {
116116
Nodes.erase(std::remove(Nodes.begin(), Nodes.end(), N), Nodes.end());
117117
}
118-
friend DGNode::~DGNode(); // For eraseFromBundle().
118+
friend void DGNode::setSchedBundle(SchedBundle &); // For eraseFromBunde().
119+
friend DGNode::~DGNode(); // For eraseFromBundle().
119120

120121
public:
121122
SchedBundle() = default;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ bool PredIterator::operator==(const PredIterator &Other) const {
7171
return OpIt == Other.OpIt && MemIt == Other.MemIt;
7272
}
7373

74+
void DGNode::setSchedBundle(SchedBundle &SB) {
75+
if (this->SB != nullptr)
76+
this->SB->eraseFromBundle(this);
77+
this->SB = &SB;
78+
}
79+
7480
DGNode::~DGNode() {
7581
if (SB == nullptr)
7682
return;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,37 @@ define void @foo(ptr %ptr, i8 %v0, i8 %v1, i8 %v2, i8 %v3) {
186186
}
187187
}
188188

189+
// Check that assigning a bundle to a DAG Node that is already assigned to a
190+
// bundle, removes the node from the old bundle.
191+
TEST_F(SchedulerTest, SchedBundleReassign) {
192+
parseIR(C, R"IR(
193+
define void @foo(ptr %ptr, i8 %v0, i8 %v1, i8 %v2) {
194+
store i8 %v0, ptr %ptr
195+
store i8 %v1, ptr %ptr
196+
store i8 %v2, ptr %ptr
197+
ret void
198+
}
199+
)IR");
200+
llvm::Function *LLVMF = &*M->getFunction("foo");
201+
sandboxir::Context Ctx(C);
202+
auto *F = Ctx.createFunction(LLVMF);
203+
auto *BB = &*F->begin();
204+
auto It = BB->begin();
205+
auto *S0 = cast<sandboxir::StoreInst>(&*It++);
206+
auto *S1 = cast<sandboxir::StoreInst>(&*It++);
207+
auto *S2 = cast<sandboxir::StoreInst>(&*It++);
208+
209+
sandboxir::DependencyGraph DAG(getAA(*LLVMF), Ctx);
210+
DAG.extend({&*BB->begin(), BB->getTerminator()});
211+
auto *SN0 = DAG.getNode(S0);
212+
auto *SN1 = DAG.getNode(S1);
213+
auto *SN2 = DAG.getNode(S2);
214+
sandboxir::SchedBundle BndlOld({SN0, SN1});
215+
sandboxir::SchedBundle BndlNew({SN0, SN2});
216+
EXPECT_THAT(BndlOld, testing::ElementsAre(SN1));
217+
EXPECT_THAT(BndlNew, testing::ElementsAre(SN0, SN2));
218+
}
219+
189220
TEST_F(SchedulerTest, Basic) {
190221
parseIR(C, R"IR(
191222
define void @foo(ptr %ptr, i8 %v0, i8 %v1) {

0 commit comments

Comments
 (0)