8000 [opt] folding graph requires unique id on equivalence mappings so foldings sort deterministically by copybara-service[bot] · Pull Request #5601 · google/xls · GitHub
[go: up one dir, main page]

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions xls/passes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5104,6 +5104,7 @@ cc_test(
"//xls/ir:function_builder",
"//xls/ir:ir_test_base",
"//xls/ir:op",
"//xls/ir:source_location",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/status:statusor",
Expand Down
74 changes: 70 additions & 4 deletions xls/passes/folding_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ FoldingGraph::FoldingGraph(
std::sort(foldable_actions.begin(), foldable_actions.end(),
[](const std::unique_ptr<BinaryFoldingAction>& a,
const std::unique_ptr<BinaryFoldingAction>& b) {
return std::make_tuple(a->GetFrom()->id(), a->GetTo()->id(),
a->GetFromPartId(), a->GetToPartId()) <
std::make_tuple(b->GetFrom()->id(), b->GetTo()->id(),
b->GetFromPartId(), b->GetToPartId());
return std::make_pair(a->GetFromId(), a->GetToId()) <
std::make_pair(b->GetFromId(), b->GetToId());
});

// Add the nodes
Expand Down Expand Up @@ -388,6 +386,7 @@ absl::StatusOr<std::unique_ptr<NaryFoldingAction>> NaryFoldingAction::Clone(
return CompositionPart{
.mapping = shared_cloned,
.part = it->second,
.composition_id = comp.composition_id,
};
};

Expand Down Expand Up @@ -487,6 +486,73 @@ absl::StatusOr<std::unique_ptr<NaryFoldingAction>> NaryFoldingAction::Clone(
return clone_action;
}

namespace {

class CompositionIdChecker {
public:
absl::Status Check(const Node* node,
const std::optional<CompositionPart>& comp) {
if (!comp.has_value()) {
return absl::OkStatus();
}
XLS_RET_CHECK(comp->part != nullptr);
if (comp->composition_id == CompositionPart::kUnassignedCompositionId) {
return absl::InvalidArgumentError(absl::StrFormat(
"CompositionPart for node %s has unassigned composition_id",
node->ToString()));
}
const FunctionBase* fn = comp->part->function_base();
auto [id_it, id_inserted] =
node_comp_id_to_fn_.try_emplace({node, comp->composition_id}, fn);
if (!id_inserted && id_it->second != fn) {
return absl::InvalidArgumentError(absl::StrFormat(
"Distinct decompositions of node %s share composition_id %d",
node->ToString(), comp->composition_id));
}
auto [fn_it, fn_inserted] =
node_fn_to_comp_id_.try_emplace({node, fn}, comp->composition_id);
if (!fn_inserted && fn_it->second != comp->composition_id) {
return absl::InvalidArgumentError(absl::StrFormat(
"Decomposition of node %s has inconsistent composition_ids: %d vs %d",
node->ToString(), fn_it->second, comp->composition_id));
}
return absl::OkStatus();
}

absl::Status Check(const BinaryFoldingAction& action) {
XLS_RETURN_IF_ERROR(Check(action.GetFrom(), action.from_composition()));
return Check(action.GetTo(), action.to_composition());
}

private:
absl::flat_hash_map<std::pair<const Node*, int64_t>, const FunctionBase*>
node_comp_id_to_fn_;
absl::flat_hash_map<std::pair<const Node*, const FunctionBase*>, int64_t>
node_fn_to_comp_id_;
};

} // namespace

absl::Status VerifyUniqueCompositionIds(
absl::Span<BinaryFoldingAction* const> actions) {
CompositionIdChecker checker;
for (const BinaryFoldingAction* action : actions) {
XLS_RET_CHECK(action != nullptr);
XLS_RETURN_IF_ERROR(checker.Check(*action));
}
return absl::OkStatus();
}

absl::Status VerifyUniqueCompositionIds(
absl::Span<const std::unique_ptr<BinaryFoldingAction>> actions) {
CompositionIdChecker checker;
for (const std::unique_ptr<BinaryFoldingAction>& action : actions) {
XLS_RET_CHECK(action != nullptr);
XLS_RETURN_IF_ERROR(checker.Check(*action));
}
return absl::OkStatus();
}

absl::StatusOr<bool> WouldCommittingFoldingActionCreateDataCycle(
const NodeForwardDependencyAnalysis& nda, const BinaryFoldingAction& fold) {
const FunctionBase* f = nda.bound_function();
Expand Down
84 changes: 64 additions & 20 deletions xls/passes/folding_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/types/span.h"
Expand All @@ -40,15 +41,35 @@ namespace xls {

// Identifies a node `part` produced by an EquivalenceMapping application.
struct CompositionPart {
// The EquivalenceMapping that produced the `part`, where the part is stored
// in a temporary package created by the mapping.
std::shared_ptr<EquivalenceMapping> mapping;
Node* part;

// Comparing mappings is non trivial; it is expensive to compare mapping's
// destination temporary packages and not full proof: two mappings can produce
// isomorphic temp IR and only differ in their operand/output handling. So we
// require the folding graph user to provide a unique ID for each composition.
// This is not optional, and the default value indicates the user forgot to
// assign an ID which we use to return a status error.
constexpr static int64_t kUnassignedCompositionId = -1;
int64_t composition_id = kUnassignedCompositionId;

// If the `part` node is the same, the mapping producing it is the same.
bool operator==(const CompositionPart& other) const {
return part == other.part;
return composition_id == other.composition_id && part == other.part;
}
};

// Ordering key for sources and destinations of folding actions.
struct NodeOrPartId {
int64_t node_id;
std::optional<int64_t> composition_id = std::nullopt;
std::optional<int64_t> part_id = std::nullopt;

auto operator<=>(const NodeOrPartId&) const = default;
};

// This class represents a single folding action where the destination of the
// folding operation is a single IR node.
//
Expand Down Expand Up @@ -93,12 +114,14 @@ class FoldingAction {
return to_composition_.has_value() ? to_composition_->part : to_;
}

// Returns the id of the decomposed destination part, or nullopt if not
// decomposing the destination.
std::optional<int64_t> GetToPartId() const {
return to_composition_.has_value()
? std::make_optional(to_composition_->part->id())
: std::nullopt;
// Returns an ordering key for the destination.
NodeOrPartId GetToId() const {
if (!to_composition_.has_value()) {
return NodeOrPartId{.node_id = to_->id()};
}
return NodeOrPartId{.node_id = to_->id(),
.composition_id = to_composition_->composition_id,
.part_id = to_composition_->part->id()};
}

// The (operand -> node) edges that impact visibility of the 'to' node. Each
Expand Down Expand Up @@ -184,12 +207,14 @@ class BinaryFoldingAction : public FoldingAction {
return from_composition_.has_value() ? from_composition_->part : from_;
}

// Returns the id of the decomposed source part, or nullopt if not decomposing
// the source.
std::optional<int64_t> GetFromPartId() const {
return from_composition_.has_value()
? std::make_optional(from_composition_->part->id())
: std::nullopt;
// Returns an ordering key for the source.
NodeOrPartId GetFromId() const {
if (!from_composition_.has_value()) {
return NodeOrPartId{.node_id = from_->id()};
}
return NodeOrPartId{.node_id = from_->id(),
.composition_id = from_composition_->composition_id,
.part_id = from_composition_->part->id()};
}

const VisibilityEdges& GetFromVisibilityEdges() const { return from_edges_; }
Expand All @@ -200,17 +225,19 @@ class BinaryFoldingAction : public FoldingAction {
return from_composition_;
}

// Returns "FROM_NAME -> TO_NAME" where each name is either "NODE_NAME" or
// "NODE_NAME:PART_NAME" if the node is being decomposed.
// Returns "FROM_NAME->TO_NAME" where each name is either "NODE_NAME" or
// "NODE_NAME:COMPOSITION_ID:PART_NAME" if the node is being decomposed.
std::string GetName() const {
auto name_with_part = [](Node* node, Node* node_or_part) {
if (node_or_part == node) {
auto name_with_part = [](Node* node,
const std::optional<CompositionPart>& comp) {
if (!comp.has_value()) {
return node->GetName();
}
return absl::StrCat(node->GetName(), ":", node_or_part->GetName());
return absl::StrCat(node->GetName(), ":", comp->composition_id, ":",
comp->part->GetName());
};
return absl::StrCat(name_with_part(from_, GetFromOrPart()), "->",
name_with_part(GetTo(), GetToOrPart()));
return absl::StrCat(name_with_part(from_, from_composition_), "->",
name_with_part(GetTo(), to_composition()));
}

private:
Expand Down Expand Up @@ -253,6 +280,15 @@ class NaryFoldingAction : public FoldingAction {
return composition.has_value() ? composition->part : node;
}

NodeOrPartId GetNodeOrPartId() const {
if (!composition.has_value()) {
return NodeOrPartId{.node_id = node->id()};
}
return NodeOrPartId{.node_id = node->id(),
.composition_id = composition->composition_id,
.part_id = composition->part->id()};
}

bool operator==(const NodeVisibility& other) const = default;
};

Expand Down Expand Up @@ -359,6 +395,14 @@ class FoldingGraph {
void IdentifyCliques();
};

// Returns an error if any action's CompositionPart has an unassigned id, if
// distinct decompositions of the same node share an id, or if parts of the same
// decomposition use different ids.
absl::Status VerifyUniqueCompositionIds(
absl::Span<BinaryFoldingAction* const> actions);
absl::Status VerifyUniqueCompositionIds(
absl::Span<const std::unique_ptr<BinaryFoldingAction>> actions);

// Checks if building visibility expression as result of performing this folding
// would create a data cycle.
absl::StatusOr<bool> WouldCommittingFoldingActionCreateDataCycle(
Expand Down
Loading
Loading
0