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/raw_ostream.h" |
| 19 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 20 | namespace mca { |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | uint64_t ResourceState::selectNextInSequence() { |
| 25 | assert(isReady()); |
| 26 | uint64_t Next = getNextInSequence(); |
| 27 | while (!isSubResourceReady(Next)) { |
| 28 | updateNextInSequence(); |
| 29 | Next = getNextInSequence(); |
| 30 | } |
| 31 | return Next; |
| 32 | } |
| 33 | |
| 34 | #ifndef NDEBUG |
| 35 | void ResourceState::dump() const { |
| 36 | dbgs() << "MASK: " << ResourceMask << ", SIZE_MASK: " << ResourceSizeMask |
| 37 | << ", NEXT: " << NextInSequenceMask << ", RDYMASK: " << ReadyMask |
| 38 | << ", BufferSize=" << BufferSize |
| 39 | << ", AvailableSlots=" << AvailableSlots |
| 40 | << ", Reserved=" << Unavailable << '\n'; |
| 41 | } |
| 42 | #endif |
| 43 | |
Andrea Di Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 44 | void ResourceManager::initialize(const llvm::MCSchedModel &SM) { |
| 45 | computeProcResourceMasks(SM, ProcResID2Mask); |
| 46 | for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) |
| 47 | addResource(*SM.getProcResource(I), I, ProcResID2Mask[I]); |
| 48 | } |
| 49 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 50 | // Adds a new resource state in Resources, as well as a new descriptor in |
| 51 | // ResourceDescriptor. Map 'Resources' allows to quickly obtain ResourceState |
| 52 | // objects from resource mask identifiers. |
| 53 | void ResourceManager::addResource(const MCProcResourceDesc &Desc, |
Andrea Di Biagio | e1a1da1 | 2018-03-13 13:58:02 +0000 | [diff] [blame] | 54 | unsigned Index, uint64_t Mask) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 55 | assert(Resources.find(Mask) == Resources.end() && "Resource already added!"); |
Andrea Di Biagio | 0c54129 | 2018-03-10 16:55:07 +0000 | [diff] [blame] | 56 | Resources[Mask] = llvm::make_unique<ResourceState>(Desc, Index, Mask); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 59 | // Returns the actual resource consumed by this Use. |
| 60 | // First, is the primary resource ID. |
| 61 | // Second, is the specific sub-resource ID. |
| 62 | std::pair<uint64_t, uint64_t> ResourceManager::selectPipe(uint64_t ResourceID) { |
| 63 | ResourceState &RS = *Resources[ResourceID]; |
| 64 | uint64_t SubResourceID = RS.selectNextInSequence(); |
| 65 | if (RS.isAResourceGroup()) |
| 66 | return selectPipe(SubResourceID); |
| 67 | return std::pair<uint64_t, uint64_t>(ResourceID, SubResourceID); |
| 68 | } |
| 69 | |
| 70 | void ResourceState::removeFromNextInSequence(uint64_t ID) { |
| 71 | assert(NextInSequenceMask); |
| 72 | assert(countPopulation(ID) == 1); |
| 73 | if (ID > getNextInSequence()) |
| 74 | RemovedFromNextInSequence |= ID; |
| 75 | NextInSequenceMask = NextInSequenceMask & (~ID); |
| 76 | if (!NextInSequenceMask) { |
| 77 | NextInSequenceMask = ResourceSizeMask; |
| 78 | assert(NextInSequenceMask != RemovedFromNextInSequence); |
| 79 | NextInSequenceMask ^= RemovedFromNextInSequence; |
| 80 | RemovedFromNextInSequence = 0; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void ResourceManager::use(ResourceRef RR) { |
| 85 | // Mark the sub-resource referenced by RR as used. |
| 86 | ResourceState &RS = *Resources[RR.first]; |
| 87 | RS.markSubResourceAsUsed(RR.second); |
| 88 | // If there are still available units in RR.first, |
| 89 | // then we are done. |
| 90 | if (RS.isReady()) |
| 91 | return; |
| 92 | |
| 93 | // Notify to other resources that RR.first is no longer available. |
| 94 | for (const std::pair<uint64_t, UniqueResourceState> &Res : Resources) { |
| 95 | ResourceState &Current = *Res.second.get(); |
| 96 | if (!Current.isAResourceGroup() || Current.getResourceMask() == RR.first) |
| 97 | continue; |
| 98 | |
| 99 | if (Current.containsResource(RR.first)) { |
| 100 | Current.markSubResourceAsUsed(RR.first); |
| 101 | Current.removeFromNextInSequence(RR.first); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void ResourceManager::release(ResourceRef RR) { |
| 107 | ResourceState &RS = *Resources[RR.first]; |
| 108 | bool WasFullyUsed = !RS.isReady(); |
| 109 | RS.releaseSubResource(RR.second); |
| 110 | if (!WasFullyUsed) |
| 111 | return; |
| 112 | |
| 113 | for (const std::pair<uint64_t, UniqueResourceState> &Res : Resources) { |
| 114 | ResourceState &Current = *Res.second.get(); |
| 115 | if (!Current.isAResourceGroup() || Current.getResourceMask() == RR.first) |
| 116 | continue; |
| 117 | |
| 118 | if (Current.containsResource(RR.first)) |
| 119 | Current.releaseSubResource(RR.first); |
| 120 | } |
| 121 | } |
| 122 | |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 123 | ResourceStateEvent |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 124 | ResourceManager::canBeDispatched(ArrayRef<uint64_t> Buffers) const { |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 125 | ResourceStateEvent Result = ResourceStateEvent::RS_BUFFER_AVAILABLE; |
| 126 | for (uint64_t Buffer : Buffers) { |
| 127 | Result = isBufferAvailable(Buffer); |
| 128 | if (Result != ResourceStateEvent::RS_BUFFER_AVAILABLE) |
| 129 | break; |
| 130 | } |
| 131 | return Result; |
| 132 | } |
| 133 | |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 134 | void ResourceManager::reserveBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | e1a1da1 | 2018-03-13 13:58:02 +0000 | [diff] [blame] | 135 | for (const uint64_t R : Buffers) { |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 136 | reserveBuffer(R); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 137 | ResourceState &Resource = *Resources[R]; |
| 138 | if (Resource.isADispatchHazard()) { |
| 139 | assert(!Resource.isReserved()); |
| 140 | Resource.setReserved(); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 145 | void ResourceManager::releaseBuffers(ArrayRef<uint64_t> Buffers) { |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 146 | for (const uint64_t R : Buffers) |
| 147 | releaseBuffer(R); |
| 148 | } |
| 149 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 150 | bool ResourceManager::canBeIssued(const InstrDesc &Desc) const { |
| 151 | return std::all_of(Desc.Resources.begin(), Desc.Resources.end(), |
| 152 | [&](const std::pair<uint64_t, const ResourceUsage> &E) { |
| 153 | unsigned NumUnits = |
| 154 | E.second.isReserved() ? 0U : E.second.NumUnits; |
| 155 | return isReady(E.first, NumUnits); |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | // Returns true if all resources are in-order, and there is at least one |
| 160 | // resource which is a dispatch hazard (BufferSize = 0). |
| 161 | bool ResourceManager::mustIssueImmediately(const InstrDesc &Desc) { |
| 162 | if (!canBeIssued(Desc)) |
| 163 | return false; |
Andrea Di Biagio | 4037011 | 2018-05-31 20:27:46 +0000 | [diff] [blame] | 164 | bool AllInOrderResources = all_of(Desc.Buffers, [&](uint64_t BufferMask) { |
| 165 | const ResourceState &Resource = *Resources[BufferMask]; |
| 166 | return Resource.isInOrder() || Resource.isADispatchHazard(); |
| 167 | }); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 168 | if (!AllInOrderResources) |
| 169 | return false; |
| 170 | |
Andrea Di Biagio | 4037011 | 2018-05-31 20:27:46 +0000 | [diff] [blame] | 171 | return any_of(Desc.Buffers, [&](uint64_t BufferMask) { |
| 172 | return Resources[BufferMask]->isADispatchHazard(); |
| 173 | }); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 176 | void ResourceManager::issueInstruction( |
Matt Davis | ad78e66 | 2018-04-26 22:30:40 +0000 | [diff] [blame] | 177 | const InstrDesc &Desc, |
Andrea Di Biagio | 51dba7d | 2018-03-23 17:36:07 +0000 | [diff] [blame] | 178 | SmallVectorImpl<std::pair<ResourceRef, double>> &Pipes) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 179 | for (const std::pair<uint64_t, ResourceUsage> &R : Desc.Resources) { |
| 180 | const CycleSegment &CS = R.second.CS; |
| 181 | if (!CS.size()) { |
| 182 | releaseResource(R.first); |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | assert(CS.begin() == 0 && "Invalid {Start, End} cycles!"); |
| 187 | if (!R.second.isReserved()) { |
| 188 | ResourceRef Pipe = selectPipe(R.first); |
| 189 | use(Pipe); |
| 190 | BusyResources[Pipe] += CS.size(); |
Andrea Di Biagio | 0c54129 | 2018-03-10 16:55:07 +0000 | [diff] [blame] | 191 | // Replace the resource mask with a valid processor resource index. |
| 192 | const ResourceState &RS = *Resources[Pipe.first]; |
| 193 | Pipe.first = RS.getProcResourceID(); |
Andrea Di Biagio | 51dba7d | 2018-03-23 17:36:07 +0000 | [diff] [blame] | 194 | Pipes.emplace_back( |
| 195 | std::pair<ResourceRef, double>(Pipe, static_cast<double>(CS.size()))); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 196 | } else { |
| 197 | assert((countPopulation(R.first) > 1) && "Expected a group!"); |
| 198 | // Mark this group as reserved. |
| 199 | assert(R.second.isReserved()); |
| 200 | reserveResource(R.first); |
| 201 | BusyResources[ResourceRef(R.first, R.first)] += CS.size(); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void ResourceManager::cycleEvent(SmallVectorImpl<ResourceRef> &ResourcesFreed) { |
| 207 | for (std::pair<ResourceRef, unsigned> &BR : BusyResources) { |
| 208 | if (BR.second) |
| 209 | BR.second--; |
| 210 | if (!BR.second) { |
| 211 | // Release this resource. |
| 212 | const ResourceRef &RR = BR.first; |
| 213 | |
| 214 | if (countPopulation(RR.first) == 1) |
| 215 | release(RR); |
| 216 | |
| 217 | releaseResource(RR.first); |
| 218 | ResourcesFreed.push_back(RR); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | for (const ResourceRef &RF : ResourcesFreed) |
| 223 | BusyResources.erase(RF); |
| 224 | } |
| 225 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 226 | #ifndef NDEBUG |
| 227 | void Scheduler::dump() const { |
| 228 | dbgs() << "[SCHEDULER]: WaitQueue size is: " << WaitQueue.size() << '\n'; |
| 229 | dbgs() << "[SCHEDULER]: ReadyQueue size is: " << ReadyQueue.size() << '\n'; |
| 230 | dbgs() << "[SCHEDULER]: IssuedQueue size is: " << IssuedQueue.size() << '\n'; |
| 231 | Resources->dump(); |
| 232 | } |
| 233 | #endif |
| 234 | |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 235 | bool Scheduler::canBeDispatched(const InstRef &IR, |
| 236 | HWStallEvent::GenericEventType &Event) const { |
| 237 | Event = HWStallEvent::Invalid; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 238 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 239 | |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 240 | if (Desc.MayLoad && LSU->isLQFull()) |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 241 | Event = HWStallEvent::LoadQueueFull; |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 242 | else if (Desc.MayStore && LSU->isSQFull()) |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 243 | Event = HWStallEvent::StoreQueueFull; |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 244 | else { |
| 245 | switch (Resources->canBeDispatched(Desc.Buffers)) { |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 246 | default: |
| 247 | return true; |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 248 | case ResourceStateEvent::RS_BUFFER_UNAVAILABLE: |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 249 | Event = HWStallEvent::SchedulerQueueFull; |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 250 | break; |
| 251 | case ResourceStateEvent::RS_RESERVED: |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 252 | Event = HWStallEvent::DispatchGroupStall; |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 253 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 254 | } |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 255 | |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 256 | return false; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 259 | void Scheduler::issueInstructionImpl( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 260 | InstRef &IR, |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 261 | SmallVectorImpl<std::pair<ResourceRef, double>> &UsedResources) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 262 | Instruction *IS = IR.getInstruction(); |
| 263 | const InstrDesc &D = IS->getDesc(); |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 264 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 265 | // Issue the instruction and collect all the consumed resources |
| 266 | // into a vector. That vector is then used to notify the listener. |
Matt Davis | ad78e66 | 2018-04-26 22:30:40 +0000 | [diff] [blame] | 267 | Resources->issueInstruction(D, UsedResources); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 268 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 269 | // Notify the instruction that it started executing. |
| 270 | // This updates the internal state of each write. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 271 | IS->execute(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 272 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 273 | if (IS->isExecuting()) |
| 274 | IssuedQueue[IR.getSourceIndex()] = IS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 277 | // Release the buffered resources and issue the instruction. |
| 278 | void Scheduler::issueInstruction( |
| 279 | InstRef &IR, |
| 280 | SmallVectorImpl<std::pair<ResourceRef, double>> &UsedResources) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 281 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 282 | releaseBuffers(Desc.Buffers); |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 283 | issueInstructionImpl(IR, UsedResources); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 286 | void Scheduler::promoteToReadyQueue(SmallVectorImpl<InstRef> &Ready) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 287 | // Scan the set of waiting instructions and promote them to the |
| 288 | // ready queue if operands are all ready. |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 289 | for (auto I = WaitQueue.begin(), E = WaitQueue.end(); I != E;) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 290 | const unsigned IID = I->first; |
| 291 | Instruction *IS = I->second; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 292 | |
| 293 | // Check if this instruction is now ready. In case, force |
| 294 | // a transition in state using method 'update()'. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 295 | IS->update(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 296 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 297 | const InstrDesc &Desc = IS->getDesc(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 298 | bool IsMemOp = Desc.MayLoad || Desc.MayStore; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 299 | if (!IS->isReady() || (IsMemOp && !LSU->isReady({IID, IS}))) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 300 | ++I; |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 301 | continue; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 302 | } |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 303 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 304 | Ready.emplace_back(IID, IS); |
| 305 | ReadyQueue[IID] = IS; |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 306 | auto ToRemove = I; |
| 307 | ++I; |
| 308 | WaitQueue.erase(ToRemove); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 309 | } |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 312 | InstRef Scheduler::select() { |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 313 | // Give priority to older instructions in the ReadyQueue. Since the ready |
| 314 | // queue is ordered by key, this will always prioritize older instructions. |
| 315 | const auto It = std::find_if(ReadyQueue.begin(), ReadyQueue.end(), |
| 316 | [&](const QueueEntryTy &Entry) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 317 | const InstrDesc &D = Entry.second->getDesc(); |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 318 | return Resources->canBeIssued(D); |
| 319 | }); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 320 | |
Andrea Di Biagio | c752616 | 2018-04-13 15:19:07 +0000 | [diff] [blame] | 321 | if (It == ReadyQueue.end()) |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 322 | return {0, nullptr}; |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 323 | |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 324 | // We found an instruction to issue. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 325 | InstRef IR(It->first, It->second); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 326 | ReadyQueue.erase(It); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 327 | return IR; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 330 | void Scheduler::updatePendingQueue(SmallVectorImpl<InstRef> &Ready) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 331 | // Notify to instructions in the pending queue that a new cycle just |
| 332 | // started. |
| 333 | for (QueueEntryTy Entry : WaitQueue) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 334 | Entry.second->cycleEvent(); |
Andrea Di Biagio | 27c4b09 | 2018-04-24 14:53:16 +0000 | [diff] [blame] | 335 | promoteToReadyQueue(Ready); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 338 | void Scheduler::updateIssuedQueue(SmallVectorImpl<InstRef> &Executed) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 339 | for (auto I = IssuedQueue.begin(), E = IssuedQueue.end(); I != E;) { |
| 340 | const QueueEntryTy Entry = *I; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 341 | Instruction *IS = Entry.second; |
| 342 | IS->cycleEvent(); |
| 343 | if (IS->isExecuted()) { |
| 344 | Executed.push_back({Entry.first, Entry.second}); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 345 | auto ToRemove = I; |
| 346 | ++I; |
| 347 | IssuedQueue.erase(ToRemove); |
| 348 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 349 | LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction " << Entry.first |
| 350 | << " is still executing.\n"); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 351 | ++I; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 356 | void Scheduler::onInstructionExecuted(const InstRef &IR) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 357 | LSU->onInstructionExecuted(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 360 | void Scheduler::reclaimSimulatedResources(SmallVectorImpl<ResourceRef> &Freed) { |
| 361 | Resources->cycleEvent(Freed); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 364 | bool Scheduler::reserveResources(InstRef &IR) { |
| 365 | // If necessary, reserve queue entries in the load-store unit (LSU). |
| 366 | const bool Reserved = LSU->reserve(IR); |
| 367 | if (!IR.getInstruction()->isReady() || (Reserved && !LSU->isReady(IR))) { |
| 368 | LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding " << IR << " to the Wait Queue\n"); |
| 369 | WaitQueue[IR.getSourceIndex()] = IR.getInstruction(); |
| 370 | return false; |
| 371 | } |
| 372 | return true; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 373 | } |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 374 | |
Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 375 | bool Scheduler::issueImmediately(InstRef &IR) { |
| 376 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
| 377 | if (!Desc.isZeroLatency() && !Resources->mustIssueImmediately(Desc)) { |
| 378 | LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding " << IR |
| 379 | << " to the Ready Queue\n"); |
| 380 | ReadyQueue[IR.getSourceIndex()] = IR.getInstruction(); |
| 381 | return false; |
| 382 | } |
| 383 | return true; |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 386 | } // namespace mca |