Skip to content

Commit 4e4f4e8

Browse files
XLS Teamcopybara-github
authored andcommitted
Automated Code Change
PiperOrigin-RevId: 910799332
1 parent 1f7fa63 commit 4e4f4e8

4 files changed

Lines changed: 36 additions & 64 deletions

File tree

xls/ir/proc_elaboration.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ absl::StatusOr<ProcInstantiationPath> ProcElaboration::CreatePath(
509509
absl::StatusOr<std::pair<ProcElaboration::ChannelGraph,
510510
std::vector<ProcElaboration::ChannelEdge>>>
511511
ProcElaboration::BuildChannelGraph() const {
512-
ProcElaboration::ChannelGraph graph(
512+
ProcElaboration::ChannelGraph::Builder builder(
513513
/*num_nodes=*/proc_instance_ptrs_.size() + 1,
514514
/*arc_capacity=*/channel_instance_ptrs_.size());
515515
struct MissingSide {
@@ -523,7 +523,7 @@ ProcElaboration::BuildChannelGraph() const {
523523
std::vector<ProcElaboration::ChannelEdge> refs;
524524
refs.reserve(channel_instance_ptrs_.size());
525525
for (ProcInstance* p : proc_instance_ptrs_) {
526-
graph.AddNode(id);
526+
builder.AddNode(id);
527527
for (Node* n : p->proc()->nodes()) {
528528
if (n->Is<Send>()) {
529529
XLS_ASSIGN_OR_RETURN(SendChannelRef ref,
@@ -545,7 +545,8 @@ ProcElaboration::BuildChannelGraph() const {
545545
// to support all cases.
546546
continue;
547547
}
548-
ProcElaboration::ChannelId nxt = graph.AddArc(id, other_side[c]->id);
548+
ProcElaboration::ChannelId nxt =
549+
builder.AddArc(id, other_side[c]->id);
549550
XLS_RET_CHECK_EQ(nxt, refs.size());
550551
refs.push_back(
551552
{.send = ref,
@@ -574,7 +575,8 @@ ProcElaboration::BuildChannelGraph() const {
574575
// to support all cases.
575576
continue;
576577
}
577-
ProcElaboration::ChannelId nxt = graph.AddArc(other_side[c]->id, id);
578+
ProcElaboration::ChannelId nxt =
579+
builder.AddArc(other_side[c]->id, id);
578580
XLS_RET_CHECK_EQ(nxt, refs.size());
579581
refs.push_back({.send = std::get<SendChannelRef>(other_side[c]->ref),
580582
.recv = ref});
@@ -587,26 +589,24 @@ ProcElaboration::BuildChannelGraph() const {
587589
id++;
588590
}
589591
// Add external channels.
590-
graph.AddNode(proc_instance_ptrs_.size());
592+
builder.AddNode(proc_instance_ptrs_.size());
591593
for (const auto& [chan, other] : other_side) {
592594
if (!other) {
593595
continue;
594596
}
595597
const MissingSide& side = *other;
596598
if (std::holds_alternative<SendChannelRef>(side.ref)) {
597-
graph.AddArc(side.id, external_id);
599+
builder.AddArc(side.id, external_id);
598600
refs.push_back({.send = std::get<SendChannelRef>(side.ref),
599601
.recv = static_cast<Channel*>(nullptr)});
600602
} else {
601-
graph.AddArc(external_id, side.id);
603+
builder.AddArc(external_id, side.id);
602604
refs.push_back({.send = static_cast<Channel*>(nullptr),
603605
.recv = std::get<ReceiveChannelRef>(side.ref)});
604606
}
605607
}
606608
// NB Build can renumber arcs so we need to renumber the channel info.
607-
std::vector<ProcElaboration::ChannelId> permutation;
608-
graph.Build(&permutation);
609-
util::Permute(permutation, &refs);
609+
auto graph = std::move(builder).BuildGraphAndPermute(refs);
610610
return std::pair<ProcElaboration::ChannelGraph,
611611
std::vector<ProcElaboration::ChannelEdge>>(std::move(graph),
612612
std::move(refs));

xls/passes/dataflow_graph_analysis.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ DataflowGraphAnalysis::DataflowGraphAnalysis(std::vector<Node*> topo_sort_nodes,
7272
node_to_index_[nodes_[i]] = i;
7373
}
7474

75-
graph_ = std::make_unique<Graph>();
76-
graph_->AddNode(kSourceIndex);
75+
Graph::Builder builder;
76+
builder.AddNode(kSourceIndex);
7777
absl::flat_hash_map<ArcIndex, int64_t> arc_capacities;
7878
absl::flat_hash_map<Node*, ArcIndex> internal_arcs;
7979
absl::flat_hash_map<Node*, ArcIndex> source_arcs;
@@ -83,8 +83,8 @@ DataflowGraphAnalysis::DataflowGraphAnalysis(std::vector<Node*> topo_sort_nodes,
8383

8484
NodeIndex v_in = InIndex(i);
8585
NodeIndex v_out = OutIndex(i);
86-
graph_->AddNode(v_in);
87-
graph_->AddNode(v_out);
86+
builder.AddNode(v_in);
87+
builder.AddNode(v_out);
8888

8989
int64_t bit_count = node->GetType()->GetFlatBitCount();
9090

@@ -94,7 +94,7 @@ DataflowGraphAnalysis::DataflowGraphAnalysis(std::vector<Node*> topo_sort_nodes,
9494
} else if (IsDataOriginating(node)) {
9595
// These nodes originate (potentially) variable data; they are the points
9696
// of entry for unknown data.
97-
source_arcs[node] = graph_->AddArc(kSourceIndex, v_in);
97+
source_arcs[node] = builder.AddArc(kSourceIndex, v_in);
9898
arc_capacities[source_arcs[node]] = bit_count;
9999
} else if (query_engine != nullptr) {
100100
if (std::optional<SharedLeafTypeTree<TernaryVector>> ternary_value =
@@ -108,17 +108,17 @@ DataflowGraphAnalysis::DataflowGraphAnalysis(std::vector<Node*> topo_sort_nodes,
108108
}
109109
}
110110

111-
internal_arcs[node] = graph_->AddArc(v_in, v_out);
111+
internal_arcs[node] = builder.AddArc(v_in, v_out);
112112
arc_capacities[internal_arcs[node]] = unknown_bits;
113-
sink_arcs[node] = graph_->AddArc(v_in, kSinkIndex);
113+
sink_arcs[node] = builder.AddArc(v_in, kSinkIndex);
114114
arc_capacities[sink_arcs[node]] = 0;
115115
for (Node* user : node->users()) {
116-
arc_capacities[graph_->AddArc(v_out, InIndex(user))] = unknown_bits;
116+
arc_capacities[builder.AddArc(v_out, InIndex(user))] = unknown_bits;
117117
}
118118
}
119119

120120
std::vector<ArcIndex> arc_permutation;
121-
graph_->Build(&arc_permutation);
121+
graph_ = std::move(builder).Build(&arc_permutation);
122122
auto permuted = [&](ArcIndex arc) {
123123
return arc < arc_permutation.size() ? arc_permutation[arc] : arc;
124124
};

xls/passes/folding_graph.cc

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,10 @@
3939

4040
namespace 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-
7142
FoldingGraph::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(
141109
FunctionBase* FoldingGraph::function() const { return f_; }
142110

143111
void 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

175144
void 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));

xls/passes/folding_graph.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,11 @@ class FoldingGraph {
229229
absl::flat_hash_set<absl::flat_hash_set<NodeIndex>> cliques_;
230230

231231
void AddNodes(
232-
absl::Span<const std::unique_ptr<BinaryFoldingAction>> foldable_actions);
232+
absl::Span<const std::unique_ptr<BinaryFoldingAction>> foldable_actions,
233+
Graph::Builder& builder);
233234
void AddEdges(
234-
std::vector<std::unique_ptr<BinaryFoldingAction>> foldable_actions);
235+
std::vector<std::unique_ptr<BinaryFoldingAction>> foldable_actions,
236+
Graph::Builder& builder);
235237
void IdentifyCliques();
236238
};
237239

0 commit comments

Comments
 (0)