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

Congcong Cai llvmlistbot at llvm.org
Tue Mar 5 06:58:18 PST 2024


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

>From 9cbbf39d75a9fd71b37d14b5ace07b0a83119101 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Mon, 26 Feb 2024 16:34:09 +0800
Subject: [PATCH] [mlir][inline] avoid inline self-recursive function

We cannot get any benifit from inlining self-recursive function.
This patch want to detect this cases and ignore it.
---
 mlir/lib/Transforms/Inliner.cpp               |  8 +++++++
 .../Transforms/inlining-recursive-self.mlir   | 22 +++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 mlir/test/Transforms/inlining-recursive-self.mlir

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
+}



More information about the Mlir-commits mailing list