blob: d799ef07b2ae496d348a331d5d31f267ce4067bd [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
14#include "Scheduler.h"
15#include "Backend.h"
Clement Courbet844f22d2018-03-13 13:11:01 +000016#include "HWEventListener.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000017#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
19
20#define DEBUG_TYPE "llvm-mca"
21
22namespace mca {
23
24using namespace llvm;
25
26uint64_t ResourceState::selectNextInSequence() {
27 assert(isReady());
28 uint64_t Next = getNextInSequence();
29 while (!isSubResourceReady(Next)) {
30 updateNextInSequence();
31 Next = getNextInSequence();
32 }
33 return Next;
34}
35
36#ifndef NDEBUG
37void ResourceState::dump() const {
38 dbgs() << "MASK: " << ResourceMask << ", SIZE_MASK: " << ResourceSizeMask
39 << ", NEXT: " << NextInSequenceMask << ", RDYMASK: " << ReadyMask
40 << ", BufferSize=" << BufferSize
41 << ", AvailableSlots=" << AvailableSlots
42 << ", Reserved=" << Unavailable << '\n';
43}
44#endif
45
46// Adds a new resource state in Resources, as well as a new descriptor in
47// ResourceDescriptor. Map 'Resources' allows to quickly obtain ResourceState
48// objects from resource mask identifiers.
49void ResourceManager::addResource(const MCProcResourceDesc &Desc,
Andrea Di Biagio0c541292018-03-10 16:55:07 +000050 unsigned Index,
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000051 uint64_t Mask) {
52 assert(Resources.find(Mask) == Resources.end() && "Resource already added!");
Andrea Di Biagio0c541292018-03-10 16:55:07 +000053 Resources[Mask] = llvm::make_unique<ResourceState>(Desc, Index, Mask);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000054}
55
56// Populate vector ProcResID2Mask with resource masks. One per each processor
57// resource declared by the scheduling model.
58void ResourceManager::computeProcResourceMasks(const MCSchedModel &SM) {
59 unsigned ProcResourceID = 0;
60
61 // Create a unique bitmask for every processor resource unit.
62 // Skip resource at index 0, since it always references 'InvalidUnit'.
63 ProcResID2Mask.resize(SM.getNumProcResourceKinds());
64 for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
65 const MCProcResourceDesc &Desc = *SM.getProcResource(I);
66 if (Desc.SubUnitsIdxBegin)
67 continue;
68 ProcResID2Mask[I] = 1ULL << ProcResourceID;
69 ProcResourceID++;
70 }
71
72 // Create a unique bitmask for every processor resource group.
73 for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
74 const MCProcResourceDesc &Desc = *SM.getProcResource(I);
75 if (!Desc.SubUnitsIdxBegin)
76 continue;
77 ProcResID2Mask[I] |= 1ULL << ProcResourceID;
78 for (unsigned U = 0; U < Desc.NumUnits; ++U) {
79 uint64_t OtherMask = ProcResID2Mask[Desc.SubUnitsIdxBegin[U]];
80 ProcResID2Mask[I] |= OtherMask;
81 }
82 ProcResourceID++;
83 }
84}
85
86// Returns the actual resource consumed by this Use.
87// First, is the primary resource ID.
88// Second, is the specific sub-resource ID.
89std::pair<uint64_t, uint64_t> ResourceManager::selectPipe(uint64_t ResourceID) {
90 ResourceState &RS = *Resources[ResourceID];
91 uint64_t SubResourceID = RS.selectNextInSequence();
92 if (RS.isAResourceGroup())
93 return selectPipe(SubResourceID);
94 return std::pair<uint64_t, uint64_t>(ResourceID, SubResourceID);
95}
96
97void ResourceState::removeFromNextInSequence(uint64_t ID) {
98 assert(NextInSequenceMask);
99 assert(countPopulation(ID) == 1);
100 if (ID > getNextInSequence())
101 RemovedFromNextInSequence |= ID;
102 NextInSequenceMask = NextInSequenceMask & (~ID);
103 if (!NextInSequenceMask) {
104 NextInSequenceMask = ResourceSizeMask;
105 assert(NextInSequenceMask != RemovedFromNextInSequence);
106 NextInSequenceMask ^= RemovedFromNextInSequence;
107 RemovedFromNextInSequence = 0;
108 }
109}
110
111void ResourceManager::use(ResourceRef RR) {
112 // Mark the sub-resource referenced by RR as used.
113 ResourceState &RS = *Resources[RR.first];
114 RS.markSubResourceAsUsed(RR.second);
115 // If there are still available units in RR.first,
116 // then we are done.
117 if (RS.isReady())
118 return;
119
120 // Notify to other resources that RR.first is no longer available.
121 for (const std::pair<uint64_t, UniqueResourceState> &Res : Resources) {
122 ResourceState &Current = *Res.second.get();
123 if (!Current.isAResourceGroup() || Current.getResourceMask() == RR.first)
124 continue;
125
126 if (Current.containsResource(RR.first)) {
127 Current.markSubResourceAsUsed(RR.first);
128 Current.removeFromNextInSequence(RR.first);
129 }
130 }
131}
132
133void ResourceManager::release(ResourceRef RR) {
134 ResourceState &RS = *Resources[RR.first];
135 bool WasFullyUsed = !RS.isReady();
136 RS.releaseSubResource(RR.second);
137 if (!WasFullyUsed)
138 return;
139
140 for (const std::pair<uint64_t, UniqueResourceState> &Res : Resources) {
141 ResourceState &Current = *Res.second.get();
142 if (!Current.isAResourceGroup() || Current.getResourceMask() == RR.first)
143 continue;
144
145 if (Current.containsResource(RR.first))
146 Current.releaseSubResource(RR.first);
147 }
148}
149
150void ResourceManager::reserveDispatchHazardResources(const InstrDesc &Desc) {
151 for (const uint64_t R : Desc.Buffers) {
152 ResourceState &Resource = *Resources[R];
153 if (Resource.isADispatchHazard()) {
154 assert(!Resource.isReserved());
155 Resource.setReserved();
156 }
157 }
158}
159
160bool ResourceManager::canBeIssued(const InstrDesc &Desc) const {
161 return std::all_of(Desc.Resources.begin(), Desc.Resources.end(),
162 [&](const std::pair<uint64_t, const ResourceUsage> &E) {
163 unsigned NumUnits =
164 E.second.isReserved() ? 0U : E.second.NumUnits;
165 return isReady(E.first, NumUnits);
166 });
167}
168
169// Returns true if all resources are in-order, and there is at least one
170// resource which is a dispatch hazard (BufferSize = 0).
171bool ResourceManager::mustIssueImmediately(const InstrDesc &Desc) {
172 if (!canBeIssued(Desc))
173 return false;
174 bool AllInOrderResources = std::all_of(
175 Desc.Buffers.begin(), Desc.Buffers.end(), [&](const unsigned BufferMask) {
176 const ResourceState &Resource = *Resources[BufferMask];
177 return Resource.isInOrder() || Resource.isADispatchHazard();
178 });
179 if (!AllInOrderResources)
180 return false;
181
182 return std::any_of(Desc.Buffers.begin(), Desc.Buffers.end(),
183 [&](const unsigned BufferMask) {
184 return Resources[BufferMask]->isADispatchHazard();
185 });
186}
187
188double ResourceManager::getRThroughput(const InstrDesc &ID) const {
189 double RThroughput = 0;
190 for (const std::pair<uint64_t, ResourceUsage> &Usage : ID.Resources) {
191 const CycleSegment &CS = Usage.second.CS;
192 assert(CS.begin() == 0);
193
194 if (Usage.second.isReserved()) {
195 RThroughput = std::max(RThroughput, (double)CS.size());
196 continue;
197 }
198
199 unsigned Population = std::max(1U, countPopulation(Usage.first) - 1);
200 unsigned NumUnits = Population * getNumUnits(Usage.first);
201 NumUnits -= Usage.second.NumUnits - 1;
202 unsigned Cycles = CS.size();
203 RThroughput = std::max(RThroughput, (double)Cycles / NumUnits);
204 }
205 return RThroughput;
206}
207
208void ResourceManager::issueInstruction(
209 unsigned Index, const InstrDesc &Desc,
210 SmallVectorImpl<std::pair<ResourceRef, unsigned>> &Pipes) {
211 releaseBuffers(Desc);
212 for (const std::pair<uint64_t, ResourceUsage> &R : Desc.Resources) {
213 const CycleSegment &CS = R.second.CS;
214 if (!CS.size()) {
215 releaseResource(R.first);
216 continue;
217 }
218
219 assert(CS.begin() == 0 && "Invalid {Start, End} cycles!");
220 if (!R.second.isReserved()) {
221 ResourceRef Pipe = selectPipe(R.first);
222 use(Pipe);
223 BusyResources[Pipe] += CS.size();
Andrea Di Biagio0c541292018-03-10 16:55:07 +0000224 // Replace the resource mask with a valid processor resource index.
225 const ResourceState &RS = *Resources[Pipe.first];
226 Pipe.first = RS.getProcResourceID();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000227 Pipes.emplace_back(std::pair<ResourceRef, unsigned>(Pipe, CS.size()));
228 } else {
229 assert((countPopulation(R.first) > 1) && "Expected a group!");
230 // Mark this group as reserved.
231 assert(R.second.isReserved());
232 reserveResource(R.first);
233 BusyResources[ResourceRef(R.first, R.first)] += CS.size();
234 }
235 }
236}
237
238void ResourceManager::cycleEvent(SmallVectorImpl<ResourceRef> &ResourcesFreed) {
239 for (std::pair<ResourceRef, unsigned> &BR : BusyResources) {
240 if (BR.second)
241 BR.second--;
242 if (!BR.second) {
243 // Release this resource.
244 const ResourceRef &RR = BR.first;
245
246 if (countPopulation(RR.first) == 1)
247 release(RR);
248
249 releaseResource(RR.first);
250 ResourcesFreed.push_back(RR);
251 }
252 }
253
254 for (const ResourceRef &RF : ResourcesFreed)
255 BusyResources.erase(RF);
256}
257
258Instruction *Scheduler::scheduleInstruction(unsigned Idx, Instruction *MCIS) {
259 assert(WaitQueue.find(Idx) == WaitQueue.end());
260 assert(ReadyQueue.find(Idx) == ReadyQueue.end());
261 assert(IssuedQueue.find(Idx) == IssuedQueue.end());
262
263 // Special case where MCIS is a zero-latency instruction. A zero-latency
264 // instruction doesn't consume any scheduler resources. That is because it
265 // doesn't need to be executed. Most of the times, zero latency instructions
266 // are removed at register renaming stage. For example, register-register
267 // moves can be removed at register renaming stage by creating new aliases.
268 // Zero-idiom instruction (for example: a `xor reg, reg`) can also be
269 // eliminated at register renaming stage, since we know in advance that those
270 // clear their output register.
271 if (MCIS->isZeroLatency()) {
Andrea Di Biagio373c38a2018-03-08 20:21:55 +0000272 notifyInstructionReady(Idx);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000273 MCIS->forceExecuted();
274 notifyInstructionIssued(Idx, {});
275 notifyInstructionExecuted(Idx);
276 return MCIS;
277 }
278
279 // Consume entries in the reservation stations.
280 const InstrDesc &Desc = MCIS->getDesc();
281 Resources->reserveBuffers(Desc);
282
283 // Mark units with BufferSize=0 as reserved. These resources will only
284 // be released after MCIS is issued, and all the ResourceCycles for
285 // those units have been consumed.
286 Resources->reserveDispatchHazardResources(Desc);
287
288 bool MayLoad = Desc.MayLoad;
289 bool MayStore = Desc.MayStore;
290 if (MayLoad || MayStore)
291 LSU->reserve(Idx, MayLoad, MayStore, Desc.HasSideEffects);
292
293 MCIS->dispatch();
294 bool IsReady = MCIS->isReady();
295 if (IsReady && (MayLoad || MayStore))
296 IsReady &= LSU->isReady(Idx);
297
298 if (!IsReady) {
299 DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx << " to the Wait Queue\n");
300 WaitQueue[Idx] = MCIS;
301 return MCIS;
302 }
303 notifyInstructionReady(Idx);
304
305 // Special case where the instruction is ready, and it uses an in-order
306 // dispatch/issue processor resource. The instruction is issued immediately to
307 // the pipelines. Any other in-order buffered resources (i.e. BufferSize=1)
308 // are consumed.
309 if (Resources->mustIssueImmediately(Desc)) {
310 DEBUG(dbgs() << "[SCHEDULER] Instruction " << Idx
311 << " issued immediately\n");
312 issueInstruction(MCIS, Idx);
313 return MCIS;
314 }
315
316 DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx << " to the Ready Queue\n");
317 ReadyQueue[Idx] = MCIS;
318 return MCIS;
319}
320
321void Scheduler::cycleEvent(unsigned /* unused */) {
322 SmallVector<ResourceRef, 8> ResourcesFreed;
323 Resources->cycleEvent(ResourcesFreed);
324
325 for (const ResourceRef &RR : ResourcesFreed)
326 notifyResourceAvailable(RR);
327
328 updateIssuedQueue();
329 updatePendingQueue();
330 issue();
331}
332
333#ifndef NDEBUG
334void Scheduler::dump() const {
335 dbgs() << "[SCHEDULER]: WaitQueue size is: " << WaitQueue.size() << '\n';
336 dbgs() << "[SCHEDULER]: ReadyQueue size is: " << ReadyQueue.size() << '\n';
337 dbgs() << "[SCHEDULER]: IssuedQueue size is: " << IssuedQueue.size() << '\n';
338 Resources->dump();
339}
340#endif
341
342Scheduler::Event Scheduler::canBeDispatched(const InstrDesc &Desc) const {
343 if (Desc.MayLoad && LSU->isLQFull())
344 return HWS_LD_QUEUE_UNAVAILABLE;
345 if (Desc.MayStore && LSU->isSQFull())
346 return HWS_ST_QUEUE_UNAVAILABLE;
347
348 Scheduler::Event Event;
349 switch (Resources->canBeDispatched(Desc)) {
350 case ResourceStateEvent::RS_BUFFER_AVAILABLE:
351 Event = HWS_AVAILABLE;
352 break;
353 case ResourceStateEvent::RS_BUFFER_UNAVAILABLE:
354 Event = HWS_QUEUE_UNAVAILABLE;
355 break;
356 case ResourceStateEvent::RS_RESERVED:
357 Event = HWS_DISPATCH_GROUP_RESTRICTION;
358 }
359 return Event;
360}
361
362void Scheduler::issueInstruction(Instruction *IS, unsigned InstrIndex) {
363 // Issue the instruction and collect all the consumed resources
364 // into a vector. That vector is then used to notify the listener.
365 // Most instructions consume very few resurces (typically one or
366 // two resources). We use a small vector here, and conservatively
367 // initialize its capacity to 4. This should address the majority of
368 // the cases.
369 SmallVector<std::pair<ResourceRef, unsigned>, 4> UsedResources;
370
371 const InstrDesc &D = IS->getDesc();
372 Resources->issueInstruction(InstrIndex, D, UsedResources);
373 // Notify the instruction that it started executing.
374 // This updates the internal state of each write.
375 IS->execute();
376
377 if (D.MaxLatency) {
378 IssuedQueue[InstrIndex] = IS;
379 notifyInstructionIssued(InstrIndex, UsedResources);
380 } else {
381 // A zero latency instruction which reads and/or updates registers.
382 notifyInstructionIssued(InstrIndex, UsedResources);
383 notifyInstructionExecuted(InstrIndex);
384 }
385}
386
387void Scheduler::issue() {
388 std::vector<unsigned> ToRemove;
389 for (const QueueEntryTy QueueEntry : ReadyQueue) {
390 // Give priority to older instructions in ReadyQueue. The ready queue is
391 // ordered by key, and therefore older instructions are visited first.
392 Instruction *IS = QueueEntry.second;
393 const InstrDesc &D = IS->getDesc();
394 if (!Resources->canBeIssued(D))
395 continue;
396 unsigned InstrIndex = QueueEntry.first;
397 issueInstruction(IS, InstrIndex);
398 ToRemove.emplace_back(InstrIndex);
399 }
400
401 for (const unsigned InstrIndex : ToRemove)
402 ReadyQueue.erase(InstrIndex);
403}
404
405void Scheduler::updatePendingQueue() {
406 // Scan the set of waiting instructions and promote them to the
407 // ready queue if operands are all ready.
408 for (auto I = WaitQueue.begin(), E = WaitQueue.end(); I != E;) {
409 const QueueEntryTy Entry = *I;
410 Entry.second->cycleEvent();
411
412 const InstrDesc &Desc = Entry.second->getDesc();
413 bool IsMemOp = Desc.MayLoad || Desc.MayStore;
414 bool IsReady = Entry.second->isReady();
415 if (IsReady && IsMemOp)
416 IsReady &= LSU->isReady(Entry.first);
417
418 if (IsReady) {
419 notifyInstructionReady(Entry.first);
420 ReadyQueue[Entry.first] = Entry.second;
421 auto ToRemove = I;
422 ++I;
423 WaitQueue.erase(ToRemove);
424 } else {
425 ++I;
426 }
427 }
428}
429
430void Scheduler::updateIssuedQueue() {
431 for (auto I = IssuedQueue.begin(), E = IssuedQueue.end(); I != E;) {
432 const QueueEntryTy Entry = *I;
433 Entry.second->cycleEvent();
434 if (Entry.second->isExecuted()) {
435 notifyInstructionExecuted(Entry.first);
436 auto ToRemove = I;
437 ++I;
438 IssuedQueue.erase(ToRemove);
439 } else {
440 DEBUG(dbgs() << "[SCHEDULER]: Instruction " << Entry.first
441 << " is still executing.\n");
442 ++I;
443 }
444 }
445}
446
447void Scheduler::notifyInstructionIssued(
448 unsigned Index, const ArrayRef<std::pair<ResourceRef, unsigned>> &Used) {
Clement Courbet844f22d2018-03-13 13:11:01 +0000449 DEBUG(dbgs() << "[E] Instruction Issued: " << Index << '\n';
450 for (const std::pair<ResourceRef, unsigned> &Resource
451 : Used) {
452 dbgs() << "[E] Resource Used: [" << Resource.first.first << '.'
453 << Resource.first.second << "]\n";
454 dbgs() << " cycles: " << Resource.second << '\n';
455 });
456 Owner->notifyInstructionEvent(HWInstructionIssuedEvent(Index, Used));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000457}
458
459void Scheduler::notifyInstructionExecuted(unsigned Index) {
460 LSU->onInstructionExecuted(Index);
Clement Courbet844f22d2018-03-13 13:11:01 +0000461 DEBUG(dbgs() << "[E] Instruction Executed: " << Index << '\n');
462 Owner->notifyInstructionEvent(
463 HWInstructionEvent(HWInstructionEvent::Executed, Index));
464
465 const Instruction &IS = Owner->getInstruction(Index);
466 DU->onInstructionExecuted(IS.getRCUTokenID());
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000467}
468
469void Scheduler::notifyInstructionReady(unsigned Index) {
Clement Courbet844f22d2018-03-13 13:11:01 +0000470 DEBUG(dbgs() << "[E] Instruction Ready: " << Index << '\n');
471 Owner->notifyInstructionEvent(
472 HWInstructionEvent(HWInstructionEvent::Ready, Index));
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000473}
474
475void Scheduler::notifyResourceAvailable(const ResourceRef &RR) {
476 Owner->notifyResourceAvailable(RR);
477}
478} // namespace mca