ReductionRules.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //===----------- ReductionRules.h - Reduction Rules -------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Reduction Rules.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_PBQP_REDUCTIONRULES_H
  14. #define LLVM_CODEGEN_PBQP_REDUCTIONRULES_H
  15. #include "Graph.h"
  16. #include "Math.h"
  17. #include "Solution.h"
  18. namespace llvm {
  19. namespace PBQP {
  20. /// \brief Reduce a node of degree one.
  21. ///
  22. /// Propagate costs from the given node, which must be of degree one, to its
  23. /// neighbor. Notify the problem domain.
  24. template <typename GraphT>
  25. void applyR1(GraphT &G, typename GraphT::NodeId NId) {
  26. typedef typename GraphT::NodeId NodeId;
  27. typedef typename GraphT::EdgeId EdgeId;
  28. typedef typename GraphT::Vector Vector;
  29. typedef typename GraphT::Matrix Matrix;
  30. typedef typename GraphT::RawVector RawVector;
  31. assert(G.getNodeDegree(NId) == 1 &&
  32. "R1 applied to node with degree != 1.");
  33. EdgeId EId = *G.adjEdgeIds(NId).begin();
  34. NodeId MId = G.getEdgeOtherNodeId(EId, NId);
  35. const Matrix &ECosts = G.getEdgeCosts(EId);
  36. const Vector &XCosts = G.getNodeCosts(NId);
  37. RawVector YCosts = G.getNodeCosts(MId);
  38. // Duplicate a little to avoid transposing matrices.
  39. if (NId == G.getEdgeNode1Id(EId)) {
  40. for (unsigned j = 0; j < YCosts.getLength(); ++j) {
  41. PBQPNum Min = ECosts[0][j] + XCosts[0];
  42. for (unsigned i = 1; i < XCosts.getLength(); ++i) {
  43. PBQPNum C = ECosts[i][j] + XCosts[i];
  44. if (C < Min)
  45. Min = C;
  46. }
  47. YCosts[j] += Min;
  48. }
  49. } else {
  50. for (unsigned i = 0; i < YCosts.getLength(); ++i) {
  51. PBQPNum Min = ECosts[i][0] + XCosts[0];
  52. for (unsigned j = 1; j < XCosts.getLength(); ++j) {
  53. PBQPNum C = ECosts[i][j] + XCosts[j];
  54. if (C < Min)
  55. Min = C;
  56. }
  57. YCosts[i] += Min;
  58. }
  59. }
  60. G.setNodeCosts(MId, YCosts);
  61. G.disconnectEdge(EId, MId);
  62. }
  63. template <typename GraphT>
  64. void applyR2(GraphT &G, typename GraphT::NodeId NId) {
  65. typedef typename GraphT::NodeId NodeId;
  66. typedef typename GraphT::EdgeId EdgeId;
  67. typedef typename GraphT::Vector Vector;
  68. typedef typename GraphT::Matrix Matrix;
  69. typedef typename GraphT::RawMatrix RawMatrix;
  70. assert(G.getNodeDegree(NId) == 2 &&
  71. "R2 applied to node with degree != 2.");
  72. const Vector &XCosts = G.getNodeCosts(NId);
  73. typename GraphT::AdjEdgeItr AEItr = G.adjEdgeIds(NId).begin();
  74. EdgeId YXEId = *AEItr,
  75. ZXEId = *(++AEItr);
  76. NodeId YNId = G.getEdgeOtherNodeId(YXEId, NId),
  77. ZNId = G.getEdgeOtherNodeId(ZXEId, NId);
  78. bool FlipEdge1 = (G.getEdgeNode1Id(YXEId) == NId),
  79. FlipEdge2 = (G.getEdgeNode1Id(ZXEId) == NId);
  80. const Matrix *YXECosts = FlipEdge1 ?
  81. new Matrix(G.getEdgeCosts(YXEId).transpose()) :
  82. &G.getEdgeCosts(YXEId);
  83. const Matrix *ZXECosts = FlipEdge2 ?
  84. new Matrix(G.getEdgeCosts(ZXEId).transpose()) :
  85. &G.getEdgeCosts(ZXEId);
  86. unsigned XLen = XCosts.getLength(),
  87. YLen = YXECosts->getRows(),
  88. ZLen = ZXECosts->getRows();
  89. RawMatrix Delta(YLen, ZLen);
  90. for (unsigned i = 0; i < YLen; ++i) {
  91. for (unsigned j = 0; j < ZLen; ++j) {
  92. PBQPNum Min = (*YXECosts)[i][0] + (*ZXECosts)[j][0] + XCosts[0];
  93. for (unsigned k = 1; k < XLen; ++k) {
  94. PBQPNum C = (*YXECosts)[i][k] + (*ZXECosts)[j][k] + XCosts[k];
  95. if (C < Min) {
  96. Min = C;
  97. }
  98. }
  99. Delta[i][j] = Min;
  100. }
  101. }
  102. if (FlipEdge1)
  103. delete YXECosts;
  104. if (FlipEdge2)
  105. delete ZXECosts;
  106. EdgeId YZEId = G.findEdge(YNId, ZNId);
  107. if (YZEId == G.invalidEdgeId()) {
  108. YZEId = G.addEdge(YNId, ZNId, Delta);
  109. } else {
  110. const Matrix &YZECosts = G.getEdgeCosts(YZEId);
  111. if (YNId == G.getEdgeNode1Id(YZEId)) {
  112. G.updateEdgeCosts(YZEId, Delta + YZECosts);
  113. } else {
  114. G.updateEdgeCosts(YZEId, Delta.transpose() + YZECosts);
  115. }
  116. }
  117. G.disconnectEdge(YXEId, YNId);
  118. G.disconnectEdge(ZXEId, ZNId);
  119. // TODO: Try to normalize newly added/modified edge.
  120. }
  121. #ifndef NDEBUG
  122. // Does this Cost vector have any register options ?
  123. template <typename VectorT>
  124. bool hasRegisterOptions(const VectorT &V) {
  125. unsigned VL = V.getLength();
  126. // An empty or spill only cost vector does not provide any register option.
  127. if (VL <= 1)
  128. return false;
  129. // If there are registers in the cost vector, but all of them have infinite
  130. // costs, then ... there is no available register.
  131. for (unsigned i = 1; i < VL; ++i)
  132. if (V[i] != std::numeric_limits<PBQP::PBQPNum>::infinity())
  133. return true;
  134. return false;
  135. }
  136. #endif
  137. // \brief Find a solution to a fully reduced graph by backpropagation.
  138. //
  139. // Given a graph and a reduction order, pop each node from the reduction
  140. // order and greedily compute a minimum solution based on the node costs, and
  141. // the dependent costs due to previously solved nodes.
  142. //
  143. // Note - This does not return the graph to its original (pre-reduction)
  144. // state: the existing solvers destructively alter the node and edge
  145. // costs. Given that, the backpropagate function doesn't attempt to
  146. // replace the edges either, but leaves the graph in its reduced
  147. // state.
  148. template <typename GraphT, typename StackT>
  149. Solution backpropagate(GraphT& G, StackT stack) {
  150. typedef GraphBase::NodeId NodeId;
  151. typedef typename GraphT::Matrix Matrix;
  152. typedef typename GraphT::RawVector RawVector;
  153. Solution s;
  154. while (!stack.empty()) {
  155. NodeId NId = stack.back();
  156. stack.pop_back();
  157. RawVector v = G.getNodeCosts(NId);
  158. #ifndef NDEBUG
  159. // Although a conservatively allocatable node can be allocated to a register,
  160. // spilling it may provide a lower cost solution. Assert here that spilling
  161. // is done by choice, not because there were no register available.
  162. if (G.getNodeMetadata(NId).wasConservativelyAllocatable())
  163. assert(hasRegisterOptions(v) && "A conservatively allocatable node "
  164. "must have available register options");
  165. #endif
  166. for (auto EId : G.adjEdgeIds(NId)) {
  167. const Matrix& edgeCosts = G.getEdgeCosts(EId);
  168. if (NId == G.getEdgeNode1Id(EId)) {
  169. NodeId mId = G.getEdgeNode2Id(EId);
  170. v += edgeCosts.getColAsVector(s.getSelection(mId));
  171. } else {
  172. NodeId mId = G.getEdgeNode1Id(EId);
  173. v += edgeCosts.getRowAsVector(s.getSelection(mId));
  174. }
  175. }
  176. s.setSelection(NId, v.minIndex());
  177. }
  178. return s;
  179. }
  180. } // namespace PBQP
  181. } // namespace llvm
  182. #endif