Skip to content

Commit c1ed47c

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 eddfebd commit c1ed47c

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
@@ -4985,19 +4985,23 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
49854985
// The current debug location already has the DISubprogram for the outlined
49864986
// function that will be created for the target op. We save it here so that
49874987
// we can set it on the outlined function.
4988-
llvm::DebugLoc OutlinedFnLoc = builder.getCurrentDebugLocation();
4988+
llvm::DebugLoc outlinedFnLoc = builder.getCurrentDebugLocation();
4989+
if (failed(checkImplementationStatus(opInst)))
4990+
return failure();
4991+
49894992
// During the handling of target op, we will generate instructions in the
4990-
// parent function like call to oulined function or branch to new BasicBlock.
4991-
// We set the debug location here to parent function so that those get the
4992-
// correct debug locations. For outlined functions, the normal MLIR op
4993+
// parent function like call to the oulined function or branch to a new
4994+
// BasicBlock. We set the debug location here to parent function so that those
4995+
// get the correct debug locations. For outlined functions, the normal MLIR op
49934996
// conversion will automatically pick the correct location.
49944997
llvm::BasicBlock *parentBB = builder.GetInsertBlock();
4995-
if (parentBB && !parentBB->empty())
4996-
builder.SetCurrentDebugLocation(parentBB->back().getDebugLoc());
4997-
else
4998-
builder.SetCurrentDebugLocation(llvm::DebugLoc());
4999-
if (failed(checkImplementationStatus(opInst)))
5000-
return failure();
4998+
assert(parentBB);
4999+
llvm::Function *parentLLVMFn = parentBB->getParent();
5000+
assert(parentLLVMFn);
5001+
if (llvm::DISubprogram *SP = parentLLVMFn->getSubprogram())
5002+
builder.SetCurrentDebugLocation(llvm::DILocation::get(
5003+
parentLLVMFn->getContext(), outlinedFnLoc.getLine(),
5004+
outlinedFnLoc.getCol(), SP, outlinedFnLoc.getInlinedAt()));
50015005

50025006
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
50035007
bool isTargetDevice = ompBuilder->Config.isTargetDevice();
@@ -5092,8 +5096,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
50925096
assert(llvmParentFn && llvmOutlinedFn &&
50935097
"Both parent and outlined functions must exist at this point");
50945098

5095-
if (OutlinedFnLoc && llvmParentFn->getSubprogram())
5096-
llvmOutlinedFn->setSubprogram(OutlinedFnLoc->getScope()->getSubprogram());
5099+
if (outlinedFnLoc && llvmParentFn->getSubprogram())
5100+
llvmOutlinedFn->setSubprogram(outlinedFnLoc->getScope()->getSubprogram());
50975101

50985102
if (auto attr = llvmParentFn->getFnAttribute("target-cpu");
50995103
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)