Skip to content

Commit ad23127

Browse files
authored
[mlir][inline] avoid inline self-recursive function (llvm#83092)
1 parent 83c9244 commit ad23127

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

mlir/lib/Transforms/Utils/Inliner.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "mlir/Support/DebugStringHelper.h"
2222
#include "mlir/Transforms/InliningUtils.h"
2323
#include "llvm/ADT/SCCIterator.h"
24+
#include "llvm/ADT/STLExtras.h"
2425
#include "llvm/ADT/SmallPtrSet.h"
2526
#include "llvm/Support/Debug.h"
2627

@@ -711,6 +712,13 @@ bool Inliner::Impl::shouldInline(ResolvedCall &resolvedCall) {
711712
if (resolvedCall.call->hasTrait<OpTrait::IsTerminator>())
712713
return false;
713714

715+
// Don't allow inlining if the target is a self-recursive function.
716+
if (llvm::count_if(*resolvedCall.targetNode,
717+
[&](CallGraphNode::Edge const &edge) -> bool {
718+
return edge.getTarget() == resolvedCall.targetNode;
719+
}) > 0)
720+
return false;
721+
714722
// Don't allow inlining if the target is an ancestor of the call. This
715723
// prevents inlining recursively.
716724
Region *callableRegion = resolvedCall.targetNode->getCallableRegion();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: mlir-opt %s -inline='default-pipeline=''' | FileCheck %s
2+
// RUN: mlir-opt %s --mlir-disable-threading -inline='default-pipeline=''' | FileCheck %s
3+
4+
// CHECK-LABEL: func.func @b0
5+
func.func @b0() {
6+
// CHECK: call @b0
7+
// CHECK-NEXT: call @b1
8+
// CHECK-NEXT: call @b0
9+
// CHECK-NEXT: call @b1
10+
// CHECK-NEXT: call @b0
11+
func.call @b0() : () -> ()
12+
func.call @b1() : () -> ()
13+
func.call @b0() : () -> ()
14+
func.call @b1() : () -> ()
15+
func.call @b0() : () -> ()
16+
return
17+
}
18+
func.func @b1() {
19+
func.call @b1() : () -> ()
20+
func.call @b1() : () -> ()
21+
return
22+
}

0 commit comments

Comments
 (0)