Skip to content

Commit

Permalink
Move ProgramStructure out the BMv2 folder such that it can be used in…
Browse files Browse the repository at this point in the history
… other back ends.

Signed-off-by: fruffy <fruffy@nyu.edu>
  • Loading branch information
fruffy committed Jul 1, 2024
1 parent 2f5a4eb commit 98dfbfd
Show file tree
Hide file tree
Showing 27 changed files with 158 additions and 123 deletions.
13 changes: 13 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,24 @@ cc_library(
],
)

cc_library(
name = "p4c_backends_common_lib",
srcs = glob(["backends/common/*.cpp"]),
hdrs = glob(["backends/common/*.h"]),
deps = [
":ir_frontend_midend_control_plane",
":lib",
],
)

cc_library(
name = "p4c_bmv2_common_lib",
srcs = glob(["backends/bmv2/common/*.cpp"]),
hdrs = glob(["backends/bmv2/common/*.h"]),
deps = [
":ir_frontend_midend_control_plane",
":lib",
":p4c_backends_common_lib",
],
)

Expand All @@ -283,6 +294,7 @@ cc_library(
deps = [
":ir_frontend_midend_control_plane",
":lib",
":p4c_backends_common_lib",
":p4c_bmv2_common_lib",
],
)
Expand All @@ -305,6 +317,7 @@ cc_binary(
deps = [
":ir_frontend_midend_control_plane",
":lib",
":p4c_backends_common_lib",
":p4c_bmv2_common_lib",
":p4c_bmv2_simple_lib",
],
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ add_subdirectory (control-plane)
# share the same set of IR classes (frontend and backend). Backends such as the DPDK backend
# can introduce additional IR classes and cpp sources which need to be added to
# EXTENSION_IR_SOURCES.
add_subdirectory(backends/common)
if (ENABLE_BMV2)
add_subdirectory (backends/bmv2)
endif ()
Expand Down
8 changes: 1 addition & 7 deletions backends/bmv2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ set (BMV2_PSA_SWITCH_SRCS
psa_switch/midend.h
psa_switch/psaSwitch.cpp
psa_switch/psaSwitch.h
psa_switch/psaProgramStructure.cpp
psa_switch/psaProgramStructure.h
psa_switch/options.cpp
psa_switch/options.h
)
Expand All @@ -53,9 +51,7 @@ set (BMV2_BACKEND_COMMON_SRCS
common/header.cpp
common/helpers.cpp
common/lower.cpp
common/metermap.cpp
common/parser.cpp
common/programStructure.cpp
)

set (BMV2_BACKEND_COMMON_HDRS
Expand All @@ -73,18 +69,16 @@ set (BMV2_BACKEND_COMMON_HDRS
common/header.h
common/helpers.h
common/lower.h
common/metermap.h
common/midend.h
common/options.h
common/parser.h
common/programStructure.h
common/sharedActionSelectorCheck.h
)

set (IR_DEF_FILES ${IR_DEF_FILES} ${CMAKE_CURRENT_SOURCE_DIR}/bmv2.def PARENT_SCOPE)

add_library(bmv2backend STATIC ${BMV2_BACKEND_COMMON_SRCS})
target_link_libraries(bmv2backend ir-generated frontend)
target_link_libraries(bmv2backend ir-generated frontend backends-common)

add_executable(p4c-bm2-ss ${BMV2_SIMPLE_SWITCH_SRCS})
target_link_libraries (p4c-bm2-ss bmv2backend ${P4C_LIBRARIES} ${P4C_LIB_DEPS})
Expand Down
1 change: 0 additions & 1 deletion backends/bmv2/common/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ limitations under the License.
#include "lib/json.h"
#include "lib/log.h"
#include "lib/nullstream.h"
#include "metermap.h"
#include "midend/actionSynthesis.h"
#include "midend/convertEnums.h"
#include "midend/removeComplexExpressions.h"
Expand Down
8 changes: 4 additions & 4 deletions backends/bmv2/common/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
#ifndef BACKENDS_BMV2_COMMON_EXPRESSION_H_
#define BACKENDS_BMV2_COMMON_EXPRESSION_H_

#include "backends/common/programStructure.h"
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/p4/coreLibrary.h"
#include "frontends/p4/enumInstance.h"
Expand All @@ -26,7 +27,6 @@ limitations under the License.
#include "lib/big_int_util.h"
#include "lib/json.h"
#include "lower.h"
#include "programStructure.h"

namespace BMV2 {

Expand All @@ -51,7 +51,7 @@ class ArithmeticFixup : public Transform {
class ExpressionConverter : public Inspector {
P4::ReferenceMap *refMap;
P4::TypeMap *typeMap;
ProgramStructure *structure;
P4::ProgramStructure *structure;
P4::P4CoreLibrary &corelib;
cstring scalarsName;

Expand All @@ -68,8 +68,8 @@ class ExpressionConverter : public Inspector {
bool withConstantWidths{false};

public:
ExpressionConverter(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, ProgramStructure *structure,
cstring scalarsName)
ExpressionConverter(P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
P4::ProgramStructure *structure, cstring scalarsName)
: refMap(refMap),
typeMap(typeMap),
structure(structure),
Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/common/extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
#ifndef BACKENDS_BMV2_COMMON_EXTERN_H_
#define BACKENDS_BMV2_COMMON_EXTERN_H_

#include "backends/common/programStructure.h"
#include "frontends/p4/methodInstance.h"
#include "helpers.h"
#include "ir/ir.h"
#include "programStructure.h"

namespace BMV2 {

Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/common/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ limitations under the License.

#include "JsonObjects.h"
#include "backends/bmv2/common/options.h"
#include "backends/common/programStructure.h"
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/p4/typeMap.h"
#include "helpers.h"
#include "ir/ir.h"
#include "lib/json.h"
#include "programStructure.h"

namespace BMV2 {

Expand Down
17 changes: 14 additions & 3 deletions backends/bmv2/common/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
#define BACKENDS_BMV2_COMMON_HELPERS_H_

#include "JsonObjects.h"
#include "backends/common/programStructure.h"
#include "controlFlowGraph.h"
#include "expression.h"
#include "frontends/common/model.h"
Expand All @@ -26,7 +27,6 @@ limitations under the License.
#include "lib/cstring.h"
#include "lib/json.h"
#include "lib/ordered_map.h"
#include "programStructure.h"

namespace BMV2 {

Expand Down Expand Up @@ -58,6 +58,17 @@ class V1ModelProperties {
static const cstring validField;
};

// V1Model-specific blocks.
enum class BlockConverted {
None,
Parser,
Ingress,
Egress,
Deparser,
ChecksumCompute,
ChecksumVerify
};

namespace Standard {

/// We re-use as much code as possible between PSA and v1model. The two
Expand Down Expand Up @@ -268,7 +279,7 @@ struct ConversionContext {
/// Block currently being converted.
BlockConverted blockConverted;
/// ProgramStructure pointer.
ProgramStructure *structure;
P4::ProgramStructure *structure;
/// Expression converter is used in many places.
ExpressionConverter *conv;
/// Final json output.
Expand All @@ -286,7 +297,7 @@ struct ConversionContext {
}

ConversionContext(P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
const IR::ToplevelBlock *toplevel, ProgramStructure *structure,
const IR::ToplevelBlock *toplevel, P4::ProgramStructure *structure,
ExpressionConverter *conv, JsonObjects *json)
: refMap(refMap),
typeMap(typeMap),
Expand Down
10 changes: 5 additions & 5 deletions backends/bmv2/psa_switch/psaSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void PsaSwitchBackend::convert(const IR::ToplevelBlock *tlb) {
CHECK_NULL(tlb);
PsaCodeGenerator structure(refMap, typeMap);

auto parsePsaArch = new ParsePsaArchitecture(&structure);
auto parsePsaArch = new P4::ParsePsaArchitecture(&structure);
auto main = tlb->getMain();
if (!main) return;

Expand Down Expand Up @@ -298,16 +298,16 @@ void PsaSwitchBackend::convert(const IR::ToplevelBlock *tlb) {
program->apply(simplify);

// map IR node to compile-time allocated resource blocks.
toplevel->apply(*new BMV2::BuildResourceMap(&structure.resourceMap));
toplevel->apply(*new P4::BuildResourceMap(&structure.resourceMap));

main = toplevel->getMain();
if (!main) return; // no main
main->apply(*parsePsaArch);
if (::errorCount() > 0) return;
program = toplevel->getProgram();

PassManager toJson = {new DiscoverStructure(&structure),
new InspectPsaProgram(refMap, typeMap, &structure),
PassManager toJson = {new P4::DiscoverStructure(&structure),
new P4::InspectPsaProgram(refMap, typeMap, &structure),
new ConvertPsaToJson(refMap, typeMap, toplevel, json, &structure)};
for (const auto &pEnum : *enumMap) {
auto name = pEnum.first->getName();
Expand Down Expand Up @@ -681,7 +681,7 @@ void ExternConverter_InternetChecksum::convertExternInstance(UNUSED ConversionCo
cstring name = inst->controlPlaneName();
auto trim = inst->controlPlaneName().find(".");
auto block = inst->controlPlaneName().trim(trim);
auto psaStructure = static_cast<PsaProgramStructure *>(ctxt->structure);
auto psaStructure = static_cast<P4::PsaProgramStructure *>(ctxt->structure);
auto ingressParser = psaStructure->parsers.at("ingress"_cs)->controlPlaneName();
auto ingressDeparser = psaStructure->deparsers.at("ingress"_cs)->controlPlaneName();
auto egressParser = psaStructure->parsers.at("egress"_cs)->controlPlaneName();
Expand Down
15 changes: 8 additions & 7 deletions backends/bmv2/psa_switch/psaSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ limitations under the License.
#include "backends/bmv2/common/helpers.h"
#include "backends/bmv2/common/lower.h"
#include "backends/bmv2/common/parser.h"
#include "backends/bmv2/common/programStructure.h"
#include "backends/common/programStructure.h"
#include "backends/common/psaProgramStructure.h"
#include "frontends/common/constantFolding.h"
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/p4/coreLibrary.h"
Expand All @@ -39,14 +40,13 @@ limitations under the License.
#include "ir/ir.h"
#include "lib/big_int_util.h"
#include "lib/json.h"
#include "psaProgramStructure.h"

namespace BMV2 {

class PsaSwitchExpressionConverter : public ExpressionConverter {
public:
PsaSwitchExpressionConverter(P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
ProgramStructure *structure, cstring scalarsName)
P4::ProgramStructure *structure, cstring scalarsName)
: BMV2::ExpressionConverter(refMap, typeMap, structure, scalarsName) {}

void modelError(const char *format, const cstring field) {
Expand All @@ -56,7 +56,7 @@ class PsaSwitchExpressionConverter : public ExpressionConverter {

Util::IJson *convertParam(UNUSED const IR::Parameter *param, cstring fieldName) override {
cstring ptName = param->type->toString();
if (PsaProgramStructure::isCounterMetadata(ptName)) { // check if its counter metadata
if (P4::PsaProgramStructure::isCounterMetadata(ptName)) { // check if its counter metadata
auto jsn = new Util::JsonObject();
jsn->emplace("name"_cs, param->toString());
jsn->emplace("type"_cs, "hexstr");
Expand All @@ -77,7 +77,8 @@ class PsaSwitchExpressionConverter : public ExpressionConverter {
return nullptr;
}
return jsn;
} else if (PsaProgramStructure::isStandardMetadata(ptName)) { // check if its psa metadata
} else if (P4::PsaProgramStructure::isStandardMetadata(
ptName)) { // check if its psa metadata
auto jsn = new Util::JsonObject();

// encode the metadata type and field in json
Expand All @@ -94,10 +95,10 @@ class PsaSwitchExpressionConverter : public ExpressionConverter {
}
};

class PsaCodeGenerator : public PsaProgramStructure {
class PsaCodeGenerator : public P4::PsaProgramStructure {
public:
PsaCodeGenerator(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
: PsaProgramStructure(refMap, typeMap) {}
: P4::PsaProgramStructure(refMap, typeMap) {}

void create(ConversionContext *ctxt);
void createStructLike(ConversionContext *ctxt, const IR::Type_StructLike *st);
Expand Down
4 changes: 2 additions & 2 deletions backends/bmv2/simple_switch/simpleSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ void SimpleSwitchBackend::convert(const IR::ToplevelBlock *tlb) {
program->apply(simplify);

// map IR node to compile-time allocated resource blocks.
toplevel->apply(*new BMV2::BuildResourceMap(&structure->resourceMap));
toplevel->apply(*new P4::BuildResourceMap(&structure->resourceMap));

// field list and learn list ids in bmv2 are not consistent with ids for
// other objects: they need to start at 1 (not 0) since the id is also used
Expand All @@ -1223,7 +1223,7 @@ void SimpleSwitchBackend::convert(const IR::ToplevelBlock *tlb) {
if (!main) return; // no main
main->apply(*parseV1Arch);
program = toplevel->getProgram();
program->apply(DiscoverStructure(structure));
program->apply(P4::DiscoverStructure(structure));

/// generate error types
for (const auto &p : structure->errorCodesMap) {
Expand Down
4 changes: 2 additions & 2 deletions backends/bmv2/simple_switch/simpleSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ limitations under the License.
#include "backends/bmv2/common/header.h"
#include "backends/bmv2/common/options.h"
#include "backends/bmv2/common/parser.h"
#include "backends/bmv2/common/programStructure.h"
#include "backends/bmv2/common/sharedActionSelectorCheck.h"
#include "backends/common/programStructure.h"
#include "frontends/common/constantFolding.h"
#include "frontends/p4/evaluator/evaluator.h"
#include "frontends/p4/fromv1.0/v1model.h"
Expand All @@ -40,7 +40,7 @@ limitations under the License.

namespace BMV2 {

class V1ProgramStructure : public ProgramStructure {
class V1ProgramStructure : public P4::ProgramStructure {
public:
std::set<cstring> pipeline_controls;
std::set<cstring> non_pipeline_controls;
Expand Down
15 changes: 15 additions & 0 deletions backends/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(BACKENDS_COMMON_SRCS
metermap.cpp
programStructure.cpp
psaProgramStructure.cpp
)

add_library(backends-common STATIC ${BACKENDS_COMMON_SRCS})

target_link_libraries(backends-common
# These libraries are exposed by a header.
PUBLIC absl::bits
PUBLIC absl::strings
PUBLIC ${LIBGC_LIBRARIES}
PUBLIC ${P4C_LIBRARIES}
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include "metermap.h"
#include "backends/common/metermap.h"

namespace BMV2 {
namespace P4 {

/// @returns direct meter information from the direct meter map.
DirectMeterMap::DirectMeterInfo *DirectMeterMap::createInfo(const IR::IDeclaration *meter) {
Expand Down Expand Up @@ -90,4 +90,4 @@ void DirectMeterMap::setSize(const IR::IDeclaration *meter, unsigned size) {
info->tableSize = size;
}

} // namespace BMV2
} // namespace P4
Loading

0 comments on commit 98dfbfd

Please sign in to comment.