16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallVector.h"
30 cycle.push_back(node);
31 node = parents.lookup(node);
32 assert(node &&
"got an empty value in the cycle");
33 }
while (node != rep);
50 Value rep = cycle.front();
55 for (
auto outer = graph.begin(), e = graph.end(); outer != e; ++outer) {
56 Value target = outer->first;
57 if (cycleSet.contains(target)) {
59 unsigned parentDepth = parentDepths.lookup(target);
60 for (
const auto &inner : outer->second) {
61 Value source = inner.first;
63 if (cycleSet.contains(source))
67 std::pair<unsigned, unsigned> cost = inner.second.cost;
68 assert(parentDepth <= cost.first &&
"invalid parent depth");
75 cost.first -= parentDepth;
76 auto it = repEntries.find(source);
77 if (it == repEntries.end() || it->second.cost > cost) {
78 actualTarget[source] = target;
81 repEntries[source].cost = cost;
90 std::pair<unsigned, unsigned> bestCost;
91 auto inner = entries.begin(), innerE = entries.end();
92 while (inner != innerE) {
93 Value source = inner->first;
94 if (cycleSet.contains(source)) {
96 if (!bestSource || bestCost > inner->second.cost) {
98 bestCost = inner->second.cost;
100 entries.erase(inner++);
108 entries[rep].cost = bestCost;
109 actualSource[target] = bestSource;
115 graph[rep] = std::move(repEntries);
119 : graph(std::move(graph)), root(root) {}
124 parents[root] =
Value();
125 unsigned totalCost = 0;
130 parentDepths.reserve(graph.size());
135 for (
const auto &outer : graph) {
136 Value node = outer.first;
137 if (parents.count(node))
143 parentDepths.clear();
145 auto it = graph.find(node);
146 assert(it != graph.end() &&
"the graph is not strongly connected");
150 Value &bestSource = parents[node];
151 std::pair<unsigned, unsigned> bestCost;
152 for (
const auto &inner : it->second) {
154 if (!bestSource || bestCost > entry.
cost) {
155 bestSource = inner.first;
156 bestCost = entry.
cost;
159 assert(bestSource &&
"the graph is not strongly connected");
160 parentDepths[node] = bestCost.first;
162 totalCost += bestCost.first;
163 }
while (!parents.count(node));
166 if (parentDepths.count(node)) {
175 contract(graph, cycle, parentDepths, actualSource, actualTarget);
179 for (
auto &p : parents)
180 if (p.second == node)
183 p.second = actualSource.lookup(p.first);
186 Value parent = parents.lookup(node);
187 Value entry = actualTarget.lookup(parent);
188 cycle.push_back(node);
189 for (
size_t i = 0, e = cycle.size() - 1; i < e; ++i) {
190 totalCost += parentDepths.lookup(cycle[i]);
191 if (cycle[i] == entry)
192 parents[cycle[i]] = parent;
194 parents[cycle[i]] = cycle[i + 1];
209 for (
Value node : nodes) {
211 Value parent = parents.lookup(node);
212 assert(parent &&
"invalid parent");
213 children[parent].push_back(node);
219 result.reserve(nodes.size());
220 result.emplace_back(root,
Value());
223 for (
size_t i = 0; i < result.size(); ++i) {
224 Value node = result[i].first;
225 for (
Value child : children[node])
226 result.emplace_back(child, node);
static void contract(RootOrderingGraph &graph, ArrayRef< Value > cycle, const DenseMap< Value, unsigned > &parentDepths, DenseMap< Value, Value > &actualSource, DenseMap< Value, Value > &actualTarget)
Contracts the specified cycle in the given graph in-place.
static SmallVector< Value > getCycle(const DenseMap< Value, Value > &parents, Value rep)
Returns the cycle implied by the specified parent relation, starting at the given node.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
unsigned solve()
Runs the Edmonds' algorithm for the current graph, returning the total cost of the minimum-weight spa...
std::vector< std::pair< Value, Value > > EdgeList
A list of edges (child, parent).
OptimalBranching(RootOrderingGraph graph, Value root)
Constructs the solver for the given graph and root value.
EdgeList preOrderTraversal(ArrayRef< Value > nodes) const
Returns the computed edges as visited in the preorder traversal.
Include the generated interface declarations.
The information associated with an edge in the cost graph.
std::pair< unsigned, unsigned > cost
The depth of the connector Value w.r.t.