blob: 9ac7c3c55688d6c1ad6b40df41a4c2cd52b69957 [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===----------------------- 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 Biagio4732d43ca2018-03-14 14:57:23 +000021#include "llvm/MC/MCSubtargetInfo.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000022#include <map>
23
24namespace mca {
25
26class WriteState;
27class DispatchUnit;
28class Scheduler;
29class Backend;
30
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +000031/// \brief Manages hardware register files, and tracks data dependencies
32/// between registers.
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000033class RegisterFile {
34 const llvm::MCRegisterInfo &MRI;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000035
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +000036 // 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 Biagio3a6b0922018-03-08 13:05:02 +000046
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +000047 RegisterMappingTracker(unsigned NumMappings)
Andrea Di Biagio12ef5262018-03-21 18:11:05 +000048 : TotalMappings(NumMappings), NumUsedMappings(0) {}
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +000049 };
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000050
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +000051 // 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 Biagio12ef5262018-03-21 18:11:05 +0000103 void createNewMappings(unsigned RegisterFileMask,
104 llvm::MutableArrayRef<unsigned> UsedPhysRegs);
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +0000105
106 // Removes a previously allocated mapping from each register file in the
107 // RegisterFileMask set. This method is called from invalidateRegisterMapping.
Andrea Di Biagio12ef5262018-03-21 18:11:05 +0000108 void removeMappings(unsigned RegisterFileMask,
109 llvm::MutableArrayRef<unsigned> FreedPhysRegs);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000110
111public:
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +0000112 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 Biagio3a6b0922018-03-08 13:05:02 +0000117
118 // Creates a new register mapping for RegID.
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +0000119 // This reserves a microarchitectural register in every register file that
120 // contains RegID.
Andrea Di Biagio12ef5262018-03-21 18:11:05 +0000121 void addRegisterMapping(WriteState &WS,
122 llvm::MutableArrayRef<unsigned> UsedPhysRegs);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000123
124 // Invalidates register mappings associated to the input WriteState object.
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +0000125 // This releases previously allocated mappings for the physical register
126 // associated to the WriteState.
Andrea Di Biagio12ef5262018-03-21 18:11:05 +0000127 void invalidateRegisterMapping(const WriteState &WS,
128 llvm::MutableArrayRef<unsigned> FreedPhysRegs);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000129
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +0000130 // 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 Biagio847accd2018-03-20 19:06:34 +0000136 unsigned isAvailable(llvm::ArrayRef<unsigned> Regs) const;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000137 void collectWrites(llvm::SmallVectorImpl<WriteState *> &Writes,
138 unsigned RegID) const;
139 void updateOnRead(ReadState &RS, unsigned RegID);
Andrea Di Biagio12ef5262018-03-21 18:11:05 +0000140
Andrea Di Biagioe64f3b12018-03-18 15:33:27 +0000141 unsigned getNumRegisterFiles() const { return RegisterFiles.size(); }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000142
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.
158struct 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
176private:
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
184public:
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.
238class 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 Biagio91ab2ee2018-03-19 13:23:07 +0000248 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 Biagio3a6b0922018-03-08 13:05:02 +0000251
Andrea Di Biagio4732d43ca2018-03-14 14:57:23 +0000252 void updateRAWDependencies(ReadState &RS, const llvm::MCSubtargetInfo &STI);
Andrea Di Biagio09ea09e2018-03-22 11:39:34 +0000253 void notifyInstructionDispatched(unsigned IID,
254 llvm::ArrayRef<unsigned> UsedPhysRegs);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000255
256public:
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 Biagio91ab2ee2018-03-19 13:23:07 +0000266 Owner(B) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000267
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 Biagio91ab2ee2018-03-19 13:23:07 +0000276 bool canDispatch(unsigned Index, const Instruction &Inst) {
Andrea Di Biagioaf904b92018-03-15 16:13:12 +0000277 const InstrDesc &Desc = Inst.getDesc();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000278 assert(isAvailable(Desc.NumMicroOps));
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +0000279 return checkRCU(Index, Desc) && checkRAT(Index, Inst) &&
280 checkScheduler(Index, Desc);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000281 }
282
Andrea Di Biagio09ea09e2018-03-22 11:39:34 +0000283 void dispatch(unsigned IID, Instruction *I, const llvm::MCSubtargetInfo &STI);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000284
285 void collectWrites(llvm::SmallVectorImpl<WriteState *> &Vec,
286 unsigned RegID) const {
287 return RAT->collectWrites(Vec, RegID);
288 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000289
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 Biagio91ab2ee2018-03-19 13:23:07 +0000299 void notifyDispatchStall(unsigned Index, unsigned EventType);
300
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000301 void onInstructionExecuted(unsigned TokenID) {
302 RCU->onInstructionExecuted(TokenID);
303 }
304
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000305#ifndef NDEBUG
306 void dump() const;
307#endif
308};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000309} // namespace mca
310
311#endif