Skip to content

Commit f746817

Browse files
committed
Remove extra allocas from declare target functions.
Previously we had a problem with the debug information for variables in non-alloca location. We added extra allocas to side step that issue. That issue has been fixed so this PR removes the unnecessary allocas from the declare target functions. Also cleans up the expressions a bit.
1 parent a41b546 commit f746817

File tree

2 files changed

+12
-81
lines changed

2 files changed

+12
-81
lines changed

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

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5896,71 +5896,6 @@ static void updateDebugInfoForDeclareTargetVariables(
58965896
}
58975897
}
58985898

5899-
static void addAllocasForDeclareTargetFunctionPointerArgs(
5900-
llvm::Function *Fn, LLVM::ModuleTranslation &moduleTranslation) {
5901-
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
5902-
llvm::Module &M = ompBuilder->M;
5903-
5904-
if (!llvm::Triple(M.getTargetTriple()).isAMDGPU())
5905-
return;
5906-
5907-
if (Fn->empty())
5908-
return;
5909-
5910-
llvm::IRBuilderBase &builder = ompBuilder->Builder;
5911-
llvm::OpenMPIRBuilder::InsertPointTy curInsert = builder.saveIP();
5912-
unsigned int allocaAS = M.getDataLayout().getAllocaAddrSpace();
5913-
unsigned int defaultAS = M.getDataLayout().getProgramAddressSpace();
5914-
5915-
builder.SetInsertPoint(Fn->getEntryBlock().getFirstInsertionPt());
5916-
5917-
llvm::Type *PtrTy = builder.getPtrTy(defaultAS);
5918-
llvm::Type *AllocaPtrTy = builder.getPtrTy(allocaAS);
5919-
llvm::DIExprBuilder EB(Fn->getContext());
5920-
EB.append<llvm::DIOp::Arg>(0u, AllocaPtrTy);
5921-
EB.append<llvm::DIOp::Deref>(PtrTy);
5922-
EB.append<llvm::DIOp::Deref>(PtrTy);
5923-
llvm::DIExpression *Expr = EB.intoExpression();
5924-
5925-
// flang does not generate allocas for the arguments that are passed by ref.
5926-
// When the Argument is the location, the quality of the debug information is
5927-
// poor. The variables are defines on very few addresses and show up as
5928-
// optimized in most places. One of the reason is the interaction of DI-Op
5929-
// based ops and regular ones.
5930-
// Generating alloca seems like the best thing which is done in the loop
5931-
// below. The users are updated accordingly.
5932-
for (auto &Arg : Fn->args()) {
5933-
if (Arg.getType()->isPointerTy()) {
5934-
llvm::Value *AllocaV =
5935-
builder.CreateAlloca(Arg.getType(), allocaAS, nullptr);
5936-
llvm::Value *GenericV = allocaAS == defaultAS
5937-
? AllocaV
5938-
: ompBuilder->Builder.CreateAddrSpaceCast(
5939-
AllocaV, builder.getPtrTy(defaultAS));
5940-
llvm::StoreInst *Store = builder.CreateStore(&Arg, GenericV);
5941-
llvm::Value *Load = builder.CreateLoad(Arg.getType(), GenericV);
5942-
llvm::SmallVector<llvm::DbgVariableIntrinsic *> DbgUsers;
5943-
llvm::SmallVector<llvm::DbgVariableRecord *> DPUsers;
5944-
llvm::findDbgUsers(&Arg, DPUsers);
5945-
for (auto *DVI : DbgUsers) {
5946-
DVI->replaceVariableLocationOp(&Arg, AllocaV);
5947-
DVI->setExpression(Expr);
5948-
}
5949-
for (auto *DVR : DPUsers) {
5950-
DVR->replaceVariableLocationOp(&Arg, AllocaV);
5951-
DVR->setExpression(Expr);
5952-
}
5953-
Arg.replaceUsesWithIf(Load, [&](const llvm::Use &U) -> bool {
5954-
// We dont want to replace Arg from the store we created above.
5955-
if (const auto *SI = dyn_cast<llvm::StoreInst>(U.getUser()))
5956-
return SI != Store;
5957-
return true;
5958-
});
5959-
}
5960-
}
5961-
builder.restoreIP(curInsert);
5962-
}
5963-
59645899
// This function Add DIOp based expressions to the debug records in the
59655900
// declare target functions.
59665901

@@ -5984,13 +5919,16 @@ static void updateDebugInfoForDeclareTargetFunctions(
59845919
if (DR->getNumVariableLocationOps() != 1u)
59855920
return;
59865921
auto Loc = DR->getVariableLocationOp(0u);
5987-
if (!isa<llvm::AllocaInst>(Loc->stripPointerCasts()))
5988-
return;
5989-
llvm::AllocaInst *AI = cast<llvm::AllocaInst>(Loc->stripPointerCasts());
5990-
DR->replaceVariableLocationOp(0u, AI);
59915922
llvm::DIExprBuilder EB(Fn->getContext());
5992-
EB.append<llvm::DIOp::Arg>(0u, AI->getType());
5993-
EB.append<llvm::DIOp::Deref>(AI->getAllocatedType());
5923+
if (auto AI = dyn_cast<llvm::AllocaInst>(Loc->stripPointerCasts())) {
5924+
DR->replaceVariableLocationOp(0u, AI);
5925+
EB.append<llvm::DIOp::Arg>(0u, AI->getType());
5926+
EB.append<llvm::DIOp::Deref>(AI->getAllocatedType());
5927+
} else if (Loc->getType()->isPointerTy()) {
5928+
EB.append<llvm::DIOp::Arg>(0u, Loc->getType());
5929+
EB.append<llvm::DIOp::Deref>(Loc->getType());
5930+
} else
5931+
EB.append<llvm::DIOp::Arg>(0u, Loc->getType());
59945932
DR->setExpression(EB.intoExpression());
59955933
};
59965934

@@ -6027,11 +5965,8 @@ convertDeclareTargetAttr(Operation *op, mlir::omp::DeclareTargetAttr attribute,
60275965
if (declareType == omp::DeclareTargetDeviceType::host) {
60285966
llvmFunc->dropAllReferences();
60295967
llvmFunc->eraseFromParent();
6030-
} else {
6031-
addAllocasForDeclareTargetFunctionPointerArgs(llvmFunc,
6032-
moduleTranslation);
5968+
} else
60335969
updateDebugInfoForDeclareTargetFunctions(llvmFunc, moduleTranslation);
6034-
}
60355970
}
60365971
return success();
60375972
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2-
// XFAIL: *
2+
33
#file = #llvm.di_file<"target.f90" in "">
44
#cu = #llvm.di_compile_unit<id = distinct[0]<>,
55
sourceLanguage = DW_LANG_Fortran95, file = #file, isOptimized = false,
@@ -23,11 +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: load ptr, ptr %[[CAST]]{{.*}}
30-
// CHECK: #dbg_declare(ptr addrspace(5) %[[AL]], ![[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)), !{{.*}})
3127
// CHECK: }
3228
// CHECK: ![[SP]] = {{.*}}!DISubprogram(name: "add"{{.*}})
3329
// CHECK: ![[A]] = !DILocalVariable(name: "a", arg: 1, scope: ![[SP]]{{.*}})

0 commit comments

Comments
 (0)