39
39
#include " llvm/TargetParser/Triple.h"
40
40
#include " llvm/Transforms/Utils/BasicBlockUtils.h"
41
41
#include " llvm/Transforms/Utils/ModuleUtils.h"
42
+ #include " llvm/IR/InstIterator.h"
43
+ #include " llvm/IR/IntrinsicInst.h"
42
44
43
45
#include < any>
44
46
#include < cstdint>
@@ -5462,6 +5464,67 @@ static void updateDebugInfoForDeclareTargetVariables(
5462
5464
}
5463
5465
}
5464
5466
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.
5472
+ static void updateDebugInfoForDeclareTargetFunctions (
5473
+ llvm::Function *Fn, LLVM::ModuleTranslation &moduleTranslation) {
5474
+ llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder ();
5475
+ llvm::Module &M = ompBuilder->M ;
5476
+
5477
+ if (!((llvm::Triple (M.getTargetTriple ())).isAMDGPU ()))
5478
+ return ;
5479
+
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
+ };
5504
+ auto UpdateDebugRecord = [&](auto *DR) {
5505
+ for (auto Loc : DR->location_ops ()) {
5506
+ 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
+ }
5515
+ ExprBuilder.append <llvm::DIOp::Deref>(Loc->getType ());
5516
+ DR->setExpression (ExprBuilder.intoExpression ());
5517
+ }
5518
+ };
5519
+ for (llvm::Instruction &I : instructions (Fn)) {
5520
+ if (auto *DDI = dyn_cast<llvm::DbgVariableIntrinsic>(&I))
5521
+ UpdateDebugRecord (DDI);
5522
+
5523
+ for (llvm::DbgVariableRecord &DVR : filterDbgVars (I.getDbgRecordRange ()))
5524
+ UpdateDebugRecord (&DVR);
5525
+ }
5526
+ }
5527
+
5465
5528
static LogicalResult
5466
5529
convertDeclareTargetAttr (Operation *op, mlir::omp::DeclareTargetAttr attribute,
5467
5530
LLVM::ModuleTranslation &moduleTranslation) {
@@ -5481,12 +5544,13 @@ convertDeclareTargetAttr(Operation *op, mlir::omp::DeclareTargetAttr attribute,
5481
5544
omp::DeclareTargetDeviceType declareType =
5482
5545
attribute.getDeviceType ().getValue ();
5483
5546
5547
+ llvm::Function *llvmFunc =
5548
+ moduleTranslation.lookupFunction (funcOp.getName ());
5484
5549
if (declareType == omp::DeclareTargetDeviceType::host) {
5485
- llvm::Function *llvmFunc =
5486
- moduleTranslation.lookupFunction (funcOp.getName ());
5487
5550
llvmFunc->dropAllReferences ();
5488
5551
llvmFunc->eraseFromParent ();
5489
- }
5552
+ } else
5553
+ updateDebugInfoForDeclareTargetFunctions (llvmFunc, moduleTranslation);
5490
5554
}
5491
5555
return success ();
5492
5556
}
0 commit comments