Skip to content

Commit 5508975

Browse files
committed
[OpenMPIRBuilder] Don't drop debug info for target region.
When an outlined function is generated for omp target region, a corresponding DISubprogram was not being generated. This resulted in all the debug information for the target region being dropped. This commit adds DISubprogram for the outlined function if there is one available for the parent function. It also updates the current debug location so that the right scope is used for the entries in the outlined function. With this change in place, I can set source line breakpoint in target region and run to them in debugger.
1 parent 2a36ef5 commit 5508975

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
!RUN: %flang_fc1 -triple amdgcn-amd-amdhsa %s -debug-info-kind=line-tables-only -fopenmp -fopenmp-is-target-device -emit-llvm -o - | FileCheck %s
2+
program test
3+
implicit none
4+
5+
integer(kind = 4) :: a, b, c, d
6+
7+
!$omp target map(tofrom: a, b, c, d)
8+
a = a + 1
9+
! CHECK: !DILocation(line: [[@LINE-1]]
10+
b = a + 2
11+
! CHECK: !DILocation(line: [[@LINE-1]]
12+
c = a + 3
13+
! CHECK: !DILocation(line: [[@LINE-1]]
14+
d = a + 4
15+
! CHECK: !DILocation(line: [[@LINE-1]]
16+
!$omp end target
17+
18+
end program test

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/IR/CallingConv.h"
3232
#include "llvm/IR/Constant.h"
3333
#include "llvm/IR/Constants.h"
34+
#include "llvm/IR/DIBuilder.h"
3435
#include "llvm/IR/DebugInfoMetadata.h"
3536
#include "llvm/IR/DerivedTypes.h"
3637
#include "llvm/IR/Function.h"
@@ -6498,10 +6499,41 @@ static Function *createOutlinedFunction(
64986499
ParameterTypes.push_back(Arg->getType());
64996500
}
65006501

6502+
auto BB = Builder.GetInsertBlock();
6503+
auto M = BB->getModule();
65016504
auto FuncType = FunctionType::get(Builder.getVoidTy(), ParameterTypes,
65026505
/*isVarArg*/ false);
6503-
auto Func = Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName,
6504-
Builder.GetInsertBlock()->getModule());
6506+
auto Func =
6507+
Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName, M);
6508+
6509+
// If there's a DISubprogram associated with current function, then
6510+
// generate one for the outlined function.
6511+
if (Function *parentFunc = BB->getParent()) {
6512+
if (DISubprogram *SP = parentFunc->getSubprogram()) {
6513+
DICompileUnit *CU = SP->getUnit();
6514+
DIBuilder DB(*M, true, CU);
6515+
DebugLoc DL = Builder.getCurrentDebugLocation();
6516+
// TODO: We are using nullopt for arguments at the moment. This will need
6517+
// to be updated when debug data is being generated for variables.
6518+
DISubroutineType *Ty =
6519+
DB.createSubroutineType(DB.getOrCreateTypeArray(std::nullopt));
6520+
DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagDefinition |
6521+
DISubprogram::SPFlagOptimized |
6522+
DISubprogram::SPFlagLocalToUnit;
6523+
6524+
DISubprogram *OutlinedSP = DB.createFunction(
6525+
CU, FuncName, FuncName, SP->getFile(), DL.getLine(), Ty, DL.getLine(),
6526+
DINode::DIFlags::FlagArtificial, SPFlags);
6527+
6528+
// Attach subprogram to the function.
6529+
Func->setSubprogram(OutlinedSP);
6530+
// Update the CurrentDebugLocation in the builder so that right scope
6531+
// is used for things inside outlined function.
6532+
Builder.SetCurrentDebugLocation(
6533+
DILocation::get(Func->getContext(), DL.getLine(), DL.getCol(),
6534+
OutlinedSP, DL.getInlinedAt()));
6535+
}
6536+
}
65056537

65066538
// Save insert point.
65076539
auto OldInsertPoint = Builder.saveIP();

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6006,6 +6006,10 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
60066006
BasicBlock *FallbackBlock = Branch->getSuccessor(0);
60076007
Iter = FallbackBlock->rbegin();
60086008
CallInst *FCall = dyn_cast<CallInst>(&*(++Iter));
6009+
// 'F' has a dummy DISubprogram which causes OutlinedFunc to also
6010+
// have a DISubprogram. In this case, the call to OutlinedFunc needs
6011+
// to have a debug loc, otherwise verifier will complain.
6012+
FCall->setDebugLoc(DL);
60096013
EXPECT_NE(FCall, nullptr);
60106014

60116015
// Check that the correct aguments are passed in

0 commit comments

Comments
 (0)