Skip to content

Commit

Permalink
Remove reference map from another set of midend passes (p4lang#4939)
Browse files Browse the repository at this point in the history
* Remove refmap from NestedStructs

Signed-off-by: Anton Korobeynikov <[email protected]>

* Remove refmap from CopyStructures

Signed-off-by: Anton Korobeynikov <[email protected]>

* Remove refmap from FlattenInterfaceStructs

Signed-off-by: Anton Korobeynikov <[email protected]>

* Remove refmap from EliminateSwitch

Signed-off-by: Anton Korobeynikov <[email protected]>

* Remove unused refmap from FlattenLogMsg

Signed-off-by: Anton Korobeynikov <[email protected]>

* Remove refmap from RemoveComplexExpressions

Signed-off-by: Anton Korobeynikov <[email protected]>

* Remove refmap from hsIndexSimplify

Signed-off-by: Anton Korobeynikov <[email protected]>

* Remove reference map from LocalCopyPropagation

Signed-off-by: Anton Korobeynikov <[email protected]>

* Remove reference map from top-level ConstantFolding invocation

Signed-off-by: Anton Korobeynikov <[email protected]>

* Remove reference map from CheckExternInvocation

Signed-off-by: Anton Korobeynikov <[email protected]>

* Cleanup unused headers

Signed-off-by: Anton Korobeynikov <[email protected]>

* Fix DPDK backend bug: DismantleMuxExpressions relies on fresh reference map

Signed-off-by: Anton Korobeynikov <[email protected]>

* Apply terrible hack to PSA Switch as local copypropagation policy it uses relies on fresh refmap

Signed-off-by: Anton Korobeynikov <[email protected]>

* Simplify

Signed-off-by: Anton Korobeynikov <[email protected]>

---------

Signed-off-by: Anton Korobeynikov <[email protected]>
  • Loading branch information
asl authored Oct 3, 2024
1 parent b8edc2c commit 9132140
Show file tree
Hide file tree
Showing 57 changed files with 289 additions and 302 deletions.
2 changes: 1 addition & 1 deletion backends/bmv2/common/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const IR::Node *LowerExpressions::postorder(IR::Concat *expression) {

const IR::Node *RemoveComplexExpressions::postorder(IR::MethodCallExpression *expression) {
if (expression->arguments->size() == 0) return expression;
auto mi = P4::MethodInstance::resolve(expression, refMap, typeMap);
auto mi = P4::MethodInstance::resolve(expression, this, typeMap);
if (mi->isApply() || mi->is<P4::BuiltInMethod>()) return expression;

if (auto ef = mi->to<P4::ExternFunction>()) {
Expand Down
6 changes: 3 additions & 3 deletions backends/bmv2/common/lower.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class LowerExpressions : public Transform {

class RemoveComplexExpressions : public P4::RemoveComplexExpressions {
public:
RemoveComplexExpressions(P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
P4::RemoveComplexExpressionsPolicy *policy = nullptr)
: P4::RemoveComplexExpressions(refMap, typeMap, policy) {}
explicit RemoveComplexExpressions(P4::TypeMap *typeMap,
P4::RemoveComplexExpressionsPolicy *policy = nullptr)
: P4::RemoveComplexExpressions(typeMap, policy) {}

const IR::Node *postorder(IR::MethodCallExpression *expression) override;
};
Expand Down
16 changes: 8 additions & 8 deletions backends/bmv2/pna_nic/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream)
new P4::TypeChecking(&refMap, &typeMap),
new P4::SimplifyKey(&typeMap,
new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsMask())),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::StrengthReduction(&typeMap),
new P4::SimplifySelectCases(&typeMap, true), // require constant keysets
new P4::ExpandLookahead(&typeMap),
Expand All @@ -130,27 +130,27 @@ PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream)
new P4::StrengthReduction(&typeMap),
new P4::EliminateTuples(&typeMap),
new P4::SimplifyComparisons(&typeMap),
new P4::CopyStructures(&refMap, &typeMap),
new P4::NestedStructs(&refMap, &typeMap),
new P4::CopyStructures(&typeMap),
new P4::NestedStructs(&typeMap),
new P4::SimplifySelectList(&typeMap),
new P4::RemoveSelectBooleans(&typeMap),
new P4::FlattenHeaders(&typeMap),
new P4::FlattenInterfaceStructs(&refMap, &typeMap),
new P4::FlattenInterfaceStructs(&typeMap),
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),
new P4::ConstantFolding(&typeMap),
new P4::LocalCopyPropagation(&typeMap, nullptr, policy),
new PassRepeated(
{new P4::ConstantFolding(&refMap, &typeMap), new P4::StrengthReduction(&typeMap)}),
{new P4::ConstantFolding(&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,
"size"_cs}),
new P4::SimplifyControlFlow(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&refMap, &typeMap),
new P4::EliminateSwitch(&typeMap),
new P4::MoveActionsToTables(&refMap, &typeMap),
new P4::RemoveLeftSlices(&typeMap),
new P4::TypeChecking(&refMap, &typeMap),
Expand Down
5 changes: 2 additions & 3 deletions backends/bmv2/pna_nic/pnaNic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ void PnaNicBackend::convert(const IR::ToplevelBlock *tlb) {
new P4::TypeChecking(refMap, typeMap),
new P4::SimplifyControlFlow(typeMap),
new LowerExpressions(typeMap),
new PassRepeated(
{new P4::ConstantFolding(refMap, typeMap), new P4::StrengthReduction(typeMap)}),
new PassRepeated({new P4::ConstantFolding(typeMap), new P4::StrengthReduction(typeMap)}),
new P4::TypeChecking(refMap, typeMap),
new P4::RemoveComplexExpressions(refMap, typeMap,
new P4::RemoveComplexExpressions(typeMap,
new ProcessControls(&structure.pipeline_controls)),
new P4::SimplifyControlFlow(typeMap),
new P4::RemoveAllUnusedDeclarations(refMap, P4::RemoveUnusedPolicy()),
Expand Down
18 changes: 10 additions & 8 deletions backends/bmv2/psa_switch/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ PsaSwitchMidEnd::PsaSwitchMidEnd(CompilerOptions &options, std::ostream *outStre
[=](const Context *, const IR::Expression *e) -> bool {
auto mce = e->to<IR::MethodCallExpression>();
if (mce == nullptr) return true;
// FIXME: Add utility method to resolve declaration given a context
auto mi = P4::MethodInstance::resolve(mce, &refMap, &typeMap);
auto em = mi->to<P4::ExternMethod>();
if (em == nullptr) return true;
Expand All @@ -121,7 +122,7 @@ PsaSwitchMidEnd::PsaSwitchMidEnd(CompilerOptions &options, std::ostream *outStre
new P4::TypeChecking(&refMap, &typeMap),
new P4::SimplifyKey(&typeMap,
new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsMask())),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::StrengthReduction(&typeMap),
new P4::SimplifySelectCases(&typeMap, true), // require constant keysets
new P4::ExpandLookahead(&typeMap),
Expand All @@ -130,19 +131,20 @@ PsaSwitchMidEnd::PsaSwitchMidEnd(CompilerOptions &options, std::ostream *outStre
new P4::StrengthReduction(&typeMap),
new P4::EliminateTuples(&typeMap),
new P4::SimplifyComparisons(&typeMap),
new P4::CopyStructures(&refMap, &typeMap),
new P4::NestedStructs(&refMap, &typeMap),
new P4::CopyStructures(&typeMap),
new P4::NestedStructs(&typeMap),
new P4::SimplifySelectList(&typeMap),
new P4::RemoveSelectBooleans(&typeMap),
new P4::FlattenHeaders(&typeMap),
new P4::FlattenInterfaceStructs(&refMap, &typeMap),
new P4::FlattenInterfaceStructs(&typeMap),
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),
new P4::ConstantFolding(&typeMap),
new P4::TypeChecking(&refMap, &typeMap), // policy below relies on fresh refmap
new P4::LocalCopyPropagation(&typeMap, nullptr, policy),
new PassRepeated({
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::StrengthReduction(&typeMap),
}),
new P4::MoveDeclarations(),
Expand All @@ -156,7 +158,7 @@ PsaSwitchMidEnd::PsaSwitchMidEnd(CompilerOptions &options, std::ostream *outStre
new P4::SimplifyControlFlow(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&refMap, &typeMap),
new P4::EliminateSwitch(&typeMap),
new P4::MoveActionsToTables(&refMap, &typeMap),
new P4::RemoveLeftSlices(&typeMap),
new P4::TypeChecking(&refMap, &typeMap),
Expand Down
4 changes: 2 additions & 2 deletions backends/bmv2/psa_switch/psaSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ void PsaSwitchBackend::convert(const IR::ToplevelBlock *tlb) {
new P4::SimplifyControlFlow(typeMap),
new LowerExpressions(typeMap),
new PassRepeated({
new P4::ConstantFolding(refMap, typeMap),
new P4::ConstantFolding(typeMap),
new P4::StrengthReduction(typeMap),
}),
new P4::TypeChecking(refMap, typeMap),
new P4::RemoveComplexExpressions(refMap, typeMap,
new P4::RemoveComplexExpressions(typeMap,
new ProcessControls(&structure.pipeline_controls)),
new P4::SimplifyControlFlow(typeMap),
new P4::RemoveAllUnusedDeclarations(refMap, P4::RemoveUnusedPolicy()),
Expand Down
16 changes: 8 additions & 8 deletions backends/bmv2/simple_switch/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ SimpleSwitchMidEnd::SimpleSwitchMidEnd(CompilerOptions &options, std::ostream *o
new P4::TypeChecking(&refMap, &typeMap),
new P4::SimplifyKey(&typeMap,
new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsMask())),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::StrengthReduction(&typeMap),
new P4::SimplifySelectCases(&typeMap, true), // require constant keysets
new P4::ExpandLookahead(&typeMap),
Expand All @@ -98,19 +98,19 @@ SimpleSwitchMidEnd::SimpleSwitchMidEnd(CompilerOptions &options, std::ostream *o
new P4::StrengthReduction(&typeMap),
new P4::EliminateTuples(&typeMap),
new P4::SimplifyComparisons(&typeMap),
new P4::CopyStructures(&refMap, &typeMap),
new P4::NestedStructs(&refMap, &typeMap),
new P4::CopyStructures(&typeMap),
new P4::NestedStructs(&typeMap),
new P4::SimplifySelectList(&typeMap),
new P4::RemoveSelectBooleans(&typeMap),
new P4::FlattenHeaders(&typeMap),
new P4::FlattenInterfaceStructs(&refMap, &typeMap),
new P4::FlattenInterfaceStructs(&typeMap),
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 P4::ConstantFolding(&typeMap),
new P4::LocalCopyPropagation(&typeMap),
new PassRepeated({
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::StrengthReduction(&typeMap),
}),
new P4::SimplifyKey(&typeMap,
Expand All @@ -127,7 +127,7 @@ SimpleSwitchMidEnd::SimpleSwitchMidEnd(CompilerOptions &options, std::ostream *o
new P4::EliminateTypedef(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&refMap, &typeMap),
new P4::EliminateSwitch(&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.
Expand Down
7 changes: 3 additions & 4 deletions backends/bmv2/simple_switch/simpleSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1178,13 +1178,12 @@ void SimpleSwitchBackend::convert(const IR::ToplevelBlock *tlb) {
new P4::TypeChecking(refMap, typeMap),
new P4::SimplifyControlFlow(typeMap),
new LowerExpressions(typeMap),
new P4::ConstantFolding(refMap, typeMap, false),
new P4::ConstantFolding(typeMap, false),
new P4::TypeChecking(refMap, typeMap),
new RemoveComplexExpressions(refMap, typeMap,
new ProcessControls(&structure->pipeline_controls)),
new RemoveComplexExpressions(typeMap, new ProcessControls(&structure->pipeline_controls)),
new P4::SimplifyControlFlow(typeMap),
new P4::RemoveAllUnusedDeclarations(refMap, P4::RemoveUnusedPolicy()),
new P4::FlattenLogMsg(refMap, typeMap),
new P4::FlattenLogMsg(typeMap),
// Converts the DAG into a TREE (at least for expressions)
// This is important later for conversion to JSON.
new P4::CloneExpressions(),
Expand Down
8 changes: 5 additions & 3 deletions backends/dpdk/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License.
#include "dpdkMetadata.h"
#include "dpdkProgram.h"
#include "frontends/p4/moveDeclarations.h"
#include "frontends/p4/typeChecking/typeChecker.h"
#include "ir/dbprint.h"
#include "ir/ir.h"
#include "lib/stringify.h"
Expand Down Expand Up @@ -62,10 +63,11 @@ void DpdkBackend::convert(const IR::ToplevelBlock *tlb) {
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),
new P4::RemoveComplexExpressions(refMap, typeMap,
new P4::RemoveComplexExpressions(typeMap,
new DPDK::ProcessControls(&structure.pipeline_controls)),
new TypeChecking(refMap, typeMap), // DismantleMuxExpressions wants fresh refmap
new DismantleMuxExpressions(typeMap, refMap),
new P4::ConstantFolding(refMap, typeMap, false),
new P4::ConstantFolding(typeMap, false),
new EliminateHeaderCopy(refMap, typeMap),
new P4::TypeChecking(refMap, typeMap),
new P4::RemoveAllUnusedDeclarations(refMap, P4::RemoveUnusedPolicy()),
Expand Down Expand Up @@ -109,7 +111,7 @@ void DpdkBackend::convert(const IR::ToplevelBlock *tlb) {
new DpdkAddPseudoHeader(refMap, typeMap, is_all_args_header_fields),
new CollectProgramStructure(refMap, typeMap, &structure),
new InspectDpdkProgram(refMap, typeMap, &structure),
new CheckExternInvocation(refMap, typeMap, &structure),
new CheckExternInvocation(typeMap, &structure),
new TypeWidthValidator(),
new DpdkArchLast(),
new VisitFunctor([this, genContextJson] {
Expand Down
2 changes: 1 addition & 1 deletion backends/dpdk/dpdkArch.h
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ class CollectLocalStructAndFlatten : public PassManager {
passes.push_back(new P4::ResolveReferences(refMap));
passes.push_back(new P4::TypeInference(typeMap, false));
passes.push_back(new P4::TypeChecking(refMap, typeMap, true));
passes.push_back(new P4::FlattenInterfaceStructs(refMap, typeMap));
passes.push_back(new P4::FlattenInterfaceStructs(typeMap));
}
};

Expand Down
16 changes: 6 additions & 10 deletions backends/dpdk/dpdkCheckExternInvocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ limitations under the License.
#include "midend/checkExternInvocationCommon.h"

namespace P4 {
class ReferenceMap;
class TypeMap;
} // namespace P4

Expand Down Expand Up @@ -137,30 +136,27 @@ class CheckPNAExternInvocation : public P4::CheckExternInvocationCommon {
}

public:
CheckPNAExternInvocation(P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
DpdkProgramStructure *structure)
: P4::CheckExternInvocationCommon(refMap, typeMap), structure(structure) {
CheckPNAExternInvocation(P4::TypeMap *typeMap, DpdkProgramStructure *structure)
: P4::CheckExternInvocationCommon(typeMap), structure(structure) {
initPipeConstraints();
}
};

/// @brief Class which chooses the correct class for checking the constraints for invocations.
/// of extern methods and functions depending on the architecture.
class CheckExternInvocation : public Inspector {
P4::ReferenceMap *refMap;
P4::TypeMap *typeMap;
DpdkProgramStructure *structure;

public:
CheckExternInvocation(P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
DpdkProgramStructure *structure)
: refMap(refMap), typeMap(typeMap), structure(structure) {}
CheckExternInvocation(P4::TypeMap *typeMap, DpdkProgramStructure *structure)
: typeMap(typeMap), structure(structure) {}

bool preorder(const IR::P4Program *program) {
if (structure->isPNA()) {
LOG1("Checking extern invocations for PNA architecture.");
auto checker = new CheckPNAExternInvocation(refMap, typeMap, structure);
program->apply(*checker);
CheckPNAExternInvocation checker(typeMap, structure);
program->apply(checker, getChildContext());
} else if (structure->isPSA()) {
LOG1("Checking extern invocations for PSA architecture.");
// Add class checking PSA constraints here.
Expand Down
16 changes: 8 additions & 8 deletions backends/dpdk/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ DpdkMidEnd::DpdkMidEnd(CompilerOptions &options, std::ostream *outStream) {
new P4::SimplifyKey(
&typeMap, new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsLikeLeftValue())),
new P4::RemoveExits(&typeMap),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::StrengthReduction(&typeMap),
new P4::SimplifySelectCases(&typeMap, true),
// The lookahead implementation in DPDK target supports only a header instance as
Expand All @@ -194,23 +194,23 @@ DpdkMidEnd::DpdkMidEnd(CompilerOptions &options, std::ostream *outStream) {
new P4::StrengthReduction(&typeMap),
new P4::EliminateTuples(&typeMap),
new P4::SimplifyComparisons(&typeMap),
new P4::CopyStructures(&refMap, &typeMap, false /* errorOnMethodCall */),
new P4::NestedStructs(&refMap, &typeMap),
new P4::CopyStructures(&typeMap, false /* errorOnMethodCall */),
new P4::NestedStructs(&typeMap),
new P4::SimplifySelectList(&typeMap),
new P4::RemoveSelectBooleans(&typeMap),
new P4::FlattenHeaders(&typeMap),
new P4::FlattenInterfaceStructs(&refMap, &typeMap),
new P4::FlattenInterfaceStructs(&typeMap),
new P4::EliminateTypedef(&typeMap),
new P4::HSIndexSimplifier(&refMap, &typeMap),
new P4::HSIndexSimplifier(&typeMap),
new P4::ParsersUnroll(true, &refMap, &typeMap),
new P4::FlattenHeaderUnion(&refMap, &typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::ReplaceSelectRange(),
new P4::MoveDeclarations(), // more may have been introduced
new P4::ConstantFolding(&refMap, &typeMap),
new P4::LocalCopyPropagation(&refMap, &typeMap, nullptr, policy),
new P4::ConstantFolding(&typeMap),
new P4::LocalCopyPropagation(&typeMap, nullptr, policy),
new PassRepeated({
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::StrengthReduction(&typeMap),
}),
new P4::MoveDeclarations(),
Expand Down
6 changes: 3 additions & 3 deletions backends/ebpf/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ const IR::ToplevelBlock *MidEnd::run(EbpfOptions &options, const IR::P4Program *
new P4::SimplifyKey(
&typeMap, new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsLikeLeftValue())),
new P4::RemoveExits(&typeMap),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::SimplifySelectCases(&typeMap, false), // accept non-constant keysets
new P4::ExpandEmit(&typeMap),
new P4::HandleNoMatch(),
new P4::SimplifyParsers(),
new PassRepeated({
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::StrengthReduction(&typeMap),
}),
new P4::SimplifyComparisons(&typeMap),
Expand All @@ -103,7 +103,7 @@ const IR::ToplevelBlock *MidEnd::run(EbpfOptions &options, const IR::P4Program *
new P4::MoveDeclarations(), // more may have been introduced
new P4::RemoveSelectBooleans(&typeMap),
new P4::SingleArgumentSelect(&typeMap),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::ConstantFolding(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::TableHit(&typeMap),
new P4::RemoveLeftSlices(&typeMap),
Expand Down
Loading

0 comments on commit 9132140

Please sign in to comment.