Skip to content

Commit 4a7c389

Browse files
authored
[flang][OpenMP][debug] Add DIOp expression for declare target variables. (llvm#590)
It mimics the DIOp-based expression that clang generates for such variable. With this change in place, I see the correct value of the declare target variables in the debugger.
2 parents b9d0411 + ec36b6d commit 4a7c389

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
! RUN: %flang_fc1 -triple amdgcn-amd-amdhsa -emit-llvm -fopenmp -fopenmp-is-target-device -debug-info-kind=standalone %s -o - | FileCheck %s
2+
3+
module helper
4+
implicit none
5+
real var_x
6+
real var_y
7+
!$omp declare target(var_x)
8+
!$omp declare target(var_y)
9+
end module helper
10+
11+
subroutine init()
12+
use helper
13+
!$omp declare target
14+
var_x = 3.14
15+
var_y = 0.25
16+
end
17+
18+
! CHECK-DAG: @_QMhelperEvar_x = addrspace(1) {{.*}}!dbg ![[XE:[0-9]+]]
19+
! CHECK-DAG: @_QMhelperEvar_y = addrspace(1) {{.*}}!dbg ![[YE:[0-9]+]]
20+
! CHECK-DAG: ![[XE]] = !DIGlobalVariableExpression(var: ![[X:[0-9]+]], expr: !DIExpression(DIOpArg(0, ptr addrspace(1)), DIOpDeref(ptr addrspace(1))))
21+
! CHECK-DAG: ![[YE]] = !DIGlobalVariableExpression(var: ![[Y:[0-9]+]], expr: !DIExpression(DIOpArg(0, ptr addrspace(1)), DIOpDeref(ptr addrspace(1))))
22+
! CHECK-DAG: ![[X]] = {{.*}}!DIGlobalVariable(name: "var_x"{{.*}})
23+
! CHECK-DAG: ![[Y]] = {{.*}}!DIGlobalVariable(name: "var_y"{{.*}})

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5104,6 +5104,33 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
51045104
return success();
51055105
}
51065106

5107+
// Add DIOp based expression in the declare target variables for AMDGPU target.
5108+
static void updateDebugInfoForDeclareTargetVariables(
5109+
LLVM::GlobalOp globalOp, LLVM::ModuleTranslation &moduleTranslation) {
5110+
llvm::Module *M = moduleTranslation.getLLVMModule();
5111+
if (!llvm::Triple(M->getTargetTriple()).isAMDGPU())
5112+
return;
5113+
5114+
llvm::GlobalVariable *GV = M->getGlobalVariable(globalOp.getSymName());
5115+
if (GV) {
5116+
llvm::SmallVector<llvm::DIGlobalVariableExpression *> GVEs;
5117+
GV->getDebugInfo(GVEs);
5118+
GV->eraseMetadata(llvm::LLVMContext::MD_dbg);
5119+
llvm::DIExprBuilder ExprBuilder(M->getContext());
5120+
unsigned int globalAS = M->getDataLayout().getDefaultGlobalsAddressSpace();
5121+
auto ptrTy = llvm::PointerType::get(M->getContext(), globalAS);
5122+
ExprBuilder.append<llvm::DIOp::Arg>(0u, ptrTy);
5123+
ExprBuilder.append<llvm::DIOp::Deref>(GV->getType());
5124+
for (auto *GVE : GVEs) {
5125+
llvm::DIExpression *Old = GVE->getExpression();
5126+
assert((Old == nullptr) || (Old->getNumElements() == 0));
5127+
auto *newGVE = llvm::DIGlobalVariableExpression::get(
5128+
M->getContext(), GVE->getVariable(), ExprBuilder.intoExpression());
5129+
GV->addDebugInfo(newGVE);
5130+
}
5131+
}
5132+
}
5133+
51075134
static LogicalResult
51085135
convertDeclareTargetAttr(Operation *op, mlir::omp::DeclareTargetAttr attribute,
51095136
LLVM::ModuleTranslation &moduleTranslation) {
@@ -5135,6 +5162,7 @@ convertDeclareTargetAttr(Operation *op, mlir::omp::DeclareTargetAttr attribute,
51355162

51365163
if (LLVM::GlobalOp gOp = dyn_cast<LLVM::GlobalOp>(op)) {
51375164
llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
5165+
updateDebugInfoForDeclareTargetVariables(gOp, moduleTranslation);
51385166
if (auto *gVal = llvmModule->getNamedValue(gOp.getSymName())) {
51395167
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
51405168
bool isDeclaration = gOp.isDeclaration();

0 commit comments

Comments
 (0)