LSUnit.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //===----------------------- LSUnit.cpp --------------------------*- 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. /// A Load-Store Unit for the llvm-mca tool.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/MCA/HardwareUnits/LSUnit.h"
  14. #include "llvm/MCA/Instruction.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #define DEBUG_TYPE "llvm-mca"
  18. namespace llvm {
  19. namespace mca {
  20. LSUnitBase::LSUnitBase(const MCSchedModel &SM, unsigned LQ, unsigned SQ,
  21. bool AssumeNoAlias)
  22. : LQSize(LQ), SQSize(SQ), UsedLQEntries(0), UsedSQEntries(0),
  23. NoAlias(AssumeNoAlias), NextGroupID(1) {
  24. if (SM.hasExtraProcessorInfo()) {
  25. const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo();
  26. if (!LQSize && EPI.LoadQueueID) {
  27. const MCProcResourceDesc &LdQDesc = *SM.getProcResource(EPI.LoadQueueID);
  28. LQSize = std::max(0, LdQDesc.BufferSize);
  29. }
  30. if (!SQSize && EPI.StoreQueueID) {
  31. const MCProcResourceDesc &StQDesc = *SM.getProcResource(EPI.StoreQueueID);
  32. SQSize = std::max(0, StQDesc.BufferSize);
  33. }
  34. }
  35. }
  36. LSUnitBase::~LSUnitBase() = default;
  37. void LSUnitBase::cycleEvent() {
  38. for (const std::pair<unsigned, std::unique_ptr<MemoryGroup>> &G : Groups)
  39. G.second->cycleEvent();
  40. }
  41. #ifndef NDEBUG
  42. void LSUnitBase::dump() const {
  43. dbgs() << "[LSUnit] LQ_Size = " << getLoadQueueSize() << '\n';
  44. dbgs() << "[LSUnit] SQ_Size = " << getStoreQueueSize() << '\n';
  45. dbgs() << "[LSUnit] NextLQSlotIdx = " << getUsedLQEntries() << '\n';
  46. dbgs() << "[LSUnit] NextSQSlotIdx = " << getUsedSQEntries() << '\n';
  47. dbgs() << "\n";
  48. for (const auto &GroupIt : Groups) {
  49. const MemoryGroup &Group = *GroupIt.second;
  50. dbgs() << "[LSUnit] Group (" << GroupIt.first << "): "
  51. << "[ #Preds = " << Group.getNumPredecessors()
  52. << ", #GIssued = " << Group.getNumExecutingPredecessors()
  53. << ", #GExecuted = " << Group.getNumExecutedPredecessors()
  54. << ", #Inst = " << Group.getNumInstructions()
  55. << ", #IIssued = " << Group.getNumExecuting()
  56. << ", #IExecuted = " << Group.getNumExecuted() << '\n';
  57. }
  58. }
  59. #endif
  60. unsigned LSUnit::dispatch(const InstRef &IR) {
  61. const Instruction &IS = *IR.getInstruction();
  62. bool IsStoreBarrier = IS.isAStoreBarrier();
  63. bool IsLoadBarrier = IS.isALoadBarrier();
  64. assert((IS.getMayLoad() || IS.getMayStore()) && "Not a memory operation!");
  65. if (IS.getMayLoad())
  66. acquireLQSlot();
  67. if (IS.getMayStore())
  68. acquireSQSlot();
  69. if (IS.getMayStore()) {
  70. unsigned NewGID = createMemoryGroup();
  71. MemoryGroup &NewGroup = getGroup(NewGID);
  72. NewGroup.addInstruction();
  73. // A store may not pass a previous load or load barrier.
  74. unsigned ImmediateLoadDominator =
  75. std::max(CurrentLoadGroupID, CurrentLoadBarrierGroupID);
  76. if (ImmediateLoadDominator) {
  77. MemoryGroup &IDom = getGroup(ImmediateLoadDominator);
  78. LLVM_DEBUG(dbgs() << "[LSUnit]: GROUP DEP: (" << ImmediateLoadDominator
  79. << ") --> (" << NewGID << ")\n");
  80. IDom.addSuccessor(&NewGroup, !assumeNoAlias());
  81. }
  82. // A store may not pass a previous store barrier.
  83. if (CurrentStoreBarrierGroupID) {
  84. MemoryGroup &StoreGroup = getGroup(CurrentStoreBarrierGroupID);
  85. LLVM_DEBUG(dbgs() << "[LSUnit]: GROUP DEP: ("
  86. << CurrentStoreBarrierGroupID
  87. << ") --> (" << NewGID << ")\n");
  88. StoreGroup.addSuccessor(&NewGroup, true);
  89. }
  90. // A store may not pass a previous store.
  91. if (CurrentStoreGroupID &&
  92. (CurrentStoreGroupID != CurrentStoreBarrierGroupID)) {
  93. MemoryGroup &StoreGroup = getGroup(CurrentStoreGroupID);
  94. LLVM_DEBUG(dbgs() << "[LSUnit]: GROUP DEP: (" << CurrentStoreGroupID
  95. << ") --> (" << NewGID << ")\n");
  96. StoreGroup.addSuccessor(&NewGroup, !assumeNoAlias());
  97. }
  98. CurrentStoreGroupID = NewGID;
  99. if (IsStoreBarrier)
  100. CurrentStoreBarrierGroupID = NewGID;
  101. if (IS.getMayLoad()) {
  102. CurrentLoadGroupID = NewGID;
  103. if (IsLoadBarrier)
  104. CurrentLoadBarrierGroupID = NewGID;
  105. }
  106. return NewGID;
  107. }
  108. assert(IS.getMayLoad() && "Expected a load!");
  109. unsigned ImmediateLoadDominator =
  110. std::max(CurrentLoadGroupID, CurrentLoadBarrierGroupID);
  111. // A new load group is created if we are in one of the following situations:
  112. // 1) This is a load barrier (by construction, a load barrier is always
  113. // assigned to a different memory group).
  114. // 2) There is no load in flight (by construction we always keep loads and
  115. // stores into separate memory groups).
  116. // 3) There is a load barrier in flight. This load depends on it.
  117. // 4) There is an intervening store between the last load dispatched to the
  118. // LSU and this load. We always create a new group even if this load
  119. // does not alias the last dispatched store.
  120. // 5) There is no intervening store and there is an active load group.
  121. // However that group has already started execution, so we cannot add
  122. // this load to it.
  123. bool ShouldCreateANewGroup =
  124. IsLoadBarrier || !ImmediateLoadDominator ||
  125. CurrentLoadBarrierGroupID == ImmediateLoadDominator ||
  126. ImmediateLoadDominator <= CurrentStoreGroupID ||
  127. getGroup(ImmediateLoadDominator).isExecuting();
  128. if (ShouldCreateANewGroup) {
  129. unsigned NewGID = createMemoryGroup();
  130. MemoryGroup &NewGroup = getGroup(NewGID);
  131. NewGroup.addInstruction();
  132. // A load may not pass a previous store or store barrier
  133. // unless flag 'NoAlias' is set.
  134. if (!assumeNoAlias() && CurrentStoreGroupID) {
  135. MemoryGroup &StoreGroup = getGroup(CurrentStoreGroupID);
  136. LLVM_DEBUG(dbgs() << "[LSUnit]: GROUP DEP: (" << CurrentStoreGroupID
  137. << ") --> (" << NewGID << ")\n");
  138. StoreGroup.addSuccessor(&NewGroup, true);
  139. }
  140. // A load barrier may not pass a previous load or load barrier.
  141. if (IsLoadBarrier) {
  142. if (ImmediateLoadDominator) {
  143. MemoryGroup &LoadGroup = getGroup(ImmediateLoadDominator);
  144. LLVM_DEBUG(dbgs() << "[LSUnit]: GROUP DEP: ("
  145. << ImmediateLoadDominator
  146. << ") --> (" << NewGID << ")\n");
  147. LoadGroup.addSuccessor(&NewGroup, true);
  148. }
  149. } else {
  150. // A younger load cannot pass a older load barrier.
  151. if (CurrentLoadBarrierGroupID) {
  152. MemoryGroup &LoadGroup = getGroup(CurrentLoadBarrierGroupID);
  153. LLVM_DEBUG(dbgs() << "[LSUnit]: GROUP DEP: ("
  154. << CurrentLoadBarrierGroupID
  155. << ") --> (" << NewGID << ")\n");
  156. LoadGroup.addSuccessor(&NewGroup, true);
  157. }
  158. }
  159. CurrentLoadGroupID = NewGID;
  160. if (IsLoadBarrier)
  161. CurrentLoadBarrierGroupID = NewGID;
  162. return NewGID;
  163. }
  164. // A load may pass a previous load.
  165. MemoryGroup &Group = getGroup(CurrentLoadGroupID);
  166. Group.addInstruction();
  167. return CurrentLoadGroupID;
  168. }
  169. LSUnit::Status LSUnit::isAvailable(const InstRef &IR) const {
  170. const Instruction &IS = *IR.getInstruction();
  171. if (IS.getMayLoad() && isLQFull())
  172. return LSUnit::LSU_LQUEUE_FULL;
  173. if (IS.getMayStore() && isSQFull())
  174. return LSUnit::LSU_SQUEUE_FULL;
  175. return LSUnit::LSU_AVAILABLE;
  176. }
  177. void LSUnitBase::onInstructionExecuted(const InstRef &IR) {
  178. unsigned GroupID = IR.getInstruction()->getLSUTokenID();
  179. auto It = Groups.find(GroupID);
  180. assert(It != Groups.end() && "Instruction not dispatched to the LS unit");
  181. It->second->onInstructionExecuted(IR);
  182. if (It->second->isExecuted())
  183. Groups.erase(It);
  184. }
  185. void LSUnitBase::onInstructionRetired(const InstRef &IR) {
  186. const Instruction &IS = *IR.getInstruction();
  187. bool IsALoad = IS.getMayLoad();
  188. bool IsAStore = IS.getMayStore();
  189. assert((IsALoad || IsAStore) && "Expected a memory operation!");
  190. if (IsALoad) {
  191. releaseLQSlot();
  192. LLVM_DEBUG(dbgs() << "[LSUnit]: Instruction idx=" << IR.getSourceIndex()
  193. << " has been removed from the load queue.\n");
  194. }
  195. if (IsAStore) {
  196. releaseSQSlot();
  197. LLVM_DEBUG(dbgs() << "[LSUnit]: Instruction idx=" << IR.getSourceIndex()
  198. << " has been removed from the store queue.\n");
  199. }
  200. }
  201. void LSUnit::onInstructionExecuted(const InstRef &IR) {
  202. const Instruction &IS = *IR.getInstruction();
  203. if (!IS.isMemOp())
  204. return;
  205. LSUnitBase::onInstructionExecuted(IR);
  206. unsigned GroupID = IS.getLSUTokenID();
  207. if (!isValidGroupID(GroupID)) {
  208. if (GroupID == CurrentLoadGroupID)
  209. CurrentLoadGroupID = 0;
  210. if (GroupID == CurrentStoreGroupID)
  211. CurrentStoreGroupID = 0;
  212. if (GroupID == CurrentLoadBarrierGroupID)
  213. CurrentLoadBarrierGroupID = 0;
  214. if (GroupID == CurrentStoreBarrierGroupID)
  215. CurrentStoreBarrierGroupID = 0;
  216. }
  217. }
  218. } // namespace mca
  219. } // namespace llvm