Skip to content

Commit 5cb2db3

Browse files
authored
[SandboxVec][Scheduler] Forbid crossing BBs (#124369)
This patch updates the scheduler to forbid scheduling across BBs. It should eventually be able to handle this, but we disable it for now.
1 parent 21f04b1 commit 5cb2db3

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class Scheduler {
122122
std::optional<BasicBlock::iterator> ScheduleTopItOpt;
123123
// TODO: This is wasting memory in exchange for fast removal using a raw ptr.
124124
DenseMap<SchedBundle *, std::unique_ptr<SchedBundle>> Bndls;
125+
/// The BB that we are currently scheduling.
126+
BasicBlock *ScheduledBB = nullptr;
125127

126128
/// \Returns a scheduling bundle containing \p Instrs.
127129
SchedBundle *createBundle(ArrayRef<Instruction *> Instrs);
@@ -166,8 +168,10 @@ class Scheduler {
166168
DAG.clear();
167169
ReadyList.clear();
168170
ScheduleTopItOpt = std::nullopt;
171+
ScheduledBB = nullptr;
169172
assert(Bndls.empty() && DAG.empty() && ReadyList.empty() &&
170-
!ScheduleTopItOpt && "Expected empty state!");
173+
!ScheduleTopItOpt && ScheduledBB == nullptr &&
174+
"Expected empty state!");
171175
}
172176

173177
#ifndef NDEBUG

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ bool Scheduler::trySchedule(ArrayRef<Instruction *> Instrs) {
189189
[Instrs](Instruction *I) {
190190
return I->getParent() == (*Instrs.begin())->getParent();
191191
}) &&
192-
"Instrs not in the same BB!");
192+
"Instrs not in the same BB, should have been rejected by Legality!");
193+
if (ScheduledBB == nullptr)
194+
ScheduledBB = Instrs[0]->getParent();
195+
// We don't support crossing BBs for now.
196+
if (any_of(Instrs,
197+
[this](Instruction *I) { return I->getParent() != ScheduledBB; }))
198+
return false;
193199
auto SchedState = getBndlSchedState(Instrs);
194200
switch (SchedState) {
195201
case BndlSchedState::FullyScheduled:

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ struct SchedulerTest : public testing::Test {
5151
}
5252
};
5353

54+
static sandboxir::BasicBlock *getBasicBlockByName(sandboxir::Function *F,
55+
StringRef Name) {
56+
for (sandboxir::BasicBlock &BB : *F)
57+
if (BB.getName() == Name)
58+
return &BB;
59+
llvm_unreachable("Expected to find basic block!");
60+
}
61+
5462
TEST_F(SchedulerTest, SchedBundle) {
5563
parseIR(C, R"IR(
5664
define void @foo(ptr %ptr, i8 %v0, i8 %v1) {
@@ -237,3 +245,47 @@ define void @foo(ptr noalias %ptr0, ptr noalias %ptr1) {
237245
EXPECT_TRUE(Sched.trySchedule({Add0, Add1}));
238246
EXPECT_TRUE(Sched.trySchedule({L0, L1}));
239247
}
248+
249+
TEST_F(SchedulerTest, DontCrossBBs) {
250+
parseIR(C, R"IR(
251+
define void @foo(ptr noalias %ptr0, ptr noalias %ptr1, i8 %v0, i8 %v1) {
252+
bb0:
253+
%add0 = add i8 %v0, 0
254+
%add1 = add i8 %v1, 1
255+
br label %bb1
256+
bb1:
257+
store i8 %add0, ptr %ptr0
258+
store i8 %add1, ptr %ptr1
259+
ret void
260+
}
261+
)IR");
262+
llvm::Function *LLVMF = &*M->getFunction("foo");
263+
sandboxir::Context Ctx(C);
264+
auto *F = Ctx.createFunction(LLVMF);
265+
auto *BB0 = getBasicBlockByName(F, "bb0");
266+
auto *BB1 = getBasicBlockByName(F, "bb1");
267+
auto It = BB0->begin();
268+
auto *Add0 = &*It++;
269+
auto *Add1 = &*It++;
270+
271+
It = BB1->begin();
272+
auto *S0 = cast<sandboxir::StoreInst>(&*It++);
273+
auto *S1 = cast<sandboxir::StoreInst>(&*It++);
274+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
275+
276+
{
277+
// Schedule bottom-up
278+
sandboxir::Scheduler Sched(getAA(*LLVMF), Ctx);
279+
EXPECT_TRUE(Sched.trySchedule({Ret}));
280+
EXPECT_TRUE(Sched.trySchedule({S0, S1}));
281+
// Scheduling across blocks should fail.
282+
EXPECT_FALSE(Sched.trySchedule({Add0, Add1}));
283+
}
284+
{
285+
// Schedule top-down
286+
sandboxir::Scheduler Sched(getAA(*LLVMF), Ctx);
287+
EXPECT_TRUE(Sched.trySchedule({Add0, Add1}));
288+
// Scheduling across blocks should fail.
289+
EXPECT_FALSE(Sched.trySchedule({S0, S1}));
290+
}
291+
}

0 commit comments

Comments
 (0)