-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove apply() calls to redundant parsers (#3356)
* 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
Showing
5 changed files
with
130 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters