10b57cec5SDimitry Andric //===--------------------- Scheduler.cpp ------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // A scheduler for processor resource units and processor resource groups. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/MCA/HardwareUnits/Scheduler.h" 140b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 150b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric namespace llvm { 180b57cec5SDimitry Andric namespace mca { 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric #define DEBUG_TYPE "llvm-mca" 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric void Scheduler::initializeStrategy(std::unique_ptr<SchedulerStrategy> S) { 230b57cec5SDimitry Andric // Ensure we have a valid (non-null) strategy object. 248bcb0991SDimitry Andric Strategy = S ? std::move(S) : std::make_unique<DefaultSchedulerStrategy>(); 250b57cec5SDimitry Andric } 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric // Anchor the vtable of SchedulerStrategy and DefaultSchedulerStrategy. 280b57cec5SDimitry Andric SchedulerStrategy::~SchedulerStrategy() = default; 290b57cec5SDimitry Andric DefaultSchedulerStrategy::~DefaultSchedulerStrategy() = default; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric #ifndef NDEBUG 320b57cec5SDimitry Andric void Scheduler::dump() const { 330b57cec5SDimitry Andric dbgs() << "[SCHEDULER]: WaitSet size is: " << WaitSet.size() << '\n'; 340b57cec5SDimitry Andric dbgs() << "[SCHEDULER]: ReadySet size is: " << ReadySet.size() << '\n'; 350b57cec5SDimitry Andric dbgs() << "[SCHEDULER]: IssuedSet size is: " << IssuedSet.size() << '\n'; 360b57cec5SDimitry Andric Resources->dump(); 370b57cec5SDimitry Andric } 380b57cec5SDimitry Andric #endif 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric Scheduler::Status Scheduler::isAvailable(const InstRef &IR) { 418bcb0991SDimitry Andric ResourceStateEvent RSE = 428bcb0991SDimitry Andric Resources->canBeDispatched(IR.getInstruction()->getUsedBuffers()); 430b57cec5SDimitry Andric HadTokenStall = RSE != RS_BUFFER_AVAILABLE; 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric switch (RSE) { 460b57cec5SDimitry Andric case ResourceStateEvent::RS_BUFFER_UNAVAILABLE: 470b57cec5SDimitry Andric return Scheduler::SC_BUFFERS_FULL; 480b57cec5SDimitry Andric case ResourceStateEvent::RS_RESERVED: 490b57cec5SDimitry Andric return Scheduler::SC_DISPATCH_GROUP_STALL; 500b57cec5SDimitry Andric case ResourceStateEvent::RS_BUFFER_AVAILABLE: 510b57cec5SDimitry Andric break; 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric // Give lower priority to LSUnit stall events. 550b57cec5SDimitry Andric LSUnit::Status LSS = LSU.isAvailable(IR); 560b57cec5SDimitry Andric HadTokenStall = LSS != LSUnit::LSU_AVAILABLE; 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric switch (LSS) { 590b57cec5SDimitry Andric case LSUnit::LSU_LQUEUE_FULL: 600b57cec5SDimitry Andric return Scheduler::SC_LOAD_QUEUE_FULL; 610b57cec5SDimitry Andric case LSUnit::LSU_SQUEUE_FULL: 620b57cec5SDimitry Andric return Scheduler::SC_STORE_QUEUE_FULL; 630b57cec5SDimitry Andric case LSUnit::LSU_AVAILABLE: 640b57cec5SDimitry Andric return Scheduler::SC_AVAILABLE; 650b57cec5SDimitry Andric } 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric llvm_unreachable("Don't know how to process this LSU state result!"); 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric void Scheduler::issueInstructionImpl( 710b57cec5SDimitry Andric InstRef &IR, 72*5f757f3fSDimitry Andric SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &UsedResources) { 730b57cec5SDimitry Andric Instruction *IS = IR.getInstruction(); 740b57cec5SDimitry Andric const InstrDesc &D = IS->getDesc(); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric // Issue the instruction and collect all the consumed resources 770b57cec5SDimitry Andric // into a vector. That vector is then used to notify the listener. 780b57cec5SDimitry Andric Resources->issueInstruction(D, UsedResources); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric // Notify the instruction that it started executing. 810b57cec5SDimitry Andric // This updates the internal state of each write. 820b57cec5SDimitry Andric IS->execute(IR.getSourceIndex()); 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric IS->computeCriticalRegDep(); 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric if (IS->isMemOp()) { 870b57cec5SDimitry Andric LSU.onInstructionIssued(IR); 880b57cec5SDimitry Andric const MemoryGroup &Group = LSU.getGroup(IS->getLSUTokenID()); 890b57cec5SDimitry Andric IS->setCriticalMemDep(Group.getCriticalPredecessor()); 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric if (IS->isExecuting()) 930b57cec5SDimitry Andric IssuedSet.emplace_back(IR); 940b57cec5SDimitry Andric else if (IS->isExecuted()) 950b57cec5SDimitry Andric LSU.onInstructionExecuted(IR); 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric // Release the buffered resources and issue the instruction. 990b57cec5SDimitry Andric void Scheduler::issueInstruction( 1000b57cec5SDimitry Andric InstRef &IR, 101*5f757f3fSDimitry Andric SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &UsedResources, 1020b57cec5SDimitry Andric SmallVectorImpl<InstRef> &PendingInstructions, 1030b57cec5SDimitry Andric SmallVectorImpl<InstRef> &ReadyInstructions) { 1040b57cec5SDimitry Andric const Instruction &Inst = *IR.getInstruction(); 1050b57cec5SDimitry Andric bool HasDependentUsers = Inst.hasDependentUsers(); 1060b57cec5SDimitry Andric HasDependentUsers |= Inst.isMemOp() && LSU.hasDependentUsers(IR); 1070b57cec5SDimitry Andric 1088bcb0991SDimitry Andric Resources->releaseBuffers(Inst.getUsedBuffers()); 1090b57cec5SDimitry Andric issueInstructionImpl(IR, UsedResources); 1100b57cec5SDimitry Andric // Instructions that have been issued during this cycle might have unblocked 1110b57cec5SDimitry Andric // other dependent instructions. Dependent instructions may be issued during 1120b57cec5SDimitry Andric // this same cycle if operands have ReadAdvance entries. Promote those 1130b57cec5SDimitry Andric // instructions to the ReadySet and notify the caller that those are ready. 1140b57cec5SDimitry Andric if (HasDependentUsers) 1150b57cec5SDimitry Andric if (promoteToPendingSet(PendingInstructions)) 1160b57cec5SDimitry Andric promoteToReadySet(ReadyInstructions); 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric bool Scheduler::promoteToReadySet(SmallVectorImpl<InstRef> &Ready) { 1200b57cec5SDimitry Andric // Scan the set of waiting instructions and promote them to the 1210b57cec5SDimitry Andric // ready set if operands are all ready. 1220b57cec5SDimitry Andric unsigned PromotedElements = 0; 1230b57cec5SDimitry Andric for (auto I = PendingSet.begin(), E = PendingSet.end(); I != E;) { 1240b57cec5SDimitry Andric InstRef &IR = *I; 1250b57cec5SDimitry Andric if (!IR) 1260b57cec5SDimitry Andric break; 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric // Check if there are unsolved register dependencies. 1290b57cec5SDimitry Andric Instruction &IS = *IR.getInstruction(); 1300b57cec5SDimitry Andric if (!IS.isReady() && !IS.updatePending()) { 1310b57cec5SDimitry Andric ++I; 1320b57cec5SDimitry Andric continue; 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric // Check if there are unsolved memory dependencies. 1350b57cec5SDimitry Andric if (IS.isMemOp() && !LSU.isReady(IR)) { 1360b57cec5SDimitry Andric ++I; 1370b57cec5SDimitry Andric continue; 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction #" << IR 1410b57cec5SDimitry Andric << " promoted to the READY set.\n"); 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric Ready.emplace_back(IR); 1440b57cec5SDimitry Andric ReadySet.emplace_back(IR); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric IR.invalidate(); 1470b57cec5SDimitry Andric ++PromotedElements; 1480b57cec5SDimitry Andric std::iter_swap(I, E - PromotedElements); 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric PendingSet.resize(PendingSet.size() - PromotedElements); 1520b57cec5SDimitry Andric return PromotedElements; 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric bool Scheduler::promoteToPendingSet(SmallVectorImpl<InstRef> &Pending) { 1560b57cec5SDimitry Andric // Scan the set of waiting instructions and promote them to the 1570b57cec5SDimitry Andric // pending set if operands are all ready. 1580b57cec5SDimitry Andric unsigned RemovedElements = 0; 1590b57cec5SDimitry Andric for (auto I = WaitSet.begin(), E = WaitSet.end(); I != E;) { 1600b57cec5SDimitry Andric InstRef &IR = *I; 1610b57cec5SDimitry Andric if (!IR) 1620b57cec5SDimitry Andric break; 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric // Check if this instruction is now ready. In case, force 1650b57cec5SDimitry Andric // a transition in state using method 'updateDispatched()'. 1660b57cec5SDimitry Andric Instruction &IS = *IR.getInstruction(); 1670b57cec5SDimitry Andric if (IS.isDispatched() && !IS.updateDispatched()) { 1680b57cec5SDimitry Andric ++I; 1690b57cec5SDimitry Andric continue; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric if (IS.isMemOp() && LSU.isWaiting(IR)) { 1730b57cec5SDimitry Andric ++I; 1740b57cec5SDimitry Andric continue; 1750b57cec5SDimitry Andric } 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction #" << IR 1780b57cec5SDimitry Andric << " promoted to the PENDING set.\n"); 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric Pending.emplace_back(IR); 1810b57cec5SDimitry Andric PendingSet.emplace_back(IR); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric IR.invalidate(); 1840b57cec5SDimitry Andric ++RemovedElements; 1850b57cec5SDimitry Andric std::iter_swap(I, E - RemovedElements); 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric WaitSet.resize(WaitSet.size() - RemovedElements); 1890b57cec5SDimitry Andric return RemovedElements; 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric InstRef Scheduler::select() { 1930b57cec5SDimitry Andric unsigned QueueIndex = ReadySet.size(); 1940b57cec5SDimitry Andric for (unsigned I = 0, E = ReadySet.size(); I != E; ++I) { 1950b57cec5SDimitry Andric InstRef &IR = ReadySet[I]; 1960b57cec5SDimitry Andric if (QueueIndex == ReadySet.size() || 1970b57cec5SDimitry Andric Strategy->compare(IR, ReadySet[QueueIndex])) { 1980b57cec5SDimitry Andric Instruction &IS = *IR.getInstruction(); 1990b57cec5SDimitry Andric uint64_t BusyResourceMask = Resources->checkAvailability(IS.getDesc()); 2000b57cec5SDimitry Andric if (BusyResourceMask) 2010b57cec5SDimitry Andric IS.setCriticalResourceMask(BusyResourceMask); 2020b57cec5SDimitry Andric BusyResourceUnits |= BusyResourceMask; 2030b57cec5SDimitry Andric if (!BusyResourceMask) 2040b57cec5SDimitry Andric QueueIndex = I; 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric if (QueueIndex == ReadySet.size()) 2090b57cec5SDimitry Andric return InstRef(); 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric // We found an instruction to issue. 2120b57cec5SDimitry Andric InstRef IR = ReadySet[QueueIndex]; 2130b57cec5SDimitry Andric std::swap(ReadySet[QueueIndex], ReadySet[ReadySet.size() - 1]); 2140b57cec5SDimitry Andric ReadySet.pop_back(); 2150b57cec5SDimitry Andric return IR; 2160b57cec5SDimitry Andric } 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric void Scheduler::updateIssuedSet(SmallVectorImpl<InstRef> &Executed) { 2190b57cec5SDimitry Andric unsigned RemovedElements = 0; 2200b57cec5SDimitry Andric for (auto I = IssuedSet.begin(), E = IssuedSet.end(); I != E;) { 2210b57cec5SDimitry Andric InstRef &IR = *I; 2220b57cec5SDimitry Andric if (!IR) 2230b57cec5SDimitry Andric break; 2240b57cec5SDimitry Andric Instruction &IS = *IR.getInstruction(); 2250b57cec5SDimitry Andric if (!IS.isExecuted()) { 2260b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "[SCHEDULER]: Instruction #" << IR 2270b57cec5SDimitry Andric << " is still executing.\n"); 2280b57cec5SDimitry Andric ++I; 2290b57cec5SDimitry Andric continue; 2300b57cec5SDimitry Andric } 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric // Instruction IR has completed execution. 2330b57cec5SDimitry Andric LSU.onInstructionExecuted(IR); 2340b57cec5SDimitry Andric Executed.emplace_back(IR); 2350b57cec5SDimitry Andric ++RemovedElements; 2360b57cec5SDimitry Andric IR.invalidate(); 2370b57cec5SDimitry Andric std::iter_swap(I, E - RemovedElements); 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric IssuedSet.resize(IssuedSet.size() - RemovedElements); 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric uint64_t Scheduler::analyzeResourcePressure(SmallVectorImpl<InstRef> &Insts) { 244e8d8bef9SDimitry Andric llvm::append_range(Insts, ReadySet); 2450b57cec5SDimitry Andric return BusyResourceUnits; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric void Scheduler::analyzeDataDependencies(SmallVectorImpl<InstRef> &RegDeps, 2490b57cec5SDimitry Andric SmallVectorImpl<InstRef> &MemDeps) { 2500b57cec5SDimitry Andric const auto EndIt = PendingSet.end() - NumDispatchedToThePendingSet; 2510b57cec5SDimitry Andric for (const InstRef &IR : make_range(PendingSet.begin(), EndIt)) { 2520b57cec5SDimitry Andric const Instruction &IS = *IR.getInstruction(); 2530b57cec5SDimitry Andric if (Resources->checkAvailability(IS.getDesc())) 2540b57cec5SDimitry Andric continue; 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric if (IS.isMemOp() && LSU.isPending(IR)) 2570b57cec5SDimitry Andric MemDeps.emplace_back(IR); 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric if (IS.isPending()) 2600b57cec5SDimitry Andric RegDeps.emplace_back(IR); 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric } 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric void Scheduler::cycleEvent(SmallVectorImpl<ResourceRef> &Freed, 2650b57cec5SDimitry Andric SmallVectorImpl<InstRef> &Executed, 2660b57cec5SDimitry Andric SmallVectorImpl<InstRef> &Pending, 2670b57cec5SDimitry Andric SmallVectorImpl<InstRef> &Ready) { 2680b57cec5SDimitry Andric LSU.cycleEvent(); 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric // Release consumed resources. 2710b57cec5SDimitry Andric Resources->cycleEvent(Freed); 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric for (InstRef &IR : IssuedSet) 2740b57cec5SDimitry Andric IR.getInstruction()->cycleEvent(); 2750b57cec5SDimitry Andric updateIssuedSet(Executed); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric for (InstRef &IR : PendingSet) 2780b57cec5SDimitry Andric IR.getInstruction()->cycleEvent(); 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric for (InstRef &IR : WaitSet) 2810b57cec5SDimitry Andric IR.getInstruction()->cycleEvent(); 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric promoteToPendingSet(Pending); 2840b57cec5SDimitry Andric promoteToReadySet(Ready); 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric NumDispatchedToThePendingSet = 0; 2870b57cec5SDimitry Andric BusyResourceUnits = 0; 2880b57cec5SDimitry Andric } 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric bool Scheduler::mustIssueImmediately(const InstRef &IR) const { 2910b57cec5SDimitry Andric const InstrDesc &Desc = IR.getInstruction()->getDesc(); 2920b57cec5SDimitry Andric if (Desc.isZeroLatency()) 2930b57cec5SDimitry Andric return true; 2940b57cec5SDimitry Andric // Instructions that use an in-order dispatch/issue processor resource must be 2950b57cec5SDimitry Andric // issued immediately to the pipeline(s). Any other in-order buffered 2960b57cec5SDimitry Andric // resources (i.e. BufferSize=1) is consumed. 2970b57cec5SDimitry Andric return Desc.MustIssueImmediately; 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric bool Scheduler::dispatch(InstRef &IR) { 3010b57cec5SDimitry Andric Instruction &IS = *IR.getInstruction(); 3028bcb0991SDimitry Andric Resources->reserveBuffers(IS.getUsedBuffers()); 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric // If necessary, reserve queue entries in the load-store unit (LSU). 3050b57cec5SDimitry Andric if (IS.isMemOp()) 3060b57cec5SDimitry Andric IS.setLSUTokenID(LSU.dispatch(IR)); 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric if (IS.isDispatched() || (IS.isMemOp() && LSU.isWaiting(IR))) { 3090b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding #" << IR << " to the WaitSet\n"); 3100b57cec5SDimitry Andric WaitSet.push_back(IR); 3110b57cec5SDimitry Andric return false; 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric if (IS.isPending() || (IS.isMemOp() && LSU.isPending(IR))) { 3150b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding #" << IR 3160b57cec5SDimitry Andric << " to the PendingSet\n"); 3170b57cec5SDimitry Andric PendingSet.push_back(IR); 3180b57cec5SDimitry Andric ++NumDispatchedToThePendingSet; 3190b57cec5SDimitry Andric return false; 3200b57cec5SDimitry Andric } 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric assert(IS.isReady() && (!IS.isMemOp() || LSU.isReady(IR)) && 3230b57cec5SDimitry Andric "Unexpected internal state found!"); 3240b57cec5SDimitry Andric // Don't add a zero-latency instruction to the Ready queue. 3250b57cec5SDimitry Andric // A zero-latency instruction doesn't consume any scheduler resources. That is 3260b57cec5SDimitry Andric // because it doesn't need to be executed, and it is often removed at register 3270b57cec5SDimitry Andric // renaming stage. For example, register-register moves are often optimized at 3280b57cec5SDimitry Andric // register renaming stage by simply updating register aliases. On some 3290b57cec5SDimitry Andric // targets, zero-idiom instructions (for example: a xor that clears the value 3300b57cec5SDimitry Andric // of a register) are treated specially, and are often eliminated at register 3310b57cec5SDimitry Andric // renaming stage. 3320b57cec5SDimitry Andric if (!mustIssueImmediately(IR)) { 3330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "[SCHEDULER] Adding #" << IR << " to the ReadySet\n"); 3340b57cec5SDimitry Andric ReadySet.push_back(IR); 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric return true; 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric } // namespace mca 3410b57cec5SDimitry Andric } // namespace llvm 342