Skip to content

Commit 6a33e58

Browse files
P4 Infra Teamcopybara-github
authored andcommitted
Add SortEntities to sequencing.cc.
PiperOrigin-RevId: 808636945
1 parent 50c20b7 commit 6a33e58

File tree

5 files changed

+446
-1
lines changed

5 files changed

+446
-1
lines changed

p4_pdpi/BUILD.bazel

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ cc_library(
156156
# Disable default arguments internally. Using them in PDPI itself is very likely a bug.
157157
local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
158158
deps = [
159+
":entity_keys",
159160
":ir_cc_proto",
160161
":names",
161162
":references",
@@ -174,6 +175,7 @@ cc_library(
174175
"@com_google_absl//absl/status",
175176
"@com_google_absl//absl/status:statusor",
176177
"@com_google_absl//absl/strings",
178+
"@com_google_absl//absl/types:optional",
177179
"@com_google_absl//absl/types:span",
178180
"@com_google_gutil//gutil:collections",
179181
"@com_google_gutil//gutil:status",
@@ -748,7 +750,7 @@ cc_test(
748750
cmd_diff_test(
749751
name = "sequencing_diff_test",
750752
actual_cmd = "$(execpath :sequencing_test_runner) $(location :main-p4info.pb.txt)",
751-
expected = "//p4_pdpi:sequencing.expected",
753+
expected = ":sequencing.expected",
752754
tools = [
753755
"main-p4info.pb.txt",
754756
":sequencing_test_runner",

p4_pdpi/sequencing.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <optional>
2020
#include <queue>
2121
#include <string>
22+
#include <tuple>
2223
#include <utility>
2324
#include <vector>
2425

@@ -30,13 +31,15 @@
3031
#include "absl/status/status.h"
3132
#include "absl/status/statusor.h"
3233
#include "absl/strings/str_cat.h"
34+
#include "absl/types/optional.h"
3335
#include "absl/types/span.h"
3436
#include "boost/graph/adjacency_list.hpp"
3537
#include "google/protobuf/repeated_ptr_field.h"
3638
#include "gutil/collections.h"
3739
#include "gutil/status.h"
3840
#include "p4/config/v1/p4info.pb.h"
3941
#include "p4/v1/p4runtime.pb.h"
42+
#include "p4_pdpi/entity_keys.h"
4043
#include "p4_pdpi/ir.pb.h"
4144
#include "p4_pdpi/names.h"
4245
#include "p4_pdpi/references.h"
@@ -376,6 +379,44 @@ absl::Status StableSortEntities(const IrP4Info& info,
376379
return absl::OkStatus();
377380
}
378381

382+
absl::Status SortEntities(const IrP4Info& info,
383+
std::vector<p4::v1::Entity>& entities) {
384+
absl::c_sort(entities, [&](const p4::v1::Entity& a, const p4::v1::Entity& b) {
385+
return a.DebugString() < b.DebugString();
386+
});
387+
388+
struct EntityTuple {
389+
p4::v1::Entity entity;
390+
EntityKey key;
391+
int dependency_rank;
392+
};
393+
394+
std::vector<EntityTuple> augmented_entities;
395+
for (p4::v1::Entity& entity : entities) {
396+
ASSIGN_OR_RETURN(EntityKey key, EntityKey::MakeEntityKey(entity));
397+
ASSIGN_OR_RETURN(std::string table_name, EntityToTableName(info, entity));
398+
ASSIGN_OR_RETURN(
399+
int dependency_rank,
400+
gutil::FindOrStatus(info.dependency_rank_by_table_name(), table_name));
401+
402+
augmented_entities.push_back({std::move(entity), key, dependency_rank});
403+
}
404+
absl::c_stable_sort(augmented_entities,
405+
[&](const EntityTuple& a, const EntityTuple& b) {
406+
return a.key < b.key;
407+
});
408+
409+
absl::c_stable_sort(augmented_entities,
410+
[&](const EntityTuple& a, const EntityTuple& b) {
411+
return a.dependency_rank > b.dependency_rank;
412+
});
413+
414+
for (int i = 0; i < entities.size(); ++i) {
415+
entities[i] = std::move(augmented_entities[i].entity);
416+
}
417+
return absl::OkStatus();
418+
}
419+
379420
absl::Status StableSortUpdates(
380421
const IrP4Info& info,
381422
google::protobuf::RepeatedPtrField<p4::v1::Update>& updates,

0 commit comments

Comments
 (0)