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