Skip to content

Commit e2f0f94

Browse files
authored
Fix incompatible DI location op types (llvm#2298)
2 parents 7512dfe + adf51a2 commit e2f0f94

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function add(a, b) result(ret)
1515
!CHECK: define float @add_({{.*}}){{.*}}!dbg ![[SP:[0-9]+]] {
1616
!CHECK: #dbg_declare({{.*}}, ![[A:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(ptr), DIOpDeref(ptr)), !{{.*}})
1717
!CHECK: #dbg_declare({{.*}}, ![[B:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(ptr), DIOpDeref(ptr)), !{{.*}})
18-
!CHECK: #dbg_declare({{.*}}, ![[RET:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(ptr)), !{{.*}})
18+
!CHECK: #dbg_declare({{.*}}, ![[RET:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(float)), !{{.*}})
1919
!CHECK: }
2020
!CHECK: ![[SP]] = {{.*}}!DISubprogram(name: "add"{{.*}})
2121
!CHECK: ![[A]] = !DILocalVariable(name: "a", arg: 1, scope: ![[SP]]{{.*}})

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5741,21 +5741,23 @@ static void addAllocasForDeclareTargetFunctionPointerArgs(
57415741
// below. The users are updated accordingly.
57425742
for (auto &Arg : Fn->args()) {
57435743
if (Arg.getType()->isPointerTy()) {
5744-
llvm::Value *V = builder.CreateAlloca(Arg.getType(), allocaAS, nullptr);
5745-
if (allocaAS != defaultAS)
5746-
V = ompBuilder->Builder.CreateAddrSpaceCast(
5747-
V, builder.getPtrTy(defaultAS));
5748-
llvm::StoreInst *Store = builder.CreateStore(&Arg, V);
5749-
llvm::Value *Load = builder.CreateLoad(Arg.getType(), V);
5744+
llvm::Value *AllocaV =
5745+
builder.CreateAlloca(Arg.getType(), allocaAS, nullptr);
5746+
llvm::Value *GenericV = allocaAS == defaultAS
5747+
? AllocaV
5748+
: ompBuilder->Builder.CreateAddrSpaceCast(
5749+
AllocaV, builder.getPtrTy(defaultAS));
5750+
llvm::StoreInst *Store = builder.CreateStore(&Arg, GenericV);
5751+
llvm::Value *Load = builder.CreateLoad(Arg.getType(), GenericV);
57505752
llvm::SmallVector<llvm::DbgVariableIntrinsic *> DbgUsers;
57515753
llvm::SmallVector<llvm::DbgVariableRecord *> DPUsers;
57525754
llvm::findDbgUsers(DbgUsers, &Arg, &DPUsers);
57535755
for (auto *DVI : DbgUsers) {
5754-
DVI->replaceVariableLocationOp(&Arg, V);
5756+
DVI->replaceVariableLocationOp(&Arg, AllocaV);
57555757
DVI->setExpression(Expr);
57565758
}
57575759
for (auto *DVR : DPUsers) {
5758-
DVR->replaceVariableLocationOp(&Arg, V);
5760+
DVR->replaceVariableLocationOp(&Arg, AllocaV);
57595761
DVR->setExpression(Expr);
57605762
}
57615763
Arg.replaceUsesWithIf(Load, [&](const llvm::Use &U) -> bool {
@@ -5785,16 +5787,21 @@ static void updateDebugInfoForDeclareTargetFunctions(
57855787
// Skip if an expression is already present.
57865788
if ((Old != nullptr) && (Old->getNumElements() != 0))
57875789
return;
5788-
for (auto Loc : DR->location_ops()) {
5789-
llvm::Type *Ty = Loc->getType();
5790-
if (auto *Ref = dyn_cast<llvm::AddrSpaceCastInst>(Loc))
5791-
Ty = Ref->getPointerOperand()->getType();
5792-
llvm::DIExprBuilder EB(Fn->getContext());
5793-
EB.append<llvm::DIOp::Arg>(0u, Ty);
5794-
EB.append<llvm::DIOp::Deref>(Loc->getType());
5795-
DR->setExpression(EB.intoExpression());
5796-
break;
5797-
}
5790+
// Skip if the there are multiple inputs.
5791+
// FIXME: Could this be an assert? More to the point, can we do this at the
5792+
// point of generating the intrinsics to begin with, rather than fixing them
5793+
// up here?
5794+
if (DR->getNumVariableLocationOps() != 1u)
5795+
return;
5796+
auto Loc = DR->getVariableLocationOp(0u);
5797+
if (!isa<llvm::AllocaInst>(Loc->stripPointerCasts()))
5798+
return;
5799+
llvm::AllocaInst *AI = cast<llvm::AllocaInst>(Loc->stripPointerCasts());
5800+
DR->replaceVariableLocationOp(0u, AI);
5801+
llvm::DIExprBuilder EB(Fn->getContext());
5802+
EB.append<llvm::DIOp::Arg>(0u, AI->getType());
5803+
EB.append<llvm::DIOp::Deref>(AI->getAllocatedType());
5804+
DR->setExpression(EB.intoExpression());
57985805
};
57995806

58005807
for (llvm::Instruction &I : instructions(Fn)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module attributes {llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_target_devic
2727
// CHECK: %[[CAST:[0-9]+]] = addrspacecast ptr addrspace(5) %[[AL]]
2828
// CHECK: store ptr %[[ARG]], ptr %[[CAST]]{{.*}}
2929
// CHECK: load ptr, ptr %[[CAST]]{{.*}}
30-
// CHECK: #dbg_declare(ptr %[[CAST]], ![[A:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(ptr), DIOpDeref(ptr)), !{{.*}})
30+
// CHECK: #dbg_declare(ptr addrspace(5) %[[AL]], ![[A:[0-9]+]], !DIExpression(DIOpArg(0, ptr addrspace(5)), DIOpDeref(ptr), DIOpDeref(ptr)), !{{.*}})
3131
// CHECK: }
3232
// CHECK: ![[SP]] = {{.*}}!DISubprogram(name: "add"{{.*}})
3333
// CHECK: ![[A]] = !DILocalVariable(name: "a", arg: 1, scope: ![[SP]]{{.*}})

0 commit comments

Comments
 (0)