Skip to content

Commit

Permalink
Do not check for shadowing repeatedly in inlining passes. (#4396)
Browse files Browse the repository at this point in the history
  • Loading branch information
kfcripps authored Feb 12, 2024
1 parent 9f24135 commit 02f89f7
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions frontends/common/resolveReferences/referenceMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void ReferenceMap::clear() {
LOG2("Clearing reference map");
pathToDeclaration.clear();
usedNames.clear();
program = nullptr;
used.clear();
thisToDeclaration.clear();
for (auto &reserved : P4::reservedWords) usedNames.insert({reserved, 0});
Expand Down
3 changes: 2 additions & 1 deletion frontends/common/resolveReferences/resolveReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ void ResolveReferences::checkShadowing(const IR::INamespace *ns) const {

Visitor::profile_t ResolveReferences::init_apply(const IR::Node *node) {
anyOrder = refMap->isV1();
if (!refMap->checkMap(node)) refMap->clear();
// Check shadowing even if the program map is up-to-date.
if (!refMap->checkMap(node) || checkShadow) refMap->clear();
return Inspector::init_apply(node);
}

Expand Down
3 changes: 2 additions & 1 deletion frontends/p4/actionsInlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void DiscoverActionsInlining::postorder(const IR::MethodCallStatement *mcs) {
}

Visitor::profile_t ActionsInliner::init_apply(const IR::Node *node) {
P4::ResolveReferences solver(refMap, true);
P4::ResolveReferences solver(refMap);
refMap->clear();
node->apply(solver);
LOG2("ActionsInliner " << toInline);
return Transform::init_apply(node);
Expand Down
7 changes: 2 additions & 5 deletions frontends/p4/actionsInlining.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ class ActionsInliner : public AbstractInliner<ActionsInlineList, AInlineWorkList
std::map<const IR::MethodCallStatement *, const IR::P4Action *> *replMap;

public:
explicit ActionsInliner(bool isv1) : refMap(new P4::ReferenceMap()), replMap(nullptr) {
refMap->setIsV1(isv1);
}
explicit ActionsInliner(P4::ReferenceMap *refMap) : refMap(refMap), replMap(nullptr) {}
Visitor::profile_t init_apply(const IR::Node *node) override;
const IR::Node *preorder(IR::P4Parser *cont) override {
prune();
Expand All @@ -73,8 +71,7 @@ class InlineActions : public PassManager {
InlineActions(ReferenceMap *refMap, TypeMap *typeMap) {
passes.push_back(new TypeChecking(refMap, typeMap));
passes.push_back(new DiscoverActionsInlining(&actionsToInline, refMap, typeMap));
passes.push_back(
new InlineActionsDriver(&actionsToInline, new ActionsInliner(refMap->isV1())));
passes.push_back(new InlineActionsDriver(&actionsToInline, new ActionsInliner(refMap)));
passes.push_back(new RemoveAllUnusedDeclarations(refMap));
setName("InlineActions");
}
Expand Down
7 changes: 5 additions & 2 deletions frontends/p4/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ const IR::P4Program *FrontEnd::run(const CompilerOptions &options, const IR::P4P
new ValidateParsedProgram(),
// Synthesize some built-in constructs
new CreateBuiltins(),
new ResolveReferences(&refMap, true), // check shadowing
new ResolveReferences(&refMap, /* checkShadow */ true),
// First pass of constant folding, before types are known --
// may be needed to compute types.
new ConstantFolding(&refMap, nullptr),
// Desugars direct parser and control applications
// into instantiations followed by application
new InstantiateDirectCalls(&refMap),
new ResolveReferences(&refMap), // check shadowing
new ResolveReferences(&refMap),
new Deprecated(&refMap),
new CheckNamedArgs(),
// Type checking and type inference. Also inserts
Expand Down Expand Up @@ -258,6 +258,9 @@ const IR::P4Program *FrontEnd::run(const CompilerOptions &options, const IR::P4P
new SimplifyControlFlow(&refMap, &typeMap),
});
passes.addPasses({
// Check for shadowing after all inlining passes. We disable this
// check during inlining since it significantly slows compilation.
new ResolveReferences(&refMap, /* checkShadow */ true),
new HierarchicalNames(),
new FrontEndLast(),
});
Expand Down
3 changes: 2 additions & 1 deletion frontends/p4/functionsInlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void FunctionsInliner::end_apply(const IR::Node *) {
}

Visitor::profile_t FunctionsInliner::init_apply(const IR::Node *node) {
P4::ResolveReferences solver(refMap, true);
P4::ResolveReferences solver(refMap);
refMap->clear();
node->apply(solver);
LOG2("FunctionsInliner " << toInline);
return Transform::init_apply(node);
Expand Down
1 change: 1 addition & 0 deletions frontends/p4/inlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ bool DiscoverInlining::preorder(const IR::ParserBlock *block) {
Visitor::profile_t GeneralInliner::init_apply(const IR::Node *node) {
ResolveReferences solver(refMap);
TypeChecking typeChecker(refMap, typeMap);
refMap->clear();
node->apply(solver);
(void)node->apply(typeChecker);
return AbstractInliner::init_apply(node);
Expand Down
7 changes: 3 additions & 4 deletions frontends/p4/inlining.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,12 @@ class GeneralInliner : public AbstractInliner<InlineList, InlineSummary> {
bool optimizeParserInlining;

public:
explicit GeneralInliner(bool isv1, bool _optimizeParserInlining)
: refMap(new ReferenceMap()),
explicit GeneralInliner(ReferenceMap *refMap, bool _optimizeParserInlining)
: refMap(refMap),
typeMap(new TypeMap()),
workToDo(nullptr),
optimizeParserInlining(_optimizeParserInlining) {
setName("GeneralInliner");
refMap->setIsV1(isv1);
visitDagOnce = false;
}
// controlled visiting order
Expand Down Expand Up @@ -436,7 +435,7 @@ class InlinePass : public PassManager {
: PassManager({new TypeChecking(refMap, typeMap),
new DiscoverInlining(&toInline, refMap, typeMap, evaluator),
new InlineDriver<InlineList, InlineSummary>(
&toInline, new GeneralInliner(refMap->isV1(), optimizeParserInlining)),
&toInline, new GeneralInliner(refMap, optimizeParserInlining)),
new RemoveAllUnusedDeclarations(refMap)}) {
setName("InlinePass");
}
Expand Down

0 comments on commit 02f89f7

Please sign in to comment.