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