1 //===--------------------- Scheduler.cpp ------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // A scheduler for processor resource units and processor resource groups. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/MCA/HardwareUnits/Scheduler.h" 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/raw_ostream.h" 16 17 namespace llvm { 18 namespace mca { 19 20 #define DEBUG_TYPE "llvm-mca" 21 22 void Scheduler::initializeStrategy(std::unique_ptr<SchedulerStrategy> S) { 23 // Ensure we have a valid (non-null) strategy object. 24 Strategy = S ? std::move(S) : llvm::make_unique<DefaultSchedulerStrategy>(); 25 } 26 27 // Anchor the vtable of SchedulerStrategy and DefaultSchedulerStrategy. 28 SchedulerStrategy::~SchedulerStrategy() = default; 29 DefaultSchedulerStrategy::~DefaultSchedulerStrategy() = default; 30 31 #ifndef NDEBUG 32 void Scheduler::dump() const { 33 dbgs() << "[SCHEDULER]: WaitSet size is: " << WaitSet.size() << '\n'; 34 dbgs() << "[SCHEDULER]: ReadySet size is: " << ReadySet.size() << '\n'; 35 dbgs() << "[SCHEDULER]: IssuedSet size is: " << IssuedSet.size() << '\n'; 36 Resources->dump(); 37 } 38 #endif 39 40 Scheduler::Status Scheduler::isAvailable(const InstRef &IR) { 41 const InstrDesc &Desc = IR.getInstruction()->getDesc(); 42 43 ResourceStateEvent RSE = Resources->canBeDispatched(Desc.Buffers); 44 HadTokenStall = RSE != RS_BUFFER_AVAILABLE; 45 46 switch (RSE) { 47 case ResourceStateEvent::RS_BUFFER_UNAVAILABLE: 48 return Scheduler::SC_BUFFERS_FULL; 49 case ResourceStateEvent::RS_RESERVED: 50 return Scheduler::SC_DISPATCH_GROUP_STALL; 51 case ResourceStateEvent::RS_BUFFER_AVAILABLE: 52 break; 53 } 54 55 // Give lower priority to LSUnit stall events. 56 LSUnit::Status LSS = LSU.isAvailable(IR); 57 HadTokenStall = LSS != LSUnit::LSU_AVAILABLE; 58 59 switch (LSS) { 60 case LSUnit::LSU_LQUEUE_FULL: 61 return Scheduler::SC_LOAD_QUEUE_FULL; 62 case LSUnit::LSU_SQUEUE_FULL: 63 return Scheduler::SC_STORE_QUEUE_FULL; 64 case LSUnit::LSU_AVAILABLE: 65 return Scheduler::SC_AVAILABLE; 66 } 67 68 llvm_unreachable("Don't know how to process this LSU state result!"); 69 } 70 71 void Scheduler::issueInstructionImpl( 72 InstRef &IR, 73 SmallVectorImpl<std::pair<ResourceRef, ResourceCycles>> &UsedResources) { 74 Instruction *IS = IR.getInstruction(); 75 const InstrDesc &D = IS->getDesc(); 76 77 // Issue the instruction and collect all the consumed resources 78 // into a vector. That vector is then used to notify the listener. 79 Resources->issueInstruction(D, UsedResources); 80 81 // Notify the instruction that it started executing. 82 // This updates the internal state of each write. 83 IS->execute(IR.getSourceIndex()); 84 85 if (IS->isExecuting()) 86 IssuedSet.emplace_back(IR); 87 else if (IS->isExecuted()) 88 LSU.onInstructionExecuted(IR); 89 } 90 91 // Release the buffered resources and issue the instruction. 92 void Scheduler::issueInstruction( 93 InstRef &IR, 94 SmallVectorImpl<std::pair<ResourceRef, ResourceCycles>> &UsedResources, 95 SmallVectorImpl<InstRef> &ReadyInstructions) { 96 const Instruction &Inst = *IR.getInstruction(); 97 bool HasDependentUsers = Inst.hasDependentUsers(); 98 99 Resources->releaseBuffers(Inst.getDesc().Buffers); 100 issueInstructionImpl(IR, UsedResources); 101 // Instructions that have been issued during this cycle might have unblocked 102 // other dependent instructions. Dependent instructions may be issued during 103 // this same cycle if operands have ReadAdvance entries. Promote those 104 // instructions to the ReadySet and notify the caller that those are ready. 105 if (HasDependentUsers && promoteToPendingSet()) 106 promoteToReadySet(ReadyInstructions); 107 } 108 109 bool Scheduler::promoteToReadySet(SmallVectorImpl<InstRef> &Ready) { 110 // Scan the set of waiting instructions and promote them to the 111 // ready set if operands are all ready. 112 unsigned PromotedElements = 0; 113 for (auto I = PendingSet.begin(), E = PendingSet.end(); I != E;) { 114 InstRef &IR = *I; 115 if (!IR) 116 break; 117 118 // Check if there are still unsolved memory dependencies. 119 Instruction &IS = *IR.getInstruction(); 120 if (IS.isMemOp()) { 121 unsigned CriticalMemDep = LSU.isReady(IR); 122 if (CriticalMemDep != IR.getSourceIndex()) { 123 IS.setCriticalMemDep(CriticalMemDep); 124 ++I; 125 continue; 126 } 127 } 128 129 // Check if this instruction is now ready. In case, force 130 // a transition in state using method 'update()'. 131 if (!IS.isReady() && !IS.updatePending()) { 132 ++I; 133 continue; 134 } 135 LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction #" << IR 136 << " promoted to the READY set.\n"); 137 138 Ready.emplace_back(IR); 139 ReadySet.emplace_back(IR); 140 141 IR.invalidate(); 142 ++PromotedElements; 143 std::iter_swap(I, E - PromotedElements); 144 } 145 146 PendingSet.resize(PendingSet.size() - PromotedElements); 147 return PromotedElements; 148 } 149 150 bool Scheduler::promoteToPendingSet() { 151 // Scan the set of waiting instructions and promote them to the 152 // pending set if operands are all ready. 153 unsigned RemovedElements = 0; 154 for (auto I = WaitSet.begin(), E = WaitSet.end(); I != E;) { 155 InstRef &IR = *I; 156 if (!IR) 157 break; 158 159 // Check if this instruction is now ready. In case, force 160 // a transition in state using method 'update()'. 161 Instruction &IS = *IR.getInstruction(); 162 if (IS.isDispatched() && !IS.updateDispatched()) { 163 ++I; 164 continue; 165 } 166 LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction #" << IR 167 << " promoted to the PENDING set.\n"); 168 169 PendingSet.emplace_back(IR); 170 171 IR.invalidate(); 172 ++RemovedElements; 173 std::iter_swap(I, E - RemovedElements); 174 } 175 176 WaitSet.resize(WaitSet.size() - RemovedElements); 177 return RemovedElements; 178 } 179 180 InstRef Scheduler::select() { 181 unsigned QueueIndex = ReadySet.size(); 182 for (unsigned I = 0, E = ReadySet.size(); I != E; ++I) { 183 InstRef &IR = ReadySet[I]; 184 if (QueueIndex == ReadySet.size() || 185 Strategy->compare(IR, ReadySet[QueueIndex])) { 186 const InstrDesc &D = IR.getInstruction()->getDesc(); 187 uint64_t BusyResourceMask = Resources->checkAvailability(D); 188 IR.getInstruction()->updateCriticalResourceMask(BusyResourceMask); 189 BusyResourceUnits |= BusyResourceMask; 190 if (!BusyResourceMask) 191 QueueIndex = I; 192 } 193 } 194 195 if (QueueIndex == ReadySet.size()) 196 return InstRef(); 197 198 // We found an instruction to issue. 199 InstRef IR = ReadySet[QueueIndex]; 200 std::swap(ReadySet[QueueIndex], ReadySet[ReadySet.size() - 1]); 201 ReadySet.pop_back(); 202 return IR; 203 } 204 205 void Scheduler::updateIssuedSet(SmallVectorImpl<InstRef> &Executed) { 206 unsigned RemovedElements = 0; 207 for (auto I = IssuedSet.begin(), E = IssuedSet.end(); I != E;) { 208 InstRef &IR = *I; 209 if (!IR) 210 break; 211 Instruction &IS = *IR.getInstruction(); 212 if (!IS.isExecuted()) { 213 LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction #" << IR 214 << " is still executing.\n"); 215 ++I; 216 continue; 217 } 218 219 // Instruction IR has completed execution. 220 LSU.onInstructionExecuted(IR); 221 Executed.emplace_back(IR); 222 ++RemovedElements; 223 IR.invalidate(); 224 std::iter_swap(I, E - RemovedElements); 225 } 226 227 IssuedSet.resize(IssuedSet.size() - RemovedElements); 228 } 229 230 void Scheduler::cycleEvent(SmallVectorImpl<ResourceRef> &Freed, 231 SmallVectorImpl<InstRef> &Executed, 232 SmallVectorImpl<InstRef> &Ready) { 233 // Release consumed resources. 234 Resources->cycleEvent(Freed); 235 236 for (InstRef &IR : IssuedSet) 237 IR.getInstruction()->cycleEvent(); 238 updateIssuedSet(Executed); 239 240 for (InstRef &IR : PendingSet) 241 IR.getInstruction()->cycleEvent(); 242 243 for (InstRef &IR : WaitSet) 244 IR.getInstruction()->cycleEvent(); 245 246 promoteToPendingSet(); 247 promoteToReadySet(Ready); 248 249 NumDispatchedToThePendingSet = 0; 250 BusyResourceUnits = 0; 251 } 252 253 bool Scheduler::mustIssueImmediately(const InstRef &IR) const { 254 const InstrDesc &Desc = IR.getInstruction()->getDesc(); 255 if (Desc.isZeroLatency()) 256 return true; 257 // Instructions that use an in-order dispatch/issue processor resource must be 258 // issued immediately to the pipeline(s). Any other in-order buffered 259 // resources (i.e. BufferSize=1) is consumed. 260 return Desc.MustIssueImmediately; 261 } 262 263 bool Scheduler::dispatch(const InstRef &IR) { 264 const Instruction &IS = *IR.getInstruction(); 265 const InstrDesc &Desc = IS.getDesc(); 266 Resources->reserveBuffers(Desc.Buffers); 267 268 // If necessary, reserve queue entries in the load-store unit (LSU). 269 if (IS.isMemOp()) 270 LSU.dispatch(IR); 271 272 if (IS.isPending()) { 273 LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding #" << IR 274 << " to the PendingSet\n"); 275 PendingSet.push_back(IR); 276 ++NumDispatchedToThePendingSet; 277 return false; 278 } 279 280 if (!IS.isReady() || 281 (IS.isMemOp() && LSU.isReady(IR) != IR.getSourceIndex())) { 282 LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding #" << IR << " to the WaitSet\n"); 283 WaitSet.push_back(IR); 284 return false; 285 } 286 287 // Don't add a zero-latency instruction to the Ready queue. 288 // A zero-latency instruction doesn't consume any scheduler resources. That is 289 // because it doesn't need to be executed, and it is often removed at register 290 // renaming stage. For example, register-register moves are often optimized at 291 // register renaming stage by simply updating register aliases. On some 292 // targets, zero-idiom instructions (for example: a xor that clears the value 293 // of a register) are treated specially, and are often eliminated at register 294 // renaming stage. 295 if (!mustIssueImmediately(IR)) { 296 LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding #" << IR << " to the ReadySet\n"); 297 ReadySet.push_back(IR); 298 } 299 300 return true; 301 } 302 303 } // namespace mca 304 } // namespace llvm 305