From 9f37b1794198696c6d8635380fbc36f3851a97d7 Mon Sep 17 00:00:00 2001 From: PINS Team Date: Wed, 5 Jun 2024 18:15:40 +0000 Subject: [PATCH] PUBLIC: [P4-Constraints] Fix reproducibility bug. Use Ordered Maps in symbolic interpreter. PiperOrigin-RevId: 640586545 --- p4_constraints/backend/BUILD.bazel | 1 + p4_constraints/backend/symbolic_interpreter.cc | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/p4_constraints/backend/BUILD.bazel b/p4_constraints/backend/BUILD.bazel index 445dd06..b7354cd 100644 --- a/p4_constraints/backend/BUILD.bazel +++ b/p4_constraints/backend/BUILD.bazel @@ -158,6 +158,7 @@ cc_library( ":constraint_info", ":errors", "//gutils:collections", + "//gutils:ordered_map", "//gutils:overload", "//gutils:source_location", "//gutils:status", diff --git a/p4_constraints/backend/symbolic_interpreter.cc b/p4_constraints/backend/symbolic_interpreter.cc index 3130f6f..e38bd73 100644 --- a/p4_constraints/backend/symbolic_interpreter.cc +++ b/p4_constraints/backend/symbolic_interpreter.cc @@ -32,6 +32,7 @@ #include "absl/strings/string_view.h" #include "absl/strings/strip.h" #include "gutils/collections.h" +#include "gutils/ordered_map.h" #include "gutils/overload.h" #include "gutils/source_location.h" #include "gutils/status_builder.h" @@ -690,7 +691,9 @@ absl::StatusOr ConstraintSolver::ConcretizeEntry() { table_entry.set_table_id(table_info_.id); // Construct match fields by evaluating their respective entries in the model. - for (const auto& [key_name, key_info] : table_info_.keys_by_name) { + // Ordered for reproducibility. + for (const auto& [key_name, key_info] : + gutils::Ordered(table_info_.keys_by_name)) { ASSIGN_OR_RETURN(bool key_should_be_skipped, skip_key_named_(key_name)); if (key_should_be_skipped) continue; @@ -776,7 +779,9 @@ absl::StatusOr ConstraintSolver::Create( // Add keys to solver and map and determine whether the table needs a // priority. bool requires_priority = false; - for (const auto& [key_name, key_info] : table.keys_by_name) { + // Ordered for reproducibility. + for (const auto& [key_name, key_info] : + gutils::Ordered(constraint_solver.table_info_.keys_by_name)) { if (key_info.type.has_ternary() || key_info.type.has_optional_match()) { // In P4Runtime, all tables with ternaries or optionals require priorities // for their entries.