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