[Mlir-commits] [mlir] [mlir][inline] avoid inline self-recursive function (PR #83092)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Feb 26 17:40:18 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: Congcong Cai (HerrCai0907)

<details>
<summary>Changes</summary>

We cannot get any benifit from inlining self-recursive function. This patch want to detect this cases and ignore it.

---
Full diff: https://github.com/llvm/llvm-project/pull/83092.diff


2 Files Affected:

- (modified) mlir/lib/Transforms/Inliner.cpp (+8) 
- (added) mlir/test/Transforms/inlining-recursive-self.mlir (+22) 


``````````diff
diff --git a/mlir/lib/Transforms/Inliner.cpp b/mlir/lib/Transforms/Inliner.cpp
index b32b0fc28c78b0..b92ee49403c74c 100644
--- a/mlir/lib/Transforms/Inliner.cpp
+++ b/mlir/lib/Transforms/Inliner.cpp
@@ -455,6 +455,14 @@ static bool shouldInline(ResolvedCall &resolvedCall) {
   if (callableRegion->isAncestor(resolvedCall.call->getParentRegion()))
     return false;
 
+  // Don't allow inlining if the target is a self-recursive function.
+  if (std::count_if(resolvedCall.targetNode->begin(),
+                    resolvedCall.targetNode->end(),
+                    [&](CallGraphNode::Edge const &edge) -> bool {
+                      return edge.getTarget() == resolvedCall.targetNode;
+                    }) > 0)
+    return false;
+
   // Don't allow inlining if the callee has multiple blocks (unstructured
   // control flow) but we cannot be sure that the caller region supports that.
   bool calleeHasMultipleBlocks =
diff --git a/mlir/test/Transforms/inlining-recursive-self.mlir b/mlir/test/Transforms/inlining-recursive-self.mlir
new file mode 100644
index 00000000000000..5cc922db8e9786
--- /dev/null
+++ b/mlir/test/Transforms/inlining-recursive-self.mlir
@@ -0,0 +1,22 @@
+// RUN: mlir-opt %s -inline='default-pipeline=''' | FileCheck %s
+// RUN: mlir-opt %s --mlir-disable-threading -inline='default-pipeline=''' | FileCheck %s
+
+// CHECK-LABEL: func.func @b0
+func.func @b0() {
+  // CHECK:         call @b0
+  // CHECK-NEXT:    call @b1
+  // CHECK-NEXT:    call @b0
+  // CHECK-NEXT:    call @b1
+  // CHECK-NEXT:    call @b0
+  func.call @b0() : () -> ()
+  func.call @b1() : () -> ()
+  func.call @b0() : () -> ()
+  func.call @b1() : () -> ()
+  func.call @b0() : () -> ()
+  return
+}
+func.func @b1() {
+  func.call @b1() : () -> ()
+  func.call @b1() : () -> ()
+  return
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/83092


More information about the Mlir-commits mailing list