This is an archive of the discontinued LLVM Phabricator instance.

[InlineCost] CallAnalyzer: use TTI info for extractvalue - they are free (PR50099)
ClosedPublic

Authored by lebedev.ri on Apr 24 2021, 1:13 AM.

Details

Summary

It seems incorrect to use TTI data in some places, and override it in others.
In this case, TTI says that extractvalue are free, yet we bill them.

While this doesn't address https://bugs.llvm.org/show_bug.cgi?id=50099 yet,
it reduces the cost from 55 to 50 while the threshold is 45.

Diff Detail

Event Timeline

lebedev.ri created this revision.Apr 24 2021, 1:13 AM
lebedev.ri requested review of this revision.Apr 24 2021, 1:13 AM
xbolva00 added inline comments.
llvm/lib/Analysis/InlineCost.cpp
1757

Just question: this check is also in visitInstruction so maybe you can just return visitInstruction(I)?

lebedev.ri marked an inline comment as done.Apr 24 2021, 10:27 AM
lebedev.ri added inline comments.
llvm/lib/Analysis/InlineCost.cpp
1757

Not really, they have different side-effects.

aeubanks added inline comments.Apr 24 2021, 12:42 PM
llvm/lib/Analysis/InlineCost.cpp
1757

do you mean the disableSROA? shouldn't that be happening anyway? or else we should handle SROA for extractvalue.

1766

should we do the same for insertvalue?

llvm/test/Transforms/Inline/X86/extractvalue.ll
1

can the first RUN be deleted?

lebedev.ri marked 3 inline comments as done.Apr 24 2021, 12:55 PM
lebedev.ri added inline comments.
llvm/lib/Analysis/InlineCost.cpp
1757

do you mean the disableSROA?

Yes.

shouldn't that be happening anyway? or else we should handle SROA for extractvalue.

Well, not as per the comment at the end of this function:

// SROA can look through these but give them a cost.
return false;

Regardless of whether or not that is wrong, it seems like a separate problem for another patch.

Regardless, deferring to visitInstruction() seems wrong,
because i think getUserCost() is much cheaper than the simplifyInstruction()
that would run first.

1766

For consistency - maybe.
To model them as free - are insertvalues free? D66098 only modelled extractvalues.
So again, seems like a separate issue.

llvm/test/Transforms/Inline/X86/extractvalue.ll
1

I'm only following the example in other tests in the directory.
I think if it's not needed, then perhaps all other such runlines should be dropped first?

lebedev.ri marked 2 inline comments as done.Apr 24 2021, 1:04 PM

@aeubanks thank you for taking a look!

aeubanks added inline comments.Apr 26 2021, 11:14 AM
llvm/lib/Analysis/InlineCost.cpp
1757

If we first check getUserCost() then we may miss out on some simplification that CallAnalyzer::simplifyInstruction() could do for future users of the extractelement. It seems more in line (hehe) with the rest of CallAnalyzer to call simplifyInstruction() first. Lots of CallAnalyzer methods immediately call simplifyInstruction().

Also, looking at SROA.cpp, it doesn't seem to handle extractelements.

1766

oh, of course insertvalues aren't free, ignore my comment

lebedev.ri marked an inline comment as done and 4 inline comments as not done.Apr 26 2021, 1:01 PM

@aeubanks thank you for taking a look!

llvm/lib/Analysis/InlineCost.cpp
1757

Aha, okay, let's do that then.

1766

Note that i'm not saying that they are/aren't free.
I'm only saying that currently they are modelled as non-free.
I *suspect* they may also be free, but they are so rare
that i haven't seen a motivational example including them...

lebedev.ri marked 4 inline comments as done.

Address inline comment.

looks like the child patch somehow found its way here

llvm/lib/Analysis/InlineCost.cpp
1763

I think Base::visitExtractValue() is the proper way

lebedev.ri marked an inline comment as done.

@aeubanks thank you for taking a look!
Addressing inline comments.

llvm/lib/Analysis/InlineCost.cpp
1763

Well, then all the code that uses this "proper way" is likely broken,
and either doesn't have test coverage,
or the test changes weren't analyzed well enough.

Because then we don't fall back to CallAnalyzer::visitInstruction(),
and don't consider it as zero-cost as per costmodel...

overall looks good now, just nits

llvm/lib/Analysis/InlineCost.cpp
1763

I thought Base::visitExtractValue() ends up calling CallAnalyzer::visitInstruction(). Base::visitExtractValue() goes through InstVisitor's instruction hierarchy to figure out what to call next. Currently it always ends up calling CallAnalyzer::visitInstruction(), but if in the future InstVisitor put something in between visitExtractValue() and visitInstruction() then we'd want to respect that.

lebedev.ri marked an inline comment as done.Apr 29 2021, 11:32 AM

@aeubanks thank you for taking a look!
The nit is non-addressable, so no changes are made.

llvm/lib/Analysis/InlineCost.cpp
1763

I thought Base::visitExtractValue() ends up calling CallAnalyzer::visitInstruction().

That is not what is happening, no.
Moreover, i'm not sure InstVisitor would ever do that.

aeubanks added inline comments.Apr 29 2021, 2:58 PM
llvm/lib/Analysis/InlineCost.cpp
1763

I took a closer look, InstVisitor does do something like that, but in this case it calls CallAnalyzer::visitUnaryInstruction() before getting to visitInstruction(). If we delete CallAnalyzer::visitUnaryInstruction() what I suggested should work. D101577 to remove visitUnaryInstruction().

This revision was not accepted when it landed; it landed in state Needs Review.Apr 30 2021, 3:55 AM
This revision was automatically updated to reflect the committed changes.
lebedev.ri marked an inline comment as done.

@aeubanks thank you for the review and cleaning the CallAnalyzer's InstVisitor!