[Mlir-commits] [mlir] [mlir][inliner] optimize self-recursive function detection (PR #88452)
Congcong Cai
llvmlistbot at llvm.org
Sun May 12 06:08:25 PDT 2024
================
@@ -705,24 +715,30 @@ Inliner::Impl::inlineCallsInSCC(InlinerInterfaceImpl &inlinerIface,
return success(inlinedAnyCalls);
}
+static bool isSelfRecursiveFunction(CallGraphNode *node) {
+ return llvm::find_if(*node, [&](const CallGraphNode::Edge &edge) -> bool {
+ return edge.getTarget() == node;
+ }) != node->end();
+}
+
/// Returns true if the given call should be inlined.
-bool Inliner::Impl::shouldInline(ResolvedCall &resolvedCall) {
+bool Inliner::Impl::shouldInline(
+ ResolvedCall &resolvedCall,
+ const llvm::SmallPtrSetImpl<Region *> &recursiveCallRegions) {
// Don't allow inlining terminator calls. We currently don't support this
// case.
if (resolvedCall.call->hasTrait<OpTrait::IsTerminator>())
return false;
- // Don't allow inlining if the target is a self-recursive function.
- if (llvm::count_if(*resolvedCall.targetNode,
- [&](CallGraphNode::Edge const &edge) -> bool {
- return edge.getTarget() == resolvedCall.targetNode;
- }) > 0)
- return false;
-
- // Don't allow inlining if the target is an ancestor of the call. This
- // prevents inlining recursively.
Region *callableRegion = resolvedCall.targetNode->getCallableRegion();
- if (callableRegion->isAncestor(resolvedCall.call->getParentRegion()))
+
+ // Don't allow inlining this following cases to prevent inlining recursively.
+ // 1. target has at least an edge back to itself in original call graph.
----------------
HerrCai0907 wrote:
Then how can we avoid infinite loop, `foo` can be inlined again and again.
I meet a issue, when I use `inliner` in some code like following example, exponential explosion happen.
```
void foo(int value) {
foo(value-1);
foo(value-1);
}
void bar() {
foo(10);
foo(10);
}
```
https://github.com/llvm/llvm-project/pull/88452
More information about the Mlir-commits
mailing list