[Mlir-commits] [mlir] [mlir][gpu] Fix bug with gpu.printf global location (PR #142872)

Adam Straw llvmlistbot at llvm.org
Wed Jun 4 17:14:57 PDT 2025


https://github.com/adstraw updated https://github.com/llvm/llvm-project/pull/142872

>From ad1374af682c88ded36197c3433bf804d6611137 Mon Sep 17 00:00:00 2001
From: Adam Straw <astraw at nvidia.com>
Date: Wed, 4 Jun 2025 22:37:50 +0000
Subject: [PATCH 1/3] [mlir][gpu] Fix bug with gpu.printf global location

---
 mlir/include/mlir/IR/Location.h               | 10 ++++++++
 .../Conversion/GPUCommon/GPUOpsLowering.cpp   | 14 +++++++----
 .../GPUToNVVM/gpu-to-nvvm-debuginfo.mlir      | 24 +++++++++++++++++++
 3 files changed, 44 insertions(+), 4 deletions(-)
 create mode 100644 mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir

diff --git a/mlir/include/mlir/IR/Location.h b/mlir/include/mlir/IR/Location.h
index 5b1cf300295e5..d286befe1920c 100644
--- a/mlir/include/mlir/IR/Location.h
+++ b/mlir/include/mlir/IR/Location.h
@@ -21,6 +21,7 @@ namespace mlir {
 
 class Location;
 class WalkResult;
+class UnknownLoc;
 
 //===----------------------------------------------------------------------===//
 // LocationAttr
@@ -53,6 +54,15 @@ class LocationAttr : public Attribute {
     return result;
   }
 
+  /// Return an instance of the given location type if one is nested under the
+  /// current location else return unknown location.
+  template <typename T, typename UnknownT = UnknownLoc>
+  LocationAttr findInstanceOfOrUnknown() {
+    if (T result = findInstanceOf<T>())
+      return result;
+    return UnknownT::get(getContext());
+  }
+
   /// Methods for support type inquiry through isa, cast, and dyn_cast.
   static bool classof(Attribute attr);
 };
diff --git a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
index dea3d99704d9b..609f549a9f009 100644
--- a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
+++ b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
@@ -543,14 +543,20 @@ LogicalResult GPUPrintfOpToVPrintfLowering::matchAndRewrite(
   // the device code, not the host code
   auto moduleOp = gpuPrintfOp->getParentOfType<gpu::GPUModuleOp>();
 
+  // Convert the location to a valid global location of type FileLineColLoc if
+  // found else UnknownLoc. Remove any metadata from the location which is not
+  // valid for a global location.
+  Location globalLoc = loc->findInstanceOfOrUnknown<FileLineColLoc>();
+
   auto vprintfType =
       LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {ptrType, ptrType});
-  LLVM::LLVMFuncOp vprintfDecl =
-      getOrDefineFunction(moduleOp, loc, rewriter, "vprintf", vprintfType);
+  LLVM::LLVMFuncOp vprintfDecl = getOrDefineFunction(
+      moduleOp, globalLoc, rewriter, "vprintf", vprintfType);
 
   // Create the global op or find an existing one.
-  LLVM::GlobalOp global = getOrCreateStringConstant(
-      rewriter, loc, moduleOp, llvmI8, "printfFormat_", adaptor.getFormat());
+  LLVM::GlobalOp global =
+      getOrCreateStringConstant(rewriter, globalLoc, moduleOp, llvmI8,
+                                "printfFormat_", adaptor.getFormat());
 
   // Get a pointer to the format string's first element
   Value globalPtr = rewriter.create<LLVM::AddressOfOp>(loc, global);
diff --git a/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir b/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir
new file mode 100644
index 0000000000000..66cac733785b3
--- /dev/null
+++ b/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir
@@ -0,0 +1,24 @@
+// RUN: mlir-opt %s -convert-gpu-to-nvvm='has-redux=1' -mlir-print-debuginfo | FileCheck %s
+
+#di_file = #llvm.di_file<"foo.mlir" in "/tmp/">
+#di_compile_unit = #llvm.di_compile_unit<
+  id = distinct[0]<>, sourceLanguage = DW_LANG_C, file = #di_file,
+  producer = "MLIR", isOptimized = true, emissionKind = Full
+>
+#di_subprogram = #llvm.di_subprogram<
+  compileUnit = #di_compile_unit, scope = #di_file, name = "test_const_printf_with_loc",
+  file = #di_file, subprogramFlags = "Definition"
+>
+
+// CHECK-DAG: [[LOC:#[a-zA-Z0-9_]+]] = loc("foo.mlir":0:0)
+#loc = loc("foo.mlir":0:0)
+
+gpu.module @test_module_56 {
+  // CHECK-DAG: llvm.mlir.global internal constant @[[$PRINT_GLOBAL0:[A-Za-z0-9_]+]]("Hello, world with location\0A\00") {addr_space = 0 : i32} loc([[LOC]])
+  // CHECK-DAG: llvm.func @vprintf(!llvm.ptr, !llvm.ptr) -> i32 loc([[LOC]])
+
+  gpu.func @test_const_printf_with_loc() {
+    gpu.printf "Hello, world with location\n" loc(fused<#di_subprogram>[#loc])
+    gpu.return
+  }
+}

>From 632abb7994241284af87147d2120822b34c425b7 Mon Sep 17 00:00:00 2001
From: Adam Straw <astraw at nvidia.com>
Date: Wed, 4 Jun 2025 23:48:33 +0000
Subject: [PATCH 2/3] add more detail to the comment describing the bug

---
 mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp          | 6 +++---
 mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
index 609f549a9f009..01ca5e99a9aff 100644
--- a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
+++ b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
@@ -543,9 +543,9 @@ LogicalResult GPUPrintfOpToVPrintfLowering::matchAndRewrite(
   // the device code, not the host code
   auto moduleOp = gpuPrintfOp->getParentOfType<gpu::GPUModuleOp>();
 
-  // Convert the location to a valid global location of type FileLineColLoc if
-  // found else UnknownLoc. Remove any metadata from the location which is not
-  // valid for a global location.
+  // Create a valid global location removing any metadata attached to the
+  // location as debug info metadata inside of a function cannot be used outside
+  // of that function.
   Location globalLoc = loc->findInstanceOfOrUnknown<FileLineColLoc>();
 
   auto vprintfType =
diff --git a/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir b/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir
index 66cac733785b3..a5924f0f5aab8 100644
--- a/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir
+++ b/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir
@@ -13,7 +13,7 @@
 // CHECK-DAG: [[LOC:#[a-zA-Z0-9_]+]] = loc("foo.mlir":0:0)
 #loc = loc("foo.mlir":0:0)
 
-gpu.module @test_module_56 {
+gpu.module @test_module_1 {
   // CHECK-DAG: llvm.mlir.global internal constant @[[$PRINT_GLOBAL0:[A-Za-z0-9_]+]]("Hello, world with location\0A\00") {addr_space = 0 : i32} loc([[LOC]])
   // CHECK-DAG: llvm.func @vprintf(!llvm.ptr, !llvm.ptr) -> i32 loc([[LOC]])
 

>From 81e3c0e2c252df6f6be6c7414f4d1d442fc4ea95 Mon Sep 17 00:00:00 2001
From: Adam Straw <astraw at nvidia.com>
Date: Thu, 5 Jun 2025 00:13:54 +0000
Subject: [PATCH 3/3] add comment to test describing what it does

---
 mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir | 1 +
 1 file changed, 1 insertion(+)

diff --git a/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir b/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir
index a5924f0f5aab8..08c5800fe93b3 100644
--- a/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir
+++ b/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir
@@ -13,6 +13,7 @@
 // CHECK-DAG: [[LOC:#[a-zA-Z0-9_]+]] = loc("foo.mlir":0:0)
 #loc = loc("foo.mlir":0:0)
 
+// Check that debug info metadata from the function is removed from the global location.
 gpu.module @test_module_1 {
   // CHECK-DAG: llvm.mlir.global internal constant @[[$PRINT_GLOBAL0:[A-Za-z0-9_]+]]("Hello, world with location\0A\00") {addr_space = 0 : i32} loc([[LOC]])
   // CHECK-DAG: llvm.func @vprintf(!llvm.ptr, !llvm.ptr) -> i32 loc([[LOC]])



More information about the Mlir-commits mailing list