[PATCH] D145516: [Inliner] Avoid excessive inlining through devirtualised calls

Jeremy Morse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 30 07:03:02 PDT 2023


jmorse updated this revision to Diff 509647.
jmorse added a comment.
Herald added subscribers: ChuanqiXu, haicheng.

I've changed my mind: actually there are scenarios where being inlined into a different context should enable inlining that was previously skipped, for example where an unlikely branch can statically found to be always true after inlining, justifying inlining whatever's down that branch. This has exposed the deeper issue and I've now got an updated test that does not rely on profile data.

I believe today any mutually recursive functions will be inlined until they become directly recursive, after which inlining is prohibited. The trouble comes when the mutual inlining is hidden because of a prior decision to not inline a call site, that then becomes enabled in a new + different context, which then enables a cycle to be explored by InlinerPass::run. The non-profile-data example below is very similar to before, but hides the cycle by making the longname <=> rec_call_to_longname interaction dependent on a function pointer passed in to rec_call_to_longname. Neither can be inlined into each other by themselves; but in the context of the `a` function, all the function calls get devirtualised and the cycle explored, exponentially in the chain from `e` to `a`. Attaching the "function-inline-cost-multiplier" attribute suppresses this. I also patch part of the inline-cost computation that can accidentally send the cost negative, thus defeating multipliers.

This is still rare because:

- Passing a function a pointer to itself isn't a common pattern,
- I'm effectively forcing the chain of functions from `e` to `a` to be inlined by marking them `internal`, which get an inlining threshold boost,

But there could be other scenarios where


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D145516/new/

https://reviews.llvm.org/D145516

Files:
  llvm/lib/Analysis/InlineCost.cpp
  llvm/lib/Transforms/IPO/Inliner.cpp
  llvm/test/Transforms/Inline/devirtualize-7.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145516.509647.patch
Type: text/x-patch
Size: 5526 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230330/49427a17/attachment.bin>


More information about the llvm-commits mailing list