Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===----------------------- Dispatch.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 | /// |
| 11 | /// This file implements classes that are used to model register files, |
| 12 | /// reorder buffers and the hardware dispatch logic. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_TOOLS_LLVM_MCA_DISPATCH_H |
| 17 | #define LLVM_TOOLS_LLVM_MCA_DISPATCH_H |
| 18 | |
| 19 | #include "Instruction.h" |
| 20 | #include "llvm/MC/MCRegisterInfo.h" |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSubtargetInfo.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 22 | #include <map> |
| 23 | |
| 24 | namespace mca { |
| 25 | |
| 26 | class WriteState; |
| 27 | class DispatchUnit; |
| 28 | class Scheduler; |
| 29 | class Backend; |
| 30 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 31 | /// \brief Manages hardware register files, and tracks data dependencies |
| 32 | /// between registers. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 33 | class RegisterFile { |
| 34 | const llvm::MCRegisterInfo &MRI; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 35 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 36 | // Each register file is described by an instance of RegisterMappingTracker. |
| 37 | // RegisterMappingTracker tracks the number of register mappings dynamically |
| 38 | // allocated during the execution. |
| 39 | struct RegisterMappingTracker { |
| 40 | // Total number of register mappings that are available for register |
| 41 | // renaming. A value of zero for this field means: this register file has |
| 42 | // an unbounded number of registers. |
| 43 | const unsigned TotalMappings; |
| 44 | // Number of mappings that are currently in use. |
| 45 | unsigned NumUsedMappings; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 46 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 47 | RegisterMappingTracker(unsigned NumMappings) |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 48 | : TotalMappings(NumMappings), NumUsedMappings(0) {} |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 49 | }; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 50 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 51 | // This is where information related to the various register files is kept. |
| 52 | // This set always contains at least one register file at index #0. That |
| 53 | // register file "sees" all the physical registers declared by the target, and |
| 54 | // (by default) it allows an unbound number of mappings. |
| 55 | // Users can limit the number of mappings that can be created by register file |
| 56 | // #0 through the command line flag `-register-file-size`. |
| 57 | llvm::SmallVector<RegisterMappingTracker, 4> RegisterFiles; |
| 58 | |
| 59 | // RegisterMapping objects are mainly used to track physical register |
| 60 | // definitions. A WriteState object describes a register definition, and it is |
| 61 | // used to track RAW dependencies (see Instruction.h). A RegisterMapping |
| 62 | // object also specifies the set of register files. The mapping between |
| 63 | // physreg and register files is done using a "register file mask". |
| 64 | // |
| 65 | // A register file mask identifies a set of register files. Each bit of the |
| 66 | // mask representation references a specific register file. |
| 67 | // For example: |
| 68 | // 0b0001 --> Register file #0 |
| 69 | // 0b0010 --> Register file #1 |
| 70 | // 0b0100 --> Register file #2 |
| 71 | // |
| 72 | // Note that this implementation allows register files to overlap. |
| 73 | // The maximum number of register files allowed by this implementation is 32. |
| 74 | using RegisterMapping = std::pair<WriteState *, unsigned>; |
| 75 | |
| 76 | // This map contains one entry for each physical register defined by the |
| 77 | // processor scheduling model. |
| 78 | std::vector<RegisterMapping> RegisterMappings; |
| 79 | |
| 80 | // This method creates a new RegisterMappingTracker for a register file that |
| 81 | // contains all the physical registers specified by the register classes in |
| 82 | // the 'RegisterClasses' set. |
| 83 | // |
| 84 | // The long term goal is to let scheduling models optionally describe register |
| 85 | // files via tablegen definitions. This is still a work in progress. |
| 86 | // For example, here is how a tablegen definition for a x86 FP register file |
| 87 | // that features AVX might look like: |
| 88 | // |
| 89 | // def FPRegisterFile : RegisterFile<[VR128RegClass, VR256RegClass], 60> |
| 90 | // |
| 91 | // Here FPRegisterFile contains all the registers defined by register class |
| 92 | // VR128RegClass and VR256RegClass. FPRegisterFile implements 60 |
| 93 | // registers which can be used for register renaming purpose. |
| 94 | // |
| 95 | // The list of register classes is then converted by the tablegen backend into |
| 96 | // a list of register class indices. That list, along with the number of |
| 97 | // available mappings, is then used to create a new RegisterMappingTracker. |
| 98 | void addRegisterFile(llvm::ArrayRef<unsigned> RegisterClasses, |
| 99 | unsigned NumTemps); |
| 100 | |
| 101 | // Allocates a new register mapping in every register file specified by the |
| 102 | // register file mask. This method is called from addRegisterMapping. |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 103 | void createNewMappings(unsigned RegisterFileMask, |
| 104 | llvm::MutableArrayRef<unsigned> UsedPhysRegs); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 105 | |
| 106 | // Removes a previously allocated mapping from each register file in the |
| 107 | // RegisterFileMask set. This method is called from invalidateRegisterMapping. |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 108 | void removeMappings(unsigned RegisterFileMask, |
| 109 | llvm::MutableArrayRef<unsigned> FreedPhysRegs); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 110 | |
| 111 | public: |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 112 | RegisterFile(const llvm::MCRegisterInfo &mri, unsigned TempRegs = 0) |
| 113 | : MRI(mri), RegisterMappings(MRI.getNumRegs(), {nullptr, 0U}) { |
| 114 | addRegisterFile({}, TempRegs); |
| 115 | // TODO: teach the scheduling models how to specify multiple register files. |
| 116 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 117 | |
| 118 | // Creates a new register mapping for RegID. |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 119 | // This reserves a microarchitectural register in every register file that |
| 120 | // contains RegID. |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 121 | void addRegisterMapping(WriteState &WS, |
| 122 | llvm::MutableArrayRef<unsigned> UsedPhysRegs); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 123 | |
| 124 | // Invalidates register mappings associated to the input WriteState object. |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 125 | // This releases previously allocated mappings for the physical register |
| 126 | // associated to the WriteState. |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 127 | void invalidateRegisterMapping(const WriteState &WS, |
| 128 | llvm::MutableArrayRef<unsigned> FreedPhysRegs); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 129 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 130 | // Checks if there are enough microarchitectural registers in the register |
| 131 | // files. Returns a "response mask" where each bit is the response from a |
| 132 | // RegisterMappingTracker. |
| 133 | // For example: if all register files are available, then the response mask |
| 134 | // is a bitmask of all zeroes. If Instead register file #1 is not available, |
| 135 | // then the response mask is 0b10. |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 136 | unsigned isAvailable(llvm::ArrayRef<unsigned> Regs) const; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 137 | void collectWrites(llvm::SmallVectorImpl<WriteState *> &Writes, |
| 138 | unsigned RegID) const; |
| 139 | void updateOnRead(ReadState &RS, unsigned RegID); |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 140 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 141 | unsigned getNumRegisterFiles() const { return RegisterFiles.size(); } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 142 | |
| 143 | #ifndef NDEBUG |
| 144 | void dump() const; |
| 145 | #endif |
| 146 | }; |
| 147 | |
| 148 | /// \brief tracks which instructions are in-flight (i.e. dispatched but not |
| 149 | /// retired) in the OoO backend. |
| 150 | /// |
| 151 | /// This class checks on every cycle if/which instructions can be retired. |
| 152 | /// Instructions are retired in program order. |
| 153 | /// In the event of instruction retired, the DispatchUnit object that owns |
| 154 | /// this RetireControlUnit gets notified. |
| 155 | /// On instruction retired, register updates are all architecturally |
| 156 | /// committed, and any temporary registers originally allocated for the |
| 157 | /// retired instruction are freed. |
| 158 | struct RetireControlUnit { |
| 159 | // A "token" (object of class RUToken) is created by the retire unit for every |
| 160 | // instruction dispatched to the schedulers. Flag 'Executed' is used to |
| 161 | // quickly check if an instruction has reached the write-back stage. A token |
| 162 | // also carries information related to the number of entries consumed by the |
| 163 | // instruction in the reorder buffer. The idea is that those entries will |
| 164 | // become available again once the instruction is retired. On every cycle, |
| 165 | // the RCU (Retire Control Unit) scans every token starting to search for |
| 166 | // instructions that are ready to retire. retired. Instructions are retired |
| 167 | // in program order. Only 'Executed' instructions are eligible for retire. |
| 168 | // Note that the size of the reorder buffer is defined by the scheduling model |
| 169 | // via field 'NumMicroOpBufferSize'. |
| 170 | struct RUToken { |
| 171 | unsigned Index; // Instruction index. |
| 172 | unsigned NumSlots; // Slots reserved to this instruction. |
| 173 | bool Executed; // True if the instruction is past the WB stage. |
| 174 | }; |
| 175 | |
| 176 | private: |
| 177 | unsigned NextAvailableSlotIdx; |
| 178 | unsigned CurrentInstructionSlotIdx; |
| 179 | unsigned AvailableSlots; |
| 180 | unsigned MaxRetirePerCycle; // 0 means no limit. |
| 181 | std::vector<RUToken> Queue; |
| 182 | DispatchUnit *Owner; |
| 183 | |
| 184 | public: |
| 185 | RetireControlUnit(unsigned NumSlots, unsigned RPC, DispatchUnit *DU) |
| 186 | : NextAvailableSlotIdx(0), CurrentInstructionSlotIdx(0), |
| 187 | AvailableSlots(NumSlots), MaxRetirePerCycle(RPC), Owner(DU) { |
| 188 | assert(NumSlots && "Expected at least one slot!"); |
| 189 | Queue.resize(NumSlots); |
| 190 | } |
| 191 | |
| 192 | bool isFull() const { return !AvailableSlots; } |
| 193 | bool isEmpty() const { return AvailableSlots == Queue.size(); } |
| 194 | bool isAvailable(unsigned Quantity = 1) const { |
| 195 | // Some instructions may declare a number of uOps which exceedes the size |
| 196 | // of the reorder buffer. To avoid problems, cap the amount of slots to |
| 197 | // the size of the reorder buffer. |
| 198 | Quantity = std::min(Quantity, static_cast<unsigned>(Queue.size())); |
| 199 | return AvailableSlots >= Quantity; |
| 200 | } |
| 201 | |
| 202 | // Reserves a number of slots, and returns a new token. |
| 203 | unsigned reserveSlot(unsigned Index, unsigned NumMicroOps); |
| 204 | |
| 205 | /// Retires instructions in program order. |
| 206 | void cycleEvent(); |
| 207 | |
| 208 | void onInstructionExecuted(unsigned TokenID); |
| 209 | |
| 210 | #ifndef NDEBUG |
| 211 | void dump() const; |
| 212 | #endif |
| 213 | }; |
| 214 | |
| 215 | // \brief Implements the hardware dispatch logic. |
| 216 | // |
| 217 | // This class is responsible for the dispatch stage, in which instructions are |
| 218 | // dispatched in groups to the Scheduler. An instruction can be dispatched if |
| 219 | // functional units are available. |
| 220 | // To be more specific, an instruction can be dispatched to the Scheduler if: |
| 221 | // 1) There are enough entries in the reorder buffer (implemented by class |
| 222 | // RetireControlUnit) to accomodate all opcodes. |
| 223 | // 2) There are enough temporaries to rename output register operands. |
| 224 | // 3) There are enough entries available in the used buffered resource(s). |
| 225 | // |
| 226 | // The number of micro opcodes that can be dispatched in one cycle is limited by |
| 227 | // the value of field 'DispatchWidth'. A "dynamic dispatch stall" occurs when |
| 228 | // processor resources are not available (i.e. at least one of the |
| 229 | // abovementioned checks fails). Dispatch stall events are counted during the |
| 230 | // entire execution of the code, and displayed by the performance report when |
| 231 | // flag '-verbose' is specified. |
| 232 | // |
| 233 | // If the number of micro opcodes of an instruction is bigger than |
| 234 | // DispatchWidth, then it can only be dispatched at the beginning of one cycle. |
| 235 | // The DispatchUnit will still have to wait for a number of cycles (depending on |
| 236 | // the DispatchWidth and the number of micro opcodes) before it can serve other |
| 237 | // instructions. |
| 238 | class DispatchUnit { |
| 239 | unsigned DispatchWidth; |
| 240 | unsigned AvailableEntries; |
| 241 | unsigned CarryOver; |
| 242 | Scheduler *SC; |
| 243 | |
| 244 | std::unique_ptr<RegisterFile> RAT; |
| 245 | std::unique_ptr<RetireControlUnit> RCU; |
| 246 | Backend *Owner; |
| 247 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 248 | bool checkRAT(unsigned Index, const Instruction &Desc); |
| 249 | bool checkRCU(unsigned Index, const InstrDesc &Desc); |
| 250 | bool checkScheduler(unsigned Index, const InstrDesc &Desc); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 251 | |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 252 | void updateRAWDependencies(ReadState &RS, const llvm::MCSubtargetInfo &STI); |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 253 | void notifyInstructionDispatched(unsigned IID, |
| 254 | llvm::ArrayRef<unsigned> UsedPhysRegs); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 255 | |
| 256 | public: |
| 257 | DispatchUnit(Backend *B, const llvm::MCRegisterInfo &MRI, |
| 258 | unsigned MicroOpBufferSize, unsigned RegisterFileSize, |
| 259 | unsigned MaxRetirePerCycle, unsigned MaxDispatchWidth, |
| 260 | Scheduler *Sched) |
| 261 | : DispatchWidth(MaxDispatchWidth), AvailableEntries(MaxDispatchWidth), |
| 262 | CarryOver(0U), SC(Sched), |
| 263 | RAT(llvm::make_unique<RegisterFile>(MRI, RegisterFileSize)), |
| 264 | RCU(llvm::make_unique<RetireControlUnit>(MicroOpBufferSize, |
| 265 | MaxRetirePerCycle, this)), |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 266 | Owner(B) {} |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 267 | |
| 268 | unsigned getDispatchWidth() const { return DispatchWidth; } |
| 269 | |
| 270 | bool isAvailable(unsigned NumEntries) const { |
| 271 | return NumEntries <= AvailableEntries || AvailableEntries == DispatchWidth; |
| 272 | } |
| 273 | |
| 274 | bool isRCUEmpty() const { return RCU->isEmpty(); } |
| 275 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 276 | bool canDispatch(unsigned Index, const Instruction &Inst) { |
Andrea Di Biagio | af904b9 | 2018-03-15 16:13:12 +0000 | [diff] [blame] | 277 | const InstrDesc &Desc = Inst.getDesc(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 278 | assert(isAvailable(Desc.NumMicroOps)); |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 279 | return checkRCU(Index, Desc) && checkRAT(Index, Inst) && |
| 280 | checkScheduler(Index, Desc); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 283 | void dispatch(unsigned IID, Instruction *I, const llvm::MCSubtargetInfo &STI); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 284 | |
| 285 | void collectWrites(llvm::SmallVectorImpl<WriteState *> &Vec, |
| 286 | unsigned RegID) const { |
| 287 | return RAT->collectWrites(Vec, RegID); |
| 288 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 289 | |
| 290 | void cycleEvent(unsigned Cycle) { |
| 291 | RCU->cycleEvent(); |
| 292 | AvailableEntries = |
| 293 | CarryOver >= DispatchWidth ? 0 : DispatchWidth - CarryOver; |
| 294 | CarryOver = CarryOver >= DispatchWidth ? CarryOver - DispatchWidth : 0U; |
| 295 | } |
| 296 | |
| 297 | void notifyInstructionRetired(unsigned Index); |
| 298 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 299 | void notifyDispatchStall(unsigned Index, unsigned EventType); |
| 300 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 301 | void onInstructionExecuted(unsigned TokenID) { |
| 302 | RCU->onInstructionExecuted(TokenID); |
| 303 | } |
| 304 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 305 | #ifndef NDEBUG |
| 306 | void dump() const; |
| 307 | #endif |
| 308 | }; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 309 | } // namespace mca |
| 310 | |
| 311 | #endif |