Skip to content

Commit 9b75f26

Browse files
committed
Handle review comments.
I figured out the reason that backend was dropping debug information for arguments. So we dont need to add an alloca just for the debug now. I have updated the patch and tests accordingly. Also handled other review comments.
1 parent 47b748a commit 9b75f26

File tree

3 files changed

+11
-44
lines changed

3 files changed

+11
-44
lines changed

flang/test/Integration/amdgpu/debug-declare-target-function-var.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ function add(a, b) result(ret)
1313
end
1414

1515
!CHECK: define float @add_({{.*}}){{.*}}!dbg ![[SP:[0-9]+]] {
16-
!CHECK: #dbg_declare({{.*}}, ![[A:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(ptr), DIOpDeref(ptr)), !{{.*}})
17-
!CHECK: #dbg_declare({{.*}}, ![[B:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(ptr), DIOpDeref(ptr)), !{{.*}})
16+
!CHECK: #dbg_declare({{.*}}, ![[A:[0-9]+]], !DIExpression(DIOpArg(0, ptr), DIOpDeref(ptr)), !{{.*}})
17+
!CHECK: #dbg_declare({{.*}}, ![[B:[0-9]+]], !DIExpression(DIOpArg(0, ptr), DIOpDeref(ptr)), !{{.*}})
1818
!CHECK: #dbg_declare({{.*}}, ![[RET:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(ptr)), !{{.*}})
1919
!CHECK: }
2020
!CHECK: ![[SP]] = {{.*}}!DISubprogram(name: "add"{{.*}})

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

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5464,54 +5464,24 @@ static void updateDebugInfoForDeclareTargetVariables(
54645464
}
54655465
}
54665466

5467-
// This function handle any adjustments needed in declare target function to
5468-
// generate valid debug info for AMDGPU. It does 2 main things:
5469-
// 1. Add DIOp based expressions
5470-
// 2. If a debug reocrd points to function Argument as location then change it
5471-
// to use an alloca instead.
5467+
// This function adds DIOp based expressions in declare target function to
5468+
// generate valid debug info for AMDGPU.
5469+
54725470
static void updateDebugInfoForDeclareTargetFunctions(
54735471
llvm::Function *Fn, LLVM::ModuleTranslation &moduleTranslation) {
54745472
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
54755473
llvm::Module &M = ompBuilder->M;
54765474

5477-
if (!((llvm::Triple(M.getTargetTriple())).isAMDGPU()))
5475+
if (!llvm::Triple(M.getTargetTriple()).isAMDGPU())
54785476
return;
54795477

5480-
llvm::IRBuilderBase &builder = ompBuilder->Builder;
5481-
llvm::OpenMPIRBuilder::InsertPointTy curInsert = builder.saveIP();
5482-
unsigned int allocaAS = M.getDataLayout().getAllocaAddrSpace();
5483-
unsigned int defaultAS = M.getDataLayout().getProgramAddressSpace();
5484-
5485-
// In most cases, function argument are passed by reference in Fortran. In
5486-
// such cases, flang does not generate an alloca for such arguments. The
5487-
// debug record also point to the Argument* as the location. The AMDGPU
5488-
// backend drops the debug record in such cases. To side step this issue,
5489-
// we generate an alloca and store the Argument pointer in it. Then we can
5490-
// use that alloca as variable location and it works fine. Note that the
5491-
// expression used in the debug record has doubel indirection now. One for
5492-
// reading Argument* from the alloca and then reading the variable value from
5493-
// the Argument*.
5494-
auto genAlloca = [&](llvm::Value *Arg) {
5495-
builder.SetInsertPoint(Fn->getEntryBlock().getFirstInsertionPt());
5496-
llvm::Value *V = builder.CreateAlloca(Arg->getType(), allocaAS, nullptr);
5497-
if (allocaAS != defaultAS && Arg->getType()->isPointerTy())
5498-
V = ompBuilder->Builder.CreateAddrSpaceCast(V,
5499-
builder.getPtrTy(defaultAS));
5500-
builder.CreateStore(Arg, V);
5501-
builder.restoreIP(curInsert);
5502-
return V;
5503-
};
55045478
auto UpdateDebugRecord = [&](auto *DR) {
55055479
for (auto Loc : DR->location_ops()) {
55065480
llvm::DIExprBuilder ExprBuilder(Fn->getContext());
5507-
ExprBuilder.append<llvm::DIOp::Arg>(
5508-
0u, ompBuilder->Builder.getPtrTy(allocaAS));
5509-
if (auto *Arg = dyn_cast<llvm::Argument>(Loc)) {
5510-
ExprBuilder.append<llvm::DIOp::Deref>(
5511-
ompBuilder->Builder.getPtrTy(defaultAS));
5512-
llvm::Value *newLoc = genAlloca(Arg);
5513-
DR->replaceVariableLocationOp(Arg, newLoc);
5514-
}
5481+
llvm::Type *Ty = Loc->getType();
5482+
if (auto *Ref = dyn_cast<llvm::AddrSpaceCastInst>(Loc))
5483+
Ty = Ref->getPointerOperand()->getType();
5484+
ExprBuilder.append<llvm::DIOp::Arg>(0u, Ty);
55155485
ExprBuilder.append<llvm::DIOp::Deref>(Loc->getType());
55165486
DR->setExpression(ExprBuilder.intoExpression());
55175487
}

mlir/test/Target/LLVMIR/omptarget-decl-target-fn-debug-amdgpu.mlir

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ module attributes {llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_target_devic
2323
#loc3 = loc(fused<#sp>[#loc1])
2424

2525
// CHECK: define{{.*}}@add(ptr %[[ARG:[0-9]+]]){{.*}}!dbg ![[SP:[0-9]+]] {
26-
// CHECK: %[[AL:[0-9]+]] = alloca{{.*}}
27-
// CHECK: %[[CAST:[0-9]+]] = addrspacecast ptr addrspace(5) %[[AL]]
28-
// CHECK: store ptr %[[ARG]], ptr %[[CAST]]{{.*}}
29-
// CHECK: #dbg_declare(ptr %[[CAST]], ![[A:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(ptr), DIOpDeref(ptr)), !{{.*}})
26+
// CHECK: #dbg_declare(ptr %[[ARG]], ![[A:[0-9]+]], !DIExpression(DIOpArg(0, ptr), DIOpDeref(ptr)), !{{.*}})
3027
// CHECK: }
3128
// CHECK: ![[SP]] = {{.*}}!DISubprogram(name: "add"{{.*}})
3229
// CHECK: ![[A]] = !DILocalVariable(name: "a", arg: 1, scope: ![[SP]]{{.*}})

0 commit comments

Comments
 (0)