20#define DEBUG_TYPE "llvm-mca"
22void Scheduler::initializeStrategy(std::unique_ptr<SchedulerStrategy> S) {
24 Strategy = S ? std::move(S) : std::make_unique<DefaultSchedulerStrategy>();
33 dbgs() <<
"[SCHEDULER]: WaitSet size is: " << WaitSet.size() <<
'\n';
34 dbgs() <<
"[SCHEDULER]: ReadySet size is: " << ReadySet.size() <<
'\n';
35 dbgs() <<
"[SCHEDULER]: IssuedSet size is: " << IssuedSet.size() <<
'\n';
42 Resources->canBeDispatched(
IR.getInstruction()->getUsedBuffers());
57 Resources->checkAvailability(
IR.getInstruction()->getDesc())) {
78void Scheduler::issueInstructionImpl(
80 SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &UsedResources) {
90 IS->execute(
IR.getSourceIndex());
92 IS->computeCriticalRegDep();
98 IS->setCriticalMemDep(MemDep);
101 if (IS->isExecuting())
102 IssuedSet.emplace_back(
IR);
103 else if (IS->isExecuted())
110 SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &UsedResources,
115 HasDependentUsers |= Inst.
isMemOp() && LSU.hasDependentUsers(
IR);
118 issueInstructionImpl(
IR, UsedResources);
123 if (HasDependentUsers)
124 if (promoteToPendingSet(PendingInstructions))
125 promoteToReadySet(ReadyInstructions);
131 unsigned PromotedElements = 0;
132 for (
auto I = PendingSet.begin(),
E = PendingSet.end();
I !=
E;) {
139 if (!IS.isReady() && !IS.updatePending()) {
150 <<
" promoted to the READY set.\n");
152 Ready.emplace_back(
IR);
153 ReadySet.emplace_back(
IR);
157 std::iter_swap(
I,
E - PromotedElements);
160 PendingSet.resize(PendingSet.size() - PromotedElements);
161 return PromotedElements;
164bool Scheduler::promoteToPendingSet(SmallVectorImpl<InstRef> &Pending) {
167 unsigned RemovedElements = 0;
168 for (
auto I = WaitSet.begin(),
E = WaitSet.end();
I !=
E;) {
176 if (
IS.isDispatched() && !
IS.updateDispatched()) {
181 if (
IS.isMemOp() && LSU.isWaiting(
IR)) {
187 <<
" promoted to the PENDING set.\n");
189 Pending.emplace_back(
IR);
190 PendingSet.emplace_back(
IR);
194 std::iter_swap(
I,
E - RemovedElements);
197 WaitSet.resize(WaitSet.size() - RemovedElements);
198 return RemovedElements;
202 unsigned QueueIndex = ReadySet.size();
203 for (
unsigned I = 0,
E = ReadySet.size();
I !=
E; ++
I) {
205 if (QueueIndex == ReadySet.size() ||
206 Strategy->compare(
IR, ReadySet[QueueIndex])) {
208 uint64_t BusyResourceMask = Resources->checkAvailability(IS.getDesc());
209 if (BusyResourceMask)
210 IS.setCriticalResourceMask(BusyResourceMask);
211 BusyResourceUnits |= BusyResourceMask;
212 if (!BusyResourceMask)
217 if (QueueIndex == ReadySet.size())
222 std::swap(ReadySet[QueueIndex], ReadySet[ReadySet.size() - 1]);
228 unsigned RemovedElements = 0;
229 for (
auto I = IssuedSet.begin(),
E = IssuedSet.end();
I !=
E;) {
234 if (!IS.isExecuted()) {
236 <<
" is still executing.\n");
246 std::iter_swap(
I,
E - RemovedElements);
249 IssuedSet.resize(IssuedSet.size() - RemovedElements);
254 return BusyResourceUnits;
259 const auto EndIt = PendingSet.end() - NumDispatchedToThePendingSet;
262 if (Resources->checkAvailability(IS.getDesc()))
265 if (IS.isMemOp() && LSU.isPending(
IR))
280 Resources->cycleEvent(Freed);
283 IR.getInstruction()->cycleEvent();
284 updateIssuedSet(Executed);
287 IR.getInstruction()->cycleEvent();
290 IR.getInstruction()->cycleEvent();
292 promoteToPendingSet(Pending);
293 promoteToReadySet(Ready);
295 NumDispatchedToThePendingSet = 0;
296 BusyResourceUnits = 0;
301 if (
Desc.isZeroLatency())
306 return Desc.MustIssueImmediately;
311 Resources->reserveBuffers(IS.getUsedBuffers());
315 IS.setLSUTokenID(LSU.dispatch(
IR));
317 if (IS.isDispatched() || (IS.isMemOp() && LSU.isWaiting(
IR))) {
319 WaitSet.push_back(
IR);
323 if (IS.isPending() || (IS.isMemOp() && LSU.isPending(
IR))) {
325 <<
" to the PendingSet\n");
326 PendingSet.push_back(
IR);
327 ++NumDispatchedToThePendingSet;
331 assert(IS.isReady() && (!IS.isMemOp() || LSU.isReady(
IR)) &&
332 "Unexpected internal state found!");
343 ReadySet.push_back(
IR);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
DXIL Remove Unused Resources
Legalize the Machine IR a function s Machine IR
A scheduler for Processor Resource Units and Processor Resource Groups.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
~DefaultSchedulerStrategy() override
An InstRef contains both a SourceMgr index and Instruction pair.
bool hasDependentUsers() const
An instruction propagated through the simulated instruction pipeline.
uint64_t getUsedBuffers() const
virtual bool isReady(const InstRef &IR) const =0
Check if a peviously dispatched instruction IR is now ready for execution.
virtual void onInstructionExecuted(const InstRef &IR)=0
virtual const CriticalDependency getCriticalPredecessor(unsigned GroupId)=0
virtual void onInstructionIssued(const InstRef &IR)=0
virtual ~SchedulerStrategy()
@ SC_DISPATCH_GROUP_STALL
LLVM_ABI InstRef select()
Select the next instruction to issue from the ReadySet.
LLVM_ABI void issueInstruction(InstRef &IR, SmallVectorImpl< std::pair< ResourceRef, ReleaseAtCycles > > &Used, SmallVectorImpl< InstRef > &Pending, SmallVectorImpl< InstRef > &Ready)
Issue an instruction and populates a vector of used pipeline resources, and a vector of instructions ...
LLVM_ABI Status isAvailable(const InstRef &IR)
Check if the instruction in 'IR' can be dispatched during this cycle.
LLVM_ABI void analyzeDataDependencies(SmallVectorImpl< InstRef > &RegDeps, SmallVectorImpl< InstRef > &MemDeps)
This method is called by the ExecuteStage at the end of each cycle to identify bottlenecks caused by ...
LLVM_ABI bool dispatch(InstRef &IR)
Reserves buffer and LSUnit queue resources that are necessary to issue this instruction.
LLVM_ABI void cycleEvent(SmallVectorImpl< ResourceRef > &Freed, SmallVectorImpl< InstRef > &Executed, SmallVectorImpl< InstRef > &Pending, SmallVectorImpl< InstRef > &Ready)
This routine notifies the Scheduler that a new cycle just started.
LLVM_ABI bool mustIssueImmediately(const InstRef &IR) const
Returns true if IR has to be issued immediately, or if IR is a zero latency instruction.
LLVM_ABI uint64_t analyzeResourcePressure(SmallVectorImpl< InstRef > &Insts)
Returns a mask of busy resources, and populates vector Insts with instructions that could not be issu...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ResourceStateEvent
Used to notify the internal state of a processor resource.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
A critical data dependency descriptor.
An instruction descriptor.