Skip to content
Open
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
4 changes: 3 additions & 1 deletion p4_pdpi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ cc_library(
# Disable default arguments internally. Using them in PDPI itself is very likely a bug.
local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
deps = [
":entity_keys",
":ir_cc_proto",
":names",
":references",
Expand All @@ -171,6 +172,7 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_gutil//gutil:collections",
"@com_google_gutil//gutil:status",
Expand Down Expand Up @@ -745,7 +747,7 @@ cc_test(
cmd_diff_test(
name = "sequencing_diff_test",
actual_cmd = "$(execpath :sequencing_test_runner) $(location :main-p4info.pb.txt)",
expected = "//p4_pdpi:sequencing.expected",
expected = ":sequencing.expected",
tools = [
"main-p4info.pb.txt",
":sequencing_test_runner",
Expand Down
41 changes: 41 additions & 0 deletions p4_pdpi/sequencing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <optional>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

Expand All @@ -30,13 +31,15 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
#include "boost/graph/adjacency_list.hpp"
#include "google/protobuf/repeated_ptr_field.h"
#include "gutil/collections.h"
#include "gutil/status.h"
#include "p4/config/v1/p4info.pb.h"
#include "p4/v1/p4runtime.pb.h"
#include "p4_pdpi/entity_keys.h"
#include "p4_pdpi/ir.pb.h"
#include "p4_pdpi/names.h"
#include "p4_pdpi/references.h"
Expand Down Expand Up @@ -376,6 +379,44 @@ absl::Status StableSortEntities(const IrP4Info& info,
return absl::OkStatus();
}

absl::Status SortEntities(const IrP4Info& info,
std::vector<p4::v1::Entity>& entities) {
absl::c_sort(entities, [&](const p4::v1::Entity& a, const p4::v1::Entity& b) {
return a.DebugString() < b.DebugString();
});

struct EntityTuple {
p4::v1::Entity entity;
EntityKey key;
int order;
};

std::vector<EntityTuple> augmented_entities;
for (p4::v1::Entity& entity : entities) {
ASSIGN_OR_RETURN(EntityKey key, EntityKey::MakeEntityKey(entity));
ASSIGN_OR_RETURN(std::string table_name, EntityToTableName(info, entity));
ASSIGN_OR_RETURN(
int order,
gutil::FindOrStatus(info.dependency_rank_by_table_name(), table_name));

augmented_entities.push_back({std::move(entity), key, order});
}
absl::c_stable_sort(augmented_entities,
[&](const EntityTuple& a, const EntityTuple& b) {
return a.key < b.key;
});

absl::c_stable_sort(augmented_entities,
[&](const EntityTuple& a, const EntityTuple& b) {
return a.order > b.order;
});

for (int i = 0; i < entities.size(); ++i) {
entities[i] = std::move(augmented_entities[i].entity);
}
return absl::OkStatus();
}

absl::Status StableSortUpdates(
const IrP4Info& info,
google::protobuf::RepeatedPtrField<p4::v1::Update>& updates,
Expand Down
Loading