ReductionRules.h 7.1 KB

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