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

Teach functions inliner to inline into if condition #5073

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

asl
Copy link
Contributor

@asl asl commented Dec 19, 2024

The problem is hidden in many cases by side effects ordering, however, it could be further exposed for action arguments after actions inlining.

Without this PR we're ending in ICE trying to perform an inline as inliner can only do this for plain MethodCallStatement and AssignmentStatement.

@asl asl requested review from vlstill, ChrisDodd and fruffy December 19, 2024 00:45
Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
@asl asl force-pushed the func-inline-cond branch from cdbbbc8 to 7ba5781 Compare December 19, 2024 02:42
@fruffy fruffy added the core Topics concerning the core segments of the compiler (frontend, midend, parser) label Dec 19, 2024
@jafingerhut
Copy link
Contributor

What happens today, without these enhancements?

Do function calls within if condition expressions simply not get expanded, and then it is up to the back end to deal with it, or not?

@asl
Copy link
Contributor Author

asl commented Dec 22, 2024

What happens today, without these enhancements?

We're having a compiler internal error. So, this is not a performance enhancement, rather then ability to compile certain code (see testcase :) )

To be precise, this BUG_CHECK fires: https://github.com/p4lang/p4c/pull/5073/files#diff-e4d3e15d12ab83675782d2e894a3a319c7b9b4bc72c85af23571a194efc0f8e4L40

@ChrisDodd
Copy link
Contributor

One would expect that side-effect ordering would pull the function call out of the if condition into a separate assignment to a temp, but perhaps if running with a backend that disables side-effect ordering. The change just looks like it is duplicating the effect of side-effect ordering of calls in if conditions as part of the inline, which is perhaps more efficient -- only create one temp bool instead of two, though later local-copyprop will get rid of the extra one.

@asl
Copy link
Contributor Author

asl commented Dec 23, 2024

@ChrisDodd

but perhaps if running with a backend that disables side-effect ordering.

It is not a backend that disables side effect ordering. The testcase reproduces with plain p4c. Side effect ordering is "broken" after actions inlining: see, there are no "side effects" for y1 in action a, however, they will appear after actions inlining.

Overall, relying on side effect ordering seems to be a huge downside as it is pretty fragile (in terms of optimizations), it does create lots of temporaries that make memory consumption of e.g. use-def to skyrocket. It looks like side effect ordering is an alternative to e.g. SSA form, however, it does not hold the same invariants as proper SSA form, so one cannot rely on them.

BUG_CHECK(bool(RTTI::isAny<IR::MethodCallStatement, IR::AssignmentStatement>(stat)),
"%1%: unexpected statement with call", stat);
BUG_CHECK(
bool(RTTI::isAny<IR::MethodCallStatement, IR::AssignmentStatement, IR::IfStatement>(stat)),
Copy link
Contributor

Choose a reason for hiding this comment

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

The bool( cast should not be necessary.

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.

5 participants