Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/komaljai/p4c into support_c…
Browse files Browse the repository at this point in the history
…onst_entries
  • Loading branch information
komaljai committed Jan 4, 2024
2 parents c111586 + aaa5c7d commit d78ee9f
Show file tree
Hide file tree
Showing 94 changed files with 707 additions and 581 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Set the required options for a (mostly) static release build.
if(BUILD_STATIC_RELEASE_SANS_GLIBC)
message(STATUS "Building static release binaries")
message(WARNING "glibc is linked dynamically in current static builds.")
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
# Link Boost statically
Expand Down
2 changes: 1 addition & 1 deletion backends/dpdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ add_custom_target (dpdk_runtime_dir
# placed in the build directory inside `control-plane` directory
add_custom_command(
OUTPUT ${DPDK_P4RUNTIME_INFO_GEN_SRCS} ${DPDK_P4RUNTIME_INFO_GEN_HDRS}
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
COMMAND ${Protobuf_PROTOC_EXECUTABLE}
--proto_path ${DPDK_P4RUNTIME_DIR}
--proto_path ${P4RUNTIME_STD_DIR}
--cpp_out ${CMAKE_CURRENT_BINARY_DIR}/p4/config
Expand Down
7 changes: 7 additions & 0 deletions backends/p4tools/modules/testgen/core/program_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class ProgramInfo : public ICastable {
/// The generated dcg.
const NodesCallGraph *dcg = nullptr;

/// A vector that maps the architecture parameters of each pipe to the corresponding
/// global architecture variables. For example, this map specifies which parameter of each pipe
/// refers to the input header.
// The arch map needs to be public to be subclassed.
/// @returns a reference to the architecture map defined in this target
[[nodiscard]] virtual const ArchSpec &getArchSpec() const = 0;

/// @returns the series of nodes that has been computed by this particular target.
[[nodiscard]] const std::vector<Continuation::Command> *getPipelineSequence() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ void ExprStepper::evalInternalExternMethodCall(const IR::MethodCallExpression *c
const ExecutionState &state, SmallStepEvaluator::Result &result) {
const auto *blockRef = args->at(0)->expression->checkedTo<IR::StringLiteral>();
const auto *block = state.findDecl(new IR::Path(blockRef->value));
const auto *archSpec = TestgenTarget::getArchSpec();
auto blockName = block->getName().name;
auto &nextState = state.clone();
auto canonicalName = getProgramInfo().getCanonicalBlockName(blockName);
const auto &archSpec = getProgramInfo().getArchSpec();
const auto *blockApply = block->to<IR::IApply>();
CHECK_NULL(blockApply);
const auto *blockParams = blockApply->getApplyParameters();
Expand All @@ -306,7 +306,7 @@ void ExprStepper::evalInternalExternMethodCall(const IR::MethodCallExpression *c
nextState.setProperty("inUndefinedState", false);
for (size_t paramIdx = 0; paramIdx < blockParams->size(); ++paramIdx) {
const auto *internalParam = blockParams->getParameter(paramIdx);
auto externalParamName = archSpec->getParamName(canonicalName, paramIdx);
auto externalParamName = archSpec.getParamName(canonicalName, paramIdx);
nextState.copyIn(TestgenTarget::get(), internalParam, externalParamName);
}
nextState.setProperty("inUndefinedState", currentTaint);
Expand All @@ -327,7 +327,7 @@ void ExprStepper::evalInternalExternMethodCall(const IR::MethodCallExpression *c
const ExecutionState &state, SmallStepEvaluator::Result &result) {
const auto *blockRef = args->at(0)->expression->checkedTo<IR::StringLiteral>();
const auto *block = state.findDecl(new IR::Path(blockRef->value));
const auto *archSpec = TestgenTarget::getArchSpec();
const auto &archSpec = getProgramInfo().getArchSpec();
auto blockName = block->getName().name;
auto &nextState = state.clone();
auto canonicalName = getProgramInfo().getCanonicalBlockName(blockName);
Expand All @@ -341,7 +341,7 @@ void ExprStepper::evalInternalExternMethodCall(const IR::MethodCallExpression *c
nextState.setProperty("inUndefinedState", false);
for (size_t paramIdx = 0; paramIdx < blockParams->size(); ++paramIdx) {
const auto *internalParam = blockParams->getParameter(paramIdx);
auto externalParamName = archSpec->getParamName(canonicalName, paramIdx);
auto externalParamName = archSpec.getParamName(canonicalName, paramIdx);
nextState.copyOut(internalParam, externalParamName);
}
nextState.setProperty("inUndefinedState", currentTaint);
Expand Down
2 changes: 0 additions & 2 deletions backends/p4tools/modules/testgen/core/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ const ProgramInfo *TestgenTarget::initProgram(const IR::P4Program *program) {
return get().initProgramImpl(program);
}

const ArchSpec *TestgenTarget::getArchSpec() { return get().getArchSpecImpl(); }

ExprStepper *TestgenTarget::getExprStepper(ExecutionState &state, AbstractSolver &solver,
const ProgramInfo &programInfo) {
return get().getExprStepperImpl(state, solver, programInfo);
Expand Down
10 changes: 0 additions & 10 deletions backends/p4tools/modules/testgen/core/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ class TestgenTarget : public Target {
static ExprStepper *getExprStepper(ExecutionState &state, AbstractSolver &solver,
const ProgramInfo &programInfo);

/// A vector that maps the architecture parameters of each pipe to the corresponding
/// global architecture variables. For example, this map specifies which parameter of each pipe
/// refers to the input header.
// The arch map needs to be public to be subclassed.
/// @returns a reference to the architecture map defined in this target
static const ArchSpec *getArchSpec();

protected:
/// @see @initProgram.
const ProgramInfo *initProgramImpl(const IR::P4Program *program) const;
Expand All @@ -72,9 +65,6 @@ class TestgenTarget : public Target {
virtual ExprStepper *getExprStepperImpl(ExecutionState &state, AbstractSolver &solver,
const ProgramInfo &programInfo) const = 0;

/// @see getArchSpec
[[nodiscard]] virtual const ArchSpec *getArchSpecImpl() const = 0;

explicit TestgenTarget(std::string deviceName, std::string archName);
};

Expand Down
5 changes: 2 additions & 3 deletions backends/p4tools/modules/testgen/targets/bmv2/cmd_stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ void Bmv2V1ModelCmdStepper::initializeTargetEnvironment(ExecutionState &nextStat
// error parser_error;
// bit<3> priority;

auto programInfo = getProgramInfo();
const auto *archSpec = TestgenTarget::getArchSpec();
const auto &programInfo = getProgramInfo();
const auto &target = TestgenTarget::get();
const auto *programmableBlocks = programInfo.getProgrammableBlocks();

Expand All @@ -73,7 +72,7 @@ void Bmv2V1ModelCmdStepper::initializeTargetEnvironment(ExecutionState &nextStat
size_t blockIdx = 0;
for (const auto &blockTuple : *programmableBlocks) {
const auto *typeDecl = blockTuple.second;
const auto *archMember = archSpec->getArchMember(blockIdx);
const auto *archMember = programInfo.getArchSpec().getArchMember(blockIdx);
nextState.initializeBlockParams(target, typeDecl, &archMember->blockParams);
blockIdx++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ void Bmv2V1ModelExprStepper::processClone(const ExecutionState &state,
const auto *applyBlock = typeDecl->checkedTo<IR::P4Control>();
const auto *params = applyBlock->getApplyParameters();
auto blockIndex = 2;
const auto *archSpec = TestgenTarget::getArchSpec();
const auto *archMember = archSpec->getArchMember(blockIndex);
const auto *archMember = progInfo->getArchSpec().getArchMember(blockIndex);
for (size_t paramIdx = 0; paramIdx < params->size(); ++paramIdx) {
const auto *param = params->getParameter(paramIdx);
const auto &archRef = archMember->blockParams.at(paramIdx);
Expand Down
44 changes: 33 additions & 11 deletions backends/p4tools/modules/testgen/targets/bmv2/program_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ Bmv2V1ModelProgramInfo::Bmv2V1ModelProgramInfo(

// Just concatenate everything together.
// Iterate through the (ordered) pipes of the target architecture.
const auto *archSpec = TestgenTarget::getArchSpec();
BUG_CHECK(archSpec->getArchVectorSize() == programmableBlocks.size(),
const auto &archSpec = getArchSpec();
BUG_CHECK(archSpec.getArchVectorSize() == programmableBlocks.size(),
"The BMV2 architecture requires %1% pipes (provided %2% pipes).",
archSpec->getArchVectorSize(), programmableBlocks.size());
archSpec.getArchVectorSize(), programmableBlocks.size());

/// Compute the series of nodes corresponding to the in-order execution of top-level
/// pipeline-component instantiations. For a standard v1model, this produces
Expand Down Expand Up @@ -112,6 +112,8 @@ const IR::P4Table *Bmv2V1ModelProgramInfo::getTableofDirectExtern(
return it->second;
}

const ArchSpec &Bmv2V1ModelProgramInfo::getArchSpec() const { return ARCH_SPEC; }

const ordered_map<cstring, const IR::Type_Declaration *>
*Bmv2V1ModelProgramInfo::getProgrammableBlocks() const {
return &programmableBlocks;
Expand All @@ -123,8 +125,6 @@ int Bmv2V1ModelProgramInfo::getGress(const IR::Type_Declaration *decl) const {

std::vector<Continuation::Command> Bmv2V1ModelProgramInfo::processDeclaration(
const IR::Type_Declaration *typeDecl, size_t blockIdx) const {
// Get the architecture specification for this target.
const auto *archSpec = TestgenTarget::getArchSpec();
const auto &options = TestgenOptions::get();
// Collect parameters.
const auto *applyBlock = typeDecl->to<IR::IApply>();
Expand All @@ -133,7 +133,7 @@ std::vector<Continuation::Command> Bmv2V1ModelProgramInfo::processDeclaration(
typeDecl->node_type_name());
}
// Retrieve the current canonical pipe in the architecture spec using the pipe index.
const auto *archMember = archSpec->getArchMember(blockIdx);
const auto *archMember = getArchSpec().getArchMember(blockIdx);

std::vector<Continuation::Command> cmds;
// Copy-in.
Expand Down Expand Up @@ -238,9 +238,9 @@ const IR::PathExpression *Bmv2V1ModelProgramInfo::getBlockParam(cstring blockLab
paramType = resolveProgramType(program, tn);
}

const auto *archSpec = TestgenTarget::getArchSpec();
auto archIndex = archSpec->getBlockIndex(blockLabel);
auto archRef = archSpec->getParamName(archIndex, paramIndex);
const auto &archSpec = getArchSpec();
auto archIndex = archSpec.getBlockIndex(blockLabel);
auto archRef = archSpec.getParamName(archIndex, paramIndex);
return new IR::PathExpression(paramType, new IR::Path(archRef));
}

Expand All @@ -255,10 +255,32 @@ const IR::Member *Bmv2V1ModelProgramInfo::getParserParamVar(const IR::P4Parser *
const auto *paramString = parser->getApplyParameters()->parameters.at(paramIndex);
structLabel = paramString->name;
} else {
const auto *archSpec = TestgenTarget::getArchSpec();
structLabel = archSpec->getParamName("Parser", paramIndex);
structLabel = ARCH_SPEC.getParamName("Parser", paramIndex);
}
return new IR::Member(type, new IR::PathExpression(structLabel), paramLabel);
}

const ArchSpec Bmv2V1ModelProgramInfo::ARCH_SPEC =
ArchSpec("V1Switch", {// parser Parser<H, M>(packet_in b,
// out H parsedHdr,
// inout M meta,
// inout standard_metadata_t standard_metadata);
{"Parser", {nullptr, "*hdr", "*meta", "*standard_metadata"}},
// control VerifyChecksum<H, M>(inout H hdr,
// inout M meta);
{"VerifyChecksum", {"*hdr", "*meta"}},
// control Ingress<H, M>(inout H hdr,
// inout M meta,
// inout standard_metadata_t standard_metadata);
{"Ingress", {"*hdr", "*meta", "*standard_metadata"}},
// control Egress<H, M>(inout H hdr,
// inout M meta,
// inout standard_metadata_t standard_metadata);
{"Egress", {"*hdr", "*meta", "*standard_metadata"}},
// control ComputeChecksum<H, M>(inout H hdr,
// inout M meta);
{"ComputeChecksum", {"*hdr", "*meta"}},
// control Deparser<H>(packet_out b, in H hdr);
{"Deparser", {nullptr, "*hdr"}}});

} // namespace P4Tools::P4Testgen::Bmv2
6 changes: 6 additions & 0 deletions backends/p4tools/modules/testgen/targets/bmv2/program_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class Bmv2V1ModelProgramInfo : public ProgramInfo {
/// @returns the table associated with the direct extern
const IR::P4Table *getTableofDirectExtern(const IR::IDeclaration *directExternDecl) const;

/// @see ProgramInfo::getArchSpec
[[nodiscard]] const ArchSpec &getArchSpec() const override;

/// @returns the programmable blocks of the program. Should be 6.
[[nodiscard]] const ordered_map<cstring, const IR::Type_Declaration *> *getProgrammableBlocks()
const;
Expand Down Expand Up @@ -74,6 +77,9 @@ class Bmv2V1ModelProgramInfo : public ProgramInfo {
/// variable.
static const IR::Member *getParserParamVar(const IR::P4Parser *parser, const IR::Type *type,
size_t paramIndex, cstring paramLabel);

/// @see ProgramInfo::getArchSpec
static const ArchSpec ARCH_SPEC;
};

} // namespace P4Tools::P4Testgen::Bmv2
Expand Down
27 changes: 1 addition & 26 deletions backends/p4tools/modules/testgen/targets/bmv2/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const Bmv2V1ModelProgramInfo *Bmv2V1ModelTestgenTarget::initProgramImpl(
for (size_t idx = 0; idx < blocks.size(); ++idx) {
const auto *declType = blocks.at(idx);

auto canonicalName = ARCH_SPEC.getArchMember(idx)->blockName;
auto canonicalName = Bmv2V1ModelProgramInfo::ARCH_SPEC.getArchMember(idx)->blockName;
programmableBlocks.emplace(canonicalName, declType);

if (idx < 3) {
Expand Down Expand Up @@ -83,29 +83,4 @@ Bmv2V1ModelExprStepper *Bmv2V1ModelTestgenTarget::getExprStepperImpl(
return new Bmv2V1ModelExprStepper(state, solver, programInfo);
}

const ArchSpec Bmv2V1ModelTestgenTarget::ARCH_SPEC =
ArchSpec("V1Switch", {// parser Parser<H, M>(packet_in b,
// out H parsedHdr,
// inout M meta,
// inout standard_metadata_t standard_metadata);
{"Parser", {nullptr, "*hdr", "*meta", "*standard_metadata"}},
// control VerifyChecksum<H, M>(inout H hdr,
// inout M meta);
{"VerifyChecksum", {"*hdr", "*meta"}},
// control Ingress<H, M>(inout H hdr,
// inout M meta,
// inout standard_metadata_t standard_metadata);
{"Ingress", {"*hdr", "*meta", "*standard_metadata"}},
// control Egress<H, M>(inout H hdr,
// inout M meta,
// inout standard_metadata_t standard_metadata);
{"Egress", {"*hdr", "*meta", "*standard_metadata"}},
// control ComputeChecksum<H, M>(inout H hdr,
// inout M meta);
{"ComputeChecksum", {"*hdr", "*meta"}},
// control Deparser<H>(packet_out b, in H hdr);
{"Deparser", {nullptr, "*hdr"}}});

const ArchSpec *Bmv2V1ModelTestgenTarget::getArchSpecImpl() const { return &ARCH_SPEC; }

} // namespace P4Tools::P4Testgen::Bmv2
4 changes: 0 additions & 4 deletions backends/p4tools/modules/testgen/targets/bmv2/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ class Bmv2V1ModelTestgenTarget : public TestgenTarget {
Bmv2V1ModelExprStepper *getExprStepperImpl(ExecutionState &state, AbstractSolver &solver,
const ProgramInfo &programInfo) const override;

[[nodiscard]] const ArchSpec *getArchSpecImpl() const override;

private:
Bmv2V1ModelTestgenTarget();

static const ArchSpec ARCH_SPEC;
};

} // namespace P4Tools::P4Testgen::Bmv2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function(validate_protobuf testfile testfolder)
file(APPEND ${testfile} "for item in \${txtpbfiles[@]}\n")
file(APPEND ${testfile} "do\n")
file(APPEND ${testfile} "\techo \"Found \${item}\"\n")
file(APPEND ${testfile} "\t${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${CMAKE_CURRENT_LIST_DIR}/../proto --proto_path=${P4RUNTIME_STD_DIR} --proto_path=${P4C_SOURCE_DIR}/control-plane --encode=p4testgen.TestCase p4testgen.proto < \${item} > /dev/null\n")
file(APPEND ${testfile} "\t${PROTOC_BINARY} -I${Protobuf_INCLUDE_DIR} -I${CMAKE_CURRENT_LIST_DIR}/../proto -I${P4RUNTIME_STD_DIR} -I${P4C_SOURCE_DIR}/control-plane --encode=p4testgen.TestCase p4testgen.proto < \${item} > /dev/null\n")
file(APPEND ${testfile} "done\n")
endfunction(validate_protobuf)

Expand All @@ -60,7 +60,7 @@ function(validate_protobuf_ir testfile testfolder)
file(APPEND ${testfile} "for item in \${txtpbfiles[@]}\n")
file(APPEND ${testfile} "do\n")
file(APPEND ${testfile} "\techo \"Found \${item}\"\n")
file(APPEND ${testfile} "\t${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${CMAKE_CURRENT_LIST_DIR}/../proto --proto_path=${P4RUNTIME_STD_DIR} --proto_path=${P4C_SOURCE_DIR} --proto_path=${P4C_SOURCE_DIR}/control-plane --encode=p4testgen_ir.TestCase p4testgen_ir.proto < \${item} > /dev/null\n")
file(APPEND ${testfile} "\t${PROTOC_BINARY} -I${Protobuf_INCLUDE_DIR} -I${CMAKE_CURRENT_LIST_DIR}/../proto -I${P4RUNTIME_STD_DIR} -I${P4C_SOURCE_DIR} -I${P4C_SOURCE_DIR}/control-plane --encode=p4testgen_ir.TestCase p4testgen_ir.proto < \${item} > /dev/null\n")
file(APPEND ${testfile} "done\n")
endfunction(validate_protobuf_ir)

Expand Down
5 changes: 2 additions & 3 deletions backends/p4tools/modules/testgen/targets/ebpf/cmd_stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ const EBPFProgramInfo &EBPFCmdStepper::getProgramInfo() const {
}

void EBPFCmdStepper::initializeTargetEnvironment(ExecutionState &nextState) const {
auto programInfo = getProgramInfo();
const auto *archSpec = TestgenTarget::getArchSpec();
const auto &programInfo = getProgramInfo();
const auto &target = TestgenTarget::get();
const auto *programmableBlocks = programInfo.getProgrammableBlocks();

Expand All @@ -45,7 +44,7 @@ void EBPFCmdStepper::initializeTargetEnvironment(ExecutionState &nextState) cons
size_t blockIdx = 0;
for (const auto &blockTuple : *programmableBlocks) {
const auto *typeDecl = blockTuple.second;
const auto *archMember = archSpec->getArchMember(blockIdx);
const auto *archMember = programInfo.getArchSpec().getArchMember(blockIdx);
nextState.initializeBlockParams(target, typeDecl, &archMember->blockParams);
blockIdx++;
}
Expand Down
Loading

0 comments on commit d78ee9f

Please sign in to comment.