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 | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 14 | #include "Backend.h" |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 15 | #include "HWEventListener.h" |
Andrea Di Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 16 | #include "Scheduler.h" |
| 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( |
| 182 | unsigned Index, const InstrDesc &Desc, |
| 183 | SmallVectorImpl<std::pair<ResourceRef, unsigned>> &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 | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 199 | Pipes.emplace_back(std::pair<ResourceRef, unsigned>(Pipe, CS.size())); |
| 200 | } else { |
| 201 | assert((countPopulation(R.first) > 1) && "Expected a group!"); |
| 202 | // Mark this group as reserved. |
| 203 | assert(R.second.isReserved()); |
| 204 | reserveResource(R.first); |
| 205 | BusyResources[ResourceRef(R.first, R.first)] += CS.size(); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void ResourceManager::cycleEvent(SmallVectorImpl<ResourceRef> &ResourcesFreed) { |
| 211 | for (std::pair<ResourceRef, unsigned> &BR : BusyResources) { |
| 212 | if (BR.second) |
| 213 | BR.second--; |
| 214 | if (!BR.second) { |
| 215 | // Release this resource. |
| 216 | const ResourceRef &RR = BR.first; |
| 217 | |
| 218 | if (countPopulation(RR.first) == 1) |
| 219 | release(RR); |
| 220 | |
| 221 | releaseResource(RR.first); |
| 222 | ResourcesFreed.push_back(RR); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | for (const ResourceRef &RF : ResourcesFreed) |
| 227 | BusyResources.erase(RF); |
| 228 | } |
| 229 | |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 230 | void Scheduler::scheduleInstruction(unsigned Idx, Instruction &MCIS) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 231 | assert(WaitQueue.find(Idx) == WaitQueue.end()); |
| 232 | assert(ReadyQueue.find(Idx) == ReadyQueue.end()); |
| 233 | assert(IssuedQueue.find(Idx) == IssuedQueue.end()); |
| 234 | |
| 235 | // Special case where MCIS is a zero-latency instruction. A zero-latency |
| 236 | // instruction doesn't consume any scheduler resources. That is because it |
| 237 | // doesn't need to be executed. Most of the times, zero latency instructions |
| 238 | // are removed at register renaming stage. For example, register-register |
| 239 | // moves can be removed at register renaming stage by creating new aliases. |
| 240 | // Zero-idiom instruction (for example: a `xor reg, reg`) can also be |
| 241 | // eliminated at register renaming stage, since we know in advance that those |
| 242 | // clear their output register. |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 243 | if (MCIS.isZeroLatency()) { |
Andrea Di Biagio | 373c38a | 2018-03-08 20:21:55 +0000 | [diff] [blame] | 244 | notifyInstructionReady(Idx); |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 245 | MCIS.forceExecuted(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 246 | notifyInstructionIssued(Idx, {}); |
| 247 | notifyInstructionExecuted(Idx); |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 248 | return; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // Consume entries in the reservation stations. |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 252 | const InstrDesc &Desc = MCIS.getDesc(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 253 | |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 254 | if (!Desc.Buffers.empty()) { |
| 255 | // Reserve a slot in each buffered resource. Also, mark units with |
| 256 | // BufferSize=0 as reserved. Resources with a buffer size of zero will only |
| 257 | // be released after MCIS is issued, and all the ResourceCycles for those |
| 258 | // units have been consumed. |
| 259 | Resources->reserveBuffers(Desc.Buffers); |
| 260 | notifyReservedBuffers(Desc.Buffers); |
| 261 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 262 | |
| 263 | bool MayLoad = Desc.MayLoad; |
| 264 | bool MayStore = Desc.MayStore; |
| 265 | if (MayLoad || MayStore) |
| 266 | LSU->reserve(Idx, MayLoad, MayStore, Desc.HasSideEffects); |
| 267 | |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 268 | MCIS.dispatch(); |
| 269 | bool IsReady = MCIS.isReady(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 270 | if (IsReady && (MayLoad || MayStore)) |
| 271 | IsReady &= LSU->isReady(Idx); |
| 272 | |
| 273 | if (!IsReady) { |
| 274 | DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx << " to the Wait Queue\n"); |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 275 | WaitQueue[Idx] = &MCIS; |
| 276 | return; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 277 | } |
| 278 | notifyInstructionReady(Idx); |
| 279 | |
| 280 | // Special case where the instruction is ready, and it uses an in-order |
| 281 | // dispatch/issue processor resource. The instruction is issued immediately to |
| 282 | // the pipelines. Any other in-order buffered resources (i.e. BufferSize=1) |
| 283 | // are consumed. |
| 284 | if (Resources->mustIssueImmediately(Desc)) { |
| 285 | DEBUG(dbgs() << "[SCHEDULER] Instruction " << Idx |
| 286 | << " issued immediately\n"); |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 287 | return issueInstruction(MCIS, Idx); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx << " to the Ready Queue\n"); |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 291 | ReadyQueue[Idx] = &MCIS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void Scheduler::cycleEvent(unsigned /* unused */) { |
| 295 | SmallVector<ResourceRef, 8> ResourcesFreed; |
| 296 | Resources->cycleEvent(ResourcesFreed); |
| 297 | |
| 298 | for (const ResourceRef &RR : ResourcesFreed) |
| 299 | notifyResourceAvailable(RR); |
| 300 | |
| 301 | updateIssuedQueue(); |
| 302 | updatePendingQueue(); |
| 303 | issue(); |
| 304 | } |
| 305 | |
| 306 | #ifndef NDEBUG |
| 307 | void Scheduler::dump() const { |
| 308 | dbgs() << "[SCHEDULER]: WaitQueue size is: " << WaitQueue.size() << '\n'; |
| 309 | dbgs() << "[SCHEDULER]: ReadyQueue size is: " << ReadyQueue.size() << '\n'; |
| 310 | dbgs() << "[SCHEDULER]: IssuedQueue size is: " << IssuedQueue.size() << '\n'; |
| 311 | Resources->dump(); |
| 312 | } |
| 313 | #endif |
| 314 | |
| 315 | Scheduler::Event Scheduler::canBeDispatched(const InstrDesc &Desc) const { |
| 316 | if (Desc.MayLoad && LSU->isLQFull()) |
| 317 | return HWS_LD_QUEUE_UNAVAILABLE; |
| 318 | if (Desc.MayStore && LSU->isSQFull()) |
| 319 | return HWS_ST_QUEUE_UNAVAILABLE; |
| 320 | |
| 321 | Scheduler::Event Event; |
Andrea Di Biagio | e1a1da1 | 2018-03-13 13:58:02 +0000 | [diff] [blame] | 322 | switch (Resources->canBeDispatched(Desc.Buffers)) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 323 | case ResourceStateEvent::RS_BUFFER_AVAILABLE: |
| 324 | Event = HWS_AVAILABLE; |
| 325 | break; |
| 326 | case ResourceStateEvent::RS_BUFFER_UNAVAILABLE: |
| 327 | Event = HWS_QUEUE_UNAVAILABLE; |
| 328 | break; |
| 329 | case ResourceStateEvent::RS_RESERVED: |
| 330 | Event = HWS_DISPATCH_GROUP_RESTRICTION; |
| 331 | } |
| 332 | return Event; |
| 333 | } |
| 334 | |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 335 | void Scheduler::issueInstruction(Instruction &IS, unsigned InstrIndex) { |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 336 | const InstrDesc &D = IS.getDesc(); |
| 337 | |
| 338 | if (!D.Buffers.empty()) { |
| 339 | Resources->releaseBuffers(D.Buffers); |
| 340 | notifyReleasedBuffers(D.Buffers); |
| 341 | } |
| 342 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 343 | // Issue the instruction and collect all the consumed resources |
| 344 | // into a vector. That vector is then used to notify the listener. |
| 345 | // Most instructions consume very few resurces (typically one or |
| 346 | // two resources). We use a small vector here, and conservatively |
| 347 | // initialize its capacity to 4. This should address the majority of |
| 348 | // the cases. |
| 349 | SmallVector<std::pair<ResourceRef, unsigned>, 4> UsedResources; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 350 | Resources->issueInstruction(InstrIndex, D, UsedResources); |
| 351 | // Notify the instruction that it started executing. |
| 352 | // This updates the internal state of each write. |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 353 | IS.execute(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 354 | |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame^] | 355 | notifyInstructionIssued(InstrIndex, UsedResources); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 356 | if (D.MaxLatency) { |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame^] | 357 | assert(IS.isExecuting() && "A zero latency instruction?"); |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 358 | IssuedQueue[InstrIndex] = &IS; |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame^] | 359 | return; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 360 | } |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame^] | 361 | |
| 362 | // A zero latency instruction which reads and/or updates registers. |
| 363 | assert(IS.isExecuted() && "Instruction still executing!"); |
| 364 | notifyInstructionExecuted(InstrIndex); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void Scheduler::issue() { |
| 368 | std::vector<unsigned> ToRemove; |
| 369 | for (const QueueEntryTy QueueEntry : ReadyQueue) { |
| 370 | // Give priority to older instructions in ReadyQueue. The ready queue is |
| 371 | // ordered by key, and therefore older instructions are visited first. |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 372 | Instruction &IS = *QueueEntry.second; |
| 373 | const InstrDesc &D = IS.getDesc(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 374 | if (!Resources->canBeIssued(D)) |
| 375 | continue; |
| 376 | unsigned InstrIndex = QueueEntry.first; |
| 377 | issueInstruction(IS, InstrIndex); |
| 378 | ToRemove.emplace_back(InstrIndex); |
| 379 | } |
| 380 | |
| 381 | for (const unsigned InstrIndex : ToRemove) |
| 382 | ReadyQueue.erase(InstrIndex); |
| 383 | } |
| 384 | |
| 385 | void Scheduler::updatePendingQueue() { |
| 386 | // Scan the set of waiting instructions and promote them to the |
| 387 | // ready queue if operands are all ready. |
| 388 | for (auto I = WaitQueue.begin(), E = WaitQueue.end(); I != E;) { |
| 389 | const QueueEntryTy Entry = *I; |
| 390 | Entry.second->cycleEvent(); |
| 391 | |
| 392 | const InstrDesc &Desc = Entry.second->getDesc(); |
| 393 | bool IsMemOp = Desc.MayLoad || Desc.MayStore; |
| 394 | bool IsReady = Entry.second->isReady(); |
| 395 | if (IsReady && IsMemOp) |
| 396 | IsReady &= LSU->isReady(Entry.first); |
| 397 | |
| 398 | if (IsReady) { |
| 399 | notifyInstructionReady(Entry.first); |
| 400 | ReadyQueue[Entry.first] = Entry.second; |
| 401 | auto ToRemove = I; |
| 402 | ++I; |
| 403 | WaitQueue.erase(ToRemove); |
| 404 | } else { |
| 405 | ++I; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void Scheduler::updateIssuedQueue() { |
| 411 | for (auto I = IssuedQueue.begin(), E = IssuedQueue.end(); I != E;) { |
| 412 | const QueueEntryTy Entry = *I; |
| 413 | Entry.second->cycleEvent(); |
| 414 | if (Entry.second->isExecuted()) { |
| 415 | notifyInstructionExecuted(Entry.first); |
| 416 | auto ToRemove = I; |
| 417 | ++I; |
| 418 | IssuedQueue.erase(ToRemove); |
| 419 | } else { |
| 420 | DEBUG(dbgs() << "[SCHEDULER]: Instruction " << Entry.first |
| 421 | << " is still executing.\n"); |
| 422 | ++I; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void Scheduler::notifyInstructionIssued( |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 428 | unsigned Index, ArrayRef<std::pair<ResourceRef, unsigned>> Used) { |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 429 | DEBUG({ |
| 430 | dbgs() << "[E] Instruction Issued: " << Index << '\n'; |
| 431 | for (const std::pair<ResourceRef, unsigned> &Resource : Used) { |
| 432 | dbgs() << "[E] Resource Used: [" << Resource.first.first << '.' |
| 433 | << Resource.first.second << "]\n"; |
| 434 | dbgs() << " cycles: " << Resource.second << '\n'; |
| 435 | } |
| 436 | }); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 437 | Owner->notifyInstructionEvent(HWInstructionIssuedEvent(Index, Used)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void Scheduler::notifyInstructionExecuted(unsigned Index) { |
| 441 | LSU->onInstructionExecuted(Index); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 442 | DEBUG(dbgs() << "[E] Instruction Executed: " << Index << '\n'); |
| 443 | Owner->notifyInstructionEvent( |
| 444 | HWInstructionEvent(HWInstructionEvent::Executed, Index)); |
| 445 | |
| 446 | const Instruction &IS = Owner->getInstruction(Index); |
| 447 | DU->onInstructionExecuted(IS.getRCUTokenID()); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void Scheduler::notifyInstructionReady(unsigned Index) { |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 451 | DEBUG(dbgs() << "[E] Instruction Ready: " << Index << '\n'); |
| 452 | Owner->notifyInstructionEvent( |
| 453 | HWInstructionEvent(HWInstructionEvent::Ready, Index)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void Scheduler::notifyResourceAvailable(const ResourceRef &RR) { |
| 457 | Owner->notifyResourceAvailable(RR); |
| 458 | } |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 459 | |
| 460 | void Scheduler::notifyReservedBuffers(ArrayRef<uint64_t> Buffers) { |
| 461 | SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end()); |
| 462 | std::transform( |
| 463 | Buffers.begin(), Buffers.end(), BufferIDs.begin(), |
| 464 | [&](uint64_t Op) { return Resources->resolveResourceMask(Op); }); |
| 465 | Owner->notifyReservedBuffers(BufferIDs); |
| 466 | } |
| 467 | |
| 468 | void Scheduler::notifyReleasedBuffers(ArrayRef<uint64_t> Buffers) { |
| 469 | SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end()); |
| 470 | std::transform( |
| 471 | Buffers.begin(), Buffers.end(), BufferIDs.begin(), |
| 472 | [&](uint64_t Op) { return Resources->resolveResourceMask(Op); }); |
| 473 | Owner->notifyReleasedBuffers(BufferIDs); |
| 474 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 475 | } // namespace mca |