Skip to content

Commit 3172c61

Browse files
authored
[mlir][gpu] Fix bug with gpu.printf global location (#142872)
Bug description: Global variables and functions created during gpu.printf conversion to NVVM may contain debug info metadata from function containing the gpu.printf which cannot be used out of that function.
1 parent 627e49e commit 3172c61

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

mlir/include/mlir/IR/Location.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace mlir {
2121

2222
class Location;
2323
class WalkResult;
24+
class UnknownLoc;
2425

2526
//===----------------------------------------------------------------------===//
2627
// LocationAttr
@@ -53,6 +54,15 @@ class LocationAttr : public Attribute {
5354
return result;
5455
}
5556

57+
/// Return an instance of the given location type if one is nested under the
58+
/// current location else return unknown location.
59+
template <typename T, typename UnknownT = UnknownLoc>
60+
LocationAttr findInstanceOfOrUnknown() {
61+
if (T result = findInstanceOf<T>())
62+
return result;
63+
return UnknownT::get(getContext());
64+
}
65+
5666
/// Methods for support type inquiry through isa, cast, and dyn_cast.
5767
static bool classof(Attribute attr);
5868
};

mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,20 @@ LogicalResult GPUPrintfOpToVPrintfLowering::matchAndRewrite(
543543
// the device code, not the host code
544544
auto moduleOp = gpuPrintfOp->getParentOfType<gpu::GPUModuleOp>();
545545

546+
// Create a valid global location removing any metadata attached to the
547+
// location as debug info metadata inside of a function cannot be used outside
548+
// of that function.
549+
Location globalLoc = loc->findInstanceOfOrUnknown<FileLineColLoc>();
550+
546551
auto vprintfType =
547552
LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {ptrType, ptrType});
548-
LLVM::LLVMFuncOp vprintfDecl =
549-
getOrDefineFunction(moduleOp, loc, rewriter, "vprintf", vprintfType);
553+
LLVM::LLVMFuncOp vprintfDecl = getOrDefineFunction(
554+
moduleOp, globalLoc, rewriter, "vprintf", vprintfType);
550555

551556
// Create the global op or find an existing one.
552-
LLVM::GlobalOp global = getOrCreateStringConstant(
553-
rewriter, loc, moduleOp, llvmI8, "printfFormat_", adaptor.getFormat());
557+
LLVM::GlobalOp global =
558+
getOrCreateStringConstant(rewriter, globalLoc, moduleOp, llvmI8,
559+
"printfFormat_", adaptor.getFormat());
554560

555561
// Get a pointer to the format string's first element
556562
Value globalPtr = rewriter.create<LLVM::AddressOfOp>(loc, global);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: mlir-opt %s -convert-gpu-to-nvvm='has-redux=1' -mlir-print-debuginfo | FileCheck %s
2+
3+
#di_file = #llvm.di_file<"foo.mlir" in "/tmp/">
4+
#di_compile_unit = #llvm.di_compile_unit<
5+
id = distinct[0]<>, sourceLanguage = DW_LANG_C, file = #di_file,
6+
producer = "MLIR", isOptimized = true, emissionKind = Full
7+
>
8+
#di_subprogram = #llvm.di_subprogram<
9+
compileUnit = #di_compile_unit, scope = #di_file, name = "test_const_printf_with_loc",
10+
file = #di_file, subprogramFlags = "Definition"
11+
>
12+
13+
// CHECK-DAG: [[LOC:#[a-zA-Z0-9_]+]] = loc("foo.mlir":0:0)
14+
#loc = loc("foo.mlir":0:0)
15+
16+
// Check that debug info metadata from the function is removed from the global location.
17+
gpu.module @test_module_1 {
18+
// 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]])
19+
// CHECK-DAG: llvm.func @vprintf(!llvm.ptr, !llvm.ptr) -> i32 loc([[LOC]])
20+
21+
gpu.func @test_const_printf_with_loc() {
22+
gpu.printf "Hello, world with location\n" loc(fused<#di_subprogram>[#loc])
23+
gpu.return
24+
}
25+
}

0 commit comments

Comments
 (0)