[Mlir-commits] [mlir] [Func][GPU] Create func::ConstantOp using parents with SymbolTable trait (PR #107748)

Artem Kroviakov llvmlistbot at llvm.org
Mon Sep 9 00:58:35 PDT 2024


https://github.com/akroviakov updated https://github.com/llvm/llvm-project/pull/107748

>From da38d4ab1cf38046e1b292c0db6a1b85d9d18a0d Mon Sep 17 00:00:00 2001
From: Artem Kroviakov <artem.kroviakov at tum.de>
Date: Mon, 9 Sep 2024 07:58:11 +0000
Subject: [PATCH] [Func][GPU] Use SymbolUserOpInterface in func::ConstantOp

---
 mlir/include/mlir/Dialect/Func/IR/FuncOps.td  |  2 +-
 mlir/lib/Dialect/Func/IR/FuncOps.cpp          |  5 +++--
 .../GPU/indirect-device-func-call.mlir        | 21 +++++++++++++++++++
 3 files changed, 25 insertions(+), 3 deletions(-)
 create mode 100644 mlir/test/Dialect/GPU/indirect-device-func-call.mlir

diff --git a/mlir/include/mlir/Dialect/Func/IR/FuncOps.td b/mlir/include/mlir/Dialect/Func/IR/FuncOps.td
index f4b43a59047054..cca0008cbc2633 100644
--- a/mlir/include/mlir/Dialect/Func/IR/FuncOps.td
+++ b/mlir/include/mlir/Dialect/Func/IR/FuncOps.td
@@ -183,6 +183,7 @@ def CallIndirectOp : Func_Op<"call_indirect", [
 
 def ConstantOp : Func_Op<"constant",
     [ConstantLike, Pure,
+     DeclareOpInterfaceMethods<SymbolUserOpInterface>,
      DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {
   let summary = "constant";
   let description = [{
@@ -216,7 +217,6 @@ def ConstantOp : Func_Op<"constant",
   }];
 
   let hasFolder = 1;
-  let hasVerifier = 1;
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Func/IR/FuncOps.cpp b/mlir/lib/Dialect/Func/IR/FuncOps.cpp
index c719981769b9e1..a490b4c3c4ab43 100644
--- a/mlir/lib/Dialect/Func/IR/FuncOps.cpp
+++ b/mlir/lib/Dialect/Func/IR/FuncOps.cpp
@@ -123,12 +123,13 @@ LogicalResult CallIndirectOp::canonicalize(CallIndirectOp indirectCall,
 // ConstantOp
 //===----------------------------------------------------------------------===//
 
-LogicalResult ConstantOp::verify() {
+LogicalResult ConstantOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
   StringRef fnName = getValue();
   Type type = getType();
 
   // Try to find the referenced function.
-  auto fn = (*this)->getParentOfType<ModuleOp>().lookupSymbol<FuncOp>(fnName);
+  auto fn = symbolTable.lookupNearestSymbolFrom<FuncOp>(
+      this->getOperation(), StringAttr::get(getContext(), fnName));
   if (!fn)
     return emitOpError() << "reference to undefined function '" << fnName
                          << "'";
diff --git a/mlir/test/Dialect/GPU/indirect-device-func-call.mlir b/mlir/test/Dialect/GPU/indirect-device-func-call.mlir
new file mode 100644
index 00000000000000..91d7f1cd6c67d9
--- /dev/null
+++ b/mlir/test/Dialect/GPU/indirect-device-func-call.mlir
@@ -0,0 +1,21 @@
+// RUN: mlir-opt -test-gpu-rewrite -convert-func-to-llvm %s | FileCheck %s
+
+gpu.module @kernels {
+    // CHECK-LABEL: @hello
+    // CHECK-SAME: %[[ARG0:.*]]: f32
+    func.func @hello(%arg0 : f32) {
+        %tid_x = gpu.thread_id x
+        %csti8 = arith.constant 2 : i8
+        gpu.printf "Hello from %lld, %d, %f\n" %tid_x, %csti8, %arg0  : index, i8, f32
+        return
+    }
+    // CHECK-LABEL: @hello_indirect
+    gpu.func @hello_indirect() kernel { 
+        %cstf32 = arith.constant 3.0 : f32
+        // CHECK: %[[DEVICE_FUNC_ADDR:.*]] = llvm.mlir.addressof @hello : !llvm.ptr
+        %func_ref = func.constant @hello : (f32) -> ()
+        // CHECK: llvm.call %[[DEVICE_FUNC_ADDR]](%{{.*}}) : !llvm.ptr, (f32) -> ()
+        func.call_indirect %func_ref(%cstf32) : (f32) -> ()
+        gpu.return
+    }
+}



More information about the Mlir-commits mailing list