Skip to content

Commit

Permalink
Merge remote-tracking branch 'up/main' into replace-scapy-with-apache…
Browse files Browse the repository at this point in the history
…-code-in-p4tc-tests
  • Loading branch information
jafingerhut committed Mar 5, 2025
2 parents 4663b50 + bf2aa7b commit 639e63c
Show file tree
Hide file tree
Showing 36 changed files with 141 additions and 124 deletions.
2 changes: 1 addition & 1 deletion backends/bmv2/pna_nic/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream)
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::SimplifyControlFlow(&typeMap, true),
new P4::CompileTimeOperations(),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&typeMap),
Expand Down
4 changes: 2 additions & 2 deletions backends/bmv2/pna_nic/pnaNic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ void PnaNicBackend::convert(const IR::ToplevelBlock *tlb) {
new SkipControls(&structure.non_pipeline_controls)),
new P4::MoveActionsToTables(refMap, typeMap),
new P4::TypeChecking(refMap, typeMap),
new P4::SimplifyControlFlow(typeMap),
new P4::SimplifyControlFlow(typeMap, true),
new LowerExpressions(typeMap),
new PassRepeated({new P4::ConstantFolding(typeMap), new P4::StrengthReduction(typeMap)}),
new P4::TypeChecking(refMap, typeMap),
new P4::RemoveComplexExpressions(typeMap,
new ProcessControls(&structure.pipeline_controls)),
new P4::SimplifyControlFlow(typeMap),
new P4::SimplifyControlFlow(typeMap, true),
new P4::RemoveAllUnusedDeclarations(P4::RemoveUnusedPolicy()),
// Converts the DAG into a TREE (at least for expressions)
// This is important later for conversion to JSON.
Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/psa_switch/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ PsaSwitchMidEnd::PsaSwitchMidEnd(CompilerOptions &options, std::ostream *outStre
"psa_idle_timeout"_cs,
"size"_cs,
}),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::CompileTimeOperations(),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&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 @@ -100,7 +100,7 @@ void PsaSwitchBackend::convert(const IR::ToplevelBlock *tlb) {
new SkipControls(&structure.non_pipeline_controls)),
new P4::MoveActionsToTables(refMap, typeMap),
new P4::TypeChecking(refMap, typeMap),
new P4::SimplifyControlFlow(typeMap),
new P4::SimplifyControlFlow(typeMap, true),
new LowerExpressions(typeMap),
new PassRepeated({
new P4::ConstantFolding(typeMap),
Expand All @@ -109,7 +109,7 @@ void PsaSwitchBackend::convert(const IR::ToplevelBlock *tlb) {
new P4::TypeChecking(refMap, typeMap),
new P4::RemoveComplexExpressions(typeMap,
new ProcessControls(&structure.pipeline_controls)),
new P4::SimplifyControlFlow(typeMap),
new P4::SimplifyControlFlow(typeMap, true),
new P4::RemoveAllUnusedDeclarations(P4::RemoveUnusedPolicy()),
// Converts the DAG into a TREE (at least for expressions)
// This is important later for conversion to JSON.
Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/simple_switch/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ SimpleSwitchMidEnd::SimpleSwitchMidEnd(CompilerOptions &options, std::ostream *o
"meters"_cs,
"support_timeout"_cs,
}),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::EliminateTypedef(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&typeMap),
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 @@ -1176,12 +1176,12 @@ void SimpleSwitchBackend::convert(const IR::ToplevelBlock *tlb) {
new SkipControls(&structure->non_pipeline_controls)),
new P4::MoveActionsToTables(refMap, typeMap),
new P4::TypeChecking(nullptr, typeMap),
new P4::SimplifyControlFlow(typeMap),
new P4::SimplifyControlFlow(typeMap, true),
new LowerExpressions(typeMap),
new P4::ConstantFolding(typeMap, false),
new P4::TypeChecking(nullptr, typeMap),
new RemoveComplexExpressions(typeMap, new ProcessControls(&structure->pipeline_controls)),
new P4::SimplifyControlFlow(typeMap),
new P4::SimplifyControlFlow(typeMap, true),
new P4::RemoveAllUnusedDeclarations(P4::RemoveUnusedPolicy()),
new P4::FlattenLogMsg(typeMap),
// Converts the DAG into a TREE (at least for expressions)
Expand Down
4 changes: 2 additions & 2 deletions backends/dpdk/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ DpdkMidEnd::DpdkMidEnd(CompilerOptions &options, std::ostream *outStream) {
new P4::HSIndexSimplifier(&typeMap),
new P4::ParsersUnroll(true, &refMap, &typeMap),
new P4::FlattenHeaderUnion(&refMap, &typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::ReplaceSelectRange(),
new P4::MoveDeclarations(), // more may have been introduced
new P4::ConstantFolding(&typeMap),
Expand All @@ -215,7 +215,7 @@ DpdkMidEnd::DpdkMidEnd(CompilerOptions &options, std::ostream *outStream) {
}),
new P4::MoveDeclarations(),
validateTableProperties(options.arch),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::SimplifySwitch(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&typeMap),
Expand Down
4 changes: 2 additions & 2 deletions backends/ebpf/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const IR::ToplevelBlock *MidEnd::run(EbpfOptions &options, const IR::P4Program *
new P4::RemoveMiss(&typeMap),
new P4::EliminateInvalidHeaders(&typeMap),
new P4::EliminateNewtype(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::SimplifyKey(
&typeMap, new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsLikeLeftValue())),
new P4::RemoveExits(&typeMap),
Expand All @@ -104,7 +104,7 @@ const IR::ToplevelBlock *MidEnd::run(EbpfOptions &options, const IR::P4Program *
new P4::RemoveSelectBooleans(&typeMap),
new P4::SingleArgumentSelect(&typeMap),
new P4::ConstantFolding(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::TableHit(&typeMap),
new P4::RemoveLeftSlices(&typeMap),
new EBPF::Lower(&refMap, &typeMap),
Expand Down
4 changes: 2 additions & 2 deletions backends/p4test/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ MidEnd::MidEnd(CompilerOptions &options, std::ostream *outStream) {
}),
new P4::StrengthReduction(&typeMap),
new P4::MoveDeclarations(), // more may have been introduced
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::CompileTimeOperations(),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&typeMap),
Expand Down Expand Up @@ -167,7 +167,7 @@ MidEnd::MidEnd(CompilerOptions &options, std::ostream *outStream) {
evaluator,
[this, evaluator]() { toplevel = evaluator->getToplevelBlock(); },
new P4::FlattenHeaderUnion(&refMap, &typeMap, options.loopsUnrolling),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::MidEndLast()});
if (options.listMidendPasses) {
listPasses(*outStream, cstring::newline);
Expand Down
6 changes: 3 additions & 3 deletions backends/p4tools/common/compiler/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void MidEnd::addDefaultPasses() {
mkConvertKeys(),
mkConvertEnums(),
new P4::ConstantFolding(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
// Eliminate extraneous cases in select statements.
new P4::SimplifySelectCases(&typeMap, false),
// Expand lookahead assignments into sequences of field assignments.
Expand Down Expand Up @@ -145,7 +145,7 @@ void MidEnd::addDefaultPasses() {
}),
new P4::ConstantFolding(&typeMap),
new P4::MoveDeclarations(),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
// Replace any slices in the left side of assignments and convert them to casts.
new P4::RemoveLeftSlices(&typeMap),
// Remove loops from parsers by unrolling them as far as the stack indices allow.
Expand All @@ -155,7 +155,7 @@ void MidEnd::addDefaultPasses() {
// Convert tuples into structs.
new P4::EliminateTuples(&typeMap),
new P4::ConstantFolding(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
// Perform a last round of type-checking before passes which do not type-check begin.
new P4::TypeChecking(&refMap, &typeMap, true),
});
Expand Down
4 changes: 2 additions & 2 deletions backends/tc/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const IR::ToplevelBlock *MidEnd::run(TCOptions &options, const IR::P4Program *pr
new P4::EliminateInvalidHeaders(&typeMap),
new P4::EliminateNewtype(&typeMap),
new P4::EliminateSerEnums(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::SimplifyKey(&typeMap,
new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsLikeLeftValue())),
new P4::RemoveExits(&typeMap),
Expand All @@ -56,7 +56,7 @@ const IR::ToplevelBlock *MidEnd::run(TCOptions &options, const IR::P4Program *pr
new P4::RemoveSelectBooleans(&typeMap),
new P4::SingleArgumentSelect(&typeMap),
new P4::ConstantFolding(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::TableHit(&typeMap),
new P4::RemoveLeftSlices(&typeMap),
new EBPF::Lower(&refMap, &typeMap),
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/midend/elim_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class ElimCasts : public PassManager {
new RewriteConcatToSlices(),
new P4::ClearTypeMap(typeMap),
new BFN::TypeChecking(refMap, typeMap, true),
new P4::SimplifyControlFlow(typeMap),
new P4::SimplifyControlFlow(typeMap, true),
new P4::ClearTypeMap(typeMap),
new BFN::TypeChecking(refMap, typeMap, true),
});
Expand Down
2 changes: 1 addition & 1 deletion backends/tofino/bf-p4c/midend/simplifyIfStatement.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class SimplifyIfStatement : public PassManager {
public:
SimplifyIfStatement(ReferenceMap *refMap, TypeMap *typeMap) {
passes.push_back(new ElimCallExprInIfCond(refMap, typeMap));
passes.push_back(new SimplifyControlFlow(typeMap));
passes.push_back(new SimplifyControlFlow(typeMap, true));
}
};

Expand Down
4 changes: 2 additions & 2 deletions backends/ubpf/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const IR::ToplevelBlock *MidEnd::run(EbpfOptions &options, const IR::P4Program *
new P4::ClearTypeMap(&typeMap),
new P4::EliminateNewtype(&typeMap),
new P4::EliminateInvalidHeaders(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::SimplifyKey(
&typeMap, new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsLikeLeftValue())),
new P4::ConstantFolding(&typeMap),
Expand All @@ -100,7 +100,7 @@ const IR::ToplevelBlock *MidEnd::run(EbpfOptions &options, const IR::P4Program *
new P4::RemoveSelectBooleans(&typeMap),
new P4::SingleArgumentSelect(&typeMap),
new P4::ConstantFolding(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
new P4::SimplifyControlFlow(&typeMap, true),
new P4::TableHit(&typeMap),
new P4::RemoveLeftSlices(&typeMap),
new EBPF::Lower(&refMap, &typeMap),
Expand Down
33 changes: 18 additions & 15 deletions frontends/p4/actionsInlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ const IR::Node *ActionsInliner::preorder(IR::MethodCallStatement *statement) {
cstring newName = nameGen->newName(param->name.name.string_view());
paramRename.emplace(param, newName);
if (param->direction == IR::Direction::In || param->direction == IR::Direction::InOut) {
const auto *vardecl = new IR::Declaration_Variable(newName, param->annotations,
param->type, argument->expression);
const auto *vardecl = new IR::Declaration_Variable(
argument->srcInfo, newName, param->annotations, param->type, argument->expression);
body.push_back(vardecl);
subst.add(param, new IR::Argument(argument->srcInfo, argument->name,
new IR::PathExpression(newName)));
subst.add(param, new IR::Argument(argument->name, new IR::PathExpression(newName)));
} else if (param->direction == IR::Direction::None) {
// This works because there can be no side-effects in the evaluation of this
// argument.
Expand All @@ -108,10 +107,9 @@ const IR::Node *ActionsInliner::preorder(IR::MethodCallStatement *statement) {
subst.add(param, argument);
} else if (param->direction == IR::Direction::Out) {
// uninitialized variable
const auto *vardecl =
new IR::Declaration_Variable(newName, param->annotations, param->type);
subst.add(param, new IR::Argument(argument->srcInfo, argument->name,
new IR::PathExpression(newName)));
const auto *vardecl = new IR::Declaration_Variable(argument->srcInfo, newName,
param->annotations, param->type);
subst.add(param, new IR::Argument(argument->name, new IR::PathExpression(newName)));
body.push_back(vardecl);
}
}
Expand All @@ -136,17 +134,22 @@ const IR::Node *ActionsInliner::preorder(IR::MethodCallStatement *statement) {
if (param->direction == IR::Direction::InOut || param->direction == IR::Direction::Out) {
cstring newName = ::P4::get(paramRename, param);
auto right = new IR::PathExpression(newName);
auto copyout = new IR::AssignmentStatement(left->expression, right);
// FIXME: Technically, location should be just after the call
auto copyout = new IR::AssignmentStatement(statement->srcInfo, left->expression, right);
body.push_back(copyout);
}
}

auto annotations = callee->annotations.where([&](const IR::Annotation *a) {
return !(a->name == IR::Annotation::nameAnnotation ||
(a->name == IR::Annotation::noWarnAnnotation && a->getSingleString() == "unused"));
});
auto result = new IR::BlockStatement(statement->srcInfo,
{annotations->begin(), annotations->end()}, body);
IR::Vector<IR::Annotation> annotations =
callee->annotations.where([&](const IR::Annotation *a) {
return !(
a->name == IR::Annotation::nameAnnotation ||
(a->name == IR::Annotation::noWarnAnnotation && a->getSingleString() == "unused"));
});
annotations.push_back(new IR::Annotation(statement->srcInfo,
IR::Annotation::inlinedFromAnnotation,
{new IR::StringLiteral(callee->name.originalName)}));
auto result = new IR::BlockStatement(statement->srcInfo, annotations, body);
LOG2("Replacing " << orig << " with " << result);
return result;
}
Expand Down
10 changes: 5 additions & 5 deletions frontends/p4/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ const IR::P4Program *FrontEnd::run(const CompilerOptions &options, const IR::P4P
new Reassociation(),
new UselessCasts(&typeMap),
}),
new SimplifyControlFlow(&typeMap),
new SimplifyControlFlow(&typeMap, policy->foldInlinedFrom()),
new SwitchAddDefault,
new FrontEndDump(), // used for testing the program at this point
new RemoveAllUnusedDeclarations(*policy, true),
Expand All @@ -218,12 +218,12 @@ const IR::P4Program *FrontEnd::run(const CompilerOptions &options, const IR::P4P
new MoveInitializers(),
new SideEffectOrdering(&typeMap, policy->skipSideEffectOrdering()),
policy->removeOpAssign() ? new RemoveOpAssign() : nullptr,
new SimplifyControlFlow(&typeMap),
new SimplifyControlFlow(&typeMap, policy->foldInlinedFrom()),
new SimplifySwitch(&typeMap),
new MoveDeclarations(), // Move all local declarations to the beginning
new SimplifyDefUse(&typeMap),
new UniqueParameters(&typeMap),
new SimplifyControlFlow(&typeMap),
new SimplifyControlFlow(&typeMap, policy->foldInlinedFrom()),
new SpecializeAll(&typeMap, policy),
new RemoveParserControlFlow(&typeMap),
new RemoveReturns(),
Expand Down Expand Up @@ -259,14 +259,14 @@ const IR::P4Program *FrontEnd::run(const CompilerOptions &options, const IR::P4P
// Check for constants only after inlining
new CheckConstants(&typeMap),
new ConstantFolding(&typeMap, constantFoldingPolicy),
new SimplifyControlFlow(&typeMap),
new SimplifyControlFlow(&typeMap, policy->foldInlinedFrom()),
// more ifs may have been added to parsers
new RemoveParserControlFlow(&typeMap),
new UniqueNames(), // needed again after inlining
new MoveDeclarations(), // needed again after inlining
new SimplifyDefUse(&typeMap),
new RemoveAllUnusedDeclarations(*policy),
new SimplifyControlFlow(&typeMap),
new SimplifyControlFlow(&typeMap, policy->foldInlinedFrom()),
});
}
passes.addPasses({
Expand Down
4 changes: 4 additions & 0 deletions frontends/p4/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class FrontEndPolicy : public RemoveUnusedPolicy {
// TODO: This should probably not be allowed to be skipped at all.
virtual bool skipSideEffectOrdering() const { return false; }

/// Indicates whether control flow should fold blocks marked with @inlinedFrom annotation
/// @returns Defaults to true
virtual bool foldInlinedFrom() const { return true; }

/// Indicates whether to enable the `a - constant` to `a + (-constant)` in StrengthReduction.
/// @returns Defaults to true.
virtual bool enableSubConstToAddTransform() const { return true; }
Expand Down
Loading

0 comments on commit 639e63c

Please sign in to comment.