Andrea Di Biagio | e2492c8 | 2018-05-15 09:31:32 +0000 | [diff] [blame] | 1 | //===---------------------- RetireControlUnit.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 | /// \file |
| 10 | /// |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 11 | /// This file simulates the hardware responsible for retiring instructions. |
Andrea Di Biagio | e2492c8 | 2018-05-15 09:31:32 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Clement Courbet | cc5e6a7 | 2018-12-17 08:08:31 +0000 | [diff] [blame] | 15 | #include "llvm/MCA/HardwareUnits/RetireControlUnit.h" |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
| 17 | |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "llvm-mca" |
| 19 | |
Fangrui Song | 5a8fd65 | 2018-10-30 15:56:08 +0000 | [diff] [blame] | 20 | namespace llvm { |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 21 | namespace mca { |
| 22 | |
Andrea Di Biagio | a769912 | 2018-09-28 10:47:24 +0000 | [diff] [blame] | 23 | RetireControlUnit::RetireControlUnit(const MCSchedModel &SM) |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 24 | : NextAvailableSlotIdx(0), CurrentInstructionSlotIdx(0), |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 25 | AvailableSlots(SM.MicroOpBufferSize), MaxRetirePerCycle(0) { |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 26 | // Check if the scheduling model provides extra information about the machine |
| 27 | // processor. If so, then use that information to set the reorder buffer size |
| 28 | // and the maximum number of instructions retired per cycle. |
| 29 | if (SM.hasExtraProcessorInfo()) { |
| 30 | const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo(); |
| 31 | if (EPI.ReorderBufferSize) |
| 32 | AvailableSlots = EPI.ReorderBufferSize; |
| 33 | MaxRetirePerCycle = EPI.MaxRetirePerCycle; |
| 34 | } |
| 35 | |
| 36 | assert(AvailableSlots && "Invalid reorder buffer size!"); |
| 37 | Queue.resize(AvailableSlots); |
| 38 | } |
| 39 | |
| 40 | // Reserves a number of slots, and returns a new token. |
Andrea Di Biagio | 904684c | 2018-05-15 10:30:39 +0000 | [diff] [blame] | 41 | unsigned RetireControlUnit::reserveSlot(const InstRef &IR, |
| 42 | unsigned NumMicroOps) { |
Andrea Di Biagio | aacd5e1 | 2018-10-04 10:36:49 +0000 | [diff] [blame] | 43 | assert(isAvailable(NumMicroOps) && "Reorder Buffer unavailable!"); |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 44 | unsigned NormalizedQuantity = |
| 45 | std::min(NumMicroOps, static_cast<unsigned>(Queue.size())); |
Andrea Di Biagio | aacd5e1 | 2018-10-04 10:36:49 +0000 | [diff] [blame] | 46 | // Zero latency instructions may have zero uOps. Artificially bump this |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 47 | // value to 1. Although zero latency instructions don't consume scheduler |
| 48 | // resources, they still consume one slot in the retire queue. |
| 49 | NormalizedQuantity = std::max(NormalizedQuantity, 1U); |
| 50 | unsigned TokenID = NextAvailableSlotIdx; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 51 | Queue[NextAvailableSlotIdx] = {IR, NormalizedQuantity, false}; |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 52 | NextAvailableSlotIdx += NormalizedQuantity; |
| 53 | NextAvailableSlotIdx %= Queue.size(); |
| 54 | AvailableSlots -= NormalizedQuantity; |
| 55 | return TokenID; |
| 56 | } |
| 57 | |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 58 | const RetireControlUnit::RUToken &RetireControlUnit::peekCurrentToken() const { |
| 59 | return Queue[CurrentInstructionSlotIdx]; |
| 60 | } |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 61 | |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 62 | void RetireControlUnit::consumeCurrentToken() { |
Andrea Di Biagio | dffec12 | 2018-11-09 12:29:57 +0000 | [diff] [blame] | 63 | RetireControlUnit::RUToken &Current = Queue[CurrentInstructionSlotIdx]; |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 64 | assert(Current.NumSlots && "Reserved zero slots?"); |
Matt Davis | b5d5deb | 2018-10-24 20:27:47 +0000 | [diff] [blame] | 65 | assert(Current.IR && "Invalid RUToken in the RCU queue."); |
Andrea Di Biagio | dffec12 | 2018-11-09 12:29:57 +0000 | [diff] [blame] | 66 | Current.IR.getInstruction()->retire(); |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 67 | |
| 68 | // Update the slot index to be the next item in the circular queue. |
| 69 | CurrentInstructionSlotIdx += Current.NumSlots; |
| 70 | CurrentInstructionSlotIdx %= Queue.size(); |
| 71 | AvailableSlots += Current.NumSlots; |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void RetireControlUnit::onInstructionExecuted(unsigned TokenID) { |
| 75 | assert(Queue.size() > TokenID); |
Matt Davis | b5d5deb | 2018-10-24 20:27:47 +0000 | [diff] [blame] | 76 | assert(Queue[TokenID].Executed == false && Queue[TokenID].IR); |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 77 | Queue[TokenID].Executed = true; |
| 78 | } |
| 79 | |
| 80 | #ifndef NDEBUG |
| 81 | void RetireControlUnit::dump() const { |
| 82 | dbgs() << "Retire Unit: { Total Slots=" << Queue.size() |
| 83 | << ", Available Slots=" << AvailableSlots << " }\n"; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | } // namespace mca |
Fangrui Song | 5a8fd65 | 2018-10-30 15:56:08 +0000 | [diff] [blame] | 88 | } // namespace llvm |