1 //===--------------------- ResourceManager.h --------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// The classes here represent processor resource units and their management
11 /// strategy. These classes are managed by the Scheduler.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_MCA_RESOURCE_MANAGER_H
16 #define LLVM_MCA_RESOURCE_MANAGER_H
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"
28 /// Used to notify the internal state of a processor resource.
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.
35 /// Values of type ResourceStateEvent are returned by method
36 /// ResourceState::isBufferAvailable(), which is used to query the internal
37 /// state of a resource.
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.
42 enum ResourceStateEvent
{
44 RS_BUFFER_UNAVAILABLE
,
48 /// Resource allocation strategy used by hardware scheduler resources.
49 class ResourceStrategy
{
50 ResourceStrategy(const ResourceStrategy
&) = delete;
51 ResourceStrategy
&operator=(const ResourceStrategy
&) = delete;
55 virtual ~ResourceStrategy();
57 /// Selects a processor resource unit from a ReadyMask.
58 virtual uint64_t select(uint64_t ReadyMask
) = 0;
60 /// Called by the ResourceManager when a processor resource group, or a
61 /// processor resource with multiple units has become unavailable.
63 /// The default strategy uses this information to bias its selection logic.
64 virtual void used(uint64_t ResourceMask
) {}
67 /// Default resource allocation strategy used by processor resource groups and
68 /// processor resources with multiple units.
69 class DefaultResourceStrategy final
: public ResourceStrategy
{
70 /// A Mask of resource unit identifiers.
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
;
76 /// A simple round-robin selector for processor resource units.
77 /// Each bit of this mask identifies a sub resource within a group.
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
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').
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
95 /// When NextInSequenceMask becomes zero, it is automatically reset to the
96 /// default value (i.e. ResourceUnitMask).
97 uint64_t NextInSequenceMask
;
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.
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.
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
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
;
122 DefaultResourceStrategy(uint64_t UnitMask
)
123 : ResourceStrategy(), ResourceUnitMask(UnitMask
),
124 NextInSequenceMask(UnitMask
), RemovedFromNextInSequence(0) {}
125 virtual ~DefaultResourceStrategy() = default;
127 uint64_t select(uint64_t ReadyMask
) override
;
128 void used(uint64_t Mask
) override
;
131 /// A processor resource descriptor.
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
137 class ResourceState
{
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).
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.
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).
151 /// Example (little endian):
152 /// Resource | Mask | MSB | Group
153 /// ---------+------------+------------+------------
154 /// A | 0b000001 | 0b000001 | 0b000000
156 /// B | 0b000010 | 0b000010 | 0b000000
158 /// C | 0b010000 | 0b010000 | 0b000000
160 /// D | 0b110010 | 0b100000 | 0b010010
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
;
167 /// A ProcResource can have multiple units.
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.
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
;
179 /// A mask of ready units.
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.
186 /// A BufferSize of 1 is used by scheduler resources that force in-order
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
;
197 /// Available slots in the buffer (zero, if this is not a buffered resource).
198 unsigned AvailableSlots
;
200 /// This field is set if this resource is currently reserved.
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.
210 /// Checks for the availability of unit 'SubResMask' in the group.
211 bool isSubResourceReady(uint64_t SubResMask
) const {
212 return ReadyMask
& SubResMask
;
216 ResourceState(const MCProcResourceDesc
&Desc
, unsigned Index
, uint64_t Mask
);
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
; }
223 bool isBuffered() const { return BufferSize
> 0; }
224 bool isInOrder() const { return BufferSize
== 1; }
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
; }
230 void setReserved() { Unavailable
= true; }
231 void clearReserved() { Unavailable
= false; }
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;
237 bool isAResourceGroup() const { return IsAGroup
; }
239 bool containsResource(uint64_t ID
) const { return ResourceMask
& ID
; }
241 void markSubResourceAsUsed(uint64_t ID
) {
242 assert(isSubResourceReady(ID
));
246 void releaseSubResource(uint64_t ID
) {
247 assert(!isSubResourceReady(ID
));
251 unsigned getNumUnits() const {
252 return isAResourceGroup() ? 1U : countPopulation(ResourceSizeMask
);
255 /// Checks if there is an available slot in the resource buffer.
257 /// Returns RS_BUFFER_AVAILABLE if this is not a buffered resource, or if
258 /// there is a slot available.
260 /// Returns RS_RESERVED if this buffered resource is a dispatch hazard, and it
263 /// Returns RS_BUFFER_UNAVAILABLE if there are no available slots.
264 ResourceStateEvent
isBufferAvailable() const;
266 /// Reserve a slot in the buffer.
267 void reserveBuffer() {
272 /// Release a slot in the buffer.
273 void releaseBuffer() {
276 assert(AvailableSlots
<= static_cast<unsigned>(BufferSize
));
284 /// A resource unit identifier.
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
;
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
;
295 /// A resource manager for processor resource units and groups.
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.
303 class ResourceManager
{
304 // Set of resources available on the subtarget.
306 // There is an instance of ResourceState for every resource declared by the
307 // target scheduling model.
309 // Elements of this vector are ordered by resource kind. In particular,
310 // resource units take precedence over resource groups.
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).
318 // Example (little endian):
320 // Resource | Mask | MSB | Index
321 // ---------+---------+---------+-------
322 // A | 0b00001 | 0b00001 | 1
324 // B | 0b00100 | 0b00100 | 3
326 // C | 0b10010 | 0b10000 | 5
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
;
334 // Used to quickly identify groups that own a particular resource unit.
335 std::vector
<uint64_t> Resource2Groups
;
337 // A table that maps processor resource IDs to processor resource masks.
338 SmallVector
<uint64_t, 8> ProcResID2Mask
;
340 // A table that maps resource indices to actual processor resource IDs in the
342 SmallVector
<unsigned, 8> ResIndex2ProcResID
;
344 // Keeps track of which resources are busy, and how many cycles are left
345 // before those become usable again.
346 SmallDenseMap
<ResourceRef
, unsigned> BusyResources
;
348 // Set of processor resource units available on the target.
349 uint64_t ProcResUnitMask
;
351 // Set of processor resource units that are available during this cycle.
352 uint64_t AvailableProcResUnits
;
354 // Set of processor resource groups that are currently reserved.
355 uint64_t ReservedResourceGroups
;
357 // Returns the actual resource unit that will be used.
358 ResourceRef
selectPipe(uint64_t ResourceID
);
360 void use(const ResourceRef
&RR
);
361 void release(const ResourceRef
&RR
);
363 unsigned getNumUnits(uint64_t ResourceID
) const;
365 // Overrides the selection strategy for the processor resource with the given
367 void setCustomStrategyImpl(std::unique_ptr
<ResourceStrategy
> S
,
368 uint64_t ResourceMask
);
371 ResourceManager(const MCSchedModel
&SM
);
372 virtual ~ResourceManager() = default;
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
]);
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;
387 // Return the processor resource identifier associated to this Mask.
388 unsigned resolveResourceMask(uint64_t Mask
) const;
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
);
394 // Release buffer entries previously allocated by method reserveBuffers.
395 void releaseBuffers(ArrayRef
<uint64_t> Buffers
);
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
);
401 // Release a previously reserved processor resource.
402 void releaseResource(uint64_t ResourceID
);
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;
410 uint64_t getProcResUnitMask() const { return ProcResUnitMask
; }
411 uint64_t getAvailableProcResUnits() const { return AvailableProcResUnits
; }
413 void issueInstruction(
414 const InstrDesc
&Desc
,
415 SmallVectorImpl
<std::pair
<ResourceRef
, ResourceCycles
>> &Pipes
);
417 void cycleEvent(SmallVectorImpl
<ResourceRef
> &ResourcesFreed
);
421 for (const std::unique_ptr
<ResourceState
> &Resource
: Resources
)
429 #endif // LLVM_MCA_RESOURCE_MANAGER_H