Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===--------------------- Scheduler.cpp ------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // A scheduler for processor resource units and processor resource groups. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Andrea Di Biagio | 51dba7d | 2018-03-23 17:36:07 +0000 | [diff] [blame] | 14 | #include "Scheduler.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 15 | #include "Backend.h" |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 16 | #include "HWEventListener.h" |
Andrea Di Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 17 | #include "Support.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | #define DEBUG_TYPE "llvm-mca" |
| 22 | |
| 23 | namespace mca { |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | uint64_t ResourceState::selectNextInSequence() { |
| 28 | assert(isReady()); |
| 29 | uint64_t Next = getNextInSequence(); |
| 30 | while (!isSubResourceReady(Next)) { |
| 31 | updateNextInSequence(); |
| 32 | Next = getNextInSequence(); |
| 33 | } |
| 34 | return Next; |
| 35 | } |
| 36 | |
| 37 | #ifndef NDEBUG |
| 38 | void ResourceState::dump() const { |
| 39 | dbgs() << "MASK: " << ResourceMask << ", SIZE_MASK: " << ResourceSizeMask |
| 40 | << ", NEXT: " << NextInSequenceMask << ", RDYMASK: " << ReadyMask |
| 41 | << ", BufferSize=" << BufferSize |
| 42 | << ", AvailableSlots=" << AvailableSlots |
| 43 | << ", Reserved=" << Unavailable << '\n'; |
| 44 | } |
| 45 | #endif |
| 46 | |
Andrea Di Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 47 | void ResourceManager::initialize(const llvm::MCSchedModel &SM) { |
| 48 | computeProcResourceMasks(SM, ProcResID2Mask); |
| 49 | for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) |
| 50 | addResource(*SM.getProcResource(I), I, ProcResID2Mask[I]); |
| 51 | } |
| 52 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 53 | // Adds a new resource state in Resources, as well as a new descriptor in |
| 54 | // ResourceDescriptor. Map 'Resources' allows to quickly obtain ResourceState |
| 55 | // objects from resource mask identifiers. |
| 56 | void ResourceManager::addResource(const MCProcResourceDesc &Desc, |
Andrea Di Biagio | e1a1da1 | 2018-03-13 13:58:02 +0000 | [diff] [blame] | 57 | unsigned Index, uint64_t Mask) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 58 | assert(Resources.find(Mask) == Resources.end() && "Resource already added!"); |
Andrea Di Biagio | 0c54129 | 2018-03-10 16:55:07 +0000 | [diff] [blame] | 59 | Resources[Mask] = llvm::make_unique<ResourceState>(Desc, Index, Mask); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 62 | // Returns the actual resource consumed by this Use. |
| 63 | // First, is the primary resource ID. |
| 64 | // Second, is the specific sub-resource ID. |
| 65 | std::pair<uint64_t, uint64_t> ResourceManager::selectPipe(uint64_t ResourceID) { |
| 66 | ResourceState &RS = *Resources[ResourceID]; |
| 67 | uint64_t SubResourceID = RS.selectNextInSequence(); |
| 68 | if (RS.isAResourceGroup()) |
| 69 | return selectPipe(SubResourceID); |
| 70 | return std::pair<uint64_t, uint64_t>(ResourceID, SubResourceID); |
| 71 | } |
| 72 | |
| 73 | void ResourceState::removeFromNextInSequence(uint64_t ID) { |
| 74 | assert(NextInSequenceMask); |
| 75 | assert(countPopulation(ID) == 1); |
| 76 | if (ID > getNextInSequence()) |
| 77 | RemovedFromNextInSequence |= ID; |
| 78 | NextInSequenceMask = NextInSequenceMask & (~ID); |
| 79 | if (!NextInSequenceMask) { |
| 80 | NextInSequenceMask = ResourceSizeMask; |
| 81 | assert(NextInSequenceMask != RemovedFromNextInSequence); |
| 82 | NextInSequenceMask ^= RemovedFromNextInSequence; |
| 83 | RemovedFromNextInSequence = 0; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void ResourceManager::use(ResourceRef RR) { |
| 88 | // Mark the sub-resource referenced by RR as used. |
| 89 | ResourceState &RS = *Resources[RR.first]; |
| 90 | RS.markSubResourceAsUsed(RR.second); |
| 91 | // If there are still available units in RR.first, |
| 92 | // then we are done. |
| 93 | if (RS.isReady()) |
| 94 | return; |
| 95 | |
| 96 | // Notify to other resources that RR.first is no longer available. |
| 97 | for (const std::pair<uint64_t, UniqueResourceState> &Res : Resources) { |
| 98 | ResourceState &Current = *Res.second.get(); |
| 99 | if (!Current.isAResourceGroup() || Current.getResourceMask() == RR.first) |
| 100 | continue; |
| 101 | |
| 102 | if (Current.containsResource(RR.first)) { |
| 103 | Current.markSubResourceAsUsed(RR.first); |
| 104 | Current.removeFromNextInSequence(RR.first); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void ResourceManager::release(ResourceRef RR) { |
| 110 | ResourceState &RS = *Resources[RR.first]; |
| 111 | bool WasFullyUsed = !RS.isReady(); |
| 112 | RS.releaseSubResource(RR.second); |
| 113 | if (!WasFullyUsed) |
| 114 | return; |
| 115 | |
| 116 | for (const std::pair<uint64_t, UniqueResourceState> &Res : Resources) { |
| 117 | ResourceState &Current = *Res.second.get(); |
| 118 | if (!Current.isAResourceGroup() || Current.getResourceMask() == RR.first) |
| 119 | continue; |
| 120 | |
| 121 | if (Current.containsResource(RR.first)) |
| 122 | Current.releaseSubResource(RR.first); |
| 123 | } |
| 124 | } |
| 125 | |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 126 | ResourceStateEvent |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 127 | ResourceManager::canBeDispatched(ArrayRef<uint64_t> Buffers) const { |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 128 | ResourceStateEvent Result = ResourceStateEvent::RS_BUFFER_AVAILABLE; |
| 129 | for (uint64_t Buffer : Buffers) { |
| 130 | Result = isBufferAvailable(Buffer); |
| 131 | if (Result != ResourceStateEvent::RS_BUFFER_AVAILABLE) |
| 132 | break; |
| 133 | } |
| 134 | return Result; |
| 135 | } |
| 136 | |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 137 | void ResourceManager::reserveBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | e1a1da1 | 2018-03-13 13:58:02 +0000 | [diff] [blame] | 138 | for (const uint64_t R : Buffers) { |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 139 | reserveBuffer(R); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 140 | ResourceState &Resource = *Resources[R]; |
| 141 | if (Resource.isADispatchHazard()) { |
| 142 | assert(!Resource.isReserved()); |
| 143 | Resource.setReserved(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 148 | void ResourceManager::releaseBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 149 | for (const uint64_t R : Buffers) |
| 150 | releaseBuffer(R); |
| 151 | } |
| 152 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 153 | bool ResourceManager::canBeIssued(const InstrDesc &Desc) const { |
| 154 | return std::all_of(Desc.Resources.begin(), Desc.Resources.end(), |
| 155 | [&](const std::pair<uint64_t, const ResourceUsage> &E) { |
| 156 | unsigned NumUnits = |
| 157 | E.second.isReserved() ? 0U : E.second.NumUnits; |
| 158 | return isReady(E.first, NumUnits); |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | // Returns true if all resources are in-order, and there is at least one |
| 163 | // resource which is a dispatch hazard (BufferSize = 0). |
| 164 | bool ResourceManager::mustIssueImmediately(const InstrDesc &Desc) { |
| 165 | if (!canBeIssued(Desc)) |
| 166 | return false; |
| 167 | bool AllInOrderResources = std::all_of( |
| 168 | Desc.Buffers.begin(), Desc.Buffers.end(), [&](const unsigned BufferMask) { |
| 169 | const ResourceState &Resource = *Resources[BufferMask]; |
| 170 | return Resource.isInOrder() || Resource.isADispatchHazard(); |
| 171 | }); |
| 172 | if (!AllInOrderResources) |
| 173 | return false; |
| 174 | |
| 175 | return std::any_of(Desc.Buffers.begin(), Desc.Buffers.end(), |
| 176 | [&](const unsigned BufferMask) { |
| 177 | return Resources[BufferMask]->isADispatchHazard(); |
| 178 | }); |
| 179 | } |
| 180 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 181 | void ResourceManager::issueInstruction( |
Matt Davis | ad78e66 | 2018-04-26 22:30:40 +0000 | [diff] [blame] | 182 | const InstrDesc &Desc, |
Andrea Di Biagio | 51dba7d | 2018-03-23 17:36:07 +0000 | [diff] [blame] | 183 | SmallVectorImpl<std::pair<ResourceRef, double>> &Pipes) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 184 | for (const std::pair<uint64_t, ResourceUsage> &R : Desc.Resources) { |
| 185 | const CycleSegment &CS = R.second.CS; |
| 186 | if (!CS.size()) { |
| 187 | releaseResource(R.first); |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | assert(CS.begin() == 0 && "Invalid {Start, End} cycles!"); |
| 192 | if (!R.second.isReserved()) { |
| 193 | ResourceRef Pipe = selectPipe(R.first); |
| 194 | use(Pipe); |
| 195 | BusyResources[Pipe] += CS.size(); |
Andrea Di Biagio | 0c54129 | 2018-03-10 16:55:07 +0000 | [diff] [blame] | 196 | // Replace the resource mask with a valid processor resource index. |
| 197 | const ResourceState &RS = *Resources[Pipe.first]; |
| 198 | Pipe.first = RS.getProcResourceID(); |
Andrea Di Biagio | 51dba7d | 2018-03-23 17:36:07 +0000 | [diff] [blame] | 199 | Pipes.emplace_back( |
| 200 | std::pair<ResourceRef, double>(Pipe, static_cast<double>(CS.size()))); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 201 | } else { |
| 202 | assert((countPopulation(R.first) > 1) && "Expected a group!"); |
| 203 | // Mark this group as reserved. |
| 204 | assert(R.second.isReserved()); |
| 205 | reserveResource(R.first); |
| 206 | BusyResources[ResourceRef(R.first, R.first)] += CS.size(); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void ResourceManager::cycleEvent(SmallVectorImpl<ResourceRef> &ResourcesFreed) { |
| 212 | for (std::pair<ResourceRef, unsigned> &BR : BusyResources) { |
| 213 | if (BR.second) |
| 214 | BR.second--; |
| 215 | if (!BR.second) { |
| 216 | // Release this resource. |
| 217 | const ResourceRef &RR = BR.first; |
| 218 | |
| 219 | if (countPopulation(RR.first) == 1) |
| 220 | release(RR); |
| 221 | |
| 222 | releaseResource(RR.first); |
| 223 | ResourcesFreed.push_back(RR); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | for (const ResourceRef &RF : ResourcesFreed) |
| 228 | BusyResources.erase(RF); |
| 229 | } |
| 230 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 231 | void Scheduler::scheduleInstruction(InstRef &IR) { |
| 232 | const unsigned Idx = IR.getSourceIndex(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 233 | assert(WaitQueue.find(Idx) == WaitQueue.end()); |
| 234 | assert(ReadyQueue.find(Idx) == ReadyQueue.end()); |
| 235 | assert(IssuedQueue.find(Idx) == IssuedQueue.end()); |
| 236 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 237 | // Reserve a slot in each buffered resource. Also, mark units with |
| 238 | // BufferSize=0 as reserved. Resources with a buffer size of zero will only |
| 239 | // be released after MCIS is issued, and all the ResourceCycles for those |
| 240 | // units have been consumed. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 241 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 242 | reserveBuffers(Desc.Buffers); |
| 243 | notifyReservedBuffers(Desc.Buffers); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 244 | |
Andrea Di Biagio | 2dee62b | 2018-03-22 14:14:49 +0000 | [diff] [blame] | 245 | // If necessary, reserve queue entries in the load-store unit (LSU). |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 246 | bool Reserved = LSU->reserve(IR); |
| 247 | if (!IR.getInstruction()->isReady() || (Reserved && !LSU->isReady(IR))) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 248 | LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding " << Idx |
| 249 | << " to the Wait Queue\n"); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 250 | WaitQueue[Idx] = IR.getInstruction(); |
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 | } |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 253 | notifyInstructionReady(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 254 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 255 | // Don't add a zero-latency instruction to the Wait or Ready queue. |
| 256 | // A zero-latency instruction doesn't consume any scheduler resources. That is |
| 257 | // because it doesn't need to be executed, and it is often removed at register |
| 258 | // renaming stage. For example, register-register moves are often optimized at |
| 259 | // register renaming stage by simply updating register aliases. On some |
| 260 | // targets, zero-idiom instructions (for example: a xor that clears the value |
| 261 | // of a register) are treated speacially, and are often eliminated at register |
| 262 | // renaming stage. |
| 263 | |
| 264 | // Instructions that use an in-order dispatch/issue processor resource must be |
| 265 | // issued immediately to the pipeline(s). Any other in-order buffered |
| 266 | // resources (i.e. BufferSize=1) is consumed. |
| 267 | |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 268 | if (!Desc.isZeroLatency() && !Resources->mustIssueImmediately(Desc)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 269 | LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding " << IR |
| 270 | << " to the Ready Queue\n"); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 271 | ReadyQueue[IR.getSourceIndex()] = IR.getInstruction(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 272 | return; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 275 | LLVM_DEBUG(dbgs() << "[SCHEDULER] Instruction " << IR |
| 276 | << " issued immediately\n"); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 277 | // Release buffered resources and issue MCIS to the underlying pipelines. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 278 | issueInstruction(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Andrea Di Biagio | 3e64644 | 2018-04-12 10:49:40 +0000 | [diff] [blame] | 281 | void Scheduler::cycleEvent() { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 282 | SmallVector<ResourceRef, 8> ResourcesFreed; |
| 283 | Resources->cycleEvent(ResourcesFreed); |
| 284 | |
| 285 | for (const ResourceRef &RR : ResourcesFreed) |
| 286 | notifyResourceAvailable(RR); |
| 287 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 288 | SmallVector<InstRef, 4> InstructionIDs; |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 289 | updateIssuedQueue(InstructionIDs); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 290 | for (const InstRef &IR : InstructionIDs) |
| 291 | notifyInstructionExecuted(IR); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 292 | InstructionIDs.clear(); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 293 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 294 | updatePendingQueue(InstructionIDs); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 295 | for (const InstRef &IR : InstructionIDs) |
| 296 | notifyInstructionReady(IR); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 297 | InstructionIDs.clear(); |
| 298 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 299 | InstRef IR = select(); |
| 300 | while (IR.isValid()) { |
| 301 | issueInstruction(IR); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 302 | |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 303 | // Instructions that have been issued during this cycle might have unblocked |
| 304 | // other dependent instructions. Dependent instructions may be issued during |
| 305 | // this same cycle if operands have ReadAdvance entries. Promote those |
| 306 | // instructions to the ReadyQueue and tell to the caller that we need |
| 307 | // another round of 'issue()'. |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 308 | promoteToReadyQueue(InstructionIDs); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 309 | for (const InstRef &I : InstructionIDs) |
| 310 | notifyInstructionReady(I); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 311 | InstructionIDs.clear(); |
| 312 | |
| 313 | // Select the next instruction to issue. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 314 | IR = select(); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 315 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | #ifndef NDEBUG |
| 319 | void Scheduler::dump() const { |
| 320 | dbgs() << "[SCHEDULER]: WaitQueue size is: " << WaitQueue.size() << '\n'; |
| 321 | dbgs() << "[SCHEDULER]: ReadyQueue size is: " << ReadyQueue.size() << '\n'; |
| 322 | dbgs() << "[SCHEDULER]: IssuedQueue size is: " << IssuedQueue.size() << '\n'; |
| 323 | Resources->dump(); |
| 324 | } |
| 325 | #endif |
| 326 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 327 | bool Scheduler::canBeDispatched(const InstRef &IR) const { |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 328 | HWStallEvent::GenericEventType Type = HWStallEvent::Invalid; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 329 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 330 | |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 331 | if (Desc.MayLoad && LSU->isLQFull()) |
| 332 | Type = HWStallEvent::LoadQueueFull; |
| 333 | else if (Desc.MayStore && LSU->isSQFull()) |
| 334 | Type = HWStallEvent::StoreQueueFull; |
| 335 | else { |
| 336 | switch (Resources->canBeDispatched(Desc.Buffers)) { |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 337 | default: |
| 338 | return true; |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 339 | case ResourceStateEvent::RS_BUFFER_UNAVAILABLE: |
| 340 | Type = HWStallEvent::SchedulerQueueFull; |
| 341 | break; |
| 342 | case ResourceStateEvent::RS_RESERVED: |
| 343 | Type = HWStallEvent::DispatchGroupStall; |
| 344 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 345 | } |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 346 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 347 | Owner->notifyStallEvent(HWStallEvent(Type, IR)); |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 348 | return false; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 351 | void Scheduler::issueInstructionImpl( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 352 | InstRef &IR, |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 353 | SmallVectorImpl<std::pair<ResourceRef, double>> &UsedResources) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 354 | Instruction *IS = IR.getInstruction(); |
| 355 | const InstrDesc &D = IS->getDesc(); |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 356 | |
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. |
Matt Davis | ad78e66 | 2018-04-26 22:30:40 +0000 | [diff] [blame] | 359 | Resources->issueInstruction(D, UsedResources); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 360 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 361 | // Notify the instruction that it started executing. |
| 362 | // This updates the internal state of each write. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 363 | IS->execute(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 364 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 365 | if (IS->isExecuting()) |
| 366 | IssuedQueue[IR.getSourceIndex()] = IS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 369 | void Scheduler::issueInstruction(InstRef &IR) { |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 370 | // Release buffered resources. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 371 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 372 | releaseBuffers(Desc.Buffers); |
| 373 | notifyReleasedBuffers(Desc.Buffers); |
| 374 | |
| 375 | // Issue IS to the underlying pipelines and notify listeners. |
| 376 | SmallVector<std::pair<ResourceRef, double>, 4> Pipes; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 377 | issueInstructionImpl(IR, Pipes); |
| 378 | notifyInstructionIssued(IR, Pipes); |
| 379 | if (IR.getInstruction()->isExecuted()) |
| 380 | notifyInstructionExecuted(IR); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 383 | void Scheduler::promoteToReadyQueue(SmallVectorImpl<InstRef> &Ready) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 384 | // Scan the set of waiting instructions and promote them to the |
| 385 | // ready queue if operands are all ready. |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 386 | for (auto I = WaitQueue.begin(), E = WaitQueue.end(); I != E;) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 387 | const unsigned IID = I->first; |
| 388 | Instruction *IS = I->second; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 389 | |
| 390 | // Check if this instruction is now ready. In case, force |
| 391 | // a transition in state using method 'update()'. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 392 | IS->update(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 393 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 394 | const InstrDesc &Desc = IS->getDesc(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 395 | bool IsMemOp = Desc.MayLoad || Desc.MayStore; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 396 | if (!IS->isReady() || (IsMemOp && !LSU->isReady({IID, IS}))) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 397 | ++I; |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 398 | continue; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 399 | } |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 400 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 401 | Ready.emplace_back(IID, IS); |
| 402 | ReadyQueue[IID] = IS; |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 403 | auto ToRemove = I; |
| 404 | ++I; |
| 405 | WaitQueue.erase(ToRemove); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 406 | } |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 409 | InstRef Scheduler::select() { |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 410 | // Give priority to older instructions in the ReadyQueue. Since the ready |
| 411 | // queue is ordered by key, this will always prioritize older instructions. |
| 412 | const auto It = std::find_if(ReadyQueue.begin(), ReadyQueue.end(), |
| 413 | [&](const QueueEntryTy &Entry) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 414 | const InstrDesc &D = Entry.second->getDesc(); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 415 | return Resources->canBeIssued(D); |
| 416 | }); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 417 | |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 418 | if (It == ReadyQueue.end()) |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 419 | return {0, nullptr}; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 420 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 421 | // We found an instruction to issue. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 422 | InstRef IR(It->first, It->second); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 423 | ReadyQueue.erase(It); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 424 | return IR; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 427 | void Scheduler::updatePendingQueue(SmallVectorImpl<InstRef> &Ready) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 428 | // Notify to instructions in the pending queue that a new cycle just |
| 429 | // started. |
| 430 | for (QueueEntryTy Entry : WaitQueue) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 431 | Entry.second->cycleEvent(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 432 | promoteToReadyQueue(Ready); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 435 | void Scheduler::updateIssuedQueue(SmallVectorImpl<InstRef> &Executed) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 436 | for (auto I = IssuedQueue.begin(), E = IssuedQueue.end(); I != E;) { |
| 437 | const QueueEntryTy Entry = *I; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 438 | Instruction *IS = Entry.second; |
| 439 | IS->cycleEvent(); |
| 440 | if (IS->isExecuted()) { |
| 441 | Executed.push_back({Entry.first, Entry.second}); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 442 | auto ToRemove = I; |
| 443 | ++I; |
| 444 | IssuedQueue.erase(ToRemove); |
| 445 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 446 | LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction " << Entry.first |
| 447 | << " is still executing.\n"); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 448 | ++I; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void Scheduler::notifyInstructionIssued( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 454 | const InstRef &IR, ArrayRef<std::pair<ResourceRef, double>> Used) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 455 | LLVM_DEBUG({ |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 456 | dbgs() << "[E] Instruction Issued: " << IR << '\n'; |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 457 | for (const std::pair<ResourceRef, unsigned> &Resource : Used) { |
| 458 | dbgs() << "[E] Resource Used: [" << Resource.first.first << '.' |
| 459 | << Resource.first.second << "]\n"; |
| 460 | dbgs() << " cycles: " << Resource.second << '\n'; |
| 461 | } |
| 462 | }); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 463 | Owner->notifyInstructionEvent(HWInstructionIssuedEvent(IR, Used)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 466 | void Scheduler::notifyInstructionExecuted(const InstRef &IR) { |
| 467 | LSU->onInstructionExecuted(IR); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 468 | LLVM_DEBUG(dbgs() << "[E] Instruction Executed: " << IR << '\n'); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 469 | Owner->notifyInstructionEvent( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 470 | HWInstructionEvent(HWInstructionEvent::Executed, IR)); |
Matt Davis | 679083e | 2018-05-17 19:22:29 +0000 | [diff] [blame] | 471 | DS->onInstructionExecuted(IR.getInstruction()->getRCUTokenID()); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 474 | void Scheduler::notifyInstructionReady(const InstRef &IR) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 475 | LLVM_DEBUG(dbgs() << "[E] Instruction Ready: " << IR << '\n'); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 476 | Owner->notifyInstructionEvent( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 477 | HWInstructionEvent(HWInstructionEvent::Ready, IR)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void Scheduler::notifyResourceAvailable(const ResourceRef &RR) { |
| 481 | Owner->notifyResourceAvailable(RR); |
| 482 | } |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 483 | |
| 484 | void Scheduler::notifyReservedBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 485 | if (Buffers.empty()) |
| 486 | return; |
| 487 | |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 488 | SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end()); |
| 489 | std::transform( |
| 490 | Buffers.begin(), Buffers.end(), BufferIDs.begin(), |
| 491 | [&](uint64_t Op) { return Resources->resolveResourceMask(Op); }); |
| 492 | Owner->notifyReservedBuffers(BufferIDs); |
| 493 | } |
| 494 | |
| 495 | void Scheduler::notifyReleasedBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 496 | if (Buffers.empty()) |
| 497 | return; |
| 498 | |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 499 | SmallVector<unsigned, 4> BufferIDs(Buffers.begin(), Buffers.end()); |
| 500 | std::transform( |
| 501 | Buffers.begin(), Buffers.end(), BufferIDs.begin(), |
| 502 | [&](uint64_t Op) { return Resources->resolveResourceMask(Op); }); |
| 503 | Owner->notifyReleasedBuffers(BufferIDs); |
| 504 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 505 | } // namespace mca |