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

Avoid copying out/inout args when inlining functions #4877

Merged
merged 1 commit into from
Sep 20, 2024

Conversation

ChrisDodd
Copy link
Contributor

This is a proposal/work in progress for a way of reducing the number of copies introduced when inlining.

On particularly problematic case is when you have a big struct (eg, all the headers) that is passed around to functions as an inout argument so they can access/modify it. Introducing a copy of all the headers is particularly inefficient, and tough to later optimize away if it was unnecessary.

This change just has a minimal check -- if the actual argument passed to an inout or out argument is local to the caller, it can't possibly be accessed by the callee directly, so no copy is needed -- the inlined code can just access it directly.

A more general check would be to actually look at the callee -- if it does not access whatever is passed as an argument directly or indirectly, then no copy is needed. The indirectly part is complex -- requires looking recursively at whatever the callee calls, including any extern functions or methods on instances.

Or perhaps there should be a target-specific policy that controls this -- would allow better target-specific understanding of externs and what they do.

The problem is connected to doing too much in the frontend -- function inlining happens in the frontend (when arguably it should not), in order to allow inlining into type params (bit<N> and int<N> in particular). If we remove the general inlining in the frontend (and only do it in the midend), it would greatly help with target-specific policies and tweaks.

@fruffy fruffy added core Topics concerning the core segments of the compiler (frontend, midend, parser) run-validation Use this tag to trigger a Validation CI run. labels Aug 21, 2024
@ChrisDodd ChrisDodd force-pushed the cdodd-fninline branch 3 times, most recently from 89b945e to 0da1153 Compare September 17, 2024 21:24
@ChrisDodd ChrisDodd marked this pull request as ready for review September 17, 2024 22:41
@ChrisDodd
Copy link
Contributor Author

While it would be nice to cover more cases, this is a useful start as it is. The main thing it does not catch is when a control calls a (global) function with a local or param of the control as an inout argument. That would be nice to catch, but would require determining that the control does not do something with that local/param that will result in it effectively being aliased to something else in the global function.

Copy link
Contributor

@asl asl left a comment

Choose a reason for hiding this comment

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

In general, looks good to me, some minor comments below.

frontends/p4/functionsInlining.cpp Show resolved Hide resolved
frontends/p4/functionsInlining.cpp Outdated Show resolved Hide resolved
frontends/p4/functionsInlining.cpp Outdated Show resolved Hide resolved
@ChrisDodd ChrisDodd force-pushed the cdodd-fninline branch 2 times, most recently from 77eaad7 to 7e6ea8d Compare September 20, 2024 01:47
- If the actual argument for an out or inout argument is local to the
  calling functions, we can use it directly in the inlined function
  instead of introducing extra copies in and out.

Signed-off-by: Chris Dodd <cdodd@nvidia.com>
@ChrisDodd ChrisDodd added this pull request to the merge queue Sep 20, 2024
Merged via the queue into p4lang:main with commit 6497f39 Sep 20, 2024
18 checks passed
@ChrisDodd ChrisDodd deleted the cdodd-fninline branch September 20, 2024 05:42
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) run-validation Use this tag to trigger a Validation CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants