//===- RootOrdering.cpp - Optimal root ordering ---------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // An implementation of Edmonds' optimal branching algorithm. This is a // directed analogue of the minimum spanning tree problem for a given root. // //===----------------------------------------------------------------------===// #include "RootOrdering.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/SmallVector.h" #include <queue> #include <utility> usingnamespacemlir; usingnamespacemlir::pdl_to_pdl_interp; /// Returns the cycle implied by the specified parent relation, starting at the /// given node. static SmallVector<Value> getCycle(const DenseMap<Value, Value> &parents, Value rep) { … } /// Contracts the specified cycle in the given graph in-place. /// The parentsCost map specifies, for each node in the cycle, the lowest cost /// among the edges entering that node. Then, the nodes in the cycle C are /// replaced with a single node v_C (the first node in the cycle). All edges /// (u, v) entering the cycle, v \in C, are replaced with a single edge /// (u, v_C) with an appropriately chosen cost, and the selected node v is /// marked in the output map actualTarget[u]. All edges (u, v) leaving the /// cycle, u \in C, are replaced with a single edge (v_C, v), and the selected /// node u is marked in the ouptut map actualSource[v]. static void contract(RootOrderingGraph &graph, ArrayRef<Value> cycle, const DenseMap<Value, unsigned> &parentDepths, DenseMap<Value, Value> &actualSource, DenseMap<Value, Value> &actualTarget) { … } OptimalBranching::OptimalBranching(RootOrderingGraph graph, Value root) : … { … } unsigned OptimalBranching::solve() { … } OptimalBranching::EdgeList OptimalBranching::preOrderTraversal(ArrayRef<Value> nodes) const { … }