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( |
| 182 | unsigned Index, 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 | |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 231 | void Scheduler::scheduleInstruction(unsigned Idx, Instruction &MCIS) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 232 | assert(WaitQueue.find(Idx) == WaitQueue.end()); |
| 233 | assert(ReadyQueue.find(Idx) == ReadyQueue.end()); |
| 234 | assert(IssuedQueue.find(Idx) == IssuedQueue.end()); |
| 235 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 236 | // Reserve a slot in each buffered resource. Also, mark units with |
| 237 | // BufferSize=0 as reserved. Resources with a buffer size of zero will only |
| 238 | // be released after MCIS is issued, and all the ResourceCycles for those |
| 239 | // units have been consumed. |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 240 | const InstrDesc &Desc = MCIS.getDesc(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 241 | reserveBuffers(Desc.Buffers); |
| 242 | notifyReservedBuffers(Desc.Buffers); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 243 | |
Andrea Di Biagio | 2dee62b | 2018-03-22 14:14:49 +0000 | [diff] [blame] | 244 | // If necessary, reserve queue entries in the load-store unit (LSU). |
| 245 | bool Reserved = LSU->reserve(Idx, Desc); |
| 246 | if (!MCIS.isReady() || (Reserved && !LSU->isReady(Idx))) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 247 | DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx << " to the Wait Queue\n"); |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 248 | WaitQueue[Idx] = &MCIS; |
| 249 | return; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 250 | } |
| 251 | notifyInstructionReady(Idx); |
| 252 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 253 | // Don't add a zero-latency instruction to the Wait or Ready queue. |
| 254 | // A zero-latency instruction doesn't consume any scheduler resources. That is |
| 255 | // because it doesn't need to be executed, and it is often removed at register |
| 256 | // renaming stage. For example, register-register moves are often optimized at |
| 257 | // register renaming stage by simply updating register aliases. On some |
| 258 | // targets, zero-idiom instructions (for example: a xor that clears the value |
| 259 | // of a register) are treated speacially, and are often eliminated at register |
| 260 | // renaming stage. |
| 261 | |
| 262 | // Instructions that use an in-order dispatch/issue processor resource must be |
| 263 | // issued immediately to the pipeline(s). Any other in-order buffered |
| 264 | // resources (i.e. BufferSize=1) is consumed. |
| 265 | |
| 266 | if (!MCIS.isZeroLatency() && !Resources->mustIssueImmediately(Desc)) { |
| 267 | DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx << " to the Ready Queue\n"); |
| 268 | ReadyQueue[Idx] = &MCIS; |
| 269 | return; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 272 | DEBUG(dbgs() << "[SCHEDULER] Instruction " << Idx << " issued immediately\n"); |
| 273 | // Release buffered resources and issue MCIS to the underlying pipelines. |
| 274 | issueInstruction(Idx, MCIS); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Andrea Di Biagio | 3e64644 | 2018-04-12 10:49:40 +0000 | [diff] [blame] | 277 | void Scheduler::cycleEvent() { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 278 | SmallVector<ResourceRef, 8> ResourcesFreed; |
| 279 | Resources->cycleEvent(ResourcesFreed); |
| 280 | |
| 281 | for (const ResourceRef &RR : ResourcesFreed) |
| 282 | notifyResourceAvailable(RR); |
| 283 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 284 | SmallVector<unsigned, 4> InstructionIDs; |
| 285 | updateIssuedQueue(InstructionIDs); |
| 286 | for (unsigned Idx : InstructionIDs) |
| 287 | notifyInstructionExecuted(Idx); |
| 288 | InstructionIDs.clear(); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 289 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 290 | updatePendingQueue(InstructionIDs); |
| 291 | for (unsigned Idx : InstructionIDs) |
| 292 | notifyInstructionReady(Idx); |
| 293 | InstructionIDs.clear(); |
| 294 | |
| 295 | std::pair<unsigned, Instruction *> Inst = select(); |
| 296 | while (Inst.second) { |
| 297 | issueInstruction(Inst.first, *Inst.second); |
| 298 | |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 299 | // Instructions that have been issued during this cycle might have unblocked |
| 300 | // other dependent instructions. Dependent instructions may be issued during |
| 301 | // this same cycle if operands have ReadAdvance entries. Promote those |
| 302 | // instructions to the ReadyQueue and tell to the caller that we need |
| 303 | // another round of 'issue()'. |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 304 | promoteToReadyQueue(InstructionIDs); |
| 305 | for (unsigned Idx : InstructionIDs) |
| 306 | notifyInstructionReady(Idx); |
| 307 | InstructionIDs.clear(); |
| 308 | |
| 309 | // Select the next instruction to issue. |
| 310 | Inst = select(); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 311 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | #ifndef NDEBUG |
| 315 | void Scheduler::dump() const { |
| 316 | dbgs() << "[SCHEDULER]: WaitQueue size is: " << WaitQueue.size() << '\n'; |
| 317 | dbgs() << "[SCHEDULER]: ReadyQueue size is: " << ReadyQueue.size() << '\n'; |
| 318 | dbgs() << "[SCHEDULER]: IssuedQueue size is: " << IssuedQueue.size() << '\n'; |
| 319 | Resources->dump(); |
| 320 | } |
| 321 | #endif |
| 322 | |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 323 | bool Scheduler::canBeDispatched(unsigned Index, const InstrDesc &Desc) const { |
| 324 | HWStallEvent::GenericEventType Type = HWStallEvent::Invalid; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 325 | |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 326 | if (Desc.MayLoad && LSU->isLQFull()) |
| 327 | Type = HWStallEvent::LoadQueueFull; |
| 328 | else if (Desc.MayStore && LSU->isSQFull()) |
| 329 | Type = HWStallEvent::StoreQueueFull; |
| 330 | else { |
| 331 | switch (Resources->canBeDispatched(Desc.Buffers)) { |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 332 | default: |
| 333 | return true; |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 334 | case ResourceStateEvent::RS_BUFFER_UNAVAILABLE: |
| 335 | Type = HWStallEvent::SchedulerQueueFull; |
| 336 | break; |
| 337 | case ResourceStateEvent::RS_RESERVED: |
| 338 | Type = HWStallEvent::DispatchGroupStall; |
| 339 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 340 | } |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 341 | |
| 342 | Owner->notifyStallEvent(HWStallEvent(Type, Index)); |
| 343 | return false; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 346 | void Scheduler::issueInstructionImpl( |
| 347 | unsigned InstrIndex, Instruction &IS, |
| 348 | SmallVectorImpl<std::pair<ResourceRef, double>> &UsedResources) { |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 349 | const InstrDesc &D = IS.getDesc(); |
| 350 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 351 | // Issue the instruction and collect all the consumed resources |
| 352 | // into a vector. That vector is then used to notify the listener. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 353 | Resources->issueInstruction(InstrIndex, D, UsedResources); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 354 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 355 | // Notify the instruction that it started executing. |
| 356 | // This updates the internal state of each write. |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 357 | IS.execute(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 358 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 359 | if (IS.isExecuting()) |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 360 | IssuedQueue[InstrIndex] = &IS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 363 | void Scheduler::issueInstruction(unsigned InstrIndex, Instruction &IS) { |
| 364 | // Release buffered resources. |
| 365 | const InstrDesc &Desc = IS.getDesc(); |
| 366 | releaseBuffers(Desc.Buffers); |
| 367 | notifyReleasedBuffers(Desc.Buffers); |
| 368 | |
| 369 | // Issue IS to the underlying pipelines and notify listeners. |
| 370 | SmallVector<std::pair<ResourceRef, double>, 4> Pipes; |
| 371 | issueInstructionImpl(InstrIndex, IS, Pipes); |
| 372 | notifyInstructionIssued(InstrIndex, Pipes); |
| 373 | if (IS.isExecuted()) |
| 374 | notifyInstructionExecuted(InstrIndex); |
| 375 | } |
| 376 | |
| 377 | void Scheduler::promoteToReadyQueue(SmallVectorImpl<unsigned> &Ready) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 378 | // Scan the set of waiting instructions and promote them to the |
| 379 | // ready queue if operands are all ready. |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 380 | for (auto I = WaitQueue.begin(), E = WaitQueue.end(); I != E;) { |
| 381 | const QueueEntryTy &Entry = *I; |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 382 | unsigned IID = Entry.first; |
| 383 | Instruction &Inst = *Entry.second; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 384 | |
| 385 | // Check if this instruction is now ready. In case, force |
| 386 | // a transition in state using method 'update()'. |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 387 | Inst.update(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 388 | |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 389 | const InstrDesc &Desc = Inst.getDesc(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 390 | bool IsMemOp = Desc.MayLoad || Desc.MayStore; |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 391 | if (!Inst.isReady() || (IsMemOp && !LSU->isReady(IID))) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 392 | ++I; |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 393 | continue; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 394 | } |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 395 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 396 | Ready.emplace_back(IID); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 397 | ReadyQueue[IID] = &Inst; |
| 398 | auto ToRemove = I; |
| 399 | ++I; |
| 400 | WaitQueue.erase(ToRemove); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 401 | } |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 404 | std::pair<unsigned, Instruction *> Scheduler::select() { |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 405 | // Give priority to older instructions in the ReadyQueue. Since the ready |
| 406 | // queue is ordered by key, this will always prioritize older instructions. |
| 407 | const auto It = std::find_if(ReadyQueue.begin(), ReadyQueue.end(), |
| 408 | [&](const QueueEntryTy &Entry) { |
| 409 | const Instruction &IS = *Entry.second; |
| 410 | const InstrDesc &D = IS.getDesc(); |
| 411 | return Resources->canBeIssued(D); |
| 412 | }); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 413 | |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 414 | if (It == ReadyQueue.end()) |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 415 | return {0, nullptr}; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 416 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 417 | // We found an instruction to issue. |
| 418 | const QueueEntryTy Entry = *It; |
| 419 | ReadyQueue.erase(It); |
| 420 | return Entry; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 423 | void Scheduler::updatePendingQueue(SmallVectorImpl<unsigned> &Ready) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 424 | // Notify to instructions in the pending queue that a new cycle just |
| 425 | // started. |
| 426 | for (QueueEntryTy Entry : WaitQueue) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 427 | Entry.second->cycleEvent(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 428 | promoteToReadyQueue(Ready); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 431 | void Scheduler::updateIssuedQueue(SmallVectorImpl<unsigned> &Executed) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 432 | for (auto I = IssuedQueue.begin(), E = IssuedQueue.end(); I != E;) { |
| 433 | const QueueEntryTy Entry = *I; |
| 434 | Entry.second->cycleEvent(); |
| 435 | if (Entry.second->isExecuted()) { |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 436 | Executed.push_back(Entry.first); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 437 | auto ToRemove = I; |
| 438 | ++I; |
| 439 | IssuedQueue.erase(ToRemove); |
| 440 | } else { |
| 441 | DEBUG(dbgs() << "[SCHEDULER]: Instruction " << Entry.first |
| 442 | << " is still executing.\n"); |
| 443 | ++I; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void Scheduler::notifyInstructionIssued( |
Andrea Di Biagio | 51dba7d | 2018-03-23 17:36:07 +0000 | [diff] [blame] | 449 | unsigned Index, ArrayRef<std::pair<ResourceRef, double>> Used) { |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 450 | DEBUG({ |
| 451 | dbgs() << "[E] Instruction Issued: " << Index << '\n'; |
| 452 | for (const std::pair<ResourceRef, unsigned> &Resource : Used) { |
| 453 | dbgs() << "[E] Resource Used: [" << Resource.first.first << '.' |
| 454 | << Resource.first.second << "]\n"; |
| 455 | dbgs() << " cycles: " << Resource.second << '\n'; |
| 456 | } |
| 457 | }); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 458 | Owner->notifyInstructionEvent(HWInstructionIssuedEvent(Index, Used)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void Scheduler::notifyInstructionExecuted(unsigned Index) { |
| 462 | LSU->onInstructionExecuted(Index); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 463 | DEBUG(dbgs() << "[E] Instruction Executed: " << Index << '\n'); |
| 464 | Owner->notifyInstructionEvent( |
| 465 | HWInstructionEvent(HWInstructionEvent::Executed, Index)); |
| 466 | |
| 467 | const Instruction &IS = Owner->getInstruction(Index); |
| 468 | DU->onInstructionExecuted(IS.getRCUTokenID()); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void Scheduler::notifyInstructionReady(unsigned Index) { |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 472 | DEBUG(dbgs() << "[E] Instruction Ready: " << Index << '\n'); |
| 473 | Owner->notifyInstructionEvent( |
| 474 | HWInstructionEvent(HWInstructionEvent::Ready, Index)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void Scheduler::notifyResourceAvailable(const ResourceRef &RR) { |
| 478 | Owner->notifyResourceAvailable(RR); |
| 479 | } |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 480 | |
| 481 | void Scheduler::notifyReservedBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 482 | if (Buffers.empty()) |
| 483 | return; |
| 484 | |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 485 | SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end()); |
| 486 | std::transform( |
| 487 | Buffers.begin(), Buffers.end(), BufferIDs.begin(), |
| 488 | [&](uint64_t Op) { return Resources->resolveResourceMask(Op); }); |
| 489 | Owner->notifyReservedBuffers(BufferIDs); |
| 490 | } |
| 491 | |
| 492 | void Scheduler::notifyReleasedBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame^] | 493 | if (Buffers.empty()) |
| 494 | return; |
| 495 | |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 496 | SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end()); |
| 497 | std::transform( |
| 498 | Buffers.begin(), Buffers.end(), BufferIDs.begin(), |
| 499 | [&](uint64_t Op) { return Resources->resolveResourceMask(Op); }); |
| 500 | Owner->notifyReleasedBuffers(BufferIDs); |
| 501 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 502 | } // namespace mca |