Skip to content

Commit

Permalink
Remove apply() calls to redundant parsers (#3356)
Browse files Browse the repository at this point in the history
* Remove apply() calls to redundant parsers

A parser with an unconditional accept in its start state is
effectively a redundant/dummy parser and can be eliminated when it is
called as a subparser. This removes the apply() calls to these
subparsers, which also removes the generation of the setInvalid() call
in resetHeaders.
  • Loading branch information
bleibig authored Jun 25, 2022
1 parent 5c87a54 commit ad5b4a2
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 13 deletions.
2 changes: 2 additions & 0 deletions frontends/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set (P4_FRONTEND_SRCS
p4/parserCallGraph.cpp
p4/parserControlFlow.cpp
p4/reassociation.cpp
p4/redundantParsers.cpp
p4/removeParameters.cpp
p4/removeReturns.cpp
p4/reservedWords.cpp
Expand Down Expand Up @@ -114,6 +115,7 @@ set (P4_FRONTEND_HDRS
p4/parserCallGraph.h
p4/parserControlFlow.h
p4/reassociation.h
p4/redundantParsers.h
p4/removeParameters.h
p4/removeReturns.h
p4/reservedWords.h
Expand Down
2 changes: 2 additions & 0 deletions frontends/p4/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ limitations under the License.
#include "parseAnnotations.h"
#include "parserControlFlow.h"
#include "reassociation.h"
#include "redundantParsers.h"
#include "removeParameters.h"
#include "removeReturns.h"
#include "resetHeaders.h"
Expand Down Expand Up @@ -218,6 +219,7 @@ const IR::P4Program *FrontEnd::run(const CompilerOptions &options, const IR::P4P
new RemoveDontcareArgs(&refMap, &typeMap),
new MoveConstructors(&refMap),
new RemoveAllUnusedDeclarations(&refMap),
new RemoveRedundantParsers(&refMap, &typeMap),
new ClearTypeMap(&typeMap),
evaluator,
new Inline(&refMap, &typeMap, evaluator, options.optimizeParserInlining),
Expand Down
59 changes: 59 additions & 0 deletions frontends/p4/redundantParsers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2022 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "redundantParsers.h"

#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/p4/methodInstance.h"

namespace P4 {
bool FindRedundantParsers::preorder(const IR::P4Parser *parser) {
for (const IR::ParserState *state : parser->states) {
if (state->name != IR::ParserState::start) {
continue;
}
const auto *pathExpr = state->selectExpression->to<IR::PathExpression>();
if (!pathExpr ||
pathExpr->path->name != IR::ParserState::accept ||
!state->components.empty()) {
continue;
}
LOG4("Found redundant parser " << parser->name);
redundantParsers.insert(parser);
}
return false;
}

const IR::Node *EliminateSubparserCalls::postorder(IR::MethodCallStatement *mcs) {
auto mi = MethodInstance::resolve(mcs->methodCall, refMap, typeMap, true);
if (!mi->isApply()) return mcs;

auto apply = mi->to<ApplyMethod>()->applyObject;
auto parser = apply->to<IR::Type_Parser>();
if (!parser) return mcs;

auto declInstance = mi->object->to<IR::Declaration_Instance>();
if (!declInstance) return mcs;

auto decl = refMap->getDeclaration(declInstance->type->to<IR::Type_Name>()->path);
auto p4parser = decl->to<IR::P4Parser>();
if (!p4parser || !redundantParsers.count(p4parser)) return mcs;

LOG4("Removing apply call to redundant parser " << parser->getName()
<< ": " << *mcs);
return nullptr;
}
}
67 changes: 67 additions & 0 deletions frontends/p4/redundantParsers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2022 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef FRONTENDS_P4_REDUNDANTPARSERS_H_
#define FRONTENDS_P4_REDUNDANTPARSERS_H_

#include "ir/ir.h"
#include "frontends/p4/typeChecking/typeChecker.h"

namespace P4 {

/** Find parsers that have an unconditional "accept" in their start
* state, and put them in redundantParsers.
*/
class FindRedundantParsers : public Inspector {
std::set<const IR::P4Parser *> &redundantParsers;
bool preorder(const IR::P4Parser *parser) override;
public:
explicit FindRedundantParsers(std::set<const IR::P4Parser *> &redundantParsers)
: redundantParsers(redundantParsers) { }
};

/** Find .apply() calls on parsers that are on redundantParsers, and
* eliminate them.
*/
class EliminateSubparserCalls : public Transform {
const std::set<const IR::P4Parser *> &redundantParsers;
ReferenceMap *refMap;
TypeMap *typeMap;
const IR::Node *postorder(IR::MethodCallStatement *methodCallStmt) override;
public:
EliminateSubparserCalls(const std::set<const IR::P4Parser *> &redundantParsers,
ReferenceMap *refMap,
TypeMap *typeMap)
: redundantParsers(redundantParsers), refMap(refMap), typeMap(typeMap)
{ }
};

class RemoveRedundantParsers : public PassManager {
std::set<const IR::P4Parser *> redundantParsers;
public:
RemoveRedundantParsers(ReferenceMap *refMap, TypeMap *typeMap)
: PassManager {
new TypeChecking(refMap, typeMap, true),
new FindRedundantParsers(redundantParsers),
new EliminateSubparserCalls(redundantParsers, refMap, typeMap)
} {
setName("RemoveRedundantParsers");
}
};

}

#endif /* FRONTENDS_P4_REDUNDANTPARSERS_H_ */
13 changes: 0 additions & 13 deletions testdata/p4_16_samples_outputs/parser-arg-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@ parser Parser();
package Package(Parser p1, Parser p2);
parser Parser1_0() {
state start {
transition Inside_start;
}
state Inside_start {
transition start_0;
}
state start_0 {
transition accept;
}
}

parser Parser2_0() {
state start {
transition Inside_start_0;
}
state Inside_start_0 {
transition start_1;
}
state start_1 {
transition accept;
}
}

Package(Parser1_0(), Parser2_0()) main;

0 comments on commit ad5b4a2

Please sign in to comment.