This is an archive of the discontinued LLVM Phabricator instance.

[FunctionAttrs] Unconditionally perform argument attribute inference in the first function-attrs pass
ClosedPublic

Authored by cfang on Jul 27 2023, 12:13 AM.

Details

Summary

Argument attributes like NoAlias and ReadOnly could affect memoryssa and thus earlyCSE in the function simplification pipeline.
https://reviews.llvm.org/D145210 adjusted PostOrderFunctionAttrs placement and caused the argument attributes not referred for the use
in the pipeline. This work (initiated by @nikic) unconditionally performs argument attribute inference in the first function-attrs pass.

Based on @nikic's experiment, the compile-time impact is pretty low: http://llvm-compile-time-tracker.com/compare.php?from=74258f4189e2b6bacabd40ee6f588fd9d1b37741&to=7f7484aee497cdae817b4aa24aed8046f430a816&stat=instructions%3Au

Diff Detail

Event Timeline

cfang created this revision.Jul 27 2023, 12:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 12:13 AM
cfang requested review of this revision.Jul 27 2023, 12:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 12:13 AM
Herald added a subscriber: wdng. · View Herald Transcript
nikic added inline comments.Jul 27 2023, 12:28 AM
llvm/lib/Transforms/IPO/FunctionAttrs.cpp
1769

Add some extra comment here: "Only infer readonly etc on arguments in that case, because it can affect optimization behavior in conjunction with noalias" or something like that.

llvm/test/Transforms/PhaseOrdering/arg-attrs-affect-earlycse.ll
174 ↗(On Diff #544623)

It should be possible to massively simplify this test by using the memssa-check-limit option.

Please drop all the irrelevant noise, especially anything related to address spaces.

cfang updated this revision to Diff 544918.Jul 27 2023, 1:59 PM
cfang marked 2 inline comments as done.

Add extra comment as suggested, and further reduce the test case. Thanks.

aeubanks added inline comments.Jul 28 2023, 11:33 AM
llvm/test/Transforms/PhaseOrdering/arg-attrs-affect-earlycse.ll
1 ↗(On Diff #544918)

nit: extra space before RUN

3 ↗(On Diff #544918)

I don't think it's a good idea in these phase ordering tests to specifically check that a specific pass did something, future pipeline changes can break this; the whole point of these is that the entire pipeline gives a good result regardless of exactly what's happening in the pipelines. This should use llvm/utils/update_test_checks.py like the other phase ordering tests to check the final IR. Although I tried that for this test and this patch doesn't change it, so I think you'll need to change this test to get something where this patch actually improves something. Testing with -memssa-check-limit=1 is fine though.

6 ↗(On Diff #544918)

these function properties can be removed

48 ↗(On Diff #544918)

can be a declaration

53 ↗(On Diff #544918)

function attributes can be dropped

cfang marked 5 inline comments as done.Jul 28 2023, 2:25 PM
cfang added inline comments.
llvm/test/Transforms/PhaseOrdering/arg-attrs-affect-earlycse.ll
3 ↗(On Diff #544918)

We could not successfully extract a small test from the huge application that can show the difference after the pipeline.
Would it be fine to check the IR after early-cse<memssa>? Thanks. and appreciate the the help and suggestions.

cfang updated this revision to Diff 545281.Jul 28 2023, 2:26 PM
cfang marked an inline comment as done.

Update the test based on the suggestions.

cfang added a comment.Aug 1 2023, 10:22 AM

Is there any further suggestion on the test?
Currently we are using "update_test_checks.py" to generate checks automatically that can show the difference before and after the patch.
I have the note in the test to express our interests. This seems the best I can do at this moment. Thanks.

sorry for the late response

as I keep saying, the test is still not really a phase ordering test because it's not exercising the entire pipeline, you're printing the IR very early on in the pipeline, but you should be printing the IR at the very end of the pipeline: opt -O3 -S < %s. please find a test that is a proper phase ordering test and also is affected by this func-attrs change

the example you gave in D145210 and the test added here both are ultimately unaffected by this patch, if you look at the entire -O3 pipeline with and without this patch

aeubanks added a comment.EditedAug 8 2023, 10:55 AM

here's a test case that this patch fixes

; RUN: opt -S -O3 -memssa-check-limit=1 -memdep-block-scan-limit=1 < %s | FileCheck %s
declare void @g()

define i32 @f(ptr noalias %p, i32 %c) {
  %i = load i32, ptr %p
  call void @g()
  call void @g()
  call void @g()
  call void @g()
  call void @g()
  call void @g()
  call void @g()
  call void @g()
  call void @g()
  call void @g()
  call void @g()
  %i2 = load i32, ptr %p
  %r = sub i32 %i, %i2
  ret i32 %r
}

-memdep-block-scan-limit is the equivalent of -memssa-check-limit for memdep analysis which GVN still uses

cfang updated this revision to Diff 548315.Aug 8 2023, 12:24 PM

Update the LIT test with the example @aeubanks developed. Thanks.

nikic added inline comments.Aug 8 2023, 12:28 PM
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
7340 ↗(On Diff #548315)

Debugging leftover?

llvm/test/Transforms/PhaseOrdering/early-arg-attrs-inference.ll
3

Please pre-commit the test.

cfang updated this revision to Diff 548316.Aug 8 2023, 12:29 PM

Remove an accidental return in the code.

last nit then I think this is good to go
function-attrs<skip-non-recursive> should be renamed to something like function-attrs<non-recursive-only-arg-attrs>

llvm/lib/Transforms/Utils/SimplifyCFG.cpp
7340 ↗(On Diff #548315)

unintenitonal change?

cfang added a comment.Aug 8 2023, 12:51 PM

last nit then I think this is good to go
function-attrs<skip-non-recursive> should be renamed to something like function-attrs<non-recursive-only-arg-attrs>

Should it be function-attrs<recursive-or-arg-attrs-only> ?

last nit then I think this is good to go
function-attrs<skip-non-recursive> should be renamed to something like function-attrs<non-recursive-only-arg-attrs>

Should it be function-attrs<recursive-or-arg-attrs-only> ?

The "or" is very declarative as opposed to imperative, which I think is more confusing. I imagine function-attrs as performing everything, then <foo> is a constraint on top of that, so something like <skip-non-recursive-function-attrs> makes more sense to me.

llvm/lib/Transforms/IPO/FunctionAttrs.cpp
1769

I'd remove this comment and change below to Only infer argument attributes for non-recursive functions, because...

in terms of the request to pre-commit the test, split out the creation of the test into a separate patch (with update_test_checks.py run on it with ToT opt), then rebase this patch on top of that with update_test_checks.py reran with these changes, and then in this patch we can the diff on the test case that this patch causes

(basically the workflow explained in the comments in llvm/utils/update_test_checks.py)

cfang added a comment.Aug 8 2023, 1:51 PM

> The "or" is very declarative as opposed to imperative, which I think is more confusing. I imagine function-attrs as performing everything, then <foo> is a constraint on top of that, so something like <skip-non-recursive-function-attrs> makes more sense to me.

Do you mean to replace "skip-non-recursive" with "skip-non-recursive-function-attrs"? (this does not say anything about argument attribute explicitly)
Do you also want the variable name and related comments to be changed?
"bool SkipNonRecursive" for example?

cfang updated this revision to Diff 548345.Aug 8 2023, 1:54 PM

update a comment
add pre-commit of the LIT to show the diff from this patch.

cfang updated this revision to Diff 548451.Aug 8 2023, 8:49 PM

Rename to "skip-non-recursive-function-attrs" as suggested

cfang updated this revision to Diff 548456.Aug 8 2023, 9:35 PM
aeubanks accepted this revision.Aug 9 2023, 8:07 AM

lgtm

This revision is now accepted and ready to land.Aug 9 2023, 8:07 AM
This revision was landed with ongoing or failed builds.Aug 9 2023, 5:50 PM
This revision was automatically updated to reflect the committed changes.