[Mlir-commits] [mlir] [mlir][acc] Add inline interface to acc dialect. (PR #127761)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Feb 18 23:59:09 PST 2025


https://github.com/junfengd-nv created https://github.com/llvm/llvm-project/pull/127761

Inliner interface could make inlining happens within OpenACC region.  For example, the call site is as follows
```
      acc.loop  gang control(%arg2 : index) = (%c0_i64 : i64) to (%arg1 : i64)  step (%c1_i64 : i64) {
        %0 = arith.index_cast %arg2 : index to i64
        func.call @_mlir_set(%arg0, %0, %0) : (memref<?xf64>, i64, i64) -> ()
        acc.yield
      }
```
Inliner will check whether the acc.loop operation is valid to inline any function calls. With this change, mlir-opt -inline could inline function "mlir_set" into acc.loop region to get better performance. 

>From 405f05def7ad7f2e6bb88c7369edc2b4ac2a2e0e Mon Sep 17 00:00:00 2001
From: Junfeng Dong <junfengd at nvidia.com>
Date: Tue, 18 Feb 2025 23:46:26 -0800
Subject: [PATCH 1/2] Add inline interface to acc dialect.

---
 mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
index 8f150cb33e6f3..4a1c38fbfe613 100644
--- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
+++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
@@ -18,6 +18,7 @@
 #include "mlir/IR/OpImplementation.h"
 #include "mlir/Support/LLVM.h"
 #include "mlir/Transforms/DialectConversion.h"
+#include "mlir/Transforms/InliningUtils.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/TypeSwitch.h"
 #include "llvm/Support/LogicalResult.h"
@@ -76,6 +77,19 @@ struct LLVMPointerPointerLikeModel
                                             LLVM::LLVMPointerType> {
   Type getElementType(Type pointer) const { return Type(); }
 };
+
+struct ACCInlinerInterface : public DialectInlinerInterface {
+  using DialectInlinerInterface::DialectInlinerInterface;
+  bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned,
+                       IRMapping &valueMapping) const final {
+    return true;
+  }
+  bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
+    return true;
+  }
+  void handleTerminator(Operation *op, ValueRange valuesToRepl) const final {
+  }
+};
 } // namespace
 
 //===----------------------------------------------------------------------===//
@@ -102,6 +116,8 @@ void OpenACCDialect::initialize() {
   MemRefType::attachInterface<MemRefPointerLikeModel>(*getContext());
   LLVM::LLVMPointerType::attachInterface<LLVMPointerPointerLikeModel>(
       *getContext());
+
+  addInterfaces<ACCInlinerInterface>();
 }
 
 //===----------------------------------------------------------------------===//

>From c3577356b862bd6f8589b9bb2fdb9d957b38e6fd Mon Sep 17 00:00:00 2001
From: Junfeng Dong <junfengd at nvidia.com>
Date: Tue, 18 Feb 2025 23:58:32 -0800
Subject: [PATCH 2/2] Fix format.

---
 mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
index 4a1c38fbfe613..18882439fa4b4 100644
--- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
+++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
@@ -87,8 +87,7 @@ struct ACCInlinerInterface : public DialectInlinerInterface {
   bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
     return true;
   }
-  void handleTerminator(Operation *op, ValueRange valuesToRepl) const final {
-  }
+  void handleTerminator(Operation *op, ValueRange valuesToRepl) const final {}
 };
 } // namespace
 



More information about the Mlir-commits mailing list