Skip to content

Commit

Permalink
Run type checking before removing redundant parsers
Browse files Browse the repository at this point in the history
Also update copyright fields
  • Loading branch information
bleibig committed Jun 17, 2022
1 parent bdf5807 commit 13842c2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 30 deletions.
2 changes: 1 addition & 1 deletion frontends/p4/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +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),
new RemoveRedundantParsers(&refMap, &typeMap),
new ClearTypeMap(&typeMap),
evaluator,
new Inline(&refMap, &typeMap, evaluator, options.optimizeParserInlining),
Expand Down
11 changes: 2 additions & 9 deletions frontends/p4/redundantParsers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
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.
Expand Down Expand Up @@ -38,14 +38,7 @@ bool FindRedundantParsers::preorder(const IR::P4Parser *parser) {
}

const IR::Node *EliminateSubparserCalls::postorder(IR::MethodCallStatement *mcs) {
if (mcs->methodCall->method->type->is<IR::Type_Unknown>()) return mcs;
auto mem = mcs->methodCall->method->to<IR::Member>();
if (!mem) return mcs;

auto pe = mem->expr->to<IR::PathExpression>();
if (!pe || !pe->type->is<IR::IApply>()) return mcs;

auto mi = MethodInstance::resolve(mcs->methodCall, refMap, nullptr, true);
auto mi = MethodInstance::resolve(mcs->methodCall, refMap, typeMap, true);
if (!mi->isApply()) return mcs;

auto apply = mi->to<ApplyMethod>()->applyObject;
Expand Down
16 changes: 9 additions & 7 deletions frontends/p4/redundantParsers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022-present Barefoot Networks, Inc.
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.
Expand All @@ -18,11 +18,10 @@ limitations under the License.
#define FRONTENDS_P4_REDUNDANTPARSERS_H_

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

namespace P4 {

class ReferenceMap;

/** Find parsers that have an unconditional "accept" in their start
* state, and put them in redundantParsers.
*/
Expand All @@ -40,21 +39,24 @@ class FindRedundantParsers : public Inspector {
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)
: redundantParsers(redundantParsers), refMap(refMap)
ReferenceMap *refMap,
TypeMap *typeMap)
: redundantParsers(redundantParsers), refMap(refMap), typeMap(typeMap)
{ }
};

class RemoveRedundantParsers : public PassManager {
std::set<const IR::P4Parser *> redundantParsers;
public:
explicit RemoveRedundantParsers(ReferenceMap *refMap)
RemoveRedundantParsers(ReferenceMap *refMap, TypeMap *typeMap)
: PassManager {
new TypeChecking(refMap, typeMap, true),
new FindRedundantParsers(redundantParsers),
new EliminateSubparserCalls(redundantParsers, refMap)
new EliminateSubparserCalls(redundantParsers, refMap, typeMap)
} {
setName("RemoveRedundantParsers");
}
Expand Down
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 13842c2

Please sign in to comment.