Skip to content

Commit

Permalink
Removed inheritance from ProgramStructure in CodeGenerator Classes.
Browse files Browse the repository at this point in the history
- Pass ProgramStructure as argument in CodeGenerator methods.

Signed-off-by: Rupesh Chiluka <rchiluka@marvell.com>
  • Loading branch information
rupesh-chiluka-marvell committed Jul 28, 2024
1 parent 2eef3cb commit c58d7f1
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 68 deletions.
2 changes: 0 additions & 2 deletions backends/bmv2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ set (PORTABLE_COMMON
portable_common/options.h
portable_common/midend.cpp
portable_common/midend.h
portable_common/portableProgramStructure.cpp
portable_common/portableProgramStructure.h
)

set (BMV2_PSA_SWITCH_SRCS
Expand Down
7 changes: 4 additions & 3 deletions backends/bmv2/pna_nic/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class PnaEnumOn32Bits : public P4::ChooseEnumRepresentation {
explicit PnaEnumOn32Bits(cstring filename) : filename(filename) {}
};

PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream) : PortableMidEnd(options) {
PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream)
: PortableMidEnd(options) {
auto convertEnums = new P4::ConvertEnums(&refMap, &typeMap, new PnaEnumOn32Bits("pna.p4"_cs));
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap);
std::function<bool(const Context *, const IR::Expression *)> policy =
Expand Down Expand Up @@ -141,8 +142,8 @@ PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream) :
new P4::MoveDeclarations(), // more may have been introduced
new P4::ConstantFolding(&refMap, &typeMap),
new P4::LocalCopyPropagation(&refMap, &typeMap, nullptr, policy),
new PassRepeated({new P4::ConstantFolding(&refMap, &typeMap),
new P4::StrengthReduction(&typeMap)}),
new PassRepeated(
{new P4::ConstantFolding(&refMap, &typeMap), new P4::StrengthReduction(&typeMap)}),
new P4::MoveDeclarations(),
new P4::ValidateTableProperties({"pna_implementation"_cs, "pna_direct_counter"_cs,
"pna_direct_meter"_cs, "pna_idle_timeout"_cs,
Expand Down
4 changes: 1 addition & 3 deletions backends/bmv2/pna_nic/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
#include "frontends/common/parser_options.h"
#include "lib/exename.h"

namespace BMV2 {

} // namespace BMV2
namespace BMV2 {} // namespace BMV2
2 changes: 1 addition & 1 deletion backends/bmv2/pna_nic/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
#ifndef BACKENDS_BMV2_PNA_NIC_OPTIONS_H_
#define BACKENDS_BMV2_PNA_NIC_OPTIONS_H_

#include "backends/bmv2/portable_common/options.h"
#include "backends/bmv2/pna_nic/midend.h"
#include "backends/bmv2/portable_common/options.h"

namespace BMV2 {

Expand Down
11 changes: 7 additions & 4 deletions backends/bmv2/pna_nic/pnaNic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace BMV2 {

using namespace P4::literals;

void PnaCodeGenerator::create(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PnaCodeGenerator::create(ConversionContext *ctxt, P4::PortableProgramStructure *structure) {
createTypes(ctxt, structure);
createHeaders(ctxt, structure);
createScalars(ctxt, structure);
Expand All @@ -35,21 +35,24 @@ void PnaCodeGenerator::create(ConversionContext *ctxt, PortableProgramStructure
createGlobals();
}

void PnaCodeGenerator::createParsers(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PnaCodeGenerator::createParsers(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
{
auto cvt = new ParserConverter(ctxt, "main_parser"_cs);
auto main_control = structure->parsers.at("main_parser"_cs);
main_control->apply(*cvt);
}
}

void PnaCodeGenerator::createControls(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PnaCodeGenerator::createControls(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
auto cvt = new BMV2::ControlConverter<Standard::Arch::PNA>(ctxt, "main_control"_cs, true);
auto main_control = structure->pipelines.at("main_control"_cs);
main_control->apply(*cvt);
}

void PnaCodeGenerator::createDeparsers(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PnaCodeGenerator::createDeparsers(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
{
auto cvt = new DeparserConverter(ctxt, "main_deparser"_cs);
auto main_control = structure->deparsers.at("main_deparser"_cs);
Expand Down
8 changes: 4 additions & 4 deletions backends/bmv2/pna_nic/pnaNic.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ class PnaCodeGenerator : public PortableCodeGenerator {
// PnaCodeGenerator(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
// : PortableCodeGenerator(refMap, typeMap) {}

void create(ConversionContext *ctxt, PortableProgramStructure *structure);
void createParsers(ConversionContext *ctxt, PortableProgramStructure *structure);
void createControls(ConversionContext *ctxt, PortableProgramStructure *structure);
void createDeparsers(ConversionContext *ctxt, PortableProgramStructure *structure);
void create(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createParsers(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createControls(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createDeparsers(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
};

class ConvertPnaToJson : public Inspector {
Expand Down
11 changes: 5 additions & 6 deletions backends/bmv2/pna_nic/pnaProgramStructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
#ifndef BACKENDS_BMV2_PNA_NIC_PNAPROGRAMSTRUCTURE_H_
#define BACKENDS_BMV2_PNA_NIC_PNAPROGRAMSTRUCTURE_H_

#include "backends/bmv2/portable_common/portableProgramStructure.h"
#include "backends/common/portableProgramStructure.h"

/// TODO: this is not really specific to BMV2, it should reside somewhere else
namespace BMV2 {
Expand All @@ -28,7 +28,7 @@ enum pna_block_t {
MAIN_DEPARSER,
};

class PnaProgramStructure : public PortableProgramStructure {
class PnaProgramStructure : public P4::PortableProgramStructure {
public:
/// Architecture related information
ordered_map<const IR::Node *, pna_block_t> block_type;
Expand All @@ -50,7 +50,7 @@ class PnaProgramStructure : public PortableProgramStructure {
}
};

class ParsePnaArchitecture : public ParsePortableArchitecture {
class ParsePnaArchitecture : public P4::ParsePortableArchitecture {
PnaProgramStructure *structure;

public:
Expand All @@ -74,13 +74,12 @@ class ParsePnaArchitecture : public ParsePortableArchitecture {
}
};

class InspectPnaProgram : public InspectPortableProgram {
class InspectPnaProgram : public P4::InspectPortableProgram {
PnaProgramStructure *pinfo;

public:
InspectPnaProgram(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, PnaProgramStructure *pinfo)
: InspectPortableProgram(refMap, typeMap),
pinfo(pinfo) {
: InspectPortableProgram(refMap, typeMap), pinfo(pinfo) {
CHECK_NULL(pinfo);
setName("InspectPnaProgram");
}
Expand Down
5 changes: 2 additions & 3 deletions backends/bmv2/portable_common/midend.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ namespace BMV2 {
class PortableMidEnd : public MidEnd {
public:
// If p4c is run with option '--listMidendPasses', outStream is used for printing passes names
explicit PortableMidEnd(CompilerOptions &options)
: MidEnd(options) {}
explicit PortableMidEnd(CompilerOptions &options) : MidEnd(options) {}
};

} // namespace BMV2

#endif /* BACKENDS_BMV2_PSA_SWIBACKENDS_BMV2_PORTABLE_COMMON_MIDEND_H_TCH_MIDEND_H_ */
#endif /* BACKENDS_BMV2_PORTABLE_COMMON_MIDEND_H_ */
1 change: 0 additions & 1 deletion backends/bmv2/portable_common/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace BMV2 {

class PortableOptions : public BMV2Options {
public:

/// Process the command line arguments and set options accordingly.
std::vector<const char *> *process(int argc, char *const argv[]) override;
};
Expand Down
14 changes: 9 additions & 5 deletions backends/bmv2/portable_common/portable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace BMV2 {
using namespace P4::literals;

void PortableCodeGenerator::createStructLike(ConversionContext *ctxt, const IR::Type_StructLike *st,
PortableProgramStructure *structure) {
P4::PortableProgramStructure *structure) {
CHECK_NULL(st);
cstring name = st->controlPlaneName();
unsigned max_length = 0; // for variable-sized headers
Expand Down Expand Up @@ -87,7 +87,8 @@ void PortableCodeGenerator::createStructLike(ConversionContext *ctxt, const IR::
ctxt->json->add_header_type(name, fields, max_length_bytes);
}

void PortableCodeGenerator::createTypes(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PortableCodeGenerator::createTypes(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
for (auto kv : structure->header_types) createStructLike(ctxt, kv.second, structure);
for (auto kv : structure->metadata_types) createStructLike(ctxt, kv.second, structure);
for (auto kv : structure->header_union_types) {
Expand All @@ -108,7 +109,8 @@ void PortableCodeGenerator::createTypes(ConversionContext *ctxt, PortableProgram
// add enums to json
}

void PortableCodeGenerator::createScalars(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PortableCodeGenerator::createScalars(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
auto name = structure->scalars.begin()->first;
ctxt->json->add_header("scalars_t"_cs, name);
ctxt->json->add_header_type("scalars_t"_cs);
Expand Down Expand Up @@ -144,7 +146,8 @@ void PortableCodeGenerator::createScalars(ConversionContext *ctxt, PortableProgr
}
}

void PortableCodeGenerator::createHeaders(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PortableCodeGenerator::createHeaders(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
for (auto kv : structure->headers) {
auto type = kv.second->type->to<IR::Type_StructLike>();
ctxt->json->add_header(type->controlPlaneName(), kv.second->name);
Expand Down Expand Up @@ -187,7 +190,8 @@ void PortableCodeGenerator::createExterns() {
// add extern_instances to json
}

void PortableCodeGenerator::createActions(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PortableCodeGenerator::createActions(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
auto cvt = new ActionConverter(ctxt, true);
for (auto it : structure->actions) {
auto action = it.first;
Expand Down
16 changes: 8 additions & 8 deletions backends/bmv2/portable_common/portable.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.
#include "backends/bmv2/common/helpers.h"
#include "backends/bmv2/common/lower.h"
#include "backends/bmv2/common/parser.h"
#include "backends/common/portableProgramStructure.h"
#include "backends/common/programStructure.h"
#include "frontends/common/constantFolding.h"
#include "frontends/common/resolveReferences/referenceMap.h"
Expand All @@ -39,24 +40,23 @@ limitations under the License.
#include "ir/ir.h"
#include "lib/big_int_util.h"
#include "lib/json.h"
#include "portableProgramStructure.h"

namespace BMV2 {

class PortableCodeGenerator {
public:
// PortableCodeGenerator() {}
// : PortableProgramStructure(refMap, typeMap) {}
// : PortableProgramStructure(refMap, typeMap) {}

unsigned error_width = 32;

void createStructLike(ConversionContext *ctxt, const IR::Type_StructLike *st,
PortableProgramStructure *structure);
void createTypes(ConversionContext *ctxt, PortableProgramStructure *structure);
void createHeaders(ConversionContext *ctxt, PortableProgramStructure *structure);
void createScalars(ConversionContext *ctxt, PortableProgramStructure *structure);
P4::PortableProgramStructure *structure);
void createTypes(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createHeaders(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createScalars(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createExterns();
void createActions(ConversionContext *ctxt, PortableProgramStructure *structure);
void createActions(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createGlobals();
cstring convertHashAlgorithm(cstring algo);
};
Expand Down
4 changes: 1 addition & 3 deletions backends/bmv2/psa_switch/options.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "options.h"

namespace BMV2 {

} // namespace BMV2
namespace BMV2 {} // namespace BMV2
11 changes: 7 additions & 4 deletions backends/bmv2/psa_switch/psaSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace BMV2 {

using namespace P4::literals;

void PsaCodeGenerator::create(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PsaCodeGenerator::create(ConversionContext *ctxt, P4::PortableProgramStructure *structure) {
createTypes(ctxt, structure);
createHeaders(ctxt, structure);
createScalars(ctxt, structure);
Expand All @@ -35,7 +35,8 @@ void PsaCodeGenerator::create(ConversionContext *ctxt, PortableProgramStructure
createGlobals();
}

void PsaCodeGenerator::createParsers(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PsaCodeGenerator::createParsers(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
{
auto cvt = new ParserConverter(ctxt, "ingress_parser"_cs);
auto ingress = structure->parsers.at("ingress"_cs);
Expand All @@ -48,7 +49,8 @@ void PsaCodeGenerator::createParsers(ConversionContext *ctxt, PortableProgramStr
}
}

void PsaCodeGenerator::createControls(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PsaCodeGenerator::createControls(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
auto cvt = new BMV2::ControlConverter<Standard::Arch::PSA>(ctxt, "ingress"_cs, true);
auto ingress = structure->pipelines.at("ingress"_cs);
ingress->apply(*cvt);
Expand All @@ -58,7 +60,8 @@ void PsaCodeGenerator::createControls(ConversionContext *ctxt, PortableProgramSt
egress->apply(*cvt);
}

void PsaCodeGenerator::createDeparsers(ConversionContext *ctxt, PortableProgramStructure *structure) {
void PsaCodeGenerator::createDeparsers(ConversionContext *ctxt,
P4::PortableProgramStructure *structure) {
{
auto cvt = new DeparserConverter(ctxt, "ingress_deparser"_cs);
auto ingress = structure->deparsers.at("ingress"_cs);
Expand Down
8 changes: 4 additions & 4 deletions backends/bmv2/psa_switch/psaSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ class PsaCodeGenerator : public PortableCodeGenerator {
// PsaCodeGenerator(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
// : PortableCodeGenerator(refMap, typeMap) {}

void create(ConversionContext *ctxt, PortableProgramStructure *structure);
void createParsers(ConversionContext *ctxt, PortableProgramStructure *structure);
void createControls(ConversionContext *ctxt, PortableProgramStructure *structure);
void createDeparsers(ConversionContext *ctxt, PortableProgramStructure *structure);
void create(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createParsers(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createControls(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
void createDeparsers(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
};

class ConvertPsaToJson : public Inspector {
Expand Down
2 changes: 1 addition & 1 deletion backends/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(BACKENDS_COMMON_SRCS
metermap.cpp
programStructure.cpp
../bmv2/portable_common/portableProgramStructure.cpp
portableProgramStructure.cpp
psaProgramStructure.cpp
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

#include "portableProgramStructure.h"

namespace BMV2 {
namespace P4 {

using namespace P4::literals;

Expand All @@ -38,4 +38,4 @@ bool ParsePortableArchitecture::preorder(const IR::ToplevelBlock *block) {
return false;
}

} // namespace BMV2
} // namespace P4
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef BACKENDS_BMV2_PORTABLECOMMON_PROGRAMSTRUCTURE_H_
#define BACKENDS_BMV2_PORTABLECOMMON_PROGRAMSTRUCTURE_H_
#ifndef BACKENDS_COMMON_PORTABLEPROGRAMSTRUCTURE_H_
#define BACKENDS_COMMON_PORTABLEPROGRAMSTRUCTURE_H_

#include "backends/bmv2/common/backend.h"
#include "backends/common/programStructure.h"
#include "ir/ir.h"
#include "lib/cstring.h"

/// TODO: this is not really specific to BMV2, it should reside somewhere else.
namespace BMV2 {
namespace P4 {

class PortableProgramStructure : public P4::ProgramStructure {
public:
P4::ReferenceMap *refMap;
P4::TypeMap *typeMap;

/// We place scalar user metadata fields (i.e., bit<>, bool)
/// in the scalars map.
ordered_map<cstring, const IR::Declaration_Variable *> scalars;
unsigned scalars_width = 0;
unsigned error_width = 32;
// unsigned error_width = 32;
unsigned bool_width = 1;

ordered_map<cstring, const IR::Type_Header *> header_types;
Expand Down Expand Up @@ -78,7 +78,6 @@ class PortableProgramStructure : public P4::ProgramStructure {

class ParsePortableArchitecture : public Inspector {
public:

bool preorder(const IR::ToplevelBlock *block) override;
};

Expand All @@ -97,6 +96,6 @@ class InspectPortableProgram : public Inspector {
bool isHeaders(const IR::Type_StructLike *st);
};

} // namespace BMV2
} // namespace P4

#endif /* BACKENDS_BMV2_PORTABLECOMMON_PROGRAMSTRUCTURE_H_ */
#endif /* BACKENDS_COMMON_PORTABLEPROGRAMSTRUCTURE_H_ */
Loading

0 comments on commit c58d7f1

Please sign in to comment.