-
Notifications
You must be signed in to change notification settings - Fork 442
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
Extend & fix FunctionInliner #4801
Conversation
This PR supersedes #4800 I made it on top of it, so the difference in the test output could be seem from the top commit (5c1e37f#diff-ff529ab5b2ef38af72effa10270da859185b7a906f3e49d50dfbf4db7d790f5e). It turned out that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't say that I remember this code very well, but this seems fine.
@@ -0,0 +1,56 @@ | |||
#include <core.p4> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be probably smaller to highlight just the problem.
you don't need to use pna.p4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, right. Just wanted to add exactly the testcase from the issue.
frontends/p4/functionsInlining.cpp
Outdated
decl->srcInfo, new IR::PathExpression(newDecl->getName()), decl->initializer); | ||
auto *res = inlineBefore(callee, callExpr, assign); | ||
|
||
return new IR::Vector<IR::StatOrDecl>{newDecl, res}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am assuming this requires a block expression to be legal in the calling context.
is this always true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be missing something, but it seems to be so:
DiscoverFunctionsInlining
would collectDeclaration_Variable
's that haveMethodCallExpression
inside the initializer and only those.- In fact, at this time the only cases for such
Declaration_Variable
's are the ones that were created byfunctionInliner
by itself on the earlier iteration (for function arguments), everything else is already replaced by initializer-less ones, plus explicitAssignmentStatement
.
Are there any other cases to consider?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the only thing I can think of are constant declarations.
If a constant initializer can involve action calls, it may be a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have difficulty imagining how a constant initializer could involve an action call, since actions calls are not expressions that return values. Thus I believe they cannot be part of an expression, only a full statement by themselves.
I constant initializer could involve function calls, of course. Not sure if that is relevant here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constants are Declaration_Constant
, so they are excluded here. And per spec:
Variables declarations can appear in the following locations within a P4 program:
* In a block statement,
* In a parser state,
* In an action body,
* In a control block's apply sub-block,
* In the list of local declarations in a parser, and
* In the list of local declarations in a control.
So, I think block expression is legal in all these places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now I think you can leave it as it is. If somebody complains you have a hypothesis.
Or you can try to write a test to see if you can force declarations in this position. Maybe it's impossible. Check the shape of the program before this pass is run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, if there is an invariant that variable initializers are already de-sugared at this stage, then we should not create new ones, right? This would actually make Declaration_Variable
case here unused as everything will be handled by the existing AssignmentStatement
handle. This probably will be cleaner solution, let me try this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the MoveDeclarations is the pass which does this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, MoveDeclarations does this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I went ahead and just create uninitialized variable there. It seems that existing AssignmentStatement
case perfectly handles it, no need for something additional.
…inliner could inline only directly into RHS of an assignment statement Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
- Ensure it really can do multiple iterations via clearing stale worklist - Allow inlining for more complex cases: LHS of assignment and variable initializers Some cleanup while there Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
…coming arguments and use explicit AssignmentStatement to initialize this. This allows the existing code to handle it without additional Declaration_Variable case Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
Some cleanup while there
Fixes #4796