LLVM 22.0.0git
Scheduler.h
Go to the documentation of this file.
1//===- Scheduler.h ----------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This is the bottom-up list scheduler used by the vectorizer. It is used for
10// checking the legality of vectorization and for scheduling instructions in
11// such a way that makes vectorization possible, if legal.
12//
13// The legality check is performed by `trySchedule(Instrs)`, which will try to
14// schedule the IR until all instructions in `Instrs` can be scheduled together
15// back-to-back. If this fails then it is illegal to vectorize `Instrs`.
16//
17// Internally the scheduler uses the vectorizer-specific DependencyGraph class.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_SCHEDULER_H
22#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_SCHEDULER_H
23
27#include <queue>
28
29namespace llvm::sandboxir {
30
32public:
33 bool operator()(const DGNode *N1, const DGNode *N2) {
34 // Given that the DAG does not model dependencies such that PHIs are always
35 // at the top, or terminators always at the bottom, we need to force the
36 // priority here in the comparator of the ready list container.
37 auto *I1 = N1->getInstruction();
38 auto *I2 = N2->getInstruction();
39 bool IsTerm1 = I1->isTerminator();
40 bool IsTerm2 = I2->isTerminator();
41 if (IsTerm1 != IsTerm2)
42 // Terminators have the lowest priority.
43 return IsTerm1 > IsTerm2;
44 bool IsPHI1 = isa<PHINode>(I1);
45 bool IsPHI2 = isa<PHINode>(I2);
46 if (IsPHI1 != IsPHI2)
47 // PHIs have the highest priority.
48 return IsPHI1 < IsPHI2;
49 // Otherwise rely on the instruction order.
50 return I2->comesBefore(I1);
51 }
52};
53
54/// The list holding nodes that are ready to schedule. Used by the scheduler.
56 PriorityCmp Cmp;
57 /// Control/Other dependencies are not modeled by the DAG to save memory.
58 /// These have to be modeled in the ready list for correctness.
59 /// This means that the list will hold back nodes that need to meet such
60 /// unmodeled dependencies.
61 std::priority_queue<DGNode *, std::vector<DGNode *>, PriorityCmp> List;
62
63public:
65 void insert(DGNode *N) {
66#ifndef NDEBUG
67 assert(!N->scheduled() && "Don't insert a scheduled node!");
68 auto ListCopy = List;
69 while (!ListCopy.empty()) {
70 DGNode *Top = ListCopy.top();
71 ListCopy.pop();
72 assert(Top != N && "Node already exists in ready list!");
73 }
74#endif
75 List.push(N);
76 }
78 auto *Back = List.top();
79 List.pop();
80 return Back;
81 }
82 bool empty() const { return List.empty(); }
83 void clear() { List = {}; }
84 /// \Removes \p N if found in the ready list.
85 void remove(DGNode *N) {
86 // TODO: Use a more efficient data-structure for the ready list because the
87 // priority queue does not support fast removals.
89 Keep.reserve(List.size());
90 while (!List.empty()) {
91 auto *Top = List.top();
92 List.pop();
93 if (Top == N)
94 break;
95 Keep.push_back(Top);
96 }
97 for (auto *KeepN : Keep)
98 List.push(KeepN);
99 }
100#ifndef NDEBUG
101 void dump(raw_ostream &OS) const;
102 LLVM_DUMP_METHOD void dump() const;
103#endif // NDEBUG
104};
105
106/// The nodes that need to be scheduled back-to-back in a single scheduling
107/// cycle form a SchedBundle.
109public:
111
112private:
113 ContainerTy Nodes;
114
115 /// Called by the DGNode destructor to avoid accessing freed memory.
116 void eraseFromBundle(DGNode *N) { llvm::erase(Nodes, N); }
117 friend void DGNode::setSchedBundle(SchedBundle &); // For eraseFromBunde().
118 friend DGNode::~DGNode(); // For eraseFromBundle().
119
120public:
121 SchedBundle() = default;
122 SchedBundle(ContainerTy &&Nodes) : Nodes(std::move(Nodes)) {
123 for (auto *N : this->Nodes)
124 N->setSchedBundle(*this);
125 }
126 /// Copy CTOR (unimplemented).
127 SchedBundle(const SchedBundle &Other) = delete;
128 /// Copy Assignment (unimplemented).
131 for (auto *N : this->Nodes)
132 N->clearSchedBundle();
133 }
134 bool empty() const { return Nodes.empty(); }
135 /// Singleton bundles are created when scheduling instructions temporarily to
136 /// fill in the schedule until we schedule the vector bundle. These are
137 /// non-vector bundles containing just a single instruction.
138 bool isSingleton() const { return Nodes.size() == 1u; }
139 DGNode *back() const { return Nodes.back(); }
142 iterator begin() { return Nodes.begin(); }
143 iterator end() { return Nodes.end(); }
144 const_iterator begin() const { return Nodes.begin(); }
145 const_iterator end() const { return Nodes.end(); }
146 /// \Returns the bundle node that comes before the others in program order.
147 LLVM_ABI DGNode *getTop() const;
148 /// \Returns the bundle node that comes after the others in program order.
149 LLVM_ABI DGNode *getBot() const;
150 /// Move all bundle instructions to \p Where back-to-back.
152 /// \Returns true if all nodes in the bundle are ready.
153 bool ready() const {
154 return all_of(Nodes, [](const auto *N) { return N->ready(); });
155 }
156#ifndef NDEBUG
157 void dump(raw_ostream &OS) const;
158 LLVM_DUMP_METHOD void dump() const;
159#endif
160};
161
162/// The list scheduler.
164 /// This is a list-scheduler and this is the list containing the instructions
165 /// that are ready, meaning that all their dependency successors have already
166 /// been scheduled.
167 ReadyListContainer ReadyList;
168 /// The dependency graph is used by the scheduler to determine the legal
169 /// ordering of instructions.
170 DependencyGraph DAG;
171 friend class SchedulerInternalsAttorney; // For DAG.
172 Context &Ctx;
173 /// This is the top of the schedule, i.e. the location where the scheduler
174 /// is about to place the scheduled instructions. It gets updated as we
175 /// schedule.
176 std::optional<BasicBlock::iterator> ScheduleTopItOpt;
177 // TODO: This is wasting memory in exchange for fast removal using a raw ptr.
179 /// The BB that we are currently scheduling.
180 BasicBlock *ScheduledBB = nullptr;
181 /// The ID of the callback we register with Sandbox IR.
182 std::optional<Context::CallbackID> CreateInstrCB;
183 /// Called by Sandbox IR's callback system, after \p I has been created.
184 /// NOTE: This should run after DAG's callback has run.
185 // TODO: Perhaps call DAG's notify function from within this one?
186 LLVM_ABI void notifyCreateInstr(Instruction *I);
187
188 /// \Returns a scheduling bundle containing \p Instrs.
189 SchedBundle *createBundle(ArrayRef<Instruction *> Instrs);
190 void eraseBundle(SchedBundle *SB);
191 /// Schedule nodes until we can schedule \p Instrs back-to-back.
192 bool tryScheduleUntil(ArrayRef<Instruction *> Instrs);
193 /// Schedules all nodes in \p Bndl, marks them as scheduled, updates the
194 /// UnscheduledSuccs counter of all dependency predecessors, and adds any of
195 /// them that become ready to the ready list.
196 void scheduleAndUpdateReadyList(SchedBundle &Bndl);
197 /// The scheduling state of the instructions in the bundle.
198 enum class BndlSchedState {
199 NoneScheduled, ///> No instruction in the bundle was previously scheduled.
200 AlreadyScheduled, ///> At least one instruction in the bundle belongs to a
201 /// different non-singleton scheduling bundle.
202 TemporarilyScheduled, ///> Instructions were temporarily scheduled as
203 /// singleton bundles or some of them were not
204 /// scheduled at all. None of them were in a vector
205 ///(non-singleton) bundle.
206 FullyScheduled, ///> All instrs in the bundle were previously scheduled and
207 /// were in the same SchedBundle.
208 };
209 /// \Returns whether none/some/all of \p Instrs have been scheduled.
210 LLVM_ABI BndlSchedState
211 getBndlSchedState(ArrayRef<Instruction *> Instrs) const;
212 /// Destroy the top-most part of the schedule that includes \p Instrs.
213 void trimSchedule(ArrayRef<Instruction *> Instrs);
214 /// Disable copies.
215 Scheduler(const Scheduler &) = delete;
216 Scheduler &operator=(const Scheduler &) = delete;
217
218public:
219 Scheduler(AAResults &AA, Context &Ctx) : DAG(AA, Ctx), Ctx(Ctx) {
220 // NOTE: The scheduler's callback depends on the DAG's callback running
221 // before it and updating the DAG accordingly.
222 CreateInstrCB = Ctx.registerCreateInstrCallback(
223 [this](Instruction *I) { notifyCreateInstr(I); });
224 }
226 if (CreateInstrCB)
227 Ctx.unregisterCreateInstrCallback(*CreateInstrCB);
228 }
229 /// Tries to build a schedule that includes all of \p Instrs scheduled at the
230 /// same scheduling cycle. This essentially checks that there are no
231 /// dependencies among \p Instrs. This function may involve scheduling
232 /// intermediate instructions or canceling and re-scheduling if needed.
233 /// \Returns true on success, false otherwise.
235 /// Clear the scheduler's state, including the DAG.
236 void clear() {
237 Bndls.clear();
238 // TODO: clear view once it lands.
239 DAG.clear();
240 ReadyList.clear();
241 ScheduleTopItOpt = std::nullopt;
242 ScheduledBB = nullptr;
243 assert(Bndls.empty() && DAG.empty() && ReadyList.empty() &&
244 !ScheduleTopItOpt && ScheduledBB == nullptr &&
245 "Expected empty state!");
246 }
247
248#ifndef NDEBUG
249 void dump(raw_ostream &OS) const;
250 LLVM_DUMP_METHOD void dump() const;
251#endif
252};
253
254/// A client-attorney class for accessing the Scheduler's internals (used for
255/// unit tests).
257public:
258 static DependencyGraph &getDAG(Scheduler &Sched) { return Sched.DAG; }
259 using BndlSchedState = Scheduler::BndlSchedState;
260 static BndlSchedState getBndlSchedState(const Scheduler &Sched,
262 return Sched.getBndlSchedState(Instrs);
263 }
264};
265
266} // namespace llvm::sandboxir
267
268#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_SCHEDULER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:638
#define I(x, y, z)
Definition: MD5.cpp:58
Machine Instruction Scheduler
const NodeList & List
Definition: RDFGraph.cpp:200
raw_pwrite_stream & OS
A private abstract base class describing the concept of an individual alias analysis implementation.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
bool empty() const
Definition: DenseMap.h:119
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
Iterator for Instructions in a `BasicBlock.
Definition: BasicBlock.h:24
Contains a list of sandboxir::Instruction's.
Definition: BasicBlock.h:68
LLVM_ABI CallbackID registerCreateInstrCallback(CreateInstrCallback CB)
Register a callback that gets called right after a SandboxIR instruction is created.
Definition: Context.cpp:742
LLVM_ABI void unregisterCreateInstrCallback(CallbackID ID)
Definition: Context.cpp:749
A DependencyGraph Node that points to an Instruction and contains memory dependency edges.
void setSchedBundle(SchedBundle &SB)
Instruction * getInstruction() const
bool empty() const
\Returns true if the DAG's state is clear. Used in assertions.
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:43
bool operator()(const DGNode *N1, const DGNode *N2)
Definition: Scheduler.h:33
The list holding nodes that are ready to schedule. Used by the scheduler.
Definition: Scheduler.h:55
LLVM_DUMP_METHOD void dump() const
Definition: Scheduler.cpp:63
void remove(DGNode *N)
\Removes N if found in the ready list.
Definition: Scheduler.h:85
The nodes that need to be scheduled back-to-back in a single scheduling cycle form a SchedBundle.
Definition: Scheduler.h:108
LLVM_ABI DGNode * getBot() const
\Returns the bundle node that comes after the others in program order.
Definition: Scheduler.cpp:24
SchedBundle(ContainerTy &&Nodes)
Definition: Scheduler.h:122
SchedBundle & operator=(const SchedBundle &Other)=delete
Copy Assignment (unimplemented).
LLVM_ABI DGNode * getTop() const
\Returns the bundle node that comes before the others in program order.
Definition: Scheduler.cpp:15
bool isSingleton() const
Singleton bundles are created when scheduling instructions temporarily to fill in the schedule until ...
Definition: Scheduler.h:138
bool ready() const
\Returns true if all nodes in the bundle are ready.
Definition: Scheduler.h:153
const_iterator begin() const
Definition: Scheduler.h:144
LLVM_DUMP_METHOD void dump() const
Definition: Scheduler.cpp:48
SchedBundle(const SchedBundle &Other)=delete
Copy CTOR (unimplemented).
DGNode * back() const
Definition: Scheduler.h:139
const_iterator end() const
Definition: Scheduler.h:145
LLVM_ABI void cluster(BasicBlock::iterator Where)
Move all bundle instructions to Where back-to-back.
Definition: Scheduler.cpp:33
A client-attorney class for accessing the Scheduler's internals (used for unit tests).
Definition: Scheduler.h:256
static BndlSchedState getBndlSchedState(const Scheduler &Sched, ArrayRef< Instruction * > Instrs)
Definition: Scheduler.h:260
static DependencyGraph & getDAG(Scheduler &Sched)
Definition: Scheduler.h:258
The list scheduler.
Definition: Scheduler.h:163
LLVM_DUMP_METHOD void dump() const
Definition: Scheduler.cpp:357
LLVM_ABI bool trySchedule(ArrayRef< Instruction * > Instrs)
Tries to build a schedule that includes all of Instrs scheduled at the same scheduling cycle.
Definition: Scheduler.cpp:290
void clear()
Clear the scheduler's state, including the DAG.
Definition: Scheduler.h:236
Scheduler(AAResults &AA, Context &Ctx)
Definition: Scheduler.h:219
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1744
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition: STLExtras.h:2147
@ Other
Any other memory.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
@ Keep
No function return thunk.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
#define N