3939
4040namespace xls {
4141
42- namespace {
43-
44- // This function permutes the order of the elements of the array
45- // @array_to_permute following the permutations listed in @permutation.
46- //
47- // This function is similar to util::Permute. There are only two differences
48- // between this function and util::Permute:
49- // 1) This function uses std::move rather than relying on the copy constructor.
50- // This is important when using smart pointers.
51- // 2) This function relies on "typeof" to find the type of the elements of the
52- // array to permute.
53- template <class IntVector , class Array >
54- void Permute (const IntVector& permutation, Array* array_to_permute) {
55- if (permutation.empty ()) {
56- return ;
57- }
58- std::vector<std::remove_reference_t <decltype ((*array_to_permute)[0 ])>> temp (
59- permutation.size ());
60- for (size_t i = 0 ; i < permutation.size (); ++i) {
61- temp[i] = std::move ((*array_to_permute)[i]);
62- }
63- for (size_t i = 0 ; i < permutation.size (); ++i) {
64- (*array_to_permute)[static_cast <size_t >(permutation[i])] =
65- std::move (temp[i]);
66- }
67- }
68-
69- } // namespace
70-
7142FoldingGraph::FoldingGraph (
7243 FunctionBase* f,
7344 std::vector<std::unique_ptr<BinaryFoldingAction>> foldable_actions)
7445 : f_{f} {
75- // Allocate the graph
76- graph_ = std::make_unique<Graph>();
77-
7846 // Ensure deterministic construction of FoldingGraph
7947 std::sort (foldable_actions.begin (), foldable_actions.end (),
8048 [](const std::unique_ptr<BinaryFoldingAction>& a,
@@ -85,16 +53,16 @@ FoldingGraph::FoldingGraph(
8553 return a->GetFrom ()->id () < b->GetFrom ()->id ();
8654 });
8755
56+ Graph::Builder builder;
57+
8858 // Add the nodes
89- AddNodes (foldable_actions);
59+ AddNodes (foldable_actions, builder );
9060
9161 // Add the edges
92- AddEdges (std::move (foldable_actions));
62+ AddEdges (std::move (foldable_actions), builder );
9363
9464 // Build the graph
95- std::vector<EdgeIndex> edge_permutations;
96- graph_->Build (&edge_permutations);
97- Permute (edge_permutations, &edges_);
65+ graph_ = std::move (builder).BuildAndPermute (edges_);
9866
9967 // Print the folding graph
10068 if (VLOG_IS_ON (2 )) {
@@ -141,7 +109,8 @@ FoldingGraph::FoldingGraph(
141109FunctionBase* FoldingGraph::function () const { return f_; }
142110
143111void FoldingGraph::AddNodes (
144- absl::Span<const std::unique_ptr<BinaryFoldingAction>> foldable_actions) {
112+ absl::Span<const std::unique_ptr<BinaryFoldingAction>> foldable_actions,
113+ Graph::Builder& builder) {
145114 // Add all nodes involved in folding actions into the internal
146115 // representation.
147116 absl::flat_hash_set<Node*> already_added;
@@ -168,20 +137,21 @@ void FoldingGraph::AddNodes(
168137
169138 // Add the nodes to the graph
170139 for (size_t i = 0 ; i < nodes_.size (); ++i) {
171- graph_-> AddNode (i);
140+ builder. AddNode (i);
172141 }
173142}
174143
175144void FoldingGraph::AddEdges (
176- std::vector<std::unique_ptr<BinaryFoldingAction>> foldable_actions) {
145+ std::vector<std::unique_ptr<BinaryFoldingAction>> foldable_actions,
146+ Graph::Builder& builder) {
177147 // Add all edges to the graph
178148 for (std::unique_ptr<BinaryFoldingAction>& f : foldable_actions) {
179149 // Add a new edge into the graph to represent the current folding action
180150 NodeIndex from_index = node_to_index_.at (f->GetFrom ());
181151 NodeIndex to_index = node_to_index_.at (f->GetTo ());
182- CHECK (graph_-> IsNodeValid ( from_index));
183- CHECK (graph_-> IsNodeValid ( to_index));
184- graph_-> AddArc (from_index, to_index);
152+ CHECK (0 <= from_index && from_index < builder. num_nodes ( ));
153+ CHECK (0 <= to_index && to_index < builder. num_nodes ( ));
154+ builder. AddArc (from_index, to_index);
185155
186156 // Add the current folding action to our internal representation
187157 edges_.push_back (std::move (f));
0 commit comments