Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 1 | #include "Dispatch.h" |
| 2 | #include "RetireControlUnit.h" |
| 3 | #include "llvm/MC/MCSchedule.h" |
| 4 | #include "llvm/Support/Debug.h" |
| 5 | |
| 6 | using namespace llvm; |
| 7 | |
| 8 | #define DEBUG_TYPE "llvm-mca" |
| 9 | |
| 10 | namespace mca { |
| 11 | |
| 12 | RetireControlUnit::RetireControlUnit(const llvm::MCSchedModel &SM, |
| 13 | DispatchUnit *DU) |
| 14 | : NextAvailableSlotIdx(0), CurrentInstructionSlotIdx(0), |
| 15 | AvailableSlots(SM.MicroOpBufferSize), MaxRetirePerCycle(0), Owner(DU) { |
| 16 | // Check if the scheduling model provides extra information about the machine |
| 17 | // processor. If so, then use that information to set the reorder buffer size |
| 18 | // and the maximum number of instructions retired per cycle. |
| 19 | if (SM.hasExtraProcessorInfo()) { |
| 20 | const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo(); |
| 21 | if (EPI.ReorderBufferSize) |
| 22 | AvailableSlots = EPI.ReorderBufferSize; |
| 23 | MaxRetirePerCycle = EPI.MaxRetirePerCycle; |
| 24 | } |
| 25 | |
| 26 | assert(AvailableSlots && "Invalid reorder buffer size!"); |
| 27 | Queue.resize(AvailableSlots); |
| 28 | } |
| 29 | |
| 30 | // Reserves a number of slots, and returns a new token. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame^] | 31 | unsigned RetireControlUnit::reserveSlot(const InstRef &IR, unsigned NumMicroOps) { |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 32 | assert(isAvailable(NumMicroOps)); |
| 33 | unsigned NormalizedQuantity = |
| 34 | std::min(NumMicroOps, static_cast<unsigned>(Queue.size())); |
| 35 | // Zero latency instructions may have zero mOps. Artificially bump this |
| 36 | // value to 1. Although zero latency instructions don't consume scheduler |
| 37 | // resources, they still consume one slot in the retire queue. |
| 38 | NormalizedQuantity = std::max(NormalizedQuantity, 1U); |
| 39 | unsigned TokenID = NextAvailableSlotIdx; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame^] | 40 | Queue[NextAvailableSlotIdx] = {IR, NormalizedQuantity, false}; |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 41 | NextAvailableSlotIdx += NormalizedQuantity; |
| 42 | NextAvailableSlotIdx %= Queue.size(); |
| 43 | AvailableSlots -= NormalizedQuantity; |
| 44 | return TokenID; |
| 45 | } |
| 46 | |
| 47 | void RetireControlUnit::cycleEvent() { |
| 48 | if (isEmpty()) |
| 49 | return; |
| 50 | |
| 51 | unsigned NumRetired = 0; |
| 52 | while (!isEmpty()) { |
| 53 | if (MaxRetirePerCycle != 0 && NumRetired == MaxRetirePerCycle) |
| 54 | break; |
| 55 | RUToken &Current = Queue[CurrentInstructionSlotIdx]; |
| 56 | assert(Current.NumSlots && "Reserved zero slots?"); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame^] | 57 | assert(Current.IR.isValid() && "Invalid RUToken in the RCU queue."); |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 58 | if (!Current.Executed) |
| 59 | break; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame^] | 60 | Owner->notifyInstructionRetired(Current.IR); |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 61 | CurrentInstructionSlotIdx += Current.NumSlots; |
| 62 | CurrentInstructionSlotIdx %= Queue.size(); |
| 63 | AvailableSlots += Current.NumSlots; |
| 64 | NumRetired++; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void RetireControlUnit::onInstructionExecuted(unsigned TokenID) { |
| 69 | assert(Queue.size() > TokenID); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame^] | 70 | assert(Queue[TokenID].Executed == false && Queue[TokenID].IR.isValid()); |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 71 | Queue[TokenID].Executed = true; |
| 72 | } |
| 73 | |
| 74 | #ifndef NDEBUG |
| 75 | void RetireControlUnit::dump() const { |
| 76 | dbgs() << "Retire Unit: { Total Slots=" << Queue.size() |
| 77 | << ", Available Slots=" << AvailableSlots << " }\n"; |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | } // namespace mca |