LLVM  8.0.1
ResourceManager.h
Go to the documentation of this file.
1 //===--------------------- ResourceManager.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 /// The classes here represent processor resource units and their management
12 /// strategy. These classes are managed by the Scheduler.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_MCA_RESOURCE_MANAGER_H
17 #define LLVM_MCA_RESOURCE_MANAGER_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/MC/MCSchedule.h"
23 #include "llvm/MCA/Instruction.h"
24 #include "llvm/MCA/Support.h"
25 
26 namespace llvm {
27 namespace mca {
28 
29 /// Used to notify the internal state of a processor resource.
30 ///
31 /// A processor resource is available if it is not reserved, and there are
32 /// available slots in the buffer. A processor resource is unavailable if it
33 /// is either reserved, or the associated buffer is full. A processor resource
34 /// with a buffer size of -1 is always available if it is not reserved.
35 ///
36 /// Values of type ResourceStateEvent are returned by method
37 /// ResourceState::isBufferAvailable(), which is used to query the internal
38 /// state of a resource.
39 ///
40 /// The naming convention for resource state events is:
41 /// * Event names start with prefix RS_
42 /// * Prefix RS_ is followed by a string describing the actual resource state.
47 };
48 
49 /// Resource allocation strategy used by hardware scheduler resources.
51  ResourceStrategy(const ResourceStrategy &) = delete;
52  ResourceStrategy &operator=(const ResourceStrategy &) = delete;
53 
54 public:
56  virtual ~ResourceStrategy();
57 
58  /// Selects a processor resource unit from a ReadyMask.
59  virtual uint64_t select(uint64_t ReadyMask) = 0;
60 
61  /// Called by the ResourceManager when a processor resource group, or a
62  /// processor resource with multiple units has become unavailable.
63  ///
64  /// The default strategy uses this information to bias its selection logic.
65  virtual void used(uint64_t ResourceMask) {}
66 };
67 
68 /// Default resource allocation strategy used by processor resource groups and
69 /// processor resources with multiple units.
71  /// A Mask of resource unit identifiers.
72  ///
73  /// There is one bit set for every available resource unit.
74  /// It defaults to the value of field ResourceSizeMask in ResourceState.
75  const uint64_t ResourceUnitMask;
76 
77  /// A simple round-robin selector for processor resource units.
78  /// Each bit of this mask identifies a sub resource within a group.
79  ///
80  /// As an example, lets assume that this is a default policy for a
81  /// processor resource group composed by the following three units:
82  /// ResourceA -- 0b001
83  /// ResourceB -- 0b010
84  /// ResourceC -- 0b100
85  ///
86  /// Field NextInSequenceMask is used to select the next unit from the set of
87  /// resource units. It defaults to the value of field `ResourceUnitMasks` (in
88  /// this example, it defaults to mask '0b111').
89  ///
90  /// The round-robin selector would firstly select 'ResourceC', then
91  /// 'ResourceB', and eventually 'ResourceA'. When a resource R is used, the
92  /// corresponding bit in NextInSequenceMask is cleared. For example, if
93  /// 'ResourceC' is selected, then the new value of NextInSequenceMask becomes
94  /// 0xb011.
95  ///
96  /// When NextInSequenceMask becomes zero, it is automatically reset to the
97  /// default value (i.e. ResourceUnitMask).
98  uint64_t NextInSequenceMask;
99 
100  /// This field is used to track resource units that are used (i.e. selected)
101  /// by other groups other than the one associated with this strategy object.
102  ///
103  /// In LLVM processor resource groups are allowed to partially (or fully)
104  /// overlap. That means, a same unit may be visible to multiple groups.
105  /// This field keeps track of uses that have originated from outside of
106  /// this group. The idea is to bias the selection strategy, so that resources
107  /// that haven't been used by other groups get prioritized.
108  ///
109  /// The end goal is to (try to) keep the resource distribution as much uniform
110  /// as possible. By construction, this mask only tracks one-level of resource
111  /// usage. Therefore, this strategy is expected to be less accurate when same
112  /// units are used multiple times by other groups within a single round of
113  /// select.
114  ///
115  /// Note: an LRU selector would have a better accuracy at the cost of being
116  /// slightly more expensive (mostly in terms of runtime cost). Methods
117  /// 'select' and 'used', are always in the hot execution path of llvm-mca.
118  /// Therefore, a slow implementation of 'select' would have a negative impact
119  /// on the overall performance of the tool.
120  uint64_t RemovedFromNextInSequence;
121 
122 public:
123  DefaultResourceStrategy(uint64_t UnitMask)
124  : ResourceStrategy(), ResourceUnitMask(UnitMask),
125  NextInSequenceMask(UnitMask), RemovedFromNextInSequence(0) {}
126  virtual ~DefaultResourceStrategy() = default;
127 
128  uint64_t select(uint64_t ReadyMask) override;
129  void used(uint64_t Mask) override;
130 };
131 
132 /// A processor resource descriptor.
133 ///
134 /// There is an instance of this class for every processor resource defined by
135 /// the machine scheduling model.
136 /// Objects of class ResourceState dynamically track the usage of processor
137 /// resource units.
139  /// An index to the MCProcResourceDesc entry in the processor model.
140  const unsigned ProcResourceDescIndex;
141  /// A resource mask. This is generated by the tool with the help of
142  /// function `mca::computeProcResourceMasks' (see Support.h).
143  ///
144  /// Field ResourceMask only has one bit set if this resource state describes a
145  /// processor resource unit (i.e. this is not a group). That means, we can
146  /// quickly check if a resource is a group by simply counting the number of
147  /// bits that are set in the mask.
148  ///
149  /// The most significant bit of a mask (MSB) uniquely identifies a resource.
150  /// Remaining bits are used to describe the composition of a group (Group).
151  ///
152  /// Example (little endian):
153  /// Resource | Mask | MSB | Group
154  /// ---------+------------+------------+------------
155  /// A | 0b000001 | 0b000001 | 0b000000
156  /// | | |
157  /// B | 0b000010 | 0b000010 | 0b000000
158  /// | | |
159  /// C | 0b010000 | 0b010000 | 0b000000
160  /// | | |
161  /// D | 0b110010 | 0b100000 | 0b010010
162  ///
163  /// In this example, resources A, B and C are processor resource units.
164  /// Only resource D is a group resource, and it contains resources B and C.
165  /// That is because MSB(B) and MSB(C) are both contained within Group(D).
166  const uint64_t ResourceMask;
167 
168  /// A ProcResource can have multiple units.
169  ///
170  /// For processor resource groups this field is a mask of contained resource
171  /// units. It is obtained from ResourceMask by clearing the highest set bit.
172  /// The number of resource units in a group can be simply computed as the
173  /// population count of this field.
174  ///
175  /// For normal (i.e. non-group) resources, the number of bits set in this mask
176  /// is equivalent to the number of units declared by the processor model (see
177  /// field 'NumUnits' in 'ProcResourceUnits').
178  uint64_t ResourceSizeMask;
179 
180  /// A mask of ready units.
181  uint64_t ReadyMask;
182 
183  /// Buffered resources will have this field set to a positive number different
184  /// than zero. A buffered resource behaves like a reservation station
185  /// implementing its own buffer for out-of-order execution.
186  ///
187  /// A BufferSize of 1 is used by scheduler resources that force in-order
188  /// execution.
189  ///
190  /// A BufferSize of 0 is used to model in-order issue/dispatch resources.
191  /// Since in-order issue/dispatch resources don't implement buffers, dispatch
192  /// events coincide with issue events.
193  /// Also, no other instruction ca be dispatched/issue while this resource is
194  /// in use. Only when all the "resource cycles" are consumed (after the issue
195  /// event), a new instruction ca be dispatched.
196  const int BufferSize;
197 
198  /// Available slots in the buffer (zero, if this is not a buffered resource).
199  unsigned AvailableSlots;
200 
201  /// This field is set if this resource is currently reserved.
202  ///
203  /// Resources can be reserved for a number of cycles.
204  /// Instructions can still be dispatched to reserved resources. However,
205  /// istructions dispatched to a reserved resource cannot be issued to the
206  /// underlying units (i.e. pipelines) until the resource is released.
207  bool Unavailable;
208 
209  const bool IsAGroup;
210 
211  /// Checks for the availability of unit 'SubResMask' in the group.
212  bool isSubResourceReady(uint64_t SubResMask) const {
213  return ReadyMask & SubResMask;
214  }
215 
216 public:
217  ResourceState(const MCProcResourceDesc &Desc, unsigned Index, uint64_t Mask);
218 
219  unsigned getProcResourceID() const { return ProcResourceDescIndex; }
220  uint64_t getResourceMask() const { return ResourceMask; }
221  uint64_t getReadyMask() const { return ReadyMask; }
222  int getBufferSize() const { return BufferSize; }
223 
224  bool isBuffered() const { return BufferSize > 0; }
225  bool isInOrder() const { return BufferSize == 1; }
226 
227  /// Returns true if this is an in-order dispatch/issue resource.
228  bool isADispatchHazard() const { return BufferSize == 0; }
229  bool isReserved() const { return Unavailable; }
230 
231  void setReserved() { Unavailable = true; }
232  void clearReserved() { Unavailable = false; }
233 
234  /// Returs true if this resource is not reserved, and if there are at least
235  /// `NumUnits` available units.
236  bool isReady(unsigned NumUnits = 1) const;
237 
238  bool isAResourceGroup() const { return IsAGroup; }
239 
240  bool containsResource(uint64_t ID) const { return ResourceMask & ID; }
241 
242  void markSubResourceAsUsed(uint64_t ID) {
243  assert(isSubResourceReady(ID));
244  ReadyMask ^= ID;
245  }
246 
247  void releaseSubResource(uint64_t ID) {
248  assert(!isSubResourceReady(ID));
249  ReadyMask ^= ID;
250  }
251 
252  unsigned getNumUnits() const {
253  return isAResourceGroup() ? 1U : countPopulation(ResourceSizeMask);
254  }
255 
256  /// Checks if there is an available slot in the resource buffer.
257  ///
258  /// Returns RS_BUFFER_AVAILABLE if this is not a buffered resource, or if
259  /// there is a slot available.
260  ///
261  /// Returns RS_RESERVED if this buffered resource is a dispatch hazard, and it
262  /// is reserved.
263  ///
264  /// Returns RS_BUFFER_UNAVAILABLE if there are no available slots.
265  ResourceStateEvent isBufferAvailable() const;
266 
267  /// Reserve a slot in the buffer.
268  void reserveBuffer() {
269  if (AvailableSlots)
270  AvailableSlots--;
271  }
272 
273  /// Release a slot in the buffer.
274  void releaseBuffer() {
275  if (BufferSize > 0)
276  AvailableSlots++;
277  assert(AvailableSlots <= static_cast<unsigned>(BufferSize));
278  }
279 
280 #ifndef NDEBUG
281  void dump() const;
282 #endif
283 };
284 
285 /// A resource unit identifier.
286 ///
287 /// This is used to identify a specific processor resource unit using a pair
288 /// of indices where the 'first' index is a processor resource mask, and the
289 /// 'second' index is an index for a "sub-resource" (i.e. unit).
290 typedef std::pair<uint64_t, uint64_t> ResourceRef;
291 
292 // First: a MCProcResourceDesc index identifying a buffered resource.
293 // Second: max number of buffer entries used in this resource.
294 typedef std::pair<unsigned, unsigned> BufferUsageEntry;
295 
296 /// A resource manager for processor resource units and groups.
297 ///
298 /// This class owns all the ResourceState objects, and it is responsible for
299 /// acting on requests from a Scheduler by updating the internal state of
300 /// ResourceState objects.
301 /// This class doesn't know about instruction itineraries and functional units.
302 /// In future, it can be extended to support itineraries too through the same
303 /// public interface.
305  // Set of resources available on the subtarget.
306  //
307  // There is an instance of ResourceState for every resource declared by the
308  // target scheduling model.
309  //
310  // Elements of this vector are ordered by resource kind. In particular,
311  // resource units take precedence over resource groups.
312  //
313  // The index of a processor resource in this vector depends on the value of
314  // its mask (see the description of field ResourceState::ResourceMask). In
315  // particular, it is computed as the position of the most significant bit set
316  // (MSB) in the mask plus one (since we want to ignore the invalid resource
317  // descriptor at index zero).
318  //
319  // Example (little endian):
320  //
321  // Resource | Mask | MSB | Index
322  // ---------+---------+---------+-------
323  // A | 0b00001 | 0b00001 | 1
324  // | | |
325  // B | 0b00100 | 0b00100 | 3
326  // | | |
327  // C | 0b10010 | 0b10000 | 5
328  //
329  //
330  // The same index is also used to address elements within vector `Strategies`
331  // and vector `Resource2Groups`.
332  std::vector<std::unique_ptr<ResourceState>> Resources;
333  std::vector<std::unique_ptr<ResourceStrategy>> Strategies;
334 
335  // Used to quickly identify groups that own a particular resource unit.
336  std::vector<uint64_t> Resource2Groups;
337 
338  // A table to map processor resource IDs to processor resource masks.
339  SmallVector<uint64_t, 8> ProcResID2Mask;
340 
341  // Keeps track of which resources are busy, and how many cycles are left
342  // before those become usable again.
344 
345  // Returns the actual resource unit that will be used.
346  ResourceRef selectPipe(uint64_t ResourceID);
347 
348  void use(const ResourceRef &RR);
349  void release(const ResourceRef &RR);
350 
351  unsigned getNumUnits(uint64_t ResourceID) const;
352 
353  // Overrides the selection strategy for the processor resource with the given
354  // mask.
355  void setCustomStrategyImpl(std::unique_ptr<ResourceStrategy> S,
356  uint64_t ResourceMask);
357 
358 public:
359  ResourceManager(const MCSchedModel &SM);
360  virtual ~ResourceManager() = default;
361 
362  // Overrides the selection strategy for the resource at index ResourceID in
363  // the MCProcResourceDesc table.
364  void setCustomStrategy(std::unique_ptr<ResourceStrategy> S,
365  unsigned ResourceID) {
366  assert(ResourceID < ProcResID2Mask.size() &&
367  "Invalid resource index in input!");
368  return setCustomStrategyImpl(std::move(S), ProcResID2Mask[ResourceID]);
369  }
370 
371  // Returns RS_BUFFER_AVAILABLE if buffered resources are not reserved, and if
372  // there are enough available slots in the buffers.
373  ResourceStateEvent canBeDispatched(ArrayRef<uint64_t> Buffers) const;
374 
375  // Return the processor resource identifier associated to this Mask.
376  unsigned resolveResourceMask(uint64_t Mask) const;
377 
378  // Consume a slot in every buffered resource from array 'Buffers'. Resource
379  // units that are dispatch hazards (i.e. BufferSize=0) are marked as reserved.
380  void reserveBuffers(ArrayRef<uint64_t> Buffers);
381 
382  // Release buffer entries previously allocated by method reserveBuffers.
383  void releaseBuffers(ArrayRef<uint64_t> Buffers);
384 
385  // Reserve a processor resource. A reserved resource is not available for
386  // instruction issue until it is released.
387  void reserveResource(uint64_t ResourceID);
388 
389  // Release a previously reserved processor resource.
390  void releaseResource(uint64_t ResourceID);
391 
392  bool canBeIssued(const InstrDesc &Desc) const;
393 
394  void issueInstruction(
395  const InstrDesc &Desc,
396  SmallVectorImpl<std::pair<ResourceRef, ResourceCycles>> &Pipes);
397 
398  void cycleEvent(SmallVectorImpl<ResourceRef> &ResourcesFreed);
399 
400 #ifndef NDEBUG
401  void dump() const {
402  for (const std::unique_ptr<ResourceState> &Resource : Resources)
403  Resource->dump();
404  }
405 #endif
406 };
407 } // namespace mca
408 } // namespace llvm
409 
410 #endif // LLVM_MCA_RESOURCE_MANAGER_H
A resource manager for processor resource units and groups.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void setCustomStrategy(std::unique_ptr< ResourceStrategy > S, unsigned ResourceID)
void markSubResourceAsUsed(uint64_t ID)
bool containsResource(uint64_t ID) const
virtual void used(uint64_t ResourceMask)
Called by the ResourceManager when a processor resource group, or a processor resource with multiple ...
uint64_t getResourceMask() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
virtual uint64_t select(uint64_t ReadyMask)=0
Selects a processor resource unit from a ReadyMask.
unsigned getNumUnits() const
uint64_t getReadyMask() const
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
void releaseBuffer()
Release a slot in the buffer.
std::pair< uint64_t, uint64_t > ResourceRef
A resource unit identifier.
bool isADispatchHazard() const
Returns true if this is an in-order dispatch/issue resource.
void reserveBuffer()
Reserve a slot in the buffer.
Helper functions used by various pipeline components.
size_t size() const
Definition: SmallVector.h:53
ResourceStateEvent
Used to notify the internal state of a processor resource.
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Definition: MathExtras.h:520
Define a kind of processor resource that will be modeled by the scheduler.
Definition: MCSchedule.h:32
std::pair< unsigned, unsigned > BufferUsageEntry
An instruction descriptor.
Definition: Instruction.h:322
A processor resource descriptor.
This file defines abstractions used by the Pipeline to model register reads, register writes and inst...
void releaseSubResource(uint64_t ID)
pgo instr use
DefaultResourceStrategy(uint64_t UnitMask)
unsigned getProcResourceID() const
Default resource allocation strategy used by processor resource groups and processor resources with m...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
Resource allocation strategy used by hardware scheduler resources.
Machine model for scheduling, bundling, and heuristics.
Definition: MCSchedule.h:244