Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir] add noinline attribute to func.func/call #119970

Merged
merged 1 commit into from
Dec 15, 2024
Merged

Conversation

ftynse
Copy link
Member

@ftynse ftynse commented Dec 14, 2024

This allows for inlining to be somewhat controlled by the user instead of always inlining everything. External heuristics may be used to place no_inline attributes on invidiual calls or functions to prevent inlining.

This allows for inlining to be somewhat controlled by the user instead of
always inlining everything. External heuristics may be used to place
`no_inline` attributes on invidiual calls or functions to prevent inlining.
@llvmbot
Copy link
Member

llvmbot commented Dec 14, 2024

@llvm/pr-subscribers-mlir-func

@llvm/pr-subscribers-mlir

Author: Oleksandr "Alex" Zinenko (ftynse)

Changes

This allows for inlining to be somewhat controlled by the user instead of always inlining everything. External heuristics may be used to place no_inline attributes on invidiual calls or functions to prevent inlining.


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

3 Files Affected:

  • (modified) mlir/include/mlir/Dialect/Func/IR/FuncOps.td (+4-2)
  • (modified) mlir/lib/Dialect/Func/Extensions/InlinerExtension.cpp (+7-3)
  • (modified) mlir/test/Transforms/inlining.mlir (+23)
diff --git a/mlir/include/mlir/Dialect/Func/IR/FuncOps.td b/mlir/include/mlir/Dialect/Func/IR/FuncOps.td
index 22efe15aa83a50..237a825c191042 100644
--- a/mlir/include/mlir/Dialect/Func/IR/FuncOps.td
+++ b/mlir/include/mlir/Dialect/Func/IR/FuncOps.td
@@ -49,7 +49,8 @@ def CallOp : Func_Op<"call",
     ```
   }];
 
-  let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyType>:$operands);
+  let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyType>:$operands,
+                       UnitAttr:$no_inline);
   let results = (outs Variadic<AnyType>);
 
   let builders = [
@@ -270,7 +271,8 @@ def FuncOp : Func_Op<"func", [
                        TypeAttrOf<FunctionType>:$function_type,
                        OptionalAttr<StrAttr>:$sym_visibility,
                        OptionalAttr<DictArrayAttr>:$arg_attrs,
-                       OptionalAttr<DictArrayAttr>:$res_attrs);
+                       OptionalAttr<DictArrayAttr>:$res_attrs,
+                       UnitAttr:$no_inline);
   let regions = (region AnyRegion:$body);
 
   let builders = [OpBuilder<(ins
diff --git a/mlir/lib/Dialect/Func/Extensions/InlinerExtension.cpp b/mlir/lib/Dialect/Func/Extensions/InlinerExtension.cpp
index 719a74a29a6221..3328d58551bff1 100644
--- a/mlir/lib/Dialect/Func/Extensions/InlinerExtension.cpp
+++ b/mlir/lib/Dialect/Func/Extensions/InlinerExtension.cpp
@@ -27,10 +27,14 @@ struct FuncInlinerInterface : public DialectInlinerInterface {
   // Analysis Hooks
   //===--------------------------------------------------------------------===//
 
-  /// All call operations can be inlined.
+  /// Call operations can be inlined unless specified otherwise by attributes
+  /// on either the call or the callbale.
   bool isLegalToInline(Operation *call, Operation *callable,
                        bool wouldBeCloned) const final {
-    return true;
+    auto callOp = dyn_cast<func::CallOp>(call);
+    auto funcOp = dyn_cast<func::FuncOp>(callable);
+    return !(callOp && callOp.getNoInline()) &&
+           !(funcOp && funcOp.getNoInline());
   }
 
   /// All operations can be inlined.
@@ -38,7 +42,7 @@ struct FuncInlinerInterface : public DialectInlinerInterface {
     return true;
   }
 
-  /// All functions can be inlined.
+  /// All function bodies can be inlined.
   bool isLegalToInline(Region *, Region *, bool, IRMapping &) const final {
     return true;
   }
diff --git a/mlir/test/Transforms/inlining.mlir b/mlir/test/Transforms/inlining.mlir
index 79a2936b104fa1..65ffaad1fa859c 100644
--- a/mlir/test/Transforms/inlining.mlir
+++ b/mlir/test/Transforms/inlining.mlir
@@ -19,6 +19,29 @@ func.func @inline_with_arg(%arg0 : i32) -> i32 {
   return %0 : i32
 }
 
+// CHECK-LABEL: func @noinline_with_arg
+func.func @noinline_with_arg(%arg0 : i32) -> i32 {
+  // CHECK-NEXT: func_with_arg
+  // CHECK-NEXT: return
+
+  %0 = call @func_with_arg(%arg0) {no_inline} : (i32) -> i32
+  return %0 : i32
+}
+
+func.func @non_inlinable_func_with_arg(%c : i32) -> i32 attributes {no_inline} {
+  %b = arith.addi %c, %c : i32
+  return %b : i32
+}
+
+// CHECK-LABEL: func @noinline_with_func_arg
+func.func @noinline_with_func_arg(%arg0 : i32) -> i32 {
+  // CHECK-NEXT: non_inlinable_func_with_arg
+  // CHECK-NEXT: return
+
+  %0 = call @non_inlinable_func_with_arg(%arg0) : (i32) -> i32
+  return %0 : i32
+}
+
 // Inline a function that has multiple return operations.
 func.func @func_with_multi_return(%a : i1) -> (i32) {
   cf.cond_br %a, ^bb1, ^bb2

@ftynse ftynse merged commit d072ca1 into llvm:main Dec 15, 2024
11 checks passed
@ftynse ftynse deleted the func-noinline branch December 15, 2024 21:58
raikonenfnu added a commit to iree-org/llvm-project that referenced this pull request Dec 17, 2024
raikonenfnu added a commit to iree-org/iree that referenced this pull request Dec 17, 2024
Update LLVM to llvm/llvm-project@3f136f7
(#19479)
Carrying the following reverts

- llvm/llvm-project#116470
- llvm/llvm-project#117424
- llvm/llvm-project#119671
- llvm/llvm-project#119970

First two are carry over from previous-previous integrate. It is being
fixed in
#19451 . The last one is a from the
previous integrate.
The last one is a new error being tracked in
#19498

---------

Signed-off-by: Stanley Winata <stanley.winata@amd.com>
raikonenfnu added a commit to raikonenfnu/iree that referenced this pull request Dec 17, 2024
Update LLVM to llvm/llvm-project@b07e7b76c5d532a6 (llvm/llvm-project#120002)
Carrying the following reverts

- llvm/llvm-project#116470
- llvm/llvm-project#117424
- llvm/llvm-project#119671
- llvm/llvm-project#119970

First two are carry over from previous-previous integrate. It is being fixed in
iree-org#19451 . The last one is a from the previous integrate.
The last one is a new error being tracked in iree-org#19498

Signed-off-by: Stanley Winata <stanley.winata@amd.com>
raikonenfnu added a commit to raikonenfnu/iree that referenced this pull request Dec 17, 2024
Update LLVM to llvm/llvm-project@b07e7b76c5d532a6 (llvm/llvm-project#120002)
Carrying the following reverts

- llvm/llvm-project#116470
- llvm/llvm-project#117424
- llvm/llvm-project#119671
- llvm/llvm-project#119970

First two are carry over from previous-previous integrate. It is being fixed in
iree-org#19451 . The last one is a from the previous integrate.
The last one is a new error being tracked in iree-org#19498

Signed-off-by: Stanley Winata <stanley.winata@amd.com>
MaheshRavishankar pushed a commit to iree-org/llvm-project that referenced this pull request Dec 17, 2024
raikonenfnu added a commit to iree-org/llvm-project that referenced this pull request Dec 18, 2024
raikonenfnu added a commit to iree-org/llvm-project that referenced this pull request Dec 20, 2024
raikonenfnu added a commit to raikonenfnu/iree that referenced this pull request Dec 20, 2024
Changes in C++ mostly handle changes done in llvm/llvm-project@4b56345 that combines SCFTilingResult and SCFReductionTilingResult.

This PR also carries the following reverts

- llvm/llvm-project#119671
- llvm/llvm-project#119970

The first one is a from the previous integrate. The last one is a new error being tracked in iree-org#19498.

Signed-off-by: Stanley Winata <stanley.winata@amd.com>
raikonenfnu added a commit to raikonenfnu/iree that referenced this pull request Dec 20, 2024
Changes in C++ mostly handle changes done in llvm/llvm-project@4b56345 that combines SCFTilingResult and SCFReductionTilingResult.

This PR also carries the following reverts

- llvm/llvm-project#119671
- llvm/llvm-project#119970

The first one is a from the previous integrate. The last one is a new error being tracked in iree-org#19498.

Signed-off-by: Stanley Winata <stanley.winata@amd.com>
raikonenfnu added a commit to raikonenfnu/iree that referenced this pull request Dec 20, 2024
Changes in C++ mostly handle changes done in llvm/llvm-project@4b56345 that combines SCFTilingResult and SCFReductionTilingResult.

This PR also carries the following reverts

- llvm/llvm-project#119970

The issue related to the revert is being tracked in iree-org#19498.

Signed-off-by: Stanley Winata <stanley.winata@amd.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants