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
2 changes: 2 additions & 0 deletions .github/workflows/ci-test-debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
CTEST_PARALLEL_LEVEL: 4
IMAGE_TYPE: test
BUILD_GENERATOR: Ninja
SUPPORT_P4_14: ON

steps:
- uses: actions/checkout@v6
with:
Expand Down
88 changes: 66 additions & 22 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ package(
default_visibility = ["//visibility:public"],
)

# Configuration setting to enable P4-14 support via --define=SUPPORT_P4_14=true
config_setting(
name = "p4_14_enabled",
define_values = {
"SUPPORT_P4_14": "true",
},
visibility = ["//visibility:public"],
)

license(
name = "license",
package_name = "com_github_p4lang_p4c",
Expand Down Expand Up @@ -71,6 +80,7 @@ cc_library(
],
)

# P4-16 parser.
genyacc(
name = "p4_parser_yacc",
src = "frontends/parsers/p4/p4parser.ypp",
Expand All @@ -80,6 +90,7 @@ genyacc(
visibility = ["//visibility:private"],
)

# P4-14 parser.
genyacc(
name = "v1_parser_yacc",
src = "frontends/parsers/v1/v1parser.ypp",
Expand All @@ -89,6 +100,7 @@ genyacc(
visibility = ["//visibility:private"],
)

# P4-16 lexer.
genrule(
name = "p4lexer_lex",
srcs = ["frontends/parsers/p4/p4lexer.ll"],
Expand All @@ -97,6 +109,7 @@ genrule(
visibility = ["//visibility:private"],
)

# P4-16 lexer.
genlex(
name = "p4lexer",
src = "frontends/parsers/p4/p4lexer.lex",
Expand All @@ -105,6 +118,7 @@ genlex(
visibility = ["//visibility:private"],
)

# P4-14 lexer.
genrule(
name = "v1lexer_lex",
srcs = ["frontends/parsers/v1/v1lexer.ll"],
Expand All @@ -113,6 +127,7 @@ genrule(
visibility = ["//visibility:private"],
)

# P4-14 lexer.
genlex(
name = "v1lexer",
src = "frontends/parsers/v1/v1lexer.lex",
Expand Down Expand Up @@ -172,8 +187,11 @@ cc_binary(

filegroup(
name = "ir_extra_defs",
srcs = [
"frontends/p4-14/ir-v1.def",
srcs = select({
# Conditionally include the P4-14 IR definition file.
":p4_14_enabled": ["frontends/p4-14/ir-v1.def"],
"//conditions:default": [],
}) + [
"backends/bmv2/bmv2.def",
"backends/dpdk/dpdk.def",
"//backends/p4tools:ir_extension",
Expand Down Expand Up @@ -205,25 +223,49 @@ genrule(
visibility = ["//visibility:private"],
)

# It would be better to build these modules separately, but they have cyclic
# dependencies.
cc_library(
name = "ir_frontend_midend_control_plane",
srcs = glob([
# Define the srcs list in a variable for readability
IR_FRONTEND_MIDEND_CONTROL_PLANE_SRCS = glob(
[
"ir/**/*.cpp",
"frontends/**/*.cpp",
"midend/**/*.cpp",
"control-plane/**/*.cpp",
]) + [
"backends/dpdk/dbprint-dpdk.cpp",
"backends/dpdk/printUtils.cpp",
"backends/dpdk/spec.cpp",
"frontends/parsers/p4/p4lexer.cc",
"frontends/parsers/p4/p4parser.cc",
],
) + [
# Back end files.
"backends/dpdk/dbprint-dpdk.cpp",
"backends/dpdk/printUtils.cpp",
"backends/dpdk/spec.cpp",
"frontends/parsers/p4/p4lexer.cc",
"frontends/parsers/p4/p4parser.cc",
"ir/ir-generated.cpp",
] + select({
# Conditionally select the glob for frontend C++ files.
":p4_14_enabled": [
"frontends/parsers/v1/v1lexer.cc",
"frontends/parsers/v1/v1parser.cc",
"ir/ir-generated.cpp",
],
] + glob(["frontends/**/*.cpp"]),
"//conditions:default": glob(
["frontends/**/*.cpp"],
exclude = [
"frontends/parsers/parserDriver_v1.cpp",
"frontends/parsers/v1/v1lexer.cc",
"frontends/parsers/v1/v1parser.cc",
"frontends/p4-14/**/*.cpp",
"frontends/parsers/v1/**/*.cpp",
],
),
})

# It would be better to build these modules separately, but they have cyclic
# dependencies.
cc_library(
name = "ir_frontend_midend_control_plane",
srcs = IR_FRONTEND_MIDEND_CONTROL_PLANE_SRCS,
defines = select({
# Define SUPPORT_P4_14 for C++ code when the flag is set.
":p4_14_enabled": ["SUPPORT_P4_14"],
"//conditions:default": [],
}),
textual_hdrs = glob([
"ir/**/*.h",
"frontends/**/*.h",
Expand All @@ -233,11 +275,16 @@ cc_library(
"backends/**/*.h",
]) + [
":p4_parser_yacc",
":v1_parser_yacc",
":p4lexer",
":v1lexer",
":ir_generated_files",
],
] + select({
# Conditionally include the P4-14 IR definition file.
":p4_14_enabled": [
":v1_parser_yacc",
":v1lexer",
],
"//conditions:default": [],
}),
deps = [
":config_h",
":lib",
Expand Down Expand Up @@ -476,9 +523,6 @@ cc_library(
"backends/graphs/version.h",
],
defines = [
# Disable ADL for boost, otherwise the method resolution for
# `ordered_map` and `graph` clash on method `boost::get`
"BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP",
],
deps = [
":config_h",
Expand Down
13 changes: 11 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ OPTION (BUILD_AUTO_VAR_INIT_PATTERN "Initialize variables with pattern during bu
OPTION (ENABLE_IWYU "Enable checking includes with IWYU" OFF)
# Support a legacy option. TODO: Remove?
OPTION (ENABLE_UNIFIED_COMPILATION "Enable CMAKE_UNITY_BUILD" OFF)
# Support compilation for the legacy P4-14 language.
OPTION (SUPPORT_P4_14 "Support the deprecated P4_14 version of the language" OFF)
# Tofino requires P4-14 support.
if (ENABLE_TOFINO)
set (SUPPORT_P4_14 ON)
endif()

# Enable DumpPipe pass output
OPTION(ENABLE_DUMP_PIPE "Enable DumpPipe pass output" ON)
Expand Down Expand Up @@ -172,7 +178,7 @@ if(STATIC_BUILD_WITH_DYNAMIC_GLIBC OR STATIC_BUILD_WITH_DYNAMIC_STDLIB)
if (NOT STATIC_BUILD_WITH_DYNAMIC_STDLIB)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
endif()
add_definitions(-DP4C_STATIC_BUILD)
add_definitions("-DP4C_STATIC_BUILD")
endif()

# Ensure we enable sanitizers before fetching dependencies so the correct flags
Expand All @@ -198,6 +204,9 @@ endif ()
if (ENABLE_DUMP_PIPE)
add_definitions("-DENABLE_DUMP_PIPE=1")
endif ()
if (SUPPORT_P4_14)
add_definitions("-DSUPPORT_P4_14=1")
endif ()

# Required tools and libraries.
find_package (PythonInterp 3 REQUIRED)
Expand Down Expand Up @@ -235,7 +244,7 @@ if (ENABLE_GC)
p4c_obtain_bdwgc()
endif ()
if (ENABLE_MULTITHREAD)
add_definitions(-DMULTITHREAD)
add_definitions("-DMULTITHREAD")
endif()
list (APPEND P4C_LIB_DEPS ${CMAKE_THREAD_LIBS_INIT})
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ ARG COMPILE_WITH_CLANG=OFF
ARG ENABLE_SANITIZERS=OFF
# Only execute the steps necessary to successfully run CMake.
ARG CMAKE_ONLY=OFF
# Support P4-14 in the compiler (legacy).
ARG SUPPORT_P4_14=OFF
# Build with -ftrivial-auto-var-init=pattern to catch more bugs caused by
# uninitialized variables.
ARG BUILD_AUTO_VAR_INIT_PATTERN=OFF
Expand Down
33 changes: 24 additions & 9 deletions backends/bmv2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,17 @@ add_custom_target(linkbmv2
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CURRENT_BINARY_DIR_PATH_REL}/p4c-bm2-psa ${P4C_BINARY_DIR}/p4c-bm2-psa
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CURRENT_BINARY_DIR_PATH_REL}/p4c-bm2-pna ${P4C_BINARY_DIR}/p4c-bm2-pna
COMMAND ${CMAKE_COMMAND} -E create_symlink ${P4C_BINARY_DIR_PATH_REL}/p4include ${CMAKE_CURRENT_BINARY_DIR}/p4include
COMMAND ${CMAKE_COMMAND} -E create_symlink ${P4C_BINARY_DIR_PATH_REL}/p4_14include ${CMAKE_CURRENT_BINARY_DIR}/p4_14include
DEPENDS update_includes
)
add_dependencies(p4c_driver linkbmv2)

if (SUPPORT_P4_14)
add_custom_target(linkp414bmv2
COMMAND ${CMAKE_COMMAND} -E create_symlink ${P4C_BINARY_DIR_PATH_REL}/p4_14include ${CMAKE_CURRENT_BINARY_DIR}/p4_14include
DEPENDS update_includes
)
add_dependencies(p4c_driver linkp414bmv2)
endif()

# Tests
set(BMV2_DRIVER ${CMAKE_CURRENT_SOURCE_DIR}/run-bmv2-test.py)
Expand All @@ -159,11 +165,16 @@ set (BMV2_V1MODEL_TEST_SUITES
"${P4C_SOURCE_DIR}/testdata/p4_16_samples/fabric_*/fabric.p4"
"${P4C_SOURCE_DIR}/testdata/p4_16_samples/omec/*.p4"
"${P4C_SOURCE_DIR}/testdata/p4_16_samples/pins/*.p4"
"${P4C_SOURCE_DIR}/testdata/p4_14_samples/switch_*/switch.p4"
"${P4C_SOURCE_DIR}/testdata/p4_14_samples/*.p4"
${v1tests}
)

if (SUPPORT_P4_14)
list(APPEND BMV2_V1MODEL_TEST_SUITES
"${P4C_SOURCE_DIR}/testdata/p4_14_samples/switch_*/switch.p4"
"${P4C_SOURCE_DIR}/testdata/p4_14_samples/*.p4"
)
endif()

set (PSA_INCLUDE_PATTERNS "include.*psa.p4")
set (PSA_EXCLUDE_PATTERNS "include.*v1model.p4" "include.*dpdk")
set (P4TESTS_FOR_PSA
Expand Down Expand Up @@ -209,10 +220,6 @@ set (testExtraFlagsPNA "${testExtraFlags} --use_pna -a='--target bmv2 --arch pna
set (XFAIL_TESTS
# This test defines two lpm keys for a table.
# Even though the P4 spec allows it, it doesn't really make sense in a switch
# so we allow it to fail on BMv2.
testdata/p4_14_samples/issue60.p4
# compiler claims (incorrectly?) that c2 has mulitple successors, so is not supported
testdata/p4_14_samples/issue-1426.p4
# This test uses a feature currently unsupported in the BMv2 back-end.
testdata/p4_16_samples/issue907-bmv2.p4
# This test uses a table graph that is not implementable in BMv2
Expand All @@ -221,8 +228,6 @@ set (XFAIL_TESTS
testdata/p4_16_samples/issue1062-bmv2.p4
# This test uses an undefined extern
testdata/p4_16_samples/issue1193-bmv2.p4
# This test also uses a custom extern
testdata/p4_14_samples/issue604.p4
# This test uses an incorrect model
testdata/p4_16_samples/issue1205-bmv2.p4
# These psa tests are not ready to run on bmv2 yet
Expand Down Expand Up @@ -255,6 +260,16 @@ set (XFAIL_TESTS
testdata/p4_16_samples/pna-example-tcp-connection-tracking.p4 # Conditional execution in actions not supported
testdata/p4_16_samples/pna-extract-local-header.p4 # error: IfStatement: not supported within a deparser on this target
)
if (SUPPORT_P4_14)
list(APPEND XFAIL_TESTS
# so we allow it to fail on BMv2.
testdata/p4_14_samples/issue60.p4
# compiler claims (incorrectly?) that c2 has mulitple successors, so is not supported
testdata/p4_14_samples/issue-1426.p4
# This test also uses a custom extern
testdata/p4_14_samples/issue604.p4
)
endif()

set (BMV2_PARSER_INLINE_TESTS "${P4C_SOURCE_DIR}/testdata/p4_16_samples/parser-inline/*.p4")

Expand Down
2 changes: 2 additions & 0 deletions backends/bmv2/common/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class Backend {
enumMap(enumMap),
corelib(P4::P4CoreLibrary::instance()),
json(new BMV2::JsonObjects()) {
#ifdef SUPPORT_P4_14
refMap->setIsV1(options.isv1());
#endif
}
void serialize(std::ostream &out) const { json->toplevel->serialize(out); }
virtual void convert(const IR::ToplevelBlock *block) = 0;
Expand Down
1 change: 0 additions & 1 deletion backends/bmv2/common/controlFlowGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
#include "controlFlowGraph.h"

#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/p4-14/fromv1.0/v1model.h"
#include "frontends/p4/methodInstance.h"
#include "frontends/p4/tableApply.h"
#include "frontends/p4/typeMap.h"
Expand Down
19 changes: 10 additions & 9 deletions backends/bmv2/common/extern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.

#include "extern.h"

#include "frontends/p4-14/fromv1.0/v1model.h"
#include "lib/json.h"

namespace P4::BMV2 {
Expand Down Expand Up @@ -222,14 +221,16 @@ cstring ExternConverter::createCalculation(ConversionContext *ctxt, cstring algo
}

cstring ExternConverter::convertHashAlgorithm(cstring algorithm) {
if (algorithm == P4V1::V1Model::instance.algorithm.crc32.name) return "crc32"_cs;
if (algorithm == P4V1::V1Model::instance.algorithm.crc32_custom.name) return "crc32_custom"_cs;
if (algorithm == P4V1::V1Model::instance.algorithm.crc16.name) return "crc16"_cs;
if (algorithm == P4V1::V1Model::instance.algorithm.crc16_custom.name) return "crc16_custom"_cs;
if (algorithm == P4V1::V1Model::instance.algorithm.random.name) return "random"_cs;
if (algorithm == P4V1::V1Model::instance.algorithm.identity.name) return "identity"_cs;
if (algorithm == P4V1::V1Model::instance.algorithm.csum16.name) return "csum16"_cs;
if (algorithm == P4V1::V1Model::instance.algorithm.xor16.name) return "xor16"_cs;
if (algorithm == P4V1::V1Model::instance().algorithm.crc32.name) return "crc32"_cs;
if (algorithm == P4V1::V1Model::instance().algorithm.crc32_custom.name)
return "crc32_custom"_cs;
if (algorithm == P4V1::V1Model::instance().algorithm.crc16.name) return "crc16"_cs;
if (algorithm == P4V1::V1Model::instance().algorithm.crc16_custom.name)
return "crc16_custom"_cs;
if (algorithm == P4V1::V1Model::instance().algorithm.random.name) return "random"_cs;
if (algorithm == P4V1::V1Model::instance().algorithm.identity.name) return "identity"_cs;
if (algorithm == P4V1::V1Model::instance().algorithm.csum16.name) return "csum16"_cs;
if (algorithm == P4V1::V1Model::instance().algorithm.xor16.name) return "xor16"_cs;

::P4::error(ErrorType::ERR_UNSUPPORTED, "Unsupported algorithm %1%", algorithm);
return cstring::empty;
Expand Down
12 changes: 6 additions & 6 deletions backends/bmv2/common/extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class ExternConverter {

#define EXTERN_CONVERTER_W_FUNCTION_AND_MODEL(extern_name, model_type, model_name) \
class ExternConverter_##extern_name : public ExternConverter { \
model_type &model_name; \
ExternConverter_##extern_name() : model_name(model_type::instance) { \
const model_type &model_name; \
ExternConverter_##extern_name() : model_name(model_type::instance()) { \
registerExternConverter(cstring(#extern_name), this); \
} \
static ExternConverter_##extern_name singleton; \
Expand All @@ -95,8 +95,8 @@ class ExternConverter {

#define EXTERN_CONVERTER_W_INSTANCE_AND_MODEL(extern_name, model_type, model_name) \
class ExternConverter_##extern_name : public ExternConverter { \
model_type &model_name; \
ExternConverter_##extern_name() : model_name(model_type::instance) { \
const model_type &model_name; \
ExternConverter_##extern_name() : model_name(model_type::instance()) { \
registerExternConverter(cstring(#extern_name), this); \
} \
static ExternConverter_##extern_name singleton; \
Expand All @@ -114,8 +114,8 @@ class ExternConverter {

#define EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE_AND_MODEL(extern_name, type, name) \
class ExternConverter_##extern_name : public ExternConverter { \
type &name; \
ExternConverter_##extern_name() : name(type::instance) { \
const type &name; \
ExternConverter_##extern_name() : name(type::instance()) { \
registerExternConverter(cstring(#extern_name), this); \
} \
static ExternConverter_##extern_name singleton; \
Expand Down
Loading
Loading