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
45 changes: 44 additions & 1 deletion mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ static Location getNestedLoc(Operation *op, LLVM::DIScopeAttr scopeAttr,
return FusedLoc::get(context, {loc}, lexicalBlockFileAttr);
}

/// Adds DILexicalBlockFileAttr for operations with CallSiteLoc and operations
/// from different files than their containing function.
static void setLexicalBlockFileAttr(Operation *op) {
if (auto callSiteLoc = dyn_cast<CallSiteLoc>(op->getLoc())) {
Location opLoc = op->getLoc();

if (auto callSiteLoc = dyn_cast<CallSiteLoc>(opLoc)) {
auto callerLoc = callSiteLoc.getCaller();
auto calleeLoc = callSiteLoc.getCallee();
LLVM::DIScopeAttr scopeAttr;
Expand All @@ -122,6 +126,45 @@ static void setLexicalBlockFileAttr(Operation *op) {
op->setLoc(
CallSiteLoc::get(getNestedLoc(op, scopeAttr, calleeLoc), callerLoc));
}

return;
}

auto funcOp = op->getParentOfType<LLVM::LLVMFuncOp>();
if (!funcOp)
return;

FileLineColLoc opFileLoc = extractFileLoc(opLoc);
if (!opFileLoc)
return;

FileLineColLoc funcFileLoc = extractFileLoc(funcOp.getLoc());
if (!funcFileLoc)
return;

StringRef opFile = opFileLoc.getFilename().getValue();
StringRef funcFile = funcFileLoc.getFilename().getValue();

// Handle cross-file operations: add DILexicalBlockFileAttr when the
// operation's source file differs from its containing function.
if (opFile != funcFile) {
auto funcOpLoc = llvm::dyn_cast_if_present<FusedLoc>(funcOp.getLoc());
if (!funcOpLoc)
return;
auto scopeAttr = dyn_cast<LLVM::DISubprogramAttr>(funcOpLoc.getMetadata());
if (!scopeAttr)
return;

auto *context = op->getContext();
LLVM::DIFileAttr opFileAttr =
LLVM::DIFileAttr::get(context, llvm::sys::path::filename(opFile),
llvm::sys::path::parent_path(opFile));

LLVM::DILexicalBlockFileAttr lexicalBlockFileAttr =
LLVM::DILexicalBlockFileAttr::get(context, scopeAttr, opFileAttr, 0);

Location newLoc = FusedLoc::get(context, {opLoc}, lexicalBlockFileAttr);
op->setLoc(newLoc);
}
}

Expand Down
19 changes: 19 additions & 0 deletions mlir/test/Dialect/LLVMIR/add-debuginfo-func-scope.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,22 @@ module {
llvm.func @func_callsiteloc() loc(callsite("foo" at "mysource.cc":10:8))
} loc(unknown)

// -----

// CHECK-LABEL: llvm.func @func_cross_file_op()
// CHECK: #di_file = #llvm.di_file<"<unknown>" in "">
// CHECK: #di_file1 = #llvm.di_file<"caller.py" in "">
// CHECK: #di_file2 = #llvm.di_file<"callee.py" in "">
// CHECK: #di_subroutine_type = #llvm.di_subroutine_type<callingConvention = DW_CC_normal>
// CHECK: #di_subprogram = #llvm.di_subprogram<id = distinct[1]<>, compileUnit = #di_compile_unit, scope = #di_file1, name = "func_cross_file_op", linkageName = "func_cross_file_op", file = #di_file1, line = 5, scopeLine = 5, subprogramFlags = "Definition|Optimized", type = #di_subroutine_type>
// CHECK: #di_lexical_block_file = #llvm.di_lexical_block_file<scope = #di_subprogram, file = #di_file2, discriminator = 0>

#loc = loc("caller.py":5:1)
#loc1 = loc("callee.py":10:5)

module {
llvm.func @func_cross_file_op() {
llvm.return loc(#loc1)
} loc(#loc)
} loc(unknown)