This is an archive of the discontinued LLVM Phabricator instance.

[InlineCost][TargetTransformInfo][AMDGPU] Consider cost of alloca instructions in the caller (1/2)
ClosedPublic

Authored by jmmartinez on May 3 2023, 5:22 AM.

Details

Summary

On AMDGPU, alloca instructions have penalty that can
be avoided when SROA is applied after inlining.

This patch introduces the default implementation of
TargetTransformInfo::getCallerAllocaCost.

Diff Detail

Event Timeline

jmmartinez created this revision.May 3 2023, 5:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2023, 5:22 AM
jmmartinez requested review of this revision.May 3 2023, 5:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2023, 5:22 AM
mtrofin added a reviewer: kazu.Jun 1 2023, 7:55 AM

I'm missing the AMDGPU bit of this patch, or maybe I'm not understanding something?

Is it possible to add a small unit test, maybe in unittests/Analysis/InlineCostTest.cpp?

I'm missing the AMDGPU bit of this patch, or maybe I'm not understanding something?

Sorry, I put it on the patch stack: https://reviews.llvm.org/D149741

Is it possible to add a small unit test, maybe in unittests/Analysis/InlineCostTest.cpp?

Will do! Thanks!

jmmartinez updated this revision to Diff 528441.Jun 5 2023, 7:42 AM
  • I updated InlineCostFeatureAnalyzer::onInitializeSROAArg to match InlineCostCallAnalyzer::onInitializeSROAArg: I'm not sure this is really desired. I'd expect the ML model to figure out the cost by itself. Maybe it deserves an extra "inline-cost feature" to make the distinction (e.g. like sroa_alloca_cost or sroa_alloca_size)?
  • I've added a unittest in InlineCostTest.cpp. The unittest already present in InlineCostTest.cpp only tested if getInliningCostFeatures does provide a result. I've added a test to check the cost calculated by a small example is correct. The test would be better with lit + filecheck though (I have yet to figure how that's done for InlineCostFeaturesAnalyzer).
mtrofin accepted this revision.Jun 26 2023, 7:46 AM
mtrofin added inline comments.
llvm/unittests/Analysis/InlineCostTest.cpp
220

nit: is there something that we can test that's not 0 (to distinguish from "no effect"). Like maybe the total cost?

This revision is now accepted and ready to land.Jun 26 2023, 7:46 AM
jmmartinez added inline comments.Jun 27 2023, 7:21 AM
llvm/unittests/Analysis/InlineCostTest.cpp
220

I think that would make the test fragile, since any change in the way the cost is calculated would break the test despite not changing how the sroa savings/losses are computed.

Would it be good if I add a test for the case where the sroa cost is lost?

mtrofin added inline comments.Jun 27 2023, 7:35 AM
llvm/unittests/Analysis/InlineCostTest.cpp
220

Not sure what the concern is - if folks change the cost function (not very frequent), a test here would highlight to them whether that side-effect is desired, which is a good thing. Assuming it is, updating should be as easy as updating the number - which is also a good thing, when tracking down regressions, it offers an additional signal ("here's one effect of this change").

Adding an additional test for where the sroa cost is lost is good, too.

jmmartinez added inline comments.Jun 27 2023, 9:49 AM
llvm/unittests/Analysis/InlineCostTest.cpp
220

Adding an additional test for where the sroa cost is lost is good, too.

Will do!

Not sure what the concern is - if folks change the cost function (not very frequent), a test here would highlight to them whether that side-effect is desired, which is a good thing. Assuming it is, updating should be as easy as updating the number - which is also a good thing, when tracking down regressions, it offers an additional signal ("here's one effect of this change").

I've just realized that there is no "total cost" equivalent in the InliningCostFeatures. The only values affected by this modification are the sroa savings and losses. These costs do not get accumulated in a single value since they are used to feed the MLInlineAdvisor. Or am I missing something?

mtrofin added inline comments.Jun 27 2023, 11:35 AM
llvm/lib/Analysis/InlineCost.cpp
720–722

nit: can you do this in 2 steps, easier to read. same in onInitializeSROAArg. thanks

llvm/unittests/Analysis/InlineCostTest.cpp
220

SROAArgCosts is used for total cost estimation - see onDisableSROA

jmmartinez marked an inline comment as done.
  • Added sroa_losses unittest
  • Refactored common code in unittest
jmmartinez added inline comments.Jun 28 2023, 2:26 AM
llvm/unittests/Analysis/InlineCostTest.cpp
220

There is InlineCostCallAnalyzer::onDisableSROA, where the cost is used for the total estimation,

But there is the very similar InlineCostFeatureAnalyzer::onDisableSROA where the cost is only used for the SROA savings/losses estimation.

In the unittests the second one is getting tested, where there is no total cost estimation.

  • Updated one place where I forgot to split the increment of the sroa cost in two lines.
mtrofin accepted this revision.Jun 28 2023, 8:48 AM

lgtm