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