blob: 04899c35e78c9e3049fc105a2e43ef643f7b4ecf [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===--------------------- Scheduler.cpp ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// A scheduler for processor resource units and processor resource groups.
11//
12//===----------------------------------------------------------------------===//
13
Andrea Di Biagio51dba7d2018-03-23 17:36:07 +000014#include "Scheduler.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000015#include "Backend.h"
Clement Courbet844f22d2018-03-13 13:11:01 +000016#include "HWEventListener.h"
Andrea Di Biagio4704f032018-03-20 12:25:54 +000017#include "Support.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000018#include "llvm/Support/Debug.h"
19#include "llvm/Support/raw_ostream.h"
20
21#define DEBUG_TYPE "llvm-mca"
22
23namespace mca {
24
25using namespace llvm;
26
27uint64_t ResourceState::selectNextInSequence() {
28 assert(isReady());
29 uint64_t Next = getNextInSequence();
30 while (!isSubResourceReady(Next)) {
31 updateNextInSequence();
32 Next = getNextInSequence();
33 }
34 return Next;
35}
36
37#ifndef NDEBUG
38void ResourceState::dump() const {
39 dbgs() << "MASK: " << ResourceMask << ", SIZE_MASK: " << ResourceSizeMask
40 << ", NEXT: " << NextInSequenceMask << ", RDYMASK: " << ReadyMask
41 << ", BufferSize=" << BufferSize
42 << ", AvailableSlots=" << AvailableSlots
43 << ", Reserved=" << Unavailable << '\n';
44}
45#endif
46
Andrea Di Biagio4704f032018-03-20 12:25:54 +000047void ResourceManager::initialize(const llvm::MCSchedModel &SM) {
48 computeProcResourceMasks(SM, ProcResID2Mask);
49 for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I)
50 addResource(*SM.getProcResource(I), I, ProcResID2Mask[I]);
51}
52
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000053// Adds a new resource state in Resources, as well as a new descriptor in
54// ResourceDescriptor. Map 'Resources' allows to quickly obtain ResourceState
55// objects from resource mask identifiers.
56void ResourceManager::addResource(const MCProcResourceDesc &Desc,
Andrea Di Biagioe1a1da12018-03-13 13:58:02 +000057 unsigned Index, uint64_t Mask) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000058 assert(Resources.find(Mask) == Resources.end() && "Resource already added!");
Andrea Di Biagio0c541292018-03-10 16:55:07 +000059 Resources[Mask] = llvm::make_unique<ResourceState>(Desc, Index, Mask);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000060}
61
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000062// Returns the actual resource consumed by this Use.
63// First, is the primary resource ID.
64// Second, is the specific sub-resource ID.
65std::pair<uint64_t, uint64_t> ResourceManager::selectPipe(uint64_t ResourceID) {
66 ResourceState &RS = *Resources[ResourceID];
67 uint64_t SubResourceID = RS.selectNextInSequence();
68 if (RS.isAResourceGroup())
69 return selectPipe(SubResourceID);
70 return std::pair<uint64_t, uint64_t>(ResourceID, SubResourceID);
71}
72
73void ResourceState::removeFromNextInSequence(uint64_t ID) {
74 assert(NextInSequenceMask);
75 assert(countPopulation(ID) == 1);
76 if (ID > getNextInSequence())
77 RemovedFromNextInSequence |= ID;
78 NextInSequenceMask = NextInSequenceMask & (~ID);
79 if (!NextInSequenceMask) {
80 NextInSequenceMask = ResourceSizeMask;
81 assert(NextInSequenceMask != RemovedFromNextInSequence);
82 NextInSequenceMask ^= RemovedFromNextInSequence;
83 RemovedFromNextInSequence = 0;
84 }
85}
86
87void ResourceManager::use(ResourceRef RR) {
88 // Mark the sub-resource referenced by RR as used.
89 ResourceState &RS = *Resources[RR.first];
90 RS.markSubResourceAsUsed(RR.second);
91 // If there are still available units in RR.first,
92 // then we are done.
93 if (RS.isReady())
94 return;
95
96 // Notify to other resources that RR.first is no longer available.
97 for (const std::pair<uint64_t, UniqueResourceState> &Res : Resources) {
98 ResourceState &Current = *Res.second.get();
99 if (!Current.isAResourceGroup() || Current.getResourceMask() == RR.first)
100 continue;
101
102 if (Current.containsResource(RR.first)) {
103 Current.markSubResourceAsUsed(RR.first);
104 Current.removeFromNextInSequence(RR.first);
105 }
106 }
107}
108
109void ResourceManager::release(ResourceRef RR) {
110 ResourceState &RS = *Resources[RR.first];
111 bool WasFullyUsed = !RS.isReady();
112 RS.releaseSubResource(RR.second);
113 if (!WasFullyUsed)
114 return;
115
116 for (const std::pair<uint64_t, UniqueResourceState> &Res : Resources) {
117 ResourceState &Current = *Res.second.get();
118 if (!Current.isAResourceGroup() || Current.getResourceMask() == RR.first)
119 continue;
120
121 if (Current.containsResource(RR.first))
122 Current.releaseSubResource(RR.first);
123 }
124}
125
Andrea Di Biagio44bfcd22018-03-19 19:09:38 +0000126ResourceStateEvent
Andrea Di Biagio847accd2018-03-20 19:06:34 +0000127ResourceManager::canBeDispatched(ArrayRef<uint64_t> Buffers) const {
Andrea Di Biagio44bfcd22018-03-19 19:09:38 +0000128 ResourceStateEvent Result = ResourceStateEvent::RS_BUFFER_AVAILABLE;
129 for (uint64_t Buffer : Buffers) {
130 Result = isBufferAvailable(Buffer);
131 if (Result != ResourceStateEvent::RS_BUFFER_AVAILABLE)
132 break;
133 }
134 return Result;
135}
136
Andrea Di Biagio847accd2018-03-20 19:06:34 +0000137void ResourceManager::reserveBuffers(ArrayRef<uint64_t> Buffers) {
Andrea Di Biagioe1a1da12018-03-13 13:58:02 +0000138 for (const uint64_t R : Buffers) {
Andrea Di Biagio44bfcd22018-03-19 19:09:38 +0000139 reserveBuffer(R);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000140 ResourceState &Resource = *Resources[R];
141 if (Resource.isADispatchHazard()) {
142 assert(!Resource.isReserved());
143 Resource.setReserved();
144 }
145 }
146}
147
Andrea Di Biagio847accd2018-03-20 19:06:34 +0000148void ResourceManager::releaseBuffers(ArrayRef<uint64_t> Buffers) {
Andrea Di Biagio44bfcd22018-03-19 19:09:38 +0000149 for (const uint64_t R : Buffers)
150 releaseBuffer(R);
151}
152
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000153bool ResourceManager::canBeIssued(const InstrDesc &Desc) const {
154 return std::all_of(Desc.Resources.begin(), Desc.Resources.end(),
155 [&](const std::pair<uint64_t, const ResourceUsage> &E) {
156 unsigned NumUnits =
157 E.second.isReserved() ? 0U : E.second.NumUnits;
158 return isReady(E.first, NumUnits);
159 });
160}
161
162// Returns true if all resources are in-order, and there is at least one
163// resource which is a dispatch hazard (BufferSize = 0).
164bool ResourceManager::mustIssueImmediately(const InstrDesc &Desc) {
165 if (!canBeIssued(Desc))
166 return false;
167 bool AllInOrderResources = std::all_of(
168 Desc.Buffers.begin(), Desc.Buffers.end(), [&](const unsigned BufferMask) {
169 const ResourceState &Resource = *Resources[BufferMask];
170 return Resource.isInOrder() || Resource.isADispatchHazard();
171 });
172 if (!AllInOrderResources)
173 return false;
174
175 return std::any_of(Desc.Buffers.begin(), Desc.Buffers.end(),
176 [&](const unsigned BufferMask) {
177 return Resources[BufferMask]->isADispatchHazard();
178 });
179}
180
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000181void ResourceManager::issueInstruction(
Matt Davisad78e662018-04-26 22:30:40 +0000182 const InstrDesc &Desc,
Andrea Di Biagio51dba7d2018-03-23 17:36:07 +0000183 SmallVectorImpl<std::pair<ResourceRef, double>> &Pipes) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000184 for (const std::pair<uint64_t, ResourceUsage> &R : Desc.Resources) {
185 const CycleSegment &CS = R.second.CS;
186 if (!CS.size()) {
187 releaseResource(R.first);
188 continue;
189 }
190
191 assert(CS.begin() == 0 && "Invalid {Start, End} cycles!");
192 if (!R.second.isReserved()) {
193 ResourceRef Pipe = selectPipe(R.first);
194 use(Pipe);
195 BusyResources[Pipe] += CS.size();
Andrea Di Biagio0c541292018-03-10 16:55:07 +0000196 // Replace the resource mask with a valid processor resource index.
197 const ResourceState &RS = *Resources[Pipe.first];
198 Pipe.first = RS.getProcResourceID();
Andrea Di Biagio51dba7d2018-03-23 17:36:07 +0000199 Pipes.emplace_back(
200 std::pair<ResourceRef, double>(Pipe, static_cast<double>(CS.size())));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000201 } else {
202 assert((countPopulation(R.first) > 1) && "Expected a group!");
203 // Mark this group as reserved.
204 assert(R.second.isReserved());
205 reserveResource(R.first);
206 BusyResources[ResourceRef(R.first, R.first)] += CS.size();
207 }
208 }
209}
210
211void ResourceManager::cycleEvent(SmallVectorImpl<ResourceRef> &ResourcesFreed) {
212 for (std::pair<ResourceRef, unsigned> &BR : BusyResources) {
213 if (BR.second)
214 BR.second--;
215 if (!BR.second) {
216 // Release this resource.
217 const ResourceRef &RR = BR.first;
218
219 if (countPopulation(RR.first) == 1)
220 release(RR);
221
222 releaseResource(RR.first);
223 ResourcesFreed.push_back(RR);
224 }
225 }
226
227 for (const ResourceRef &RF : ResourcesFreed)
228 BusyResources.erase(RF);
229}
230
Matt Davis21a8d322018-05-07 18:29:15 +0000231void Scheduler::scheduleInstruction(InstRef &IR) {
232 const unsigned Idx = IR.getSourceIndex();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000233 assert(WaitQueue.find(Idx) == WaitQueue.end());
234 assert(ReadyQueue.find(Idx) == ReadyQueue.end());
235 assert(IssuedQueue.find(Idx) == IssuedQueue.end());
236
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000237 // Reserve a slot in each buffered resource. Also, mark units with
238 // BufferSize=0 as reserved. Resources with a buffer size of zero will only
239 // be released after MCIS is issued, and all the ResourceCycles for those
240 // units have been consumed.
Matt Davis21a8d322018-05-07 18:29:15 +0000241 const InstrDesc &Desc = IR.getInstruction()->getDesc();
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000242 reserveBuffers(Desc.Buffers);
243 notifyReservedBuffers(Desc.Buffers);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000244
Andrea Di Biagio2dee62b2018-03-22 14:14:49 +0000245 // If necessary, reserve queue entries in the load-store unit (LSU).
Matt Davis21a8d322018-05-07 18:29:15 +0000246 bool Reserved = LSU->reserve(IR);
247 if (!IR.getInstruction()->isReady() || (Reserved && !LSU->isReady(IR))) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000248 LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx
249 << " to the Wait Queue\n");
Matt Davis21a8d322018-05-07 18:29:15 +0000250 WaitQueue[Idx] = IR.getInstruction();
Andrea Di Biagio44bfcd22018-03-19 19:09:38 +0000251 return;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000252 }
Matt Davis21a8d322018-05-07 18:29:15 +0000253 notifyInstructionReady(IR);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000254
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000255 // Don't add a zero-latency instruction to the Wait or Ready queue.
256 // A zero-latency instruction doesn't consume any scheduler resources. That is
257 // because it doesn't need to be executed, and it is often removed at register
258 // renaming stage. For example, register-register moves are often optimized at
259 // register renaming stage by simply updating register aliases. On some
260 // targets, zero-idiom instructions (for example: a xor that clears the value
261 // of a register) are treated speacially, and are often eliminated at register
262 // renaming stage.
263
264 // Instructions that use an in-order dispatch/issue processor resource must be
265 // issued immediately to the pipeline(s). Any other in-order buffered
266 // resources (i.e. BufferSize=1) is consumed.
267
Andrea Di Biagio8ea3a342018-05-14 15:08:22 +0000268 if (!Desc.isZeroLatency() && !Resources->mustIssueImmediately(Desc)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000269 LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding " << IR
270 << " to the Ready Queue\n");
Matt Davis21a8d322018-05-07 18:29:15 +0000271 ReadyQueue[IR.getSourceIndex()] = IR.getInstruction();
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000272 return;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000273 }
274
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000275 LLVM_DEBUG(dbgs() << "[SCHEDULER] Instruction " << IR
276 << " issued immediately\n");
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000277 // Release buffered resources and issue MCIS to the underlying pipelines.
Matt Davis21a8d322018-05-07 18:29:15 +0000278 issueInstruction(IR);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000279}
280
Andrea Di Biagio3e646442018-04-12 10:49:40 +0000281void Scheduler::cycleEvent() {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000282 SmallVector<ResourceRef, 8> ResourcesFreed;
283 Resources->cycleEvent(ResourcesFreed);
284
285 for (const ResourceRef &RR : ResourcesFreed)
286 notifyResourceAvailable(RR);
287
Matt Davis21a8d322018-05-07 18:29:15 +0000288 SmallVector<InstRef, 4> InstructionIDs;
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000289 updateIssuedQueue(InstructionIDs);
Matt Davis21a8d322018-05-07 18:29:15 +0000290 for (const InstRef &IR : InstructionIDs)
291 notifyInstructionExecuted(IR);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000292 InstructionIDs.clear();
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000293
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000294 updatePendingQueue(InstructionIDs);
Matt Davis21a8d322018-05-07 18:29:15 +0000295 for (const InstRef &IR : InstructionIDs)
296 notifyInstructionReady(IR);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000297 InstructionIDs.clear();
298
Matt Davis21a8d322018-05-07 18:29:15 +0000299 InstRef IR = select();
300 while (IR.isValid()) {
301 issueInstruction(IR);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000302
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000303 // Instructions that have been issued during this cycle might have unblocked
304 // other dependent instructions. Dependent instructions may be issued during
305 // this same cycle if operands have ReadAdvance entries. Promote those
306 // instructions to the ReadyQueue and tell to the caller that we need
307 // another round of 'issue()'.
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000308 promoteToReadyQueue(InstructionIDs);
Matt Davis21a8d322018-05-07 18:29:15 +0000309 for (const InstRef &I : InstructionIDs)
310 notifyInstructionReady(I);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000311 InstructionIDs.clear();
312
313 // Select the next instruction to issue.
Matt Davis21a8d322018-05-07 18:29:15 +0000314 IR = select();
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000315 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000316}
317
318#ifndef NDEBUG
319void Scheduler::dump() const {
320 dbgs() << "[SCHEDULER]: WaitQueue size is: " << WaitQueue.size() << '\n';
321 dbgs() << "[SCHEDULER]: ReadyQueue size is: " << ReadyQueue.size() << '\n';
322 dbgs() << "[SCHEDULER]: IssuedQueue size is: " << IssuedQueue.size() << '\n';
323 Resources->dump();
324}
325#endif
326
Matt Davis21a8d322018-05-07 18:29:15 +0000327bool Scheduler::canBeDispatched(const InstRef &IR) const {
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000328 HWStallEvent::GenericEventType Type = HWStallEvent::Invalid;
Matt Davis21a8d322018-05-07 18:29:15 +0000329 const InstrDesc &Desc = IR.getInstruction()->getDesc();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000330
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000331 if (Desc.MayLoad && LSU->isLQFull())
332 Type = HWStallEvent::LoadQueueFull;
333 else if (Desc.MayStore && LSU->isSQFull())
334 Type = HWStallEvent::StoreQueueFull;
335 else {
336 switch (Resources->canBeDispatched(Desc.Buffers)) {
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000337 default:
338 return true;
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000339 case ResourceStateEvent::RS_BUFFER_UNAVAILABLE:
340 Type = HWStallEvent::SchedulerQueueFull;
341 break;
342 case ResourceStateEvent::RS_RESERVED:
343 Type = HWStallEvent::DispatchGroupStall;
344 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000345 }
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000346
Matt Davis21a8d322018-05-07 18:29:15 +0000347 Owner->notifyStallEvent(HWStallEvent(Type, IR));
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000348 return false;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000349}
350
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000351void Scheduler::issueInstructionImpl(
Matt Davis21a8d322018-05-07 18:29:15 +0000352 InstRef &IR,
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000353 SmallVectorImpl<std::pair<ResourceRef, double>> &UsedResources) {
Matt Davis21a8d322018-05-07 18:29:15 +0000354 Instruction *IS = IR.getInstruction();
355 const InstrDesc &D = IS->getDesc();
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000356
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000357 // Issue the instruction and collect all the consumed resources
358 // into a vector. That vector is then used to notify the listener.
Matt Davisad78e662018-04-26 22:30:40 +0000359 Resources->issueInstruction(D, UsedResources);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000360
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000361 // Notify the instruction that it started executing.
362 // This updates the internal state of each write.
Matt Davis21a8d322018-05-07 18:29:15 +0000363 IS->execute();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000364
Matt Davis21a8d322018-05-07 18:29:15 +0000365 if (IS->isExecuting())
366 IssuedQueue[IR.getSourceIndex()] = IS;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000367}
368
Matt Davis21a8d322018-05-07 18:29:15 +0000369void Scheduler::issueInstruction(InstRef &IR) {
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000370 // Release buffered resources.
Matt Davis21a8d322018-05-07 18:29:15 +0000371 const InstrDesc &Desc = IR.getInstruction()->getDesc();
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000372 releaseBuffers(Desc.Buffers);
373 notifyReleasedBuffers(Desc.Buffers);
374
375 // Issue IS to the underlying pipelines and notify listeners.
376 SmallVector<std::pair<ResourceRef, double>, 4> Pipes;
Matt Davis21a8d322018-05-07 18:29:15 +0000377 issueInstructionImpl(IR, Pipes);
378 notifyInstructionIssued(IR, Pipes);
379 if (IR.getInstruction()->isExecuted())
380 notifyInstructionExecuted(IR);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000381}
382
Matt Davis21a8d322018-05-07 18:29:15 +0000383void Scheduler::promoteToReadyQueue(SmallVectorImpl<InstRef> &Ready) {
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000384 // Scan the set of waiting instructions and promote them to the
385 // ready queue if operands are all ready.
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000386 for (auto I = WaitQueue.begin(), E = WaitQueue.end(); I != E;) {
Matt Davis21a8d322018-05-07 18:29:15 +0000387 const unsigned IID = I->first;
388 Instruction *IS = I->second;
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000389
390 // Check if this instruction is now ready. In case, force
391 // a transition in state using method 'update()'.
Matt Davis21a8d322018-05-07 18:29:15 +0000392 IS->update();
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000393
Matt Davis21a8d322018-05-07 18:29:15 +0000394 const InstrDesc &Desc = IS->getDesc();
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000395 bool IsMemOp = Desc.MayLoad || Desc.MayStore;
Matt Davis21a8d322018-05-07 18:29:15 +0000396 if (!IS->isReady() || (IsMemOp && !LSU->isReady({IID, IS}))) {
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000397 ++I;
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000398 continue;
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000399 }
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000400
Matt Davis21a8d322018-05-07 18:29:15 +0000401 Ready.emplace_back(IID, IS);
402 ReadyQueue[IID] = IS;
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000403 auto ToRemove = I;
404 ++I;
405 WaitQueue.erase(ToRemove);
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000406 }
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000407}
408
Matt Davis21a8d322018-05-07 18:29:15 +0000409InstRef Scheduler::select() {
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000410 // Give priority to older instructions in the ReadyQueue. Since the ready
411 // queue is ordered by key, this will always prioritize older instructions.
412 const auto It = std::find_if(ReadyQueue.begin(), ReadyQueue.end(),
413 [&](const QueueEntryTy &Entry) {
Matt Davis21a8d322018-05-07 18:29:15 +0000414 const InstrDesc &D = Entry.second->getDesc();
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000415 return Resources->canBeIssued(D);
416 });
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000417
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000418 if (It == ReadyQueue.end())
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000419 return {0, nullptr};
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000420
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000421 // We found an instruction to issue.
Matt Davis21a8d322018-05-07 18:29:15 +0000422 InstRef IR(It->first, It->second);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000423 ReadyQueue.erase(It);
Matt Davis21a8d322018-05-07 18:29:15 +0000424 return IR;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000425}
426
Matt Davis21a8d322018-05-07 18:29:15 +0000427void Scheduler::updatePendingQueue(SmallVectorImpl<InstRef> &Ready) {
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000428 // Notify to instructions in the pending queue that a new cycle just
429 // started.
430 for (QueueEntryTy Entry : WaitQueue)
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000431 Entry.second->cycleEvent();
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000432 promoteToReadyQueue(Ready);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000433}
434
Matt Davis21a8d322018-05-07 18:29:15 +0000435void Scheduler::updateIssuedQueue(SmallVectorImpl<InstRef> &Executed) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000436 for (auto I = IssuedQueue.begin(), E = IssuedQueue.end(); I != E;) {
437 const QueueEntryTy Entry = *I;
Matt Davis21a8d322018-05-07 18:29:15 +0000438 Instruction *IS = Entry.second;
439 IS->cycleEvent();
440 if (IS->isExecuted()) {
441 Executed.push_back({Entry.first, Entry.second});
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000442 auto ToRemove = I;
443 ++I;
444 IssuedQueue.erase(ToRemove);
445 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000446 LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction " << Entry.first
447 << " is still executing.\n");
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000448 ++I;
449 }
450 }
451}
452
453void Scheduler::notifyInstructionIssued(
Matt Davis21a8d322018-05-07 18:29:15 +0000454 const InstRef &IR, ArrayRef<std::pair<ResourceRef, double>> Used) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000455 LLVM_DEBUG({
Matt Davis21a8d322018-05-07 18:29:15 +0000456 dbgs() << "[E] Instruction Issued: " << IR << '\n';
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000457 for (const std::pair<ResourceRef, unsigned> &Resource : Used) {
458 dbgs() << "[E] Resource Used: [" << Resource.first.first << '.'
459 << Resource.first.second << "]\n";
460 dbgs() << " cycles: " << Resource.second << '\n';
461 }
462 });
Matt Davis21a8d322018-05-07 18:29:15 +0000463 Owner->notifyInstructionEvent(HWInstructionIssuedEvent(IR, Used));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000464}
465
Matt Davis21a8d322018-05-07 18:29:15 +0000466void Scheduler::notifyInstructionExecuted(const InstRef &IR) {
467 LSU->onInstructionExecuted(IR);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000468 LLVM_DEBUG(dbgs() << "[E] Instruction Executed: " << IR << '\n');
Clement Courbet844f22d2018-03-13 13:11:01 +0000469 Owner->notifyInstructionEvent(
Matt Davis21a8d322018-05-07 18:29:15 +0000470 HWInstructionEvent(HWInstructionEvent::Executed, IR));
Matt Davis679083e2018-05-17 19:22:29 +0000471 DS->onInstructionExecuted(IR.getInstruction()->getRCUTokenID());
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000472}
473
Matt Davis21a8d322018-05-07 18:29:15 +0000474void Scheduler::notifyInstructionReady(const InstRef &IR) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000475 LLVM_DEBUG(dbgs() << "[E] Instruction Ready: " << IR << '\n');
Clement Courbet844f22d2018-03-13 13:11:01 +0000476 Owner->notifyInstructionEvent(
Matt Davis21a8d322018-05-07 18:29:15 +0000477 HWInstructionEvent(HWInstructionEvent::Ready, IR));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000478}
479
480void Scheduler::notifyResourceAvailable(const ResourceRef &RR) {
481 Owner->notifyResourceAvailable(RR);
482}
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000483
484void Scheduler::notifyReservedBuffers(ArrayRef<uint64_t> Buffers) {
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000485 if (Buffers.empty())
486 return;
487
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000488 SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end());
489 std::transform(
490 Buffers.begin(), Buffers.end(), BufferIDs.begin(),
491 [&](uint64_t Op) { return Resources->resolveResourceMask(Op); });
492 Owner->notifyReservedBuffers(BufferIDs);
493}
494
495void Scheduler::notifyReleasedBuffers(ArrayRef<uint64_t> Buffers) {
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000496 if (Buffers.empty())
497 return;
498
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000499 SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end());
500 std::transform(
501 Buffers.begin(), Buffers.end(), BufferIDs.begin(),
502 [&](uint64_t Op) { return Resources->resolveResourceMask(Op); });
503 Owner->notifyReleasedBuffers(BufferIDs);
504}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000505} // namespace mca