Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions mlir/include/mlir/IR/Location.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace mlir {

class Location;
class WalkResult;
class UnknownLoc;

//===----------------------------------------------------------------------===//
// LocationAttr
Expand Down Expand Up @@ -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);
};
Expand Down
14 changes: 10 additions & 4 deletions mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,20 @@ LogicalResult GPUPrintfOpToVPrintfLowering::matchAndRewrite(
// the device code, not the host code
auto moduleOp = gpuPrintfOp->getParentOfType<gpu::GPUModuleOp>();

// 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 =
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);
Expand Down
25 changes: 25 additions & 0 deletions mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm-debuginfo.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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)

// 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]])

gpu.func @test_const_printf_with_loc() {
gpu.printf "Hello, world with location\n" loc(fused<#di_subprogram>[#loc])
gpu.return
}
}