Skip to content

Commit

Permalink
Remove ReferenceMap from majority of midend passes (#4936)
Browse files Browse the repository at this point in the history
* Remove refmap / typemap from ReplaceSelectRange: it does not use it at all

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Swift EliminateTypedef to ResolutionContext

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Ensure PassManager always forward proper context

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Ensure nested context is correctly propagated

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Get rid of refmap from SimplifyParsers

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Eliminate refmap in EliminateTuples

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Eliminate refmap in SimplifySelectList

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from SimplifySelectCases

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from EliminateSerEnums

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from EliminateNewtype

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from ConvertEnums

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from HandleNoMatch

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from RemoveMiss

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove more refmaps

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from TableHit

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from SimplifyKey

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from EliminateInvalidHeaders

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from OrderArguments

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Ensure Predication uses up-to-date name generator

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Remove refmap from ExpandEmit and ExpandLookahead

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Unbreak test tools

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Typo

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

---------

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
  • Loading branch information
asl authored Oct 2, 2024
1 parent 520b792 commit 49e835c
Show file tree
Hide file tree
Showing 61 changed files with 452 additions and 495 deletions.
45 changes: 22 additions & 23 deletions backends/bmv2/pna_nic/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class PnaEnumOn32Bits : public P4::ChooseEnumRepresentation {

PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream)
: PortableMidEnd(options) {
auto convertEnums = new P4::ConvertEnums(&refMap, &typeMap, new PnaEnumOn32Bits("pna.p4"_cs));
auto convertEnums = new P4::ConvertEnums(&typeMap, new PnaEnumOn32Bits("pna.p4"_cs));
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap);
std::function<bool(const Context *, const IR::Expression *)> policy =
[=](const Context *, const IR::Expression *e) -> bool {
Expand All @@ -109,36 +109,35 @@ PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream)
};
if (BMV2::PnaNicContext::get().options().loadIRFromJson == false) {
addPasses({
options.ndebug ? new P4::RemoveAssertAssume(&refMap, &typeMap) : nullptr,
options.ndebug ? new P4::RemoveAssertAssume(&typeMap) : nullptr,
new CheckUnsupported(),
new P4::RemoveMiss(&refMap, &typeMap),
new P4::EliminateNewtype(&refMap, &typeMap),
new P4::EliminateInvalidHeaders(&refMap, &typeMap),
new P4::EliminateSerEnums(&refMap, &typeMap),
new P4::RemoveMiss(&typeMap),
new P4::EliminateNewtype(&typeMap),
new P4::EliminateInvalidHeaders(&typeMap),
new P4::EliminateSerEnums(&typeMap),
convertEnums,
[this, convertEnums]() { enumMap = convertEnums->getEnumMapping(); },
new P4::OrderArguments(&refMap, &typeMap),
new P4::OrderArguments(&typeMap),
new P4::TypeChecking(&refMap, &typeMap),
new P4::SimplifyKey(
&refMap, &typeMap,
new P4::OrPolicy(new P4::IsValid(&refMap, &typeMap), new P4::IsMask())),
new P4::SimplifyKey(&typeMap,
new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsMask())),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::StrengthReduction(&typeMap),
new P4::SimplifySelectCases(&refMap, &typeMap, true), // require constant keysets
new P4::ExpandLookahead(&refMap, &typeMap),
new P4::ExpandEmit(&refMap, &typeMap),
new P4::SimplifyParsers(&refMap),
new P4::SimplifySelectCases(&typeMap, true), // require constant keysets
new P4::ExpandLookahead(&typeMap),
new P4::ExpandEmit(&typeMap),
new P4::SimplifyParsers(),
new P4::StrengthReduction(&typeMap),
new P4::EliminateTuples(&refMap, &typeMap),
new P4::SimplifyComparisons(&refMap, &typeMap),
new P4::EliminateTuples(&typeMap),
new P4::SimplifyComparisons(&typeMap),
new P4::CopyStructures(&refMap, &typeMap),
new P4::NestedStructs(&refMap, &typeMap),
new P4::SimplifySelectList(&refMap, &typeMap),
new P4::RemoveSelectBooleans(&refMap, &typeMap),
new P4::FlattenHeaders(&refMap, &typeMap),
new P4::SimplifySelectList(&typeMap),
new P4::RemoveSelectBooleans(&typeMap),
new P4::FlattenHeaders(&typeMap),
new P4::FlattenInterfaceStructs(&refMap, &typeMap),
new P4::ReplaceSelectRange(&refMap, &typeMap),
new P4::Predication(&refMap),
new P4::ReplaceSelectRange(),
new P4::Predication(),
new P4::MoveDeclarations(), // more may have been introduced
new P4::ConstantFolding(&refMap, &typeMap),
new P4::LocalCopyPropagation(&refMap, &typeMap, nullptr, policy),
Expand All @@ -150,10 +149,10 @@ PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream)
"size"_cs}),
new P4::SimplifyControlFlow(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&refMap, &typeMap),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&refMap, &typeMap),
new P4::MoveActionsToTables(&refMap, &typeMap),
new P4::RemoveLeftSlices(&refMap, &typeMap),
new P4::RemoveLeftSlices(&typeMap),
new P4::TypeChecking(&refMap, &typeMap),
new P4::MidEndLast(),
evaluator,
Expand Down
45 changes: 22 additions & 23 deletions backends/bmv2/psa_switch/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class PsaEnumOn32Bits : public P4::ChooseEnumRepresentation {

PsaSwitchMidEnd::PsaSwitchMidEnd(CompilerOptions &options, std::ostream *outStream)
: PortableMidEnd(options) {
auto convertEnums = new P4::ConvertEnums(&refMap, &typeMap, new PsaEnumOn32Bits("psa.p4"_cs));
auto convertEnums = new P4::ConvertEnums(&typeMap, new PsaEnumOn32Bits("psa.p4"_cs));
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap);
std::function<bool(const Context *, const IR::Expression *)> policy =
[=](const Context *, const IR::Expression *e) -> bool {
Expand All @@ -109,36 +109,35 @@ PsaSwitchMidEnd::PsaSwitchMidEnd(CompilerOptions &options, std::ostream *outStre
};
if (BMV2::PsaSwitchContext::get().options().loadIRFromJson == false) {
addPasses({
options.ndebug ? new P4::RemoveAssertAssume(&refMap, &typeMap) : nullptr,
options.ndebug ? new P4::RemoveAssertAssume(&typeMap) : nullptr,
new CheckUnsupported(),
new P4::RemoveMiss(&refMap, &typeMap),
new P4::EliminateNewtype(&refMap, &typeMap),
new P4::EliminateInvalidHeaders(&refMap, &typeMap),
new P4::EliminateSerEnums(&refMap, &typeMap),
new P4::RemoveMiss(&typeMap),
new P4::EliminateNewtype(&typeMap),
new P4::EliminateInvalidHeaders(&typeMap),
new P4::EliminateSerEnums(&typeMap),
convertEnums,
[this, convertEnums]() { enumMap = convertEnums->getEnumMapping(); },
new P4::OrderArguments(&refMap, &typeMap),
new P4::OrderArguments(&typeMap),
new P4::TypeChecking(&refMap, &typeMap),
new P4::SimplifyKey(
&refMap, &typeMap,
new P4::OrPolicy(new P4::IsValid(&refMap, &typeMap), new P4::IsMask())),
new P4::SimplifyKey(&typeMap,
new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsMask())),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::StrengthReduction(&typeMap),
new P4::SimplifySelectCases(&refMap, &typeMap, true), // require constant keysets
new P4::ExpandLookahead(&refMap, &typeMap),
new P4::ExpandEmit(&refMap, &typeMap),
new P4::SimplifyParsers(&refMap),
new P4::SimplifySelectCases(&typeMap, true), // require constant keysets
new P4::ExpandLookahead(&typeMap),
new P4::ExpandEmit(&typeMap),
new P4::SimplifyParsers(),
new P4::StrengthReduction(&typeMap),
new P4::EliminateTuples(&refMap, &typeMap),
new P4::SimplifyComparisons(&refMap, &typeMap),
new P4::EliminateTuples(&typeMap),
new P4::SimplifyComparisons(&typeMap),
new P4::CopyStructures(&refMap, &typeMap),
new P4::NestedStructs(&refMap, &typeMap),
new P4::SimplifySelectList(&refMap, &typeMap),
new P4::RemoveSelectBooleans(&refMap, &typeMap),
new P4::FlattenHeaders(&refMap, &typeMap),
new P4::SimplifySelectList(&typeMap),
new P4::RemoveSelectBooleans(&typeMap),
new P4::FlattenHeaders(&typeMap),
new P4::FlattenInterfaceStructs(&refMap, &typeMap),
new P4::ReplaceSelectRange(&refMap, &typeMap),
new P4::Predication(&refMap),
new P4::ReplaceSelectRange(),
new P4::Predication(),
new P4::MoveDeclarations(), // more may have been introduced
new P4::ConstantFolding(&refMap, &typeMap),
new P4::LocalCopyPropagation(&refMap, &typeMap, nullptr, policy),
Expand All @@ -156,10 +155,10 @@ PsaSwitchMidEnd::PsaSwitchMidEnd(CompilerOptions &options, std::ostream *outStre
}),
new P4::SimplifyControlFlow(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&refMap, &typeMap),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&refMap, &typeMap),
new P4::MoveActionsToTables(&refMap, &typeMap),
new P4::RemoveLeftSlices(&refMap, &typeMap),
new P4::RemoveLeftSlices(&typeMap),
new P4::TypeChecking(&refMap, &typeMap),
new P4::MidEndLast(),
evaluator,
Expand Down
53 changes: 25 additions & 28 deletions backends/bmv2/simple_switch/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,50 +74,47 @@ SimpleSwitchMidEnd::SimpleSwitchMidEnd(CompilerOptions &options, std::ostream *o
: MidEnd(options) {
auto *evaluator = new P4::EvaluatorPass(&refMap, &typeMap);
if (!BMV2::SimpleSwitchContext::get().options().loadIRFromJson) {
auto *convertEnums =
new P4::ConvertEnums(&refMap, &typeMap, new EnumOn32Bits("v1model.p4"_cs));
auto *convertEnums = new P4::ConvertEnums(&typeMap, new EnumOn32Bits("v1model.p4"_cs));
addPasses(
{options.ndebug ? new P4::RemoveAssertAssume(&refMap, &typeMap) : nullptr,
{options.ndebug ? new P4::RemoveAssertAssume(&typeMap) : nullptr,
new P4::CheckTableSize(),
new CheckUnsupported(),
new P4::RemoveMiss(&refMap, &typeMap),
new P4::EliminateNewtype(&refMap, &typeMap),
new P4::EliminateInvalidHeaders(&refMap, &typeMap),
new P4::EliminateSerEnums(&refMap, &typeMap),
new P4::RemoveMiss(&typeMap),
new P4::EliminateNewtype(&typeMap),
new P4::EliminateInvalidHeaders(&typeMap),
new P4::EliminateSerEnums(&typeMap),
convertEnums,
[this, convertEnums]() { enumMap = convertEnums->getEnumMapping(); },
new P4::OrderArguments(&refMap, &typeMap),
new P4::OrderArguments(&typeMap),
new P4::TypeChecking(&refMap, &typeMap),
new P4::SimplifyKey(
&refMap, &typeMap,
new P4::OrPolicy(new P4::IsValid(&refMap, &typeMap), new P4::IsMask())),
new P4::SimplifyKey(&typeMap,
new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsMask())),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::StrengthReduction(&typeMap),
new P4::SimplifySelectCases(&refMap, &typeMap, true), // require constant keysets
new P4::ExpandLookahead(&refMap, &typeMap),
new P4::ExpandEmit(&refMap, &typeMap),
new P4::SimplifyParsers(&refMap),
new P4::SimplifySelectCases(&typeMap, true), // require constant keysets
new P4::ExpandLookahead(&typeMap),
new P4::ExpandEmit(&typeMap),
new P4::SimplifyParsers(),
new P4::StrengthReduction(&typeMap),
new P4::EliminateTuples(&refMap, &typeMap),
new P4::SimplifyComparisons(&refMap, &typeMap),
new P4::EliminateTuples(&typeMap),
new P4::SimplifyComparisons(&typeMap),
new P4::CopyStructures(&refMap, &typeMap),
new P4::NestedStructs(&refMap, &typeMap),
new P4::SimplifySelectList(&refMap, &typeMap),
new P4::RemoveSelectBooleans(&refMap, &typeMap),
new P4::FlattenHeaders(&refMap, &typeMap),
new P4::SimplifySelectList(&typeMap),
new P4::RemoveSelectBooleans(&typeMap),
new P4::FlattenHeaders(&typeMap),
new P4::FlattenInterfaceStructs(&refMap, &typeMap),
new P4::ReplaceSelectRange(&refMap, &typeMap),
new P4::Predication(&refMap),
new P4::ReplaceSelectRange(),
new P4::Predication(),
new P4::MoveDeclarations(), // more may have been introduced
new P4::ConstantFolding(&refMap, &typeMap),
new P4::LocalCopyPropagation(&refMap, &typeMap),
new PassRepeated({
new P4::ConstantFolding(&refMap, &typeMap),
new P4::StrengthReduction(&typeMap),
}),
new P4::SimplifyKey(
&refMap, &typeMap,
new P4::OrPolicy(new P4::IsValid(&refMap, &typeMap), new P4::IsMask())),
new P4::SimplifyKey(&typeMap,
new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsMask())),
new P4::MoveDeclarations(),
new P4::ValidateTableProperties({
"implementation"_cs,
Expand All @@ -127,11 +124,11 @@ SimpleSwitchMidEnd::SimpleSwitchMidEnd(CompilerOptions &options, std::ostream *o
"support_timeout"_cs,
}),
new P4::SimplifyControlFlow(&typeMap),
new P4::EliminateTypedef(&refMap, &typeMap),
new P4::EliminateTypedef(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&refMap, &typeMap),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&refMap, &typeMap),
new P4::RemoveLeftSlices(&refMap, &typeMap),
new P4::RemoveLeftSlices(&typeMap),
// p4c-bm removed unused action parameters. To produce a compatible
// control plane API, we remove them as well for P4-14 programs.
{isv1 ? new P4::RemoveUnusedActionParameters(&refMap) : nullptr},
Expand Down
8 changes: 3 additions & 5 deletions backends/dpdk/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ void DpdkBackend::convert(const IR::ToplevelBlock *tlb) {
new DpdkArchFirst(),
new ValidateOperandSize(),
new CollectLocalStructAndFlatten(refMap, typeMap),
new P4::EliminateTypedef(refMap, typeMap),
new P4::ClearTypeMap(typeMap),
new P4::EliminateTypedef(typeMap),
new P4::TypeChecking(refMap, typeMap),
new ByteAlignment(typeMap, refMap, &structure),
new P4::SimplifyKey(
refMap, typeMap,
new P4::OrPolicy(new P4::IsValid(refMap, typeMap), new P4::IsLikeLeftValue())),
new P4::SimplifyKey(typeMap,
new P4::OrPolicy(new P4::IsValid(typeMap), new P4::IsLikeLeftValue())),
new P4::TypeChecking(refMap, typeMap),
/// TBD: implement dpdk lowering passes instead of reusing bmv2's lowering pass.
new PassRepeated({new BMV2::LowerExpressions(typeMap, DPDK_MAX_SHIFT_AMOUNT)}, 2),
Expand Down
49 changes: 24 additions & 25 deletions backends/dpdk/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class ErrorWidth : public P4::ChooseErrorRepresentation {
};

DpdkMidEnd::DpdkMidEnd(CompilerOptions &options, std::ostream *outStream) {
auto convertEnums = new P4::ConvertEnums(&refMap, &typeMap, new EnumOn32Bits());
auto convertErrors = new P4::ConvertErrors(&refMap, &typeMap, new ErrorWidth(16));
auto convertEnums = new P4::ConvertEnums(&typeMap, new EnumOn32Bits());
auto convertErrors = new P4::ConvertErrors(&typeMap, new ErrorWidth(16));
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap);
std::function<bool(const Context *, const IR::Expression *)> policy =
[=](const Context *ctx, const IR::Expression *) -> bool {
Expand Down Expand Up @@ -168,45 +168,44 @@ DpdkMidEnd::DpdkMidEnd(CompilerOptions &options, std::ostream *outStream) {

if (!DPDK::DpdkContext::get().options().loadIRFromJson) {
addPasses({
options.ndebug ? new P4::RemoveAssertAssume(&refMap, &typeMap) : nullptr,
new P4::RemoveMiss(&refMap, &typeMap),
new P4::EliminateNewtype(&refMap, &typeMap),
new P4::EliminateSerEnums(&refMap, &typeMap),
new P4::EliminateInvalidHeaders(&refMap, &typeMap),
options.ndebug ? new P4::RemoveAssertAssume(&typeMap) : nullptr,
new P4::RemoveMiss(&typeMap),
new P4::EliminateNewtype(&typeMap),
new P4::EliminateSerEnums(&typeMap),
new P4::EliminateInvalidHeaders(&typeMap),
convertEnums,
new VisitFunctor([this, convertEnums]() { enumMap = convertEnums->getEnumMapping(); }),
new P4::OrderArguments(&refMap, &typeMap),
new P4::OrderArguments(&typeMap),
new P4::TypeChecking(&refMap, &typeMap),
new P4::SimplifyKey(
&refMap, &typeMap,
new P4::OrPolicy(new P4::IsValid(&refMap, &typeMap), new P4::IsLikeLeftValue())),
&typeMap, new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsLikeLeftValue())),
new P4::RemoveExits(&typeMap),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::StrengthReduction(&typeMap),
new P4::SimplifySelectCases(&refMap, &typeMap, true),
new P4::SimplifySelectCases(&typeMap, true),
// The lookahead implementation in DPDK target supports only a header instance as
// an operand, we do not expand headers.
// Structures expanded here are then processed as base bit type in ConvertLookahead
// pass in backend.
new P4::ExpandLookahead(&refMap, &typeMap, nullptr, false),
new P4::ExpandEmit(&refMap, &typeMap),
new P4::HandleNoMatch(&refMap),
new P4::SimplifyParsers(&refMap),
new P4::ExpandLookahead(&typeMap, nullptr, false),
new P4::ExpandEmit(&typeMap),
new P4::HandleNoMatch(),
new P4::SimplifyParsers(),
new P4::StrengthReduction(&typeMap),
new P4::EliminateTuples(&refMap, &typeMap),
new P4::SimplifyComparisons(&refMap, &typeMap),
new P4::EliminateTuples(&typeMap),
new P4::SimplifyComparisons(&typeMap),
new P4::CopyStructures(&refMap, &typeMap, false /* errorOnMethodCall */),
new P4::NestedStructs(&refMap, &typeMap),
new P4::SimplifySelectList(&refMap, &typeMap),
new P4::RemoveSelectBooleans(&refMap, &typeMap),
new P4::FlattenHeaders(&refMap, &typeMap),
new P4::SimplifySelectList(&typeMap),
new P4::RemoveSelectBooleans(&typeMap),
new P4::FlattenHeaders(&typeMap),
new P4::FlattenInterfaceStructs(&refMap, &typeMap),
new P4::EliminateTypedef(&refMap, &typeMap),
new P4::EliminateTypedef(&typeMap),
new P4::HSIndexSimplifier(&refMap, &typeMap),
new P4::ParsersUnroll(true, &refMap, &typeMap),
new P4::FlattenHeaderUnion(&refMap, &typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::ReplaceSelectRange(&refMap, &typeMap),
new P4::ReplaceSelectRange(),
new P4::MoveDeclarations(), // more may have been introduced
new P4::ConstantFolding(&refMap, &typeMap),
new P4::LocalCopyPropagation(&refMap, &typeMap, nullptr, policy),
Expand All @@ -219,11 +218,11 @@ DpdkMidEnd::DpdkMidEnd(CompilerOptions &options, std::ostream *outStream) {
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifySwitch(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&refMap, &typeMap),
new P4::RemoveLeftSlices(&refMap, &typeMap),
new P4::TableHit(&typeMap),
new P4::RemoveLeftSlices(&typeMap),
new P4::TypeChecking(&refMap, &typeMap),
convertErrors,
new P4::EliminateSerEnums(&refMap, &typeMap),
new P4::EliminateSerEnums(&typeMap),
new P4::MidEndLast(),
evaluator,
new VisitFunctor([this, evaluator]() { toplevel = evaluator->getToplevelBlock(); }),
Expand Down
Loading

0 comments on commit 49e835c

Please sign in to comment.