[Mlir-commits] [mlir] [mlir][inline] avoid inline self-recursive function (PR #82997)
Congcong Cai
llvmlistbot at llvm.org
Mon Feb 26 04:59:25 PST 2024
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/82997
We cannot get any benifit from inlining self-recursive function.
This patch want to detect this cases and ignore it.
>From 67a932e98ed051834f72cbf969d5ae273fc26ff8 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 1/2] [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 | 12 ++++++++++
.../Transforms/inlining-recursive-self.mlir | 22 +++++++++++++++++++
2 files changed, 34 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..3da6e9cff81cbb 100644
--- a/mlir/lib/Transforms/Inliner.cpp
+++ b/mlir/lib/Transforms/Inliner.cpp
@@ -273,6 +273,10 @@ class CallGraphSCC {
/// Reset the nodes of this SCC with those provided.
void reset(const std::vector<CallGraphNode *> &newNodes) { nodes = newNodes; }
+ bool has(CallGraphNode *node) {
+ return std::count(nodes.begin(), nodes.end(), node) > 0;
+ }
+
/// Remove the given node from this SCC.
void remove(CallGraphNode *node) {
auto it = llvm::find(nodes, node);
@@ -455,6 +459,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
+}
>From cb00df68eb750575ef5b2f24cf409d589993a6c9 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Mon, 26 Feb 2024 20:58:06 +0800
Subject: [PATCH 2/2] fix
---
mlir/lib/Transforms/Inliner.cpp | 4 ----
1 file changed, 4 deletions(-)
diff --git a/mlir/lib/Transforms/Inliner.cpp b/mlir/lib/Transforms/Inliner.cpp
index 3da6e9cff81cbb..b92ee49403c74c 100644
--- a/mlir/lib/Transforms/Inliner.cpp
+++ b/mlir/lib/Transforms/Inliner.cpp
@@ -273,10 +273,6 @@ class CallGraphSCC {
/// Reset the nodes of this SCC with those provided.
void reset(const std::vector<CallGraphNode *> &newNodes) { nodes = newNodes; }
- bool has(CallGraphNode *node) {
- return std::count(nodes.begin(), nodes.end(), node) > 0;
- }
-
/// Remove the given node from this SCC.
void remove(CallGraphNode *node) {
auto it = llvm::find(nodes, node);
More information about the Mlir-commits
mailing list