Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove apply() calls to redundant parsers #3356

Merged
merged 4 commits into from
Jun 25, 2022

Conversation

bleibig
Copy link
Contributor

@bleibig bleibig commented May 24, 2022

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.

frontends/CMakeLists.txt Show resolved Hide resolved

}

#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpplint will want this to be followed by a comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

limitations under the License.
*/

#ifndef _P4_REDUNDANT_PARSERS_H
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the same format for the macro as in other frontend files; otherwise cpplint will be upset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,63 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not 2022?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

* state, and put them in redundantParsers.
*/
class FindRedundantParsers : public Inspector {
std::set<cstring> &redundantParsers;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why string and not IR::P4Parser?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using IR::P4Parser *, it didn't quite work because the pointers discovered in RemoveRedundantParsers did not match those found earlier, despite having the same name. Is there supposed to be a single instance of every declaration in the IR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a transform you should use getOriginal(), there are lots of examples in the code

RemoveRedundantParsers(TypeMap *typeMap)
: PassManager {
new FindRedundantParsers(redundantParsers),
new EliminateSubparserCalls(redundantParsers, typeMap)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typeMap will always be empty the way you inserted this pass.
Don't you need to invoke the typechecker first?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do you will need a handle to the referenceMap too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to run before ClearTypeMap. Though from my testing, even before the type map was not empty when this pass was run.

}

const IR::Node *EliminateSubparserCalls::postorder(IR::MethodCallStatement *mcs) {
auto method = mcs->methodCall->method;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a helper class called MethodInstance which can help with this.
It will return an ApplyMethod object for a parser invocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using that, but I was hitting some assertion failures. Maybe there are still some issues with MethodInstance, I can look into that.

@bleibig bleibig force-pushed the bleibig/redundant-parsers branch from 00f1389 to 13cb8ca Compare June 9, 2022 20:14
@bleibig
Copy link
Contributor Author

bleibig commented Jun 10, 2022

I've updated the code to use MethodInstance. It required adding some extra checks to make sure it doesn't cause an error on calls that are not .apply(). I was also able to change the set tracking the parsers to use IR::P4Parser as well.

@bleibig bleibig requested a review from mihaibudiu June 13, 2022 17:08
@@ -0,0 +1,66 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you really wait 9 years to subit this patch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}

const IR::Node *EliminateSubparserCalls::postorder(IR::MethodCallStatement *mcs) {
if (mcs->methodCall->method->type->is<IR::Type_Unknown>()) return mcs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the type field of an expression is not to be trusted unless you just set it up yourself (probably by calling typeChecking with a 'true' flag).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to run type checking before the new passes

public:
explicit RemoveRedundantParsers(ReferenceMap *refMap)
: PassManager {
new FindRedundantParsers(redundantParsers),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want the 'type' field to be correct then you have to insert a call to typeChecking here. But it's probably simpler in that case to pass the computed typeMap annd use MethodInstance with a typeMap argument.

bleibig added 4 commits June 16, 2022 16:39
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.
Also change the parser tracking to use IR::P4Parser pointers
@bleibig bleibig force-pushed the bleibig/redundant-parsers branch from 13cb8ca to 13842c2 Compare June 17, 2022 23:15
@bleibig bleibig requested a review from mihaibudiu June 21, 2022 21:35
@bleibig
Copy link
Contributor Author

bleibig commented Jun 24, 2022

It looks like the one CI failure is from an internal build system issue, otherwise it's passing. Is this ready to merge in?

@mihaibudiu mihaibudiu merged commit ad5b4a2 into p4lang:main Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants