Skip to content

Commit b0d08b3

Browse files
committed
Handle review comments.
The new way to set debug location can better handle empty basic block or function. A testcase is added as well.
1 parent f8b347b commit b0d08b3

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5302,19 +5302,23 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
53025302
// The current debug location already has the DISubprogram for the outlined
53035303
// function that will be created for the target op. We save it here so that
53045304
// we can set it on the outlined function.
5305-
llvm::DebugLoc OutlinedFnLoc = builder.getCurrentDebugLocation();
5305+
llvm::DebugLoc outlinedFnLoc = builder.getCurrentDebugLocation();
5306+
if (failed(checkImplementationStatus(opInst)))
5307+
return failure();
5308+
53065309
// During the handling of target op, we will generate instructions in the
5307-
// parent function like call to oulined function or branch to new BasicBlock.
5308-
// We set the debug location here to parent function so that those get the
5309-
// correct debug locations. For outlined functions, the normal MLIR op
5310+
// parent function like call to the oulined function or branch to a new
5311+
// BasicBlock. We set the debug location here to parent function so that those
5312+
// get the correct debug locations. For outlined functions, the normal MLIR op
53105313
// conversion will automatically pick the correct location.
53115314
llvm::BasicBlock *parentBB = builder.GetInsertBlock();
5312-
if (parentBB && !parentBB->empty())
5313-
builder.SetCurrentDebugLocation(parentBB->back().getDebugLoc());
5314-
else
5315-
builder.SetCurrentDebugLocation(llvm::DebugLoc());
5316-
if (failed(checkImplementationStatus(opInst)))
5317-
return failure();
5315+
assert(parentBB);
5316+
llvm::Function *parentLLVMFn = parentBB->getParent();
5317+
assert(parentLLVMFn);
5318+
if (llvm::DISubprogram *SP = parentLLVMFn->getSubprogram())
5319+
builder.SetCurrentDebugLocation(llvm::DILocation::get(
5320+
parentLLVMFn->getContext(), outlinedFnLoc.getLine(),
5321+
outlinedFnLoc.getCol(), SP, outlinedFnLoc.getInlinedAt()));
53185322

53195323
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
53205324
bool isTargetDevice = ompBuilder->Config.isTargetDevice();
@@ -5409,8 +5413,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
54095413
assert(llvmParentFn && llvmOutlinedFn &&
54105414
"Both parent and outlined functions must exist at this point");
54115415

5412-
if (OutlinedFnLoc && llvmParentFn->getSubprogram())
5413-
llvmOutlinedFn->setSubprogram(OutlinedFnLoc->getScope()->getSubprogram());
5416+
if (outlinedFnLoc && llvmParentFn->getSubprogram())
5417+
llvmOutlinedFn->setSubprogram(outlinedFnLoc->getScope()->getSubprogram());
54145418

54155419
if (auto attr = llvmParentFn->getFnAttribute("target-cpu");
54165420
attr.isStringAttribute())
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
module attributes {omp.is_target_device = false} {
4+
llvm.func @test() {
5+
omp.target {
6+
omp.terminator
7+
} loc(#loc4)
8+
llvm.return
9+
} loc(#loc3)
10+
}
11+
#file = #llvm.di_file<"target.f90" in "">
12+
#cu = #llvm.di_compile_unit<id = distinct[0]<>,
13+
sourceLanguage = DW_LANG_Fortran95, file = #file, isOptimized = false,
14+
emissionKind = Full>
15+
#sp_ty = #llvm.di_subroutine_type<callingConvention = DW_CC_normal>
16+
#sp = #llvm.di_subprogram<id = distinct[1]<>, compileUnit = #cu, scope = #file,
17+
name = "_QQmain", file = #file, subprogramFlags = "Definition", type = #sp_ty>
18+
#sp1 = #llvm.di_subprogram<id = distinct[2]<>, compileUnit = #cu, scope = #file,
19+
name = "__omp_offloading_target", file = #file, subprogramFlags = "Definition",
20+
type = #sp_ty>
21+
#loc1 = loc("target.f90":1:1)
22+
#loc2 = loc("target.f90":46:3)
23+
#loc3 = loc(fused<#sp>[#loc1])
24+
#loc4 = loc(fused<#sp1>[#loc2])
25+
26+
// CHECK: ![[SP:.*]] = {{.*}}!DISubprogram(name: "__omp_offloading_target"{{.*}})
27+

0 commit comments

Comments
 (0)