Skip to content

Commit

Permalink
Utility edits for P4Tools. (#3824)
Browse files Browse the repository at this point in the history
Co-authored-by: Anish Agrawal <[email protected]>
  • Loading branch information
fruffy-bfn and robotrobo authored Jan 5, 2023
1 parent 4767ea5 commit c0ea3b4
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 29 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ IncludeBlocks: Regroup
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Left
Standard: c++17
IncludeCategories:
# Matches common headers first and sorts them before project includes
- Regex: '^<.+/.*\.h>'
Expand Down
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ google-*,
-bugprone-easily-swappable-parameters,
-llvm-header-guard,
-misc-no-recursion'
FormatStyle: file
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
Expand Down
12 changes: 6 additions & 6 deletions backends/bmv2/common/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct CounterlikeTraits;

/// @ref CounterlikeTraits<> specialization for @ref CounterExtern for v1model
template <>
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL> > {
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL>> {
static const cstring name() { return "counter"; }
static const cstring directPropertyName() {
return P4V1::V1Model::instance.tableAttributes.counters.name;
Expand All @@ -185,7 +185,7 @@ struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL> > {
};

template <>
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL2020> > {
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL2020>> {
static const cstring name() { return "counter"; }
static const cstring directPropertyName() {
return P4V1::V1Model::instance.tableAttributes.counters.name;
Expand All @@ -198,7 +198,7 @@ struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL2020> >

/// @ref CounterlikeTraits<> specialization for @ref CounterExtern for PSA
template <>
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA> > {
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA>> {
static const cstring name() { return "counter"; }
static const cstring directPropertyName() { return "psa_direct_counter"; }
static const cstring typeName() { return "Counter"; }
Expand All @@ -211,7 +211,7 @@ struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA> > {

/// @ref CounterlikeTraits<> specialization for @ref MeterExtern for v1model
template <>
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL> > {
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL>> {
static const cstring name() { return "meter"; }
static const cstring directPropertyName() {
return P4V1::V1Model::instance.tableAttributes.meters.name;
Expand All @@ -223,7 +223,7 @@ struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL> > {
};

template <>
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020> > {
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020>> {
static const cstring name() { return "meter"; }
static const cstring directPropertyName() {
return P4V1::V1Model::instance.tableAttributes.meters.name;
Expand All @@ -236,7 +236,7 @@ struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020> > {

/// @ref CounterlikeTraits<> specialization for @ref MeterExtern for PSA
template <>
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA> > {
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA>> {
static const cstring name() { return "meter"; }
static const cstring directPropertyName() { return "psa_direct_meter"; }
static const cstring typeName() { return "Meter"; }
Expand Down
12 changes: 6 additions & 6 deletions backends/graphs/graphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ class Graphs : public Inspector {
using vertexProperties = boost::property<boost::vertex_attribute_t, GraphvizAttributes, Vertex>;
using edgeProperties = boost::property<
boost::edge_attribute_t, GraphvizAttributes,
boost::property<boost::edge_name_t, cstring, boost::property<boost::edge_index_t, int> > >;
boost::property<boost::edge_name_t, cstring, boost::property<boost::edge_index_t, int>>>;
using graphProperties = boost::property<
boost::graph_name_t, cstring,
boost::property<boost::graph_graph_attribute_t, GraphvizAttributes,
boost::property<boost::graph_vertex_attribute_t, GraphvizAttributes,
boost::property<boost::graph_edge_attribute_t,
GraphvizAttributes> > > >;
boost::property<
boost::graph_graph_attribute_t, GraphvizAttributes,
boost::property<boost::graph_vertex_attribute_t, GraphvizAttributes,
boost::property<boost::graph_edge_attribute_t, GraphvizAttributes>>>>;
using Graph_ = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
vertexProperties, edgeProperties, graphProperties>;
using Graph = boost::subgraph<Graph_>;
using vertex_t = boost::graph_traits<Graph>::vertex_descriptor;

using Parents = std::vector<std::pair<vertex_t, EdgeTypeIface*> >;
using Parents = std::vector<std::pair<vertex_t, EdgeTypeIface*>>;

// merge misc control statements (action calls, extern method calls,
// assignments) into a single vertex to reduce graph complexity
Expand Down
4 changes: 0 additions & 4 deletions backends/p4tools/cmake/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ if(CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()

# Use C++11.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Use libgc.
find_package(LibGc 7.2.0 REQUIRED)

Expand Down
5 changes: 5 additions & 0 deletions backends/p4tools/common/compiler/compiler_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ boost::optional<const IR::P4Program*> CompilerTarget::runCompiler(const std::str
}

boost::optional<const IR::P4Program*> CompilerTarget::runCompiler(const IR::P4Program* program) {
return get().runCompiler_impl(program);
}

boost::optional<const IR::P4Program*> CompilerTarget::runCompiler_impl(
const IR::P4Program* program) const {
const auto& self = get();

program = self.runFrontend(program);
Expand Down
3 changes: 3 additions & 0 deletions backends/p4tools/common/compiler/compiler_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class CompilerTarget : public Target {
/// @see @makeContext.
virtual ICompileContext* makeContext_impl() const;

/// @see runCompiler.
virtual boost::optional<const IR::P4Program*> runCompiler_impl(const IR::P4Program*) const;

/// This implementation just forwards the given arguments to the compiler.
///
/// @see @initCompiler.
Expand Down
1 change: 1 addition & 0 deletions backends/p4tools/common/lib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <algorithm>
#include <chrono> // NOLINT cpplint throws a warning because Google has a similar library...
#include <cstdint>
#include <ctime>
#include <iomanip>
#include <map>
Expand Down
14 changes: 14 additions & 0 deletions backends/p4tools/common/lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ class Utils {
static const IR::MethodCallExpression* generateInternalMethodCall(
cstring methodName, const std::vector<const IR::Expression*>& argVector,
const IR::Type* returnType = IR::Type_Void::get());

/// Shuffles the given iterable @param inp
template <typename T>
static void shuffle(T* inp) {
std::shuffle(inp->begin(), inp->end(), rng);
}

/// @returns a random element from the given range between @param start and @param end.
template <typename Iter>
static Iter pickRandom(Iter start, Iter end) {
int random = getRandInt(std::distance(start, end) - 1);
std::advance(start, random);
return start;
}
};

} // namespace P4Tools
Expand Down
3 changes: 3 additions & 0 deletions backends/p4tools/modules/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This .gitignore trick specifies that only folders are track within this folder. Files etc must be added with the git add -f flag. The intention is to prevent unintentional tracking of symlinks.
/*
!/*/
2 changes: 1 addition & 1 deletion backends/p4tools/modules/testgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ foreach(ext ${testgen_targets})
# Generate an option that makes it possible to disable this extension.
string(MAKE_C_IDENTIFIER ${ext} EXT_AS_IDENTIFIER)
string(TOUPPER ${EXT_AS_IDENTIFIER} EXT_AS_OPTION_NAME)
string(CONCAT ENABLE_EXT_OPTION "ENABLE_TESTGEN_TARGET_" ${EXT_AS_OPTION_NAME})
string(CONCAT ENABLE_EXT_OPTION "ENABLE_TOOLS_TARGET_" ${EXT_AS_OPTION_NAME})
string(CONCAT EXT_HELP_TEXT "Build the " ${ext} " target")
option(${ENABLE_EXT_OPTION} ${EXT_HELP_TEXT} ON)
if(${ENABLE_EXT_OPTION})
Expand Down
18 changes: 9 additions & 9 deletions control-plane/p4RuntimeArchStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace Helpers {

/// @ref CounterlikeTraits<> specialization for @ref CounterExtern for v1model
template <>
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL> > {
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL>> {
static const cstring name() { return "counter"; }
static const cstring directPropertyName() {
return P4V1::V1Model::instance.tableAttributes.counters.name;
Expand All @@ -93,7 +93,7 @@ struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL> > {
};

template <>
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL2020> > {
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL2020>> {
static const cstring name() { return "counter"; }
static const cstring directPropertyName() {
return P4V1::V1Model::instance.tableAttributes.counters.name;
Expand All @@ -116,7 +116,7 @@ struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL2020> >

/// @ref CounterlikeTraits<> specialization for @ref CounterExtern for PSA
template <>
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA> > {
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA>> {
static const cstring name() { return "counter"; }
static const cstring directPropertyName() { return "psa_direct_counter"; }
static const cstring typeName() { return "Counter"; }
Expand All @@ -139,7 +139,7 @@ struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA> > {

/// @ref CounterlikeTraits<> specialization for @ref CounterExtern for PNA
template <>
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PNA> > {
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PNA>> {
static const cstring name() { return "counter"; }
static const cstring directPropertyName() { return "pna_direct_counter"; }
static const cstring typeName() { return "Counter"; }
Expand All @@ -162,7 +162,7 @@ struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PNA> > {

/// @ref CounterlikeTraits<> specialization for @ref MeterExtern for v1model
template <>
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL> > {
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL>> {
static const cstring name() { return "meter"; }
static const cstring directPropertyName() {
return P4V1::V1Model::instance.tableAttributes.meters.name;
Expand All @@ -182,7 +182,7 @@ struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL> > {
};

template <>
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020> > {
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020>> {
static const cstring name() { return "meter"; }
static const cstring directPropertyName() {
return P4V1::V1Model::instance.tableAttributes.meters.name;
Expand All @@ -203,7 +203,7 @@ struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020> > {

/// @ref CounterlikeTraits<> specialization for @ref MeterExtern for PSA
template <>
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA> > {
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA>> {
static const cstring name() { return "meter"; }
static const cstring directPropertyName() { return "psa_direct_meter"; }
static const cstring typeName() { return "Meter"; }
Expand All @@ -224,7 +224,7 @@ struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA> > {

/// @ref CounterlikeTraits<> specialization for @ref MeterExtern for PNA
template <>
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PNA> > {
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PNA>> {
static const cstring name() { return "meter"; }
static const cstring directPropertyName() { return "pna_direct_meter"; }
static const cstring typeName() { return "Meter"; }
Expand Down Expand Up @@ -922,7 +922,7 @@ class P4RuntimeArchHandlerCommon : public P4RuntimeArchHandlerIface {
TypeMap* typeMap;
const IR::ToplevelBlock* evaluatedProgram;

std::unordered_map<cstring, std::set<cstring> > actionProfilesRefs;
std::unordered_map<cstring, std::set<cstring>> actionProfilesRefs;

/// The extern instances we've serialized so far. Used for deduplication.
std::set<p4rt_id_t> serializedInstances;
Expand Down
4 changes: 2 additions & 2 deletions lib/nullstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ limitations under the License.
#include "cstring.h"
#include "error.h"

template <class cT, class traits = std::char_traits<cT> >
template <class cT, class traits = std::char_traits<cT>>
class basic_nullbuf final : public std::basic_streambuf<cT, traits> {
typename traits::int_type overflow(typename traits::int_type c) {
return traits::not_eof(c); // indicate success
}
};

template <class cT, class traits = std::char_traits<cT> >
template <class cT, class traits = std::char_traits<cT>>
class onullstream final : public std::basic_ostream<cT, traits> {
public:
onullstream() : std::basic_ios<cT, traits>(&m_sbuf), std::basic_ostream<cT, traits>(&m_sbuf) {
Expand Down
2 changes: 1 addition & 1 deletion midend/parserUnroll.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ParserStructure {
const IR::ParserState* start;
const ParserInfo* result;
StateCallGraph* callGraph;
std::map<cstring, std::set<cstring> > statesWithHeaderStacks;
std::map<cstring, std::set<cstring>> statesWithHeaderStacks;
std::map<cstring, size_t> callsIndexes; // map for curent calls of state insite current one
void setParser(const IR::P4Parser* parser) {
CHECK_NULL(parser);
Expand Down

0 comments on commit c0ea3b4

Please sign in to comment.