14 #ifndef LLVM_CODEGEN_PBQP_REDUCTIONRULES_H 15 #define LLVM_CODEGEN_PBQP_REDUCTIONRULES_H 30 template <
typename GraphT>
33 using EdgeId =
typename GraphT::EdgeId;
34 using Vector =
typename GraphT::Vector;
36 using RawVector =
typename GraphT::RawVector;
38 assert(G.getNodeDegree(NId) == 1 &&
39 "R1 applied to node with degree != 1.");
41 EdgeId EId = *G.adjEdgeIds(NId).begin();
42 NodeId MId = G.getEdgeOtherNodeId(EId, NId);
44 const Matrix &ECosts = G.getEdgeCosts(EId);
45 const Vector &XCosts = G.getNodeCosts(NId);
46 RawVector YCosts = G.getNodeCosts(MId);
49 if (NId == G.getEdgeNode1Id(EId)) {
50 for (
unsigned j = 0; j < YCosts.getLength(); ++j) {
51 PBQPNum Min = ECosts[0][j] + XCosts[0];
52 for (
unsigned i = 1; i < XCosts.getLength(); ++i) {
53 PBQPNum C = ECosts[i][j] + XCosts[i];
60 for (
unsigned i = 0; i < YCosts.getLength(); ++i) {
61 PBQPNum Min = ECosts[i][0] + XCosts[0];
62 for (
unsigned j = 1; j < XCosts.getLength(); ++j) {
63 PBQPNum C = ECosts[i][j] + XCosts[j];
70 G.setNodeCosts(MId, YCosts);
71 G.disconnectEdge(EId, MId);
74 template <
typename GraphT>
77 using EdgeId =
typename GraphT::EdgeId;
78 using Vector =
typename GraphT::Vector;
80 using RawMatrix =
typename GraphT::RawMatrix;
82 assert(G.getNodeDegree(NId) == 2 &&
83 "R2 applied to node with degree != 2.");
85 const Vector &XCosts = G.getNodeCosts(NId);
87 typename GraphT::AdjEdgeItr AEItr = G.adjEdgeIds(NId).begin();
88 EdgeId YXEId = *AEItr,
91 NodeId YNId = G.getEdgeOtherNodeId(YXEId, NId),
92 ZNId = G.getEdgeOtherNodeId(ZXEId, NId);
94 bool FlipEdge1 = (G.getEdgeNode1Id(YXEId) == NId),
95 FlipEdge2 = (G.getEdgeNode1Id(ZXEId) == NId);
97 const Matrix *YXECosts = FlipEdge1 ?
98 new Matrix(G.getEdgeCosts(YXEId).transpose()) :
99 &G.getEdgeCosts(YXEId);
101 const Matrix *ZXECosts = FlipEdge2 ?
102 new Matrix(G.getEdgeCosts(ZXEId).transpose()) :
103 &G.getEdgeCosts(ZXEId);
105 unsigned XLen = XCosts.getLength(),
109 RawMatrix Delta(YLen, ZLen);
111 for (
unsigned i = 0; i < YLen; ++i) {
112 for (
unsigned j = 0; j < ZLen; ++j) {
113 PBQPNum Min = (*YXECosts)[i][0] + (*ZXECosts)[j][0] + XCosts[0];
114 for (
unsigned k = 1; k < XLen; ++k) {
115 PBQPNum C = (*YXECosts)[i][k] + (*ZXECosts)[j][k] + XCosts[k];
130 EdgeId YZEId = G.findEdge(YNId, ZNId);
132 if (YZEId == G.invalidEdgeId()) {
133 YZEId = G.addEdge(YNId, ZNId, Delta);
135 const Matrix &YZECosts = G.getEdgeCosts(YZEId);
136 if (YNId == G.getEdgeNode1Id(YZEId)) {
137 G.updateEdgeCosts(YZEId, Delta + YZECosts);
139 G.updateEdgeCosts(YZEId, Delta.transpose() + YZECosts);
143 G.disconnectEdge(YXEId, YNId);
144 G.disconnectEdge(ZXEId, ZNId);
151 template <
typename VectorT>
153 unsigned VL = V.getLength();
161 for (
unsigned i = 1; i < VL; ++i)
162 if (V[i] != std::numeric_limits<PBQP::PBQPNum>::infinity())
180 template <
typename GraphT,
typename StackT>
184 using RawVector =
typename GraphT::RawVector;
188 while (!stack.empty()) {
189 NodeId NId = stack.back();
192 RawVector v = G.getNodeCosts(NId);
198 if (G.getNodeMetadata(NId).wasConservativelyAllocatable())
200 "must have available register options");
203 for (
auto EId : G.adjEdgeIds(NId)) {
204 const Matrix& edgeCosts = G.getEdgeCosts(EId);
205 if (NId == G.getEdgeNode1Id(EId)) {
206 NodeId mId = G.getEdgeNode2Id(EId);
209 NodeId mId = G.getEdgeNode1Id(EId);
223 #endif // LLVM_CODEGEN_PBQP_REDUCTIONRULES_H
Represents a solution to a PBQP problem.
This class represents lattice values for constants.
Vector getRowAsVector(unsigned R) const
Returns the given row as a vector.
Vector getColAsVector(unsigned C) const
Returns the given column as a vector.
unsigned getRows() const
Return the number of rows in this matrix.
void applyR1(GraphT &G, typename GraphT::NodeId NId)
Reduce a node of degree one.
bool hasRegisterOptions(const VectorT &V)
Solution backpropagate(GraphT &G, StackT stack)
void setSelection(GraphBase::NodeId nodeId, unsigned selection)
Set the selection for a given node.
unsigned getSelection(GraphBase::NodeId nodeId) const
Get a node's selection.
void applyR2(GraphT &G, typename GraphT::NodeId NId)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())