Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 1 | //===---------------------- RetireControlUnit.h -----------------*- 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. |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_TOOLS_LLVM_MCA_RETIRE_CONTROL_UNIT_H |
| 16 | #define LLVM_TOOLS_LLVM_MCA_RETIRE_CONTROL_UNIT_H |
| 17 | |
David Blaikie | 1ca61f6 | 2018-05-09 17:28:10 +0000 | [diff] [blame] | 18 | #include "Instruction.h" |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSchedule.h" |
| 20 | |
David Blaikie | c30365c | 2018-05-11 19:21:40 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <vector> |
| 23 | |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 24 | namespace mca { |
| 25 | |
Matt Davis | 679083e | 2018-05-17 19:22:29 +0000 | [diff] [blame] | 26 | class DispatchStage; |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 27 | |
| 28 | /// This class tracks which instructions are in-flight (i.e., dispatched but not |
| 29 | /// retired) in the OoO backend. |
| 30 | // |
| 31 | /// This class checks on every cycle if/which instructions can be retired. |
| 32 | /// Instructions are retired in program order. |
Matt Davis | 679083e | 2018-05-17 19:22:29 +0000 | [diff] [blame] | 33 | /// In the event of instruction retired, the DispatchStage object that owns |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 34 | /// this RetireControlUnit (RCU) gets notified. |
| 35 | /// On instruction retired, register updates are all architecturally |
| 36 | /// committed, and any temporary registers originally allocated for the |
| 37 | /// retired instruction are freed. |
| 38 | struct RetireControlUnit { |
| 39 | // A RUToken is created by the RCU for every instruction dispatched to the |
| 40 | // schedulers. These "tokens" are managed by the RCU in its token Queue. |
| 41 | // |
| 42 | // On evey cycle ('cycleEvent'), the RCU iterates through the token queue |
| 43 | // looking for any token with its 'Executed' flag set. If a token has that |
| 44 | // flag set, then the instruction has reached the write-back stage and will |
| 45 | // be retired by the RCU. |
| 46 | // |
| 47 | // 'NumSlots' represents the number of entries consumed by the instruction in |
| 48 | // the reorder buffer. Those entries will become available again once the |
| 49 | // instruction is retired. |
| 50 | // |
| 51 | // Note that the size of the reorder buffer is defined by the scheduling |
| 52 | // model via field 'NumMicroOpBufferSize'. |
| 53 | struct RUToken { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 54 | InstRef IR; |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 55 | unsigned NumSlots; // Slots reserved to this instruction. |
| 56 | bool Executed; // True if the instruction is past the WB stage. |
| 57 | }; |
| 58 | |
| 59 | private: |
| 60 | unsigned NextAvailableSlotIdx; |
| 61 | unsigned CurrentInstructionSlotIdx; |
| 62 | unsigned AvailableSlots; |
| 63 | unsigned MaxRetirePerCycle; // 0 means no limit. |
| 64 | std::vector<RUToken> Queue; |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 65 | |
| 66 | public: |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 67 | RetireControlUnit(const llvm::MCSchedModel &SM); |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 68 | |
| 69 | bool isFull() const { return !AvailableSlots; } |
| 70 | bool isEmpty() const { return AvailableSlots == Queue.size(); } |
| 71 | bool isAvailable(unsigned Quantity = 1) const { |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 72 | // Some instructions may declare a number of uOps which exceeds the size |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 73 | // of the reorder buffer. To avoid problems, cap the amount of slots to |
| 74 | // the size of the reorder buffer. |
| 75 | Quantity = std::min(Quantity, static_cast<unsigned>(Queue.size())); |
| 76 | return AvailableSlots >= Quantity; |
| 77 | } |
| 78 | |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 79 | unsigned getMaxRetirePerCycle() const { return MaxRetirePerCycle; } |
| 80 | |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 81 | // Reserves a number of slots, and returns a new token. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 82 | unsigned reserveSlot(const InstRef &IS, unsigned NumMicroOps); |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 83 | |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 84 | // Return the current token from the RCU's circular token queue. |
| 85 | const RUToken &peekCurrentToken() const; |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 86 | |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 87 | // Advance the pointer to the next token in the circular token queue. |
| 88 | void consumeCurrentToken(); |
| 89 | |
| 90 | // Update the RCU token to represent the executed state. |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 91 | void onInstructionExecuted(unsigned TokenID); |
| 92 | |
| 93 | #ifndef NDEBUG |
| 94 | void dump() const; |
| 95 | #endif |
| 96 | }; |
| 97 | |
| 98 | } // namespace mca |
| 99 | |
| 100 | #endif // LLVM_TOOLS_LLVM_MCA_RETIRE_CONTROL_UNIT_H |