blob: 2782bac187f1b91f5999ff7e21d06bdcf4eaccd3 [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;
Andrea Di Biagio40370112018-05-31 20:27:46 +0000167 bool AllInOrderResources = all_of(Desc.Buffers, [&](uint64_t BufferMask) {
168 const ResourceState &Resource = *Resources[BufferMask];
169 return Resource.isInOrder() || Resource.isADispatchHazard();
170 });
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000171 if (!AllInOrderResources)
172 return false;
173
Andrea Di Biagio40370112018-05-31 20:27:46 +0000174 return any_of(Desc.Buffers, [&](uint64_t BufferMask) {
175 return Resources[BufferMask]->isADispatchHazard();
176 });
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000177}
178
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000179void ResourceManager::issueInstruction(
Matt Davisad78e662018-04-26 22:30:40 +0000180 const InstrDesc &Desc,
Andrea Di Biagio51dba7d2018-03-23 17:36:07 +0000181 SmallVectorImpl<std::pair<ResourceRef, double>> &Pipes) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000182 for (const std::pair<uint64_t, ResourceUsage> &R : Desc.Resources) {
183 const CycleSegment &CS = R.second.CS;
184 if (!CS.size()) {
185 releaseResource(R.first);
186 continue;
187 }
188
189 assert(CS.begin() == 0 && "Invalid {Start, End} cycles!");
190 if (!R.second.isReserved()) {
191 ResourceRef Pipe = selectPipe(R.first);
192 use(Pipe);
193 BusyResources[Pipe] += CS.size();
Andrea Di Biagio0c541292018-03-10 16:55:07 +0000194 // Replace the resource mask with a valid processor resource index.
195 const ResourceState &RS = *Resources[Pipe.first];
196 Pipe.first = RS.getProcResourceID();
Andrea Di Biagio51dba7d2018-03-23 17:36:07 +0000197 Pipes.emplace_back(
198 std::pair<ResourceRef, double>(Pipe, static_cast<double>(CS.size())));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000199 } else {
200 assert((countPopulation(R.first) > 1) && "Expected a group!");
201 // Mark this group as reserved.
202 assert(R.second.isReserved());
203 reserveResource(R.first);
204 BusyResources[ResourceRef(R.first, R.first)] += CS.size();
205 }
206 }
207}
208
209void ResourceManager::cycleEvent(SmallVectorImpl<ResourceRef> &ResourcesFreed) {
210 for (std::pair<ResourceRef, unsigned> &BR : BusyResources) {
211 if (BR.second)
212 BR.second--;
213 if (!BR.second) {
214 // Release this resource.
215 const ResourceRef &RR = BR.first;
216
217 if (countPopulation(RR.first) == 1)
218 release(RR);
219
220 releaseResource(RR.first);
221 ResourcesFreed.push_back(RR);
222 }
223 }
224
225 for (const ResourceRef &RF : ResourcesFreed)
226 BusyResources.erase(RF);
227}
228
Matt Davis21a8d322018-05-07 18:29:15 +0000229void Scheduler::scheduleInstruction(InstRef &IR) {
230 const unsigned Idx = IR.getSourceIndex();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000231 assert(WaitQueue.find(Idx) == WaitQueue.end());
232 assert(ReadyQueue.find(Idx) == ReadyQueue.end());
233 assert(IssuedQueue.find(Idx) == IssuedQueue.end());
234
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000235 // Reserve a slot in each buffered resource. Also, mark units with
236 // BufferSize=0 as reserved. Resources with a buffer size of zero will only
237 // be released after MCIS is issued, and all the ResourceCycles for those
238 // units have been consumed.
Matt Davis21a8d322018-05-07 18:29:15 +0000239 const InstrDesc &Desc = IR.getInstruction()->getDesc();
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000240 reserveBuffers(Desc.Buffers);
241 notifyReservedBuffers(Desc.Buffers);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000242
Andrea Di Biagio2dee62b2018-03-22 14:14:49 +0000243 // If necessary, reserve queue entries in the load-store unit (LSU).
Matt Davis21a8d322018-05-07 18:29:15 +0000244 bool Reserved = LSU->reserve(IR);
245 if (!IR.getInstruction()->isReady() || (Reserved && !LSU->isReady(IR))) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000246 LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx
247 << " to the Wait Queue\n");
Matt Davis21a8d322018-05-07 18:29:15 +0000248 WaitQueue[Idx] = IR.getInstruction();
Andrea Di Biagio44bfcd22018-03-19 19:09:38 +0000249 return;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000250 }
Matt Davis21a8d322018-05-07 18:29:15 +0000251 notifyInstructionReady(IR);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000252
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000253 // Don't add a zero-latency instruction to the Wait or Ready queue.
254 // A zero-latency instruction doesn't consume any scheduler resources. That is
255 // because it doesn't need to be executed, and it is often removed at register
256 // renaming stage. For example, register-register moves are often optimized at
257 // register renaming stage by simply updating register aliases. On some
258 // targets, zero-idiom instructions (for example: a xor that clears the value
259 // of a register) are treated speacially, and are often eliminated at register
260 // renaming stage.
261
262 // Instructions that use an in-order dispatch/issue processor resource must be
263 // issued immediately to the pipeline(s). Any other in-order buffered
264 // resources (i.e. BufferSize=1) is consumed.
265
Andrea Di Biagio8ea3a342018-05-14 15:08:22 +0000266 if (!Desc.isZeroLatency() && !Resources->mustIssueImmediately(Desc)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000267 LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding " << IR
268 << " to the Ready Queue\n");
Matt Davis21a8d322018-05-07 18:29:15 +0000269 ReadyQueue[IR.getSourceIndex()] = IR.getInstruction();
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000270 return;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000271 }
272
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000273 LLVM_DEBUG(dbgs() << "[SCHEDULER] Instruction " << IR
274 << " issued immediately\n");
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000275 // Release buffered resources and issue MCIS to the underlying pipelines.
Matt Davis21a8d322018-05-07 18:29:15 +0000276 issueInstruction(IR);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000277}
278
Andrea Di Biagio3e646442018-04-12 10:49:40 +0000279void Scheduler::cycleEvent() {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000280 SmallVector<ResourceRef, 8> ResourcesFreed;
281 Resources->cycleEvent(ResourcesFreed);
282
283 for (const ResourceRef &RR : ResourcesFreed)
284 notifyResourceAvailable(RR);
285
Matt Davis21a8d322018-05-07 18:29:15 +0000286 SmallVector<InstRef, 4> InstructionIDs;
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000287 updateIssuedQueue(InstructionIDs);
Matt Davis21a8d322018-05-07 18:29:15 +0000288 for (const InstRef &IR : InstructionIDs)
289 notifyInstructionExecuted(IR);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000290 InstructionIDs.clear();
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000291
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000292 updatePendingQueue(InstructionIDs);
Matt Davis21a8d322018-05-07 18:29:15 +0000293 for (const InstRef &IR : InstructionIDs)
294 notifyInstructionReady(IR);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000295 InstructionIDs.clear();
296
Matt Davis21a8d322018-05-07 18:29:15 +0000297 InstRef IR = select();
298 while (IR.isValid()) {
299 issueInstruction(IR);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000300
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000301 // Instructions that have been issued during this cycle might have unblocked
302 // other dependent instructions. Dependent instructions may be issued during
303 // this same cycle if operands have ReadAdvance entries. Promote those
304 // instructions to the ReadyQueue and tell to the caller that we need
305 // another round of 'issue()'.
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000306 promoteToReadyQueue(InstructionIDs);
Matt Davis21a8d322018-05-07 18:29:15 +0000307 for (const InstRef &I : InstructionIDs)
308 notifyInstructionReady(I);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000309 InstructionIDs.clear();
310
311 // Select the next instruction to issue.
Matt Davis21a8d322018-05-07 18:29:15 +0000312 IR = select();
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000313 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000314}
315
316#ifndef NDEBUG
317void Scheduler::dump() const {
318 dbgs() << "[SCHEDULER]: WaitQueue size is: " << WaitQueue.size() << '\n';
319 dbgs() << "[SCHEDULER]: ReadyQueue size is: " << ReadyQueue.size() << '\n';
320 dbgs() << "[SCHEDULER]: IssuedQueue size is: " << IssuedQueue.size() << '\n';
321 Resources->dump();
322}
323#endif
324
Matt Davis21a8d322018-05-07 18:29:15 +0000325bool Scheduler::canBeDispatched(const InstRef &IR) const {
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000326 HWStallEvent::GenericEventType Type = HWStallEvent::Invalid;
Matt Davis21a8d322018-05-07 18:29:15 +0000327 const InstrDesc &Desc = IR.getInstruction()->getDesc();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000328
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000329 if (Desc.MayLoad && LSU->isLQFull())
330 Type = HWStallEvent::LoadQueueFull;
331 else if (Desc.MayStore && LSU->isSQFull())
332 Type = HWStallEvent::StoreQueueFull;
333 else {
334 switch (Resources->canBeDispatched(Desc.Buffers)) {
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000335 default:
336 return true;
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000337 case ResourceStateEvent::RS_BUFFER_UNAVAILABLE:
338 Type = HWStallEvent::SchedulerQueueFull;
339 break;
340 case ResourceStateEvent::RS_RESERVED:
341 Type = HWStallEvent::DispatchGroupStall;
342 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000343 }
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000344
Matt Davis21a8d322018-05-07 18:29:15 +0000345 Owner->notifyStallEvent(HWStallEvent(Type, IR));
Andrea Di Biagiob24953b2018-04-11 18:05:23 +0000346 return false;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000347}
348
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000349void Scheduler::issueInstructionImpl(
Matt Davis21a8d322018-05-07 18:29:15 +0000350 InstRef &IR,
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000351 SmallVectorImpl<std::pair<ResourceRef, double>> &UsedResources) {
Matt Davis21a8d322018-05-07 18:29:15 +0000352 Instruction *IS = IR.getInstruction();
353 const InstrDesc &D = IS->getDesc();
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000354
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000355 // Issue the instruction and collect all the consumed resources
356 // into a vector. That vector is then used to notify the listener.
Matt Davisad78e662018-04-26 22:30:40 +0000357 Resources->issueInstruction(D, UsedResources);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000358
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000359 // Notify the instruction that it started executing.
360 // This updates the internal state of each write.
Matt Davis21a8d322018-05-07 18:29:15 +0000361 IS->execute();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000362
Matt Davis21a8d322018-05-07 18:29:15 +0000363 if (IS->isExecuting())
364 IssuedQueue[IR.getSourceIndex()] = IS;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000365}
366
Matt Davis21a8d322018-05-07 18:29:15 +0000367void Scheduler::issueInstruction(InstRef &IR) {
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000368 // Release buffered resources.
Matt Davis21a8d322018-05-07 18:29:15 +0000369 const InstrDesc &Desc = IR.getInstruction()->getDesc();
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000370 releaseBuffers(Desc.Buffers);
371 notifyReleasedBuffers(Desc.Buffers);
372
373 // Issue IS to the underlying pipelines and notify listeners.
374 SmallVector<std::pair<ResourceRef, double>, 4> Pipes;
Matt Davis21a8d322018-05-07 18:29:15 +0000375 issueInstructionImpl(IR, Pipes);
376 notifyInstructionIssued(IR, Pipes);
377 if (IR.getInstruction()->isExecuted())
378 notifyInstructionExecuted(IR);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000379}
380
Matt Davis21a8d322018-05-07 18:29:15 +0000381void Scheduler::promoteToReadyQueue(SmallVectorImpl<InstRef> &Ready) {
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000382 // Scan the set of waiting instructions and promote them to the
383 // ready queue if operands are all ready.
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000384 for (auto I = WaitQueue.begin(), E = WaitQueue.end(); I != E;) {
Matt Davis21a8d322018-05-07 18:29:15 +0000385 const unsigned IID = I->first;
386 Instruction *IS = I->second;
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000387
388 // Check if this instruction is now ready. In case, force
389 // a transition in state using method 'update()'.
Matt Davis21a8d322018-05-07 18:29:15 +0000390 IS->update();
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000391
Matt Davis21a8d322018-05-07 18:29:15 +0000392 const InstrDesc &Desc = IS->getDesc();
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000393 bool IsMemOp = Desc.MayLoad || Desc.MayStore;
Matt Davis21a8d322018-05-07 18:29:15 +0000394 if (!IS->isReady() || (IsMemOp && !LSU->isReady({IID, IS}))) {
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000395 ++I;
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000396 continue;
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000397 }
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000398
Matt Davis21a8d322018-05-07 18:29:15 +0000399 Ready.emplace_back(IID, IS);
400 ReadyQueue[IID] = IS;
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000401 auto ToRemove = I;
402 ++I;
403 WaitQueue.erase(ToRemove);
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000404 }
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000405}
406
Matt Davis21a8d322018-05-07 18:29:15 +0000407InstRef Scheduler::select() {
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000408 // Give priority to older instructions in the ReadyQueue. Since the ready
409 // queue is ordered by key, this will always prioritize older instructions.
410 const auto It = std::find_if(ReadyQueue.begin(), ReadyQueue.end(),
411 [&](const QueueEntryTy &Entry) {
Matt Davis21a8d322018-05-07 18:29:15 +0000412 const InstrDesc &D = Entry.second->getDesc();
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000413 return Resources->canBeIssued(D);
414 });
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000415
Andrea Di Biagioc7526162018-04-13 15:19:07 +0000416 if (It == ReadyQueue.end())
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000417 return {0, nullptr};
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000418
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000419 // We found an instruction to issue.
Matt Davis21a8d322018-05-07 18:29:15 +0000420 InstRef IR(It->first, It->second);
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000421 ReadyQueue.erase(It);
Matt Davis21a8d322018-05-07 18:29:15 +0000422 return IR;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000423}
424
Matt Davis21a8d322018-05-07 18:29:15 +0000425void Scheduler::updatePendingQueue(SmallVectorImpl<InstRef> &Ready) {
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000426 // Notify to instructions in the pending queue that a new cycle just
427 // started.
428 for (QueueEntryTy Entry : WaitQueue)
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000429 Entry.second->cycleEvent();
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000430 promoteToReadyQueue(Ready);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000431}
432
Matt Davis21a8d322018-05-07 18:29:15 +0000433void Scheduler::updateIssuedQueue(SmallVectorImpl<InstRef> &Executed) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000434 for (auto I = IssuedQueue.begin(), E = IssuedQueue.end(); I != E;) {
435 const QueueEntryTy Entry = *I;
Matt Davis21a8d322018-05-07 18:29:15 +0000436 Instruction *IS = Entry.second;
437 IS->cycleEvent();
438 if (IS->isExecuted()) {
439 Executed.push_back({Entry.first, Entry.second});
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000440 auto ToRemove = I;
441 ++I;
442 IssuedQueue.erase(ToRemove);
443 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000444 LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction " << Entry.first
445 << " is still executing.\n");
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000446 ++I;
447 }
448 }
449}
450
451void Scheduler::notifyInstructionIssued(
Matt Davis21a8d322018-05-07 18:29:15 +0000452 const InstRef &IR, ArrayRef<std::pair<ResourceRef, double>> Used) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000453 LLVM_DEBUG({
Matt Davis21a8d322018-05-07 18:29:15 +0000454 dbgs() << "[E] Instruction Issued: " << IR << '\n';
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000455 for (const std::pair<ResourceRef, unsigned> &Resource : Used) {
456 dbgs() << "[E] Resource Used: [" << Resource.first.first << '.'
457 << Resource.first.second << "]\n";
458 dbgs() << " cycles: " << Resource.second << '\n';
459 }
460 });
Matt Davis21a8d322018-05-07 18:29:15 +0000461 Owner->notifyInstructionEvent(HWInstructionIssuedEvent(IR, Used));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000462}
463
Matt Davis21a8d322018-05-07 18:29:15 +0000464void Scheduler::notifyInstructionExecuted(const InstRef &IR) {
465 LSU->onInstructionExecuted(IR);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000466 LLVM_DEBUG(dbgs() << "[E] Instruction Executed: " << IR << '\n');
Clement Courbet844f22d2018-03-13 13:11:01 +0000467 Owner->notifyInstructionEvent(
Matt Davis21a8d322018-05-07 18:29:15 +0000468 HWInstructionEvent(HWInstructionEvent::Executed, IR));
Matt Davis5b79ffc5b2018-05-25 18:00:25 +0000469 RCU.onInstructionExecuted(IR.getInstruction()->getRCUTokenID());
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000470}
471
Matt Davis21a8d322018-05-07 18:29:15 +0000472void Scheduler::notifyInstructionReady(const InstRef &IR) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000473 LLVM_DEBUG(dbgs() << "[E] Instruction Ready: " << IR << '\n');
Clement Courbet844f22d2018-03-13 13:11:01 +0000474 Owner->notifyInstructionEvent(
Matt Davis21a8d322018-05-07 18:29:15 +0000475 HWInstructionEvent(HWInstructionEvent::Ready, IR));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000476}
477
478void Scheduler::notifyResourceAvailable(const ResourceRef &RR) {
479 Owner->notifyResourceAvailable(RR);
480}
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000481
482void Scheduler::notifyReservedBuffers(ArrayRef<uint64_t> Buffers) {
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000483 if (Buffers.empty())
484 return;
485
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000486 SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end());
487 std::transform(
488 Buffers.begin(), Buffers.end(), BufferIDs.begin(),
489 [&](uint64_t Op) { return Resources->resolveResourceMask(Op); });
490 Owner->notifyReservedBuffers(BufferIDs);
491}
492
493void Scheduler::notifyReleasedBuffers(ArrayRef<uint64_t> Buffers) {
Andrea Di Biagio27c4b092018-04-24 14:53:16 +0000494 if (Buffers.empty())
495 return;
496
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000497 SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end());
498 std::transform(
499 Buffers.begin(), Buffers.end(), BufferIDs.begin(),
500 [&](uint64_t Op) { return Resources->resolveResourceMask(Op); });
501 Owner->notifyReleasedBuffers(BufferIDs);
502}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000503} // namespace mca