Skip to content

[P4Testgen] Reenable Tofino passes without actually linking the full Tofino compiler library #5320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ add_subdirectory (ir)

# add extensions - before the frontends as they produce IR and extra frontend sources
set(EXTENSION_IR_SOURCES)
# extra libraries that need to be linked with the generated IR.
set(EXTENSION_IR_LIBS)
# extra sources that need to be linked directly into p4test so that
# extensions can provide specific conversions (e.g., for externs)
set(EXTENSION_P4_14_CONV_SOURCES)
Expand Down Expand Up @@ -544,7 +546,7 @@ add_custom_target(genIR DEPENDS ${IR_GENERATED_SRCS})
set_source_files_properties(${IR_GENERATOR} PROPERTIES GENERATED TRUE)
add_library(ir-generated OBJECT ${IR_GENERATED_SRCS} ${EXTENSION_IR_SOURCES})
add_dependencies(ir-generated ir genIR)
target_link_libraries(ir-generated PUBLIC ir ${P4C_LIB_DEPS})
target_link_libraries(ir-generated PUBLIC ir ${P4C_LIB_DEPS} ${EXTENSION_IR_LIBS})


######################################## IR Generation End ########################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ endif()
# that. Those transitive dependencies turn out to be most of the compiler!
set(
TESTGEN_LIBS
# tofinobackend
# bfn_p4runtime
# p4cgraphs
# We do not use the target library variable here because top-level testgen will not find it.
# boost_system
${TESTGEN_LIBS}
PARENT_SCOPE
)
Expand Down
11 changes: 6 additions & 5 deletions backends/p4tools/modules/testgen/targets/tofino/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
#include <cstddef>
#include <map>
#include <string>
#include <utility>
#include <vector>

#include "backends/p4tools/common/lib/namespace_context.h"
#include "backends/p4tools/common/lib/util.h"
// TOOD: We can only use this pass because it is header-only.
#include "backends/tofino/bf-p4c/midend/rewrite_egress_intrinsic_metadata_header.h"
#include "ir/ir.h"
#include "lib/cstring.h"
#include "lib/exceptions.h"
Expand Down Expand Up @@ -71,14 +72,14 @@ P4::FrontEnd AbstractTofinoTestgenTarget::mkFrontEnd() const {
MidEnd AbstractTofinoTestgenTarget::mkMidEnd(const CompilerOptions &options) const {
// We need to initialize the device to be able to use Tofino compiler passes
// FIXME: Reenable this?
// Device::init(spec.deviceName);
Device::init(spec.deviceName, {});
auto midEnd = CompilerTarget::mkMidEnd(options);
// auto *refMap = midEnd.getRefMap();
// auto *typeMap = midEnd.getTypeMap();
auto *refMap = midEnd.getRefMap();
auto *typeMap = midEnd.getTypeMap();
midEnd.addPasses({
// Remove all unused fields in the egress intrinsic metadata
// FIXME: Reenable this.
// new BFN::RewriteEgressIntrinsicMetadataHeader(refMap, typeMap),
new BFN::RewriteEgressIntrinsicMetadataHeader(refMap, typeMap),
// Remove trailing '$' in '$valid$' key matches and
// replace subscript operator in header stack indices
// with '$'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ p4tools_add_xfail_reason(
p4tools_add_xfail_reason(
"testgen-tofino2-ptf"
"The validity bit of .* is tainted"
tna_simple_switch.p4
)

p4tools_add_xfail_reason(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ p4tools_add_xfail_reason(
p4tools_add_xfail_reason(
"testgen-tofino-ptf"
"The validity bit of .* is tainted"
tna_simple_switch.p4
)

p4tools_add_xfail_reason(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "backends/p4tools/common/lib/formulae.h"
#include "backends/p4tools/common/lib/util.h"
#include "backends/p4tools/common/lib/variables.h"
#include "backends/tofino/bf-p4c/ir/gress.h"
#include "backends/tofino/bf-p4c/specs/gress.h"
#include "ir/ir.h"
#include "ir/irutils.h"
#include "lib/exceptions.h"
Expand Down Expand Up @@ -158,8 +158,8 @@ void TofinoCmdStepper::initializeTargetEnvironment(ExecutionState &nextState) co

std::optional<const Constraint *> TofinoCmdStepper::startParserImpl(
const IR::P4Parser *parser, ExecutionState &nextState) const {
auto programINfo = getProgramInfo();
auto gress = programINfo.getGress(parser);
auto programInfo = getProgramInfo();
auto gress = programInfo.getGress(parser);

const auto *nineBitType = IR::Type_Bits::get(9);
const auto *oneBitType = IR::Type_Bits::get(1);
Expand Down Expand Up @@ -187,25 +187,25 @@ std::optional<const Constraint *> TofinoCmdStepper::startParserImpl(
case INGRESS: {
// bypass_egress is not active at the beginning.
const auto &bypassExpr =
programINfo.getParserParamVar(parser, oneBitType, 4, "bypass_egress"_cs);
programInfo.getParserParamVar(parser, oneBitType, 4, "bypass_egress"_cs);
nextState.set(bypassExpr, IR::Constant::get(oneBitType, 0));

// Check whether the parser error was referenced in this particular pipe.
const auto *pipes = programINfo.getPipes();
const auto *decl = pipes->at(programINfo.getPipeIdx(parser)).pipes.at("IngressT"_cs);
const auto *pipes = programInfo.getPipes();
const auto *decl = pipes->at(programInfo.getPipeIdx(parser)).pipes.at("IngressT"_cs);
const auto *p4control = decl->checkedTo<IR::P4Control>();
CheckParserError checkParserError;
p4control->apply(checkParserError);
nextState.setProperty("parserErrReferenced"_cs, checkParserError.hasParserError());
// Tofino implicitly drops packets if they are not set during the ingress or egress
// pipeline.
nextState.set(
programINfo.getParserParamVar(parser, nineBitType, 4, "ucast_egress_port"_cs),
programInfo.getParserParamVar(parser, nineBitType, 4, "ucast_egress_port"_cs),
new IR::UninitializedTaintExpression(nineBitType));
// Initialize parser_err with no error and set the parser error label.
// This label is used by some core externs to control target's parser error.
const auto &parserErrorLabelExpr =
programINfo.getParserParamVar(parser, parserErrorType, 5, "parser_err"_cs);
programInfo.getParserParamVar(parser, parserErrorType, 5, "parser_err"_cs);
nextState.set(parserErrorLabelExpr, IR::Constant::get(parserErrorType, 0));
nextState.setParserErrorLabel(parserErrorLabelExpr);
nextState.setProperty("gress"_cs, static_cast<uint64_t>(gress_t::INGRESS));
Expand All @@ -216,12 +216,12 @@ std::optional<const Constraint *> TofinoCmdStepper::startParserImpl(
// Retrieve the egress port associated with this parser.
// Tofino implicitly drops packets if they are not set during the ingress or egress
// pipeline.
nextState.set(programINfo.getParserParamVar(parser, nineBitType, 3, "egress_port"_cs),
nextState.set(programInfo.getParserParamVar(parser, nineBitType, 3, "egress_port"_cs),
new IR::UninitializedTaintExpression(nineBitType));
// Initialize parser_err with no error and set the parser error label.
// This label is used by some core externs to control target's parser error.
const auto &parserErrorLabelExpr =
programINfo.getParserParamVar(parser, parserErrorType, 3, "parser_err"_cs);
programInfo.getParserParamVar(parser, parserErrorType, 4, "parser_err"_cs);
nextState.set(parserErrorLabelExpr, IR::Constant::get(parserErrorType, 0));
nextState.setParserErrorLabel(parserErrorLabelExpr);
nextState.setProperty("gress"_cs, static_cast<uint64_t>(gress_t::EGRESS));
Expand Down Expand Up @@ -283,7 +283,7 @@ std::map<Continuation::Exception, Continuation> TofinoCmdStepper::getExceptionHa
case EGRESS: {
result.emplace(Continuation::Exception::PacketTooShort, Continuation::Body({}));
const auto &parserErrorVariable =
programInfo.getParserParamVar(parser, parserErrorType, 3, "parser_err"_cs);
programInfo.getParserParamVar(parser, parserErrorType, 4, "parser_err"_cs);
const auto *noMatchConst = IR::Constant::get(parserErrorVariable->type,
TofinoConstants::PARSER_ERROR_NO_MATCH);
const auto *noMatchAssign = new IR::AssignmentStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <map>
#include <vector>

#include "backends/tofino/bf-p4c/ir/gress.h"
#include "backends/tofino/bf-p4c/specs/gress.h"
#include "ir/ir.h"

#include "backends/p4tools/modules/testgen/lib/continuation.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@

#include "backends/p4tools/common/lib/util.h"
#include "backends/p4tools/common/lib/variables.h"
#include "backends/tofino/bf-p4c/ir/gress.h"
#include "ir/ir-generated.h"
#include "backends/tofino/bf-p4c/specs/gress.h"
#include "ir/ir.h"
#include "ir/irutils.h"
#include "lib/exceptions.h"
Expand Down Expand Up @@ -219,7 +218,7 @@ std::optional<const Constraint *> JBayCmdStepper::startParserImpl(const IR::P4Pa
// Initialize parser_err with no error and set the parser error label.
// This label is used by some core externs to control target's parser error.
const auto &parserErrorVariable =
programInfo.getParserParamVar(parser, parserErrorType, 3, "parser_err"_cs);
programInfo.getParserParamVar(parser, parserErrorType, 4, "parser_err"_cs);
nextState.set(parserErrorVariable, IR::Constant::get(parserErrorType, 0));
nextState.setParserErrorLabel(parserErrorVariable);
nextState.setProperty("gress"_cs, static_cast<uint64_t>(gress_t::EGRESS));
Expand Down Expand Up @@ -283,7 +282,7 @@ std::map<Continuation::Exception, Continuation> JBayCmdStepper::getExceptionHand
// Models PARSER_ERROR_NO_TCAM parser error.
// Set the parser error label to PARSER_ERROR_NO_MATCH in case of a no-match.
const auto &parserErrorVariable =
programInfo.getParserParamVar(parser, parserErrorType, 5, "parser_err"_cs);
programInfo.getParserParamVar(parser, parserErrorType, 4, "parser_err"_cs);
const auto *noMatchConst =
IR::Constant::get(parserErrorVariable->type, JBayConstants::PARSER_ERROR_NO_MATCH);
const auto *noMatchAssign = new IR::AssignmentStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <map>
#include <vector>

#include "backends/tofino/bf-p4c/ir/gress.h"
#include "backends/tofino/bf-p4c/specs/gress.h"
#include "ir/ir.h"

#include "backends/p4tools/modules/testgen/lib/continuation.h"
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ set (BF_P4C_IR_SRCS
bf-p4c/ir/control_flow_visitor.cpp
bf-p4c/ir/ir_enums.cpp
bf-p4c/ir/gateway_control_flow.cpp
bf-p4c/ir/gress.cpp
bf-p4c/ir/mau.cpp
bf-p4c/ir/thread_visitor.cpp
bf-p4c/ir/tofino.cpp
Expand All @@ -224,6 +223,7 @@ foreach(src IN LISTS BF_P4C_IR_SRCS)
set(QUAL_BF_P4C_IR_SRCS ${QUAL_BF_P4C_IR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${src})
endforeach()
set(EXTENSION_IR_SOURCES ${EXTENSION_IR_SOURCES} ${QUAL_BF_P4C_IR_SRCS} PARENT_SCOPE)
set(EXTENSION_IR_LIBS ${EXTENSION_IR_LIBS} tofinospecs PARENT_SCOPE)


add_subdirectory(bf-p4c)
Expand Down
26 changes: 18 additions & 8 deletions backends/tofino/bf-p4c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# specific language governing permissions and limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# # # #### Barefoot back-end
# # ##### Barefoot back-end

MESSAGE("-- Adding p4c-barefoot")

Expand Down Expand Up @@ -90,6 +90,21 @@ set (P4C_LIB_DEPS ${P4C_LIB_DEPS} PARENT_SCOPE)

add_subdirectory(logging)

set (BF_P4C_SPEC_SRCS
specs/arch_spec.cpp
specs/device.cpp
specs/gress.cpp
specs/mau_spec.cpp
specs/match_register_spec.cpp
specs/phv.cpp
specs/phv_spec.cpp
)

add_library(tofinospecs STATIC ${BF_P4C_SPEC_SRCS})
target_link_libraries(tofinospecs PUBLIC p4ctoolkit)
# We depend on the gen-tree-macro.h file, which is generated.
add_dependencies(tofinospecs genIR)

set (BF_P4C_MIDEND_SRCS
midend/annotate_with_in_hash.cpp
midend/check_design_pattern.cpp
Expand Down Expand Up @@ -213,7 +228,6 @@ set (BF_P4C_BACKEND_MAU_SRCS
mau/jbay_next_table.cpp
mau/mau_alloc.cpp
mau/mau_power.cpp
mau/mau_spec.cpp
mau/memories.cpp
mau/payload_gateway.cpp
mau/reduction_or.cpp
Expand Down Expand Up @@ -241,7 +255,6 @@ set (BF_P4C_BACKEND_MAU_TOFINO_SRCS
mau/tofino/action_data_bus.cpp
mau/tofino/asm_output.cpp
mau/tofino/input_xbar.cpp
mau/tofino/mau_spec.cpp
mau/tofino/memories.cpp
)

Expand Down Expand Up @@ -324,7 +337,6 @@ set (BF_P4C_BACKEND_PHV_SRCS
phv/phv_analysis.cpp
phv/phv_fields.cpp
phv/phv_parde_mau_use.cpp
phv/phv_spec.cpp
phv/split_padding.cpp
phv/table_phv_constraints.cpp
phv/validate_allocation.cpp
Expand Down Expand Up @@ -390,7 +402,6 @@ set (BF_P4C_BACKEND_PHV_SRCS

set (BF_P4C_BACKEND_ARCH_SRCS
arch/arch.cpp
arch/arch_spec.cpp
arch/add_t2na_meta.cpp
arch/intrinsic_metadata.cpp
arch/bridge_metadata.cpp
Expand Down Expand Up @@ -442,7 +453,6 @@ set (BF_P4C_BACKEND_MAIN_SRCS
asm.cpp
backend.cpp
bf-p4c-options.cpp
device.cpp
midend.cpp
)

Expand Down Expand Up @@ -806,7 +816,7 @@ endif()
install (FILES ${P4C_BINARY_DIR}/p4c_src/main.py
DESTINATION ${P4C_ARTIFACTS_OUTPUT_DIRECTORY}/p4c_src)

# # # ############# Proto
# # ############## Proto
set (BFN_P4RUNTIME_DIR ${CMAKE_CURRENT_SOURCE_DIR}/control-plane/proto)
set (BFN_P4RUNTIME_INFO_PROTO ${BFN_P4RUNTIME_DIR}/barefoot/p4info.proto)
set (BFN_P4RUNTIME_INFO_GEN_SRCS ${P4C_BINARY_DIR}/control-plane/barefoot/p4info.pb.cc)
Expand Down Expand Up @@ -840,7 +850,7 @@ target_link_libraries(bfn_p4runtime PUBLIC controlplane-gen PRIVATE absl::prefet
set_source_files_properties(${BFN_P4RUNTIME_INFO_GEN_SRCS} PROPERTIES GENERATED TRUE)
add_dependencies(bfn_p4runtime barefootdir controlplane)

# # # ############# Testing
# # ############## Testing

if (ENABLE_GTESTS)
# # Tofino-specific GTests.
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/arch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "backends/tofino/bf-p4c/arch/v1model.h"
#include "backends/tofino/bf-p4c/bf-p4c-options.h"
#include "backends/tofino/bf-p4c/common/pragma/collect_global_pragma.h"
#include "backends/tofino/bf-p4c/device.h"
#include "backends/tofino/bf-p4c/specs/device.h"
#include "frontends/p4/methodInstance.h"
#include "ir/declaration.h"
#include "ir/id.h"
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

#include "backends/tofino/bf-p4c/arch/program_structure.h"
#include "backends/tofino/bf-p4c/bf-p4c-options.h"
#include "backends/tofino/bf-p4c/ir/gress.h"
#include "backends/tofino/bf-p4c/lib/assoc.h"
#include "backends/tofino/bf-p4c/midend/type_checker.h"
#include "backends/tofino/bf-p4c/specs/gress.h"
#include "frontends/common/options.h"
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/common/resolveReferences/resolveReferences.h"
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/bridge_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#define BACKENDS_TOFINO_BF_P4C_ARCH_BRIDGE_METADATA_H_

#include "backends/tofino/bf-p4c/arch/program_structure.h"
#include "backends/tofino/bf-p4c/ir/gress.h"
#include "backends/tofino/bf-p4c/midend/path_linearizer.h"
#include "backends/tofino/bf-p4c/specs/gress.h"
#include "frontends/common/resolveReferences/resolveReferences.h"
#include "frontends/p4/evaluator/evaluator.h"
#include "ir/ir.h"
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/check_extern_invocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "check_extern_invocation.h"

#include "backends/tofino/bf-p4c/device.h"
#include "backends/tofino/bf-p4c/specs/device.h"
#include "lib/bitvec.h"

namespace BFN {
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/fromv1.0/mirror.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include "backends/tofino/bf-p4c/arch/bridge_metadata.h"
#include "backends/tofino/bf-p4c/arch/intrinsic_metadata.h"
#include "backends/tofino/bf-p4c/common/ir_utils.h"
#include "backends/tofino/bf-p4c/device.h"
#include "backends/tofino/bf-p4c/lib/pad_alignment.h"
#include "backends/tofino/bf-p4c/specs/device.h"
#include "frontends/p4-14/fromv1.0/v1model.h"
#include "frontends/p4/cloner.h"
#include "frontends/p4/coreLibrary.h"
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/fromv1.0/parser_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <boost/range/adaptor/reversed.hpp>

#include "backends/tofino/bf-p4c/common/utils.h"
#include "backends/tofino/bf-p4c/device.h"
#include "backends/tofino/bf-p4c/specs/device.h"
#include "ir/ir.h"

namespace BFN {
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/fromv1.0/phase0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
#include "backends/tofino/bf-p4c/arch/fromv1.0/programStructure.h"
#include "backends/tofino/bf-p4c/arch/tna.h"
#include "backends/tofino/bf-p4c/common/asm_output.h"
#include "backends/tofino/bf-p4c/device.h"
#include "backends/tofino/bf-p4c/lib/pad_alignment.h"
#include "backends/tofino/bf-p4c/midend/path_linearizer.h"
#include "backends/tofino/bf-p4c/midend/type_categories.h"
#include "backends/tofino/bf-p4c/midend/type_checker.h"
#include "backends/tofino/bf-p4c/parde/field_packing.h"
#include "backends/tofino/bf-p4c/specs/device.h"
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/p4-14/fromv1.0/v1model.h"
#include "frontends/p4/cloner.h"
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/fromv1.0/primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "backends/tofino/bf-p4c/arch/fromv1.0/programStructure.h"
#include "backends/tofino/bf-p4c/bf-p4c-options.h"
#include "backends/tofino/bf-p4c/device.h"
#include "backends/tofino/bf-p4c/specs/device.h"
#include "frontends/p4-14/fromv1.0/converters.h"

namespace P4 {
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/arch/fromv1.0/programStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "backends/tofino/bf-p4c/arch/fromv1.0/phase0.h"
#include "backends/tofino/bf-p4c/arch/intrinsic_metadata.h"
#include "backends/tofino/bf-p4c/common/pragma/all_pragmas.h"
#include "backends/tofino/bf-p4c/device.h"
#include "backends/tofino/bf-p4c/specs/device.h"
#include "frontends/p4-14/fromv1.0/converters.h"
#include "frontends/p4-14/header_type.h"
#include "frontends/p4-14/typecheck.h"
Expand Down
Loading
Loading