Skip to content

Commit

Permalink
Replace boost::container::flat_map with absl::btree_map in P4Tools. (#…
Browse files Browse the repository at this point in the history
…4768)

* Make SymbolicVariable part of the core IR. (#4840)

Signed-off-by: fruffy <[email protected]>

* Replace boost::container::flat_map with absl::btree_map in P4Tools.

Signed-off-by: fruffy <[email protected]>

* Review comments.

Signed-off-by: fruffy <[email protected]>

* Rebase on solver move PR.

Signed-off-by: fruffy <[email protected]>

---------

Signed-off-by: fruffy <[email protected]>
  • Loading branch information
fruffy authored Aug 1, 2024
1 parent 696ff1b commit a7d6e35
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 28 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ cc_library(
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_github_p4lang_p4runtime//:p4types_cc_proto",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:inlined_vector",
Expand Down
3 changes: 2 additions & 1 deletion backends/p4tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ cc_library(
"@boost//:multiprecision",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_z3prover_z3//:api",
"@com_google_absl//absl/container:btree",
],
)

Expand Down Expand Up @@ -163,6 +164,7 @@ cc_library(
"//:lib",
"@boost//:multiprecision",
"@com_github_pantor_inja//:inja",
"@com_google_absl//absl/container:btree",
"@nlohmann_json//:json",
],
)
Expand All @@ -181,7 +183,6 @@ cc_binary(
deps = [
":testgen_lib",
"//:lib",
"@boost//:filesystem",
"@boost//:multiprecision",
],
)
Expand Down
29 changes: 29 additions & 0 deletions backends/p4tools/common/lib/ir_compare.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef BACKENDS_P4TOOLS_COMMON_LIB_IR_COMPARE_H_
#define BACKENDS_P4TOOLS_COMMON_LIB_IR_COMPARE_H_

#include "ir/ir.h"

namespace IR {

/// Equals for StateVariable pointers. We only compare the label.
struct StateVariableEqual {
bool operator()(const IR::StateVariable *s1, const IR::StateVariable *s2) const {
return s1->equiv(*s2);
}
bool operator()(const IR::StateVariable &s1, const IR::StateVariable &s2) const {
return s1.equiv(s2);
}
};

/// Less for StateVariable pointers. We only compare the label.
struct StateVariableLess {
bool operator()(const IR::StateVariable *s1, const IR::StateVariable *s2) const {
return *s1 < *s2;
}
bool operator()(const IR::StateVariable &s1, const IR::StateVariable &s2) const {
return s1 < s2;
}
};

} // namespace IR
#endif /* BACKENDS_P4TOOLS_COMMON_LIB_IR_COMPARE_H_ */
2 changes: 0 additions & 2 deletions backends/p4tools/common/lib/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <string>
#include <utility>

#include <boost/container/vector.hpp>

#include "frontends/p4/optimizeExpressions.h"
#include "ir/indexed_vector.h"
#include "ir/irutils.h"
Expand Down
6 changes: 2 additions & 4 deletions backends/p4tools/common/lib/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@

#include <map>
#include <utility>
#include <vector>

#include <boost/container/flat_map.hpp>

#include "absl/container/btree_map.h"
#include "ir/ir.h"
#include "ir/solver.h"
#include "ir/visitor.h"

namespace P4Tools {

/// Symbolic maps map a state variable to a IR::Expression.
using SymbolicMapType = boost::container::flat_map<IR::StateVariable, const IR::Expression *>;
using SymbolicMapType = absl::btree_map<IR::StateVariable, const IR::Expression *>;

/// Represents a solution found by the solver. A model is a concretized form of a symbolic
/// environment. All the expressions in a Model must be of type IR::Literal.
Expand Down
2 changes: 0 additions & 2 deletions backends/p4tools/common/lib/symbolic_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <algorithm>
#include <utility>

#include <boost/container/vector.hpp>

#include "backends/p4tools/common/lib/model.h"
#include "ir/indexed_vector.h"
#include "ir/vector.h"
Expand Down
1 change: 0 additions & 1 deletion backends/p4tools/common/options.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef BACKENDS_P4TOOLS_COMMON_OPTIONS_H_
#define BACKENDS_P4TOOLS_COMMON_OPTIONS_H_

// Boost
#include <cstdint>
#include <optional>
#include <tuple>
Expand Down
1 change: 0 additions & 1 deletion backends/p4tools/modules/testgen/lib/execution_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <variant>
#include <vector>

#include <boost/container/vector.hpp>
#include <boost/multiprecision/cpp_int.hpp>

#include "backends/p4tools/common/compiler/convert_hs_index.h"
Expand Down
2 changes: 0 additions & 2 deletions backends/p4tools/modules/testgen/lib/final_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <variant>
#include <vector>

#include <boost/container/vector.hpp>

#include "backends/p4tools/common/lib/model.h"
#include "backends/p4tools/common/lib/symbolic_env.h"
#include "backends/p4tools/common/lib/trace_event.h"
Expand Down
11 changes: 10 additions & 1 deletion backends/p4tools/p4tools.def
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ class StateVariable : Expression {
StateVariable(ArrayIndex arr) : Expression(arr->getSourceInfo(), arr->type), ref(arr) {}

/// Implements comparisons so that StateVariables can be used as map keys.
// Delegate to IR's notion of equality.
bool operator==(const StateVariable &other) const override {
// Delegate to IR's notion of equality.
return *ref == *other.ref;
}

/// Implements comparisons so that StateVariables can be used as map keys.
/// Note that we ignore the type of the variable in the comparison.
equiv {
// We use a custom compare function.
// TODO: Is there a faster way to implement this comparison?
return compare(ref, a.ref) == 0;
}

/// Implements comparisons so that StateVariables can be used as map keys.
/// Note that we ignore the type of the variable in the comparison.
bool operator<(const StateVariable &other) const {
// We use a custom compare function.
// TODO: Is there a faster way to implement this comparison?
Expand Down
29 changes: 29 additions & 0 deletions ir/compare.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef IR_COMPARE_H_
#define IR_COMPARE_H_

#include "ir/ir.h"

namespace IR {

/// Equals for SymbolicVariable pointers. We only compare the label.
struct SymbolicVariableEqual {
bool operator()(const IR::SymbolicVariable *s1, const IR::SymbolicVariable *s2) const {
return s1->label == s2->label;
}
bool operator()(const IR::SymbolicVariable &s1, const IR::SymbolicVariable &s2) const {
return s1.label == s2.label;
}
};

/// Less for SymbolicVariable pointers. We only compare the label.
struct SymbolicVariableLess {
bool operator()(const IR::SymbolicVariable *s1, const IR::SymbolicVariable *s2) const {
return *s1 < *s2;
}
bool operator()(const IR::SymbolicVariable &s1, const IR::SymbolicVariable &s2) const {
return s1 < s2;
}
};

} // namespace IR
#endif /* IR_COMPARE_H_ */
18 changes: 4 additions & 14 deletions ir/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#include <optional>
#include <vector>

#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>

#include "absl/container/btree_map.h"
#include "ir/compare.h"
#include "ir/ir.h"
#include "lib/castable.h"
#include "lib/cstring.h"
Expand All @@ -15,18 +14,9 @@
// TODO: This should implement AbstractRepCheckedNode<Constraint>.
using Constraint = IR::Expression;

/// Comparator to compare SymbolicVariable pointers.
struct SymbolicVarComp {
bool operator()(const IR::SymbolicVariable *s1, const IR::SymbolicVariable *s2) const {
return s1->operator<(*s2);
}
};

/// This type maps symbolic variables to their value assigned by the solver.
using SymbolicMapping = boost::container::flat_map<const IR::SymbolicVariable *,
const IR::Expression *, SymbolicVarComp>;

using SymbolicSet = boost::container::flat_set<const IR::SymbolicVariable *, SymbolicVarComp>;
using SymbolicMapping =
absl::btree_map<const IR::SymbolicVariable *, const IR::Expression *, IR::SymbolicVariableLess>;

/// Provides a higher-level interface for an SMT solver.
class AbstractSolver : public ICastable {
Expand Down

0 comments on commit a7d6e35

Please sign in to comment.