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

Extend & fix FunctionInliner #4801

Merged
merged 3 commits into from
Jul 16, 2024
Merged

Extend & fix FunctionInliner #4801

merged 3 commits into from
Jul 16, 2024

Conversation

asl
Copy link
Contributor

@asl asl commented Jul 13, 2024

  • 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

Fixes #4796

@asl asl requested review from ChrisDodd and mihaibudiu July 13, 2024 02:58
@asl
Copy link
Contributor Author

asl commented Jul 13, 2024

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 FunctionInliner was not quite able to made multiple iterations as stale entries were left on the worklist and they effectively prevented the second iteration. Also, it was not able to deal with the things it created by itself during inlining – variable declarations (for arguments) that contained calls to be inlined.

@fruffy fruffy added the core Topics concerning the core segments of the compiler (frontend, midend, parser) label Jul 14, 2024
Copy link
Contributor

@mihaibudiu mihaibudiu left a 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>
Copy link
Contributor

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

Copy link
Contributor Author

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.

decl->srcInfo, new IR::PathExpression(newDecl->getName()), decl->initializer);
auto *res = inlineBefore(callee, callExpr, assign);

return new IR::Vector<IR::StatOrDecl>{newDecl, res};
Copy link
Contributor

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?

Copy link
Contributor Author

@asl asl Jul 15, 2024

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 collect Declaration_Variable's that have MethodCallExpression 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 by functionInliner by itself on the earlier iteration (for function arguments), everything else is already replaced by initializer-less ones, plus explicit AssignmentStatement.

Are there any other cases to consider?

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

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 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.

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, MoveDeclarations does this.

Copy link
Contributor Author

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.

asl added 3 commits July 15, 2024 18:02
…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>
@asl asl added this pull request to the merge queue Jul 16, 2024
Merged via the queue into p4lang:main with commit e04fdec Jul 16, 2024
17 checks passed
@asl asl deleted the 4796-fix-proper branch July 16, 2024 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Topics concerning the core segments of the compiler (frontend, midend, parser)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Compiler Bug: out_1.p4(118): year;: expected a method call"
5 participants