ResourceManager.h 18 KB

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