ScheduleDFS.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ScheduleDFS.h - ILP metric for ScheduleDAGInstrs ---------*- 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. //
  14. // Definition of an ILP metric for machine level instruction scheduling.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_SCHEDULEDFS_H
  18. #define LLVM_CODEGEN_SCHEDULEDFS_H
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/CodeGen/ScheduleDAG.h"
  21. #include <cassert>
  22. #include <cstdint>
  23. #include <vector>
  24. namespace llvm {
  25. template <typename T> class ArrayRef;
  26. class raw_ostream;
  27. /// Represent the ILP of the subDAG rooted at a DAG node.
  28. ///
  29. /// ILPValues summarize the DAG subtree rooted at each node. ILPValues are
  30. /// valid for all nodes regardless of their subtree membership.
  31. ///
  32. /// When computed using bottom-up DFS, this metric assumes that the DAG is a
  33. /// forest of trees with roots at the bottom of the schedule branching upward.
  34. struct ILPValue {
  35. unsigned InstrCount;
  36. /// Length may either correspond to depth or height, depending on direction,
  37. /// and cycles or nodes depending on context.
  38. unsigned Length;
  39. ILPValue(unsigned count, unsigned length):
  40. InstrCount(count), Length(length) {}
  41. // Order by the ILP metric's value.
  42. bool operator<(ILPValue RHS) const {
  43. return (uint64_t)InstrCount * RHS.Length
  44. < (uint64_t)Length * RHS.InstrCount;
  45. }
  46. bool operator>(ILPValue RHS) const {
  47. return RHS < *this;
  48. }
  49. bool operator<=(ILPValue RHS) const {
  50. return (uint64_t)InstrCount * RHS.Length
  51. <= (uint64_t)Length * RHS.InstrCount;
  52. }
  53. bool operator>=(ILPValue RHS) const {
  54. return RHS <= *this;
  55. }
  56. void print(raw_ostream &OS) const;
  57. void dump() const;
  58. };
  59. /// Compute the values of each DAG node for various metrics during DFS.
  60. class SchedDFSResult {
  61. friend class SchedDFSImpl;
  62. static const unsigned InvalidSubtreeID = ~0u;
  63. /// Per-SUnit data computed during DFS for various metrics.
  64. ///
  65. /// A node's SubtreeID is set to itself when it is visited to indicate that it
  66. /// is the root of a subtree. Later it is set to its parent to indicate an
  67. /// interior node. Finally, it is set to a representative subtree ID during
  68. /// finalization.
  69. struct NodeData {
  70. unsigned InstrCount = 0;
  71. unsigned SubtreeID = InvalidSubtreeID;
  72. NodeData() = default;
  73. };
  74. /// Per-Subtree data computed during DFS.
  75. struct TreeData {
  76. unsigned ParentTreeID = InvalidSubtreeID;
  77. unsigned SubInstrCount = 0;
  78. TreeData() = default;
  79. };
  80. /// Record a connection between subtrees and the connection level.
  81. struct Connection {
  82. unsigned TreeID;
  83. unsigned Level;
  84. Connection(unsigned tree, unsigned level): TreeID(tree), Level(level) {}
  85. };
  86. bool IsBottomUp;
  87. unsigned SubtreeLimit;
  88. /// DFS results for each SUnit in this DAG.
  89. std::vector<NodeData> DFSNodeData;
  90. // Store per-tree data indexed on tree ID,
  91. SmallVector<TreeData, 16> DFSTreeData;
  92. // For each subtree discovered during DFS, record its connections to other
  93. // subtrees.
  94. std::vector<SmallVector<Connection, 4>> SubtreeConnections;
  95. /// Cache the current connection level of each subtree.
  96. /// This mutable array is updated during scheduling.
  97. std::vector<unsigned> SubtreeConnectLevels;
  98. public:
  99. SchedDFSResult(bool IsBU, unsigned lim)
  100. : IsBottomUp(IsBU), SubtreeLimit(lim) {}
  101. /// Get the node cutoff before subtrees are considered significant.
  102. unsigned getSubtreeLimit() const { return SubtreeLimit; }
  103. /// Return true if this DFSResult is uninitialized.
  104. ///
  105. /// resize() initializes DFSResult, while compute() populates it.
  106. bool empty() const { return DFSNodeData.empty(); }
  107. /// Clear the results.
  108. void clear() {
  109. DFSNodeData.clear();
  110. DFSTreeData.clear();
  111. SubtreeConnections.clear();
  112. SubtreeConnectLevels.clear();
  113. }
  114. /// Initialize the result data with the size of the DAG.
  115. void resize(unsigned NumSUnits) {
  116. DFSNodeData.resize(NumSUnits);
  117. }
  118. /// Compute various metrics for the DAG with given roots.
  119. void compute(ArrayRef<SUnit> SUnits);
  120. /// Get the number of instructions in the given subtree and its
  121. /// children.
  122. unsigned getNumInstrs(const SUnit *SU) const {
  123. return DFSNodeData[SU->NodeNum].InstrCount;
  124. }
  125. /// Get the number of instructions in the given subtree not including
  126. /// children.
  127. unsigned getNumSubInstrs(unsigned SubtreeID) const {
  128. return DFSTreeData[SubtreeID].SubInstrCount;
  129. }
  130. /// Get the ILP value for a DAG node.
  131. ///
  132. /// A leaf node has an ILP of 1/1.
  133. ILPValue getILP(const SUnit *SU) const {
  134. return ILPValue(DFSNodeData[SU->NodeNum].InstrCount, 1 + SU->getDepth());
  135. }
  136. /// The number of subtrees detected in this DAG.
  137. unsigned getNumSubtrees() const { return SubtreeConnectLevels.size(); }
  138. /// Get the ID of the subtree the given DAG node belongs to.
  139. ///
  140. /// For convenience, if DFSResults have not been computed yet, give everything
  141. /// tree ID 0.
  142. unsigned getSubtreeID(const SUnit *SU) const {
  143. if (empty())
  144. return 0;
  145. assert(SU->NodeNum < DFSNodeData.size() && "New Node");
  146. return DFSNodeData[SU->NodeNum].SubtreeID;
  147. }
  148. /// Get the connection level of a subtree.
  149. ///
  150. /// For bottom-up trees, the connection level is the latency depth (in cycles)
  151. /// of the deepest connection to another subtree.
  152. unsigned getSubtreeLevel(unsigned SubtreeID) const {
  153. return SubtreeConnectLevels[SubtreeID];
  154. }
  155. /// Scheduler callback to update SubtreeConnectLevels when a tree is
  156. /// initially scheduled.
  157. void scheduleTree(unsigned SubtreeID);
  158. };
  159. raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val);
  160. } // end namespace llvm
  161. #endif // LLVM_CODEGEN_SCHEDULEDFS_H
  162. #ifdef __GNUC__
  163. #pragma GCC diagnostic pop
  164. #endif