[llvm] [SandboxVec][Scheduler] Use SchedulingPoint for pointing to scheduling front (PR #208373)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 8 20:59:13 PDT 2026
https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/208373
The scheduling point is the last instruction scheduled. If this is the first time scheduling and we are scheduling top-down then we may need to point before the beginning of the basic block. This is why a BasicBlock::iterator does not work well here.
Until now we were using the first top-most instruction for this, which is inconsistent.
>From 4bae616b6fb4fba55a2c16e18fb7b71f4f5b847e Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vasileios.porpodas at amd.com>
Date: Wed, 24 Jun 2026 23:26:11 +0000
Subject: [PATCH] [SandboxVec][Scheduler] Use SchedulingPoint for pointing to
scheduling front
The scheduling point is the last instruction scheduled.
If we this is the first time scheduling and we are scheduling top-down
then we may need to point before the beginning of the basic block.
This is why a BasicBlock::iterator does not work well.
Until now we were using the first top-most instruction for this, which
is inconsistent.
---
.../Vectorize/SandboxVectorizer/Scheduler.h | 72 +++++++++++++++++--
.../Vectorize/SandboxVectorizer/Scheduler.cpp | 27 +++++--
2 files changed, 90 insertions(+), 9 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Scheduler.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Scheduler.h
index 86ab5fb8b464b..25ad7fbc14558 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Scheduler.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Scheduler.h
@@ -25,6 +25,7 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h"
#include <queue>
+#include <variant>
namespace llvm::sandboxir {
@@ -170,6 +171,67 @@ class SchedBundle {
#endif
};
+/// The scheduling point points to the top-of-schedule (i.e., the top-most
+/// instruction of the top bundle) during bottom-up scheduling or the bottom of
+/// the schedule (i.e., the bottom-most instruction of the bottom bundle) during
+/// top-down.
+/// This points to the top-most instruction scheduled in the last scheduling
+/// attempt in bottom-up scheduling. If this is the first scheduling attempt
+/// we need the scheduling front to still point to a hypothetical last
+/// scheduling point. In bottom-up this can be at BB.end() but in top-down this
+/// can be before BB.begin(). This is why a BasicBlock::iterator is not
+/// suitable for this.
+class SchedulingPoint {
+ /// If Where contains a Block, then we are pointing before BB.begin(),
+ /// otherwise if it contains an iterator then we point to anywhere in the BB
+ /// or at BB.end().
+ std::variant<BasicBlock::iterator, BasicBlock *> Where;
+
+public:
+ SchedulingPoint(BasicBlock::iterator It) : Where(It) {}
+ SchedulingPoint(Instruction &I) : SchedulingPoint(I.getIterator()) {}
+ SchedulingPoint(BasicBlock &BB) : Where(&BB) {}
+ BasicBlock *beforeBegin() const {
+ if (std::holds_alternative<BasicBlock::iterator>(Where))
+ return nullptr;
+ return std::get<BasicBlock *>(Where);
+ }
+ BasicBlock *atEnd() const {
+ if (std::holds_alternative<BasicBlock *>(Where))
+ return nullptr;
+ auto It = std::get<BasicBlock::iterator>(Where);
+ return It == It.getNodeParent()->end() ? It.getNodeParent() : nullptr;
+ }
+ Instruction *atInstr() const {
+ assert(!beforeBegin() && !atEnd() && "Expected not before/after BB!");
+ return &*std::get<BasicBlock::iterator>(Where);
+ }
+ operator Instruction *() const { return atInstr(); }
+ BasicBlock::iterator getIterator() const {
+ assert(!beforeBegin() && "Expected in/after BB!");
+ return std::get<BasicBlock::iterator>(Where);
+ }
+ operator BasicBlock::iterator() const { return getIterator(); }
+ SchedulingPoint getNext() const {
+ assert(!atEnd() && "Expected before/in BB!");
+ if (BasicBlock *BB = beforeBegin())
+ return BB->begin();
+ return std::next(getIterator());
+ }
+ SchedulingPoint getPrev() const {
+ assert(!beforeBegin() && "Expected in/after BB!");
+ auto It = getIterator();
+ auto *BB = It.getNodeParent();
+ if (It == BB->begin())
+ return *BB;
+ return std::prev(It);
+ }
+#ifndef NDEBUG
+ void print(raw_ostream &OS) const;
+ LLVM_DUMP_METHOD void dump() const;
+#endif
+};
+
/// The list scheduler.
class Scheduler {
/// This is a list-scheduler and this is the list containing the instructions
@@ -181,10 +243,12 @@ class Scheduler {
DependencyGraph DAG;
friend class SchedulerInternalsAttorney; // For DAG.
Context &Ctx;
- /// This is the top of the schedule, i.e. the location where the scheduler
- /// is about to place the scheduled instructions. It gets updated as we
- /// schedule.
- std::optional<BasicBlock::iterator> ScheduleTopItOpt;
+ /// This is the top of the schedule during bottom-up scheduling and the bottom
+ /// of the schedule during top-down. It points to the position of the last
+ /// top-most/bottom-most instruction scheduled. It may get updated after every
+ /// trySchedule() attempt, regardless of whether scheduling succeeded or not.
+ /// It is nullopt if we have not scheduled before.
+ std::optional<SchedulingPoint> ScheduleTopItOpt;
// TODO: This is wasting memory in exchange for fast removal using a raw ptr.
DenseMap<SchedBundle *, std::unique_ptr<SchedBundle>> Bndls;
/// The BB that we are currently scheduling.
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Scheduler.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Scheduler.cpp
index 14614686c2d46..f1476582bdb27 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Scheduler.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Scheduler.cpp
@@ -76,13 +76,28 @@ void ReadyListContainer::dump() const {
dump(dbgs());
dbgs() << "\n";
}
+
+void SchedulingPoint::print(raw_ostream &OS) const {
+ if (BasicBlock *BB = beforeBegin())
+ OS << "Before begin of BB " << BB->getName();
+ else if (BasicBlock *BB = atEnd())
+ OS << "At end of BB " << BB->getName();
+ else
+ OS << "At instr: " << *atInstr();
+}
+
+void SchedulingPoint::dump() const {
+ print(dbgs());
+ dbgs() << "\n";
+}
#endif // NDEBUG
void Scheduler::scheduleAndUpdateReadyList(SchedBundle &Bndl) {
// Find where we should schedule the instructions.
assert(ScheduleTopItOpt && "Should have been set by now!");
- auto Where = Dir == SchedDirection::BottomUp ? *ScheduleTopItOpt
- : std::next(*ScheduleTopItOpt);
+ auto Where = Dir == SchedDirection::BottomUp
+ ? ScheduleTopItOpt->getIterator()
+ : ScheduleTopItOpt->getNext().getIterator();
// Move all instructions in `Bndl` to `Where`.
Bndl.cluster(Where);
// Update the last scheduled bundle.
@@ -124,7 +139,7 @@ void Scheduler::notifyCreateInstr(Instruction *I) {
// If the instruction is inserted below the top-of-schedule then we mark it as
// "scheduled".
bool IsScheduled = ScheduleTopItOpt &&
- *ScheduleTopItOpt != I->getParent()->end() &&
+ ScheduleTopItOpt->getIterator() != I->getParent()->end() &&
((Dir == SchedDirection::BottomUp &&
(*ScheduleTopItOpt.value()).comesBefore(I)) ||
(Dir == SchedDirection::TopDown &&
@@ -358,12 +373,14 @@ bool Scheduler::trySchedule(ArrayRef<Instruction *> Instrs) {
[this](Instruction *I) { return I->getParent() != ScheduledBB; }))
return false;
- auto GetSchedPoint = [](SchedDirection Dir, const auto &Instrs) {
+ auto GetSchedPoint = [](SchedDirection Dir,
+ const auto &Instrs) -> SchedulingPoint {
switch (Dir) {
case SchedDirection::BottomUp:
return std::next(VecUtils::getLowest(Instrs)->getIterator());
case SchedDirection::TopDown:
- return VecUtils::getHighest(Instrs)->getIterator();
+ return SchedulingPoint(VecUtils::getHighest(Instrs)->getIterator())
+ .getPrev();
}
llvm_unreachable("Unhandled Dir!");
};
More information about the llvm-commits
mailing list