Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===--------------------- 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 Biagio | 51dba7d | 2018-03-23 17:36:07 +0000 | [diff] [blame] | 14 | #include "Scheduler.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 15 | #include "Backend.h" |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 16 | #include "HWEventListener.h" |
Andrea Di Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 17 | #include "Support.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | #define DEBUG_TYPE "llvm-mca" |
| 22 | |
| 23 | namespace mca { |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | uint64_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 |
| 38 | void 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 Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 47 | void 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 Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 53 | // 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. |
| 56 | void ResourceManager::addResource(const MCProcResourceDesc &Desc, |
Andrea Di Biagio | e1a1da1 | 2018-03-13 13:58:02 +0000 | [diff] [blame] | 57 | unsigned Index, uint64_t Mask) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 58 | assert(Resources.find(Mask) == Resources.end() && "Resource already added!"); |
Andrea Di Biagio | 0c54129 | 2018-03-10 16:55:07 +0000 | [diff] [blame] | 59 | Resources[Mask] = llvm::make_unique<ResourceState>(Desc, Index, Mask); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 62 | // Returns the actual resource consumed by this Use. |
| 63 | // First, is the primary resource ID. |
| 64 | // Second, is the specific sub-resource ID. |
| 65 | std::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 | |
| 73 | void 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 | |
| 87 | void 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 | |
| 109 | void 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 Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 126 | ResourceStateEvent |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 127 | ResourceManager::canBeDispatched(ArrayRef<uint64_t> Buffers) const { |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 128 | 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 Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 137 | void ResourceManager::reserveBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | e1a1da1 | 2018-03-13 13:58:02 +0000 | [diff] [blame] | 138 | for (const uint64_t R : Buffers) { |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 139 | reserveBuffer(R); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 140 | ResourceState &Resource = *Resources[R]; |
| 141 | if (Resource.isADispatchHazard()) { |
| 142 | assert(!Resource.isReserved()); |
| 143 | Resource.setReserved(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 148 | void ResourceManager::releaseBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 149 | for (const uint64_t R : Buffers) |
| 150 | releaseBuffer(R); |
| 151 | } |
| 152 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 153 | bool 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). |
| 164 | bool 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 Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 181 | void ResourceManager::issueInstruction( |
Matt Davis | ad78e66 | 2018-04-26 22:30:40 +0000 | [diff] [blame] | 182 | const InstrDesc &Desc, |
Andrea Di Biagio | 51dba7d | 2018-03-23 17:36:07 +0000 | [diff] [blame] | 183 | SmallVectorImpl<std::pair<ResourceRef, double>> &Pipes) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 184 | 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 Biagio | 0c54129 | 2018-03-10 16:55:07 +0000 | [diff] [blame] | 196 | // Replace the resource mask with a valid processor resource index. |
| 197 | const ResourceState &RS = *Resources[Pipe.first]; |
| 198 | Pipe.first = RS.getProcResourceID(); |
Andrea Di Biagio | 51dba7d | 2018-03-23 17:36:07 +0000 | [diff] [blame] | 199 | Pipes.emplace_back( |
| 200 | std::pair<ResourceRef, double>(Pipe, static_cast<double>(CS.size()))); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 201 | } 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 | |
| 211 | void 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 Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 231 | void Scheduler::scheduleInstruction(InstRef &IR) { |
| 232 | const unsigned Idx = IR.getSourceIndex(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 233 | assert(WaitQueue.find(Idx) == WaitQueue.end()); |
| 234 | assert(ReadyQueue.find(Idx) == ReadyQueue.end()); |
| 235 | assert(IssuedQueue.find(Idx) == IssuedQueue.end()); |
| 236 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 237 | // 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 Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 241 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 242 | reserveBuffers(Desc.Buffers); |
| 243 | notifyReservedBuffers(Desc.Buffers); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 244 | |
Andrea Di Biagio | 2dee62b | 2018-03-22 14:14:49 +0000 | [diff] [blame] | 245 | // If necessary, reserve queue entries in the load-store unit (LSU). |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 246 | bool Reserved = LSU->reserve(IR); |
| 247 | if (!IR.getInstruction()->isReady() || (Reserved && !LSU->isReady(IR))) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 248 | DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx << " to the Wait Queue\n"); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 249 | WaitQueue[Idx] = IR.getInstruction(); |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 250 | return; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 251 | } |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 252 | notifyInstructionReady(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 253 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 254 | // Don't add a zero-latency instruction to the Wait or Ready queue. |
| 255 | // A zero-latency instruction doesn't consume any scheduler resources. That is |
| 256 | // because it doesn't need to be executed, and it is often removed at register |
| 257 | // renaming stage. For example, register-register moves are often optimized at |
| 258 | // register renaming stage by simply updating register aliases. On some |
| 259 | // targets, zero-idiom instructions (for example: a xor that clears the value |
| 260 | // of a register) are treated speacially, and are often eliminated at register |
| 261 | // renaming stage. |
Andrea Di Biagio | e047d35 | 2018-04-30 15:55:04 +0000 | [diff] [blame] | 262 | bool IsZeroLatency = !Desc.MaxLatency && Desc.Resources.empty(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 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 Biagio | e047d35 | 2018-04-30 15:55:04 +0000 | [diff] [blame] | 268 | if (!IsZeroLatency && !Resources->mustIssueImmediately(Desc)) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 269 | DEBUG(dbgs() << "[SCHEDULER] Adding " << IR << " to the Ready Queue\n"); |
| 270 | ReadyQueue[IR.getSourceIndex()] = IR.getInstruction(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 271 | return; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 274 | DEBUG(dbgs() << "[SCHEDULER] Instruction " << IR << " issued immediately\n"); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 275 | // Release buffered resources and issue MCIS to the underlying pipelines. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 276 | issueInstruction(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Andrea Di Biagio | 3e64644 | 2018-04-12 10:49:40 +0000 | [diff] [blame] | 279 | void Scheduler::cycleEvent() { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 280 | SmallVector<ResourceRef, 8> ResourcesFreed; |
| 281 | Resources->cycleEvent(ResourcesFreed); |
| 282 | |
| 283 | for (const ResourceRef &RR : ResourcesFreed) |
| 284 | notifyResourceAvailable(RR); |
| 285 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 286 | SmallVector<InstRef, 4> InstructionIDs; |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 287 | updateIssuedQueue(InstructionIDs); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 288 | for (const InstRef &IR : InstructionIDs) |
| 289 | notifyInstructionExecuted(IR); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 290 | InstructionIDs.clear(); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 291 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 292 | updatePendingQueue(InstructionIDs); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 293 | for (const InstRef &IR : InstructionIDs) |
| 294 | notifyInstructionReady(IR); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 295 | InstructionIDs.clear(); |
| 296 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 297 | InstRef IR = select(); |
| 298 | while (IR.isValid()) { |
| 299 | issueInstruction(IR); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 300 | |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 301 | // 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 Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 306 | promoteToReadyQueue(InstructionIDs); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 307 | for (const InstRef &I : InstructionIDs) |
| 308 | notifyInstructionReady(I); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 309 | InstructionIDs.clear(); |
| 310 | |
| 311 | // Select the next instruction to issue. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 312 | IR = select(); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 313 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | #ifndef NDEBUG |
| 317 | void 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 Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 325 | bool Scheduler::canBeDispatched(const InstRef &IR) const { |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 326 | HWStallEvent::GenericEventType Type = HWStallEvent::Invalid; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 327 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 328 | |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 329 | 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 Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 335 | default: |
| 336 | return true; |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 337 | case ResourceStateEvent::RS_BUFFER_UNAVAILABLE: |
| 338 | Type = HWStallEvent::SchedulerQueueFull; |
| 339 | break; |
| 340 | case ResourceStateEvent::RS_RESERVED: |
| 341 | Type = HWStallEvent::DispatchGroupStall; |
| 342 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 343 | } |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 344 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 345 | Owner->notifyStallEvent(HWStallEvent(Type, IR)); |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 346 | return false; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 349 | void Scheduler::issueInstructionImpl( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 350 | InstRef &IR, |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 351 | SmallVectorImpl<std::pair<ResourceRef, double>> &UsedResources) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 352 | Instruction *IS = IR.getInstruction(); |
| 353 | const InstrDesc &D = IS->getDesc(); |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 354 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 355 | // Issue the instruction and collect all the consumed resources |
| 356 | // into a vector. That vector is then used to notify the listener. |
Matt Davis | ad78e66 | 2018-04-26 22:30:40 +0000 | [diff] [blame] | 357 | Resources->issueInstruction(D, UsedResources); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 358 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 359 | // Notify the instruction that it started executing. |
| 360 | // This updates the internal state of each write. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 361 | IS->execute(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 362 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 363 | if (IS->isExecuting()) |
| 364 | IssuedQueue[IR.getSourceIndex()] = IS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 367 | void Scheduler::issueInstruction(InstRef &IR) { |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 368 | // Release buffered resources. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 369 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 370 | 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 Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 375 | issueInstructionImpl(IR, Pipes); |
| 376 | notifyInstructionIssued(IR, Pipes); |
| 377 | if (IR.getInstruction()->isExecuted()) |
| 378 | notifyInstructionExecuted(IR); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 381 | void Scheduler::promoteToReadyQueue(SmallVectorImpl<InstRef> &Ready) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 382 | // Scan the set of waiting instructions and promote them to the |
| 383 | // ready queue if operands are all ready. |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 384 | for (auto I = WaitQueue.begin(), E = WaitQueue.end(); I != E;) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 385 | const unsigned IID = I->first; |
| 386 | Instruction *IS = I->second; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 387 | |
| 388 | // Check if this instruction is now ready. In case, force |
| 389 | // a transition in state using method 'update()'. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 390 | IS->update(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 391 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 392 | const InstrDesc &Desc = IS->getDesc(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 393 | bool IsMemOp = Desc.MayLoad || Desc.MayStore; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 394 | if (!IS->isReady() || (IsMemOp && !LSU->isReady({IID, IS}))) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 395 | ++I; |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 396 | continue; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 397 | } |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 398 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 399 | Ready.emplace_back(IID, IS); |
| 400 | ReadyQueue[IID] = IS; |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 401 | auto ToRemove = I; |
| 402 | ++I; |
| 403 | WaitQueue.erase(ToRemove); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 404 | } |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 407 | InstRef Scheduler::select() { |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 408 | // 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 Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 412 | const InstrDesc &D = Entry.second->getDesc(); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 413 | return Resources->canBeIssued(D); |
| 414 | }); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 415 | |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 416 | if (It == ReadyQueue.end()) |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 417 | return {0, nullptr}; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 418 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 419 | // We found an instruction to issue. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 420 | InstRef IR(It->first, It->second); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 421 | ReadyQueue.erase(It); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 422 | return IR; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 425 | void Scheduler::updatePendingQueue(SmallVectorImpl<InstRef> &Ready) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 426 | // Notify to instructions in the pending queue that a new cycle just |
| 427 | // started. |
| 428 | for (QueueEntryTy Entry : WaitQueue) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 429 | Entry.second->cycleEvent(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 430 | promoteToReadyQueue(Ready); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 433 | void Scheduler::updateIssuedQueue(SmallVectorImpl<InstRef> &Executed) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 434 | for (auto I = IssuedQueue.begin(), E = IssuedQueue.end(); I != E;) { |
| 435 | const QueueEntryTy Entry = *I; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 436 | Instruction *IS = Entry.second; |
| 437 | IS->cycleEvent(); |
| 438 | if (IS->isExecuted()) { |
| 439 | Executed.push_back({Entry.first, Entry.second}); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 440 | auto ToRemove = I; |
| 441 | ++I; |
| 442 | IssuedQueue.erase(ToRemove); |
| 443 | } else { |
| 444 | DEBUG(dbgs() << "[SCHEDULER]: Instruction " << Entry.first |
| 445 | << " is still executing.\n"); |
| 446 | ++I; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void Scheduler::notifyInstructionIssued( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 452 | const InstRef &IR, ArrayRef<std::pair<ResourceRef, double>> Used) { |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 453 | DEBUG({ |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 454 | dbgs() << "[E] Instruction Issued: " << IR << '\n'; |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 455 | 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 Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 461 | Owner->notifyInstructionEvent(HWInstructionIssuedEvent(IR, Used)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 464 | void Scheduler::notifyInstructionExecuted(const InstRef &IR) { |
| 465 | LSU->onInstructionExecuted(IR); |
| 466 | DEBUG(dbgs() << "[E] Instruction Executed: " << IR << '\n'); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 467 | Owner->notifyInstructionEvent( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 468 | HWInstructionEvent(HWInstructionEvent::Executed, IR)); |
| 469 | DU->onInstructionExecuted(IR.getInstruction()->getRCUTokenID()); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 472 | void Scheduler::notifyInstructionReady(const InstRef &IR) { |
| 473 | DEBUG(dbgs() << "[E] Instruction Ready: " << IR << '\n'); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 474 | Owner->notifyInstructionEvent( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 475 | HWInstructionEvent(HWInstructionEvent::Ready, IR)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void Scheduler::notifyResourceAvailable(const ResourceRef &RR) { |
| 479 | Owner->notifyResourceAvailable(RR); |
| 480 | } |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 481 | |
| 482 | void Scheduler::notifyReservedBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 483 | if (Buffers.empty()) |
| 484 | return; |
| 485 | |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 486 | 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 | |
| 493 | void Scheduler::notifyReleasedBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 494 | if (Buffers.empty()) |
| 495 | return; |
| 496 | |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 497 | 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 Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 503 | } // namespace mca |