[flang-commits] [flang] [flang][debug] Generate DISubprogramAttr for omp::TargetOp. (PR #138039)
Sergio Afonso via flang-commits
flang-commits at lists.llvm.org
Fri Jun 13 07:28:51 PDT 2025
================
@@ -445,13 +477,88 @@ void AddDebugInfoPass::handleFuncOp(mlir::func::FuncOp funcOp,
line - 1, false);
}
+ auto addTargetOpDISP = [&](bool lineTableOnly,
+ const llvm::SmallVector<mlir::LLVM::DINodeAttr>
+ &entities) {
+ // When we process the DeclareOp inside the OpenMP target region, all the
+ // variables get the DISubprogram of the parent function of the target op as
+ // the scope. In the codegen (to llvm ir), OpenMP target op results in the
+ // creation of a separate function. As the variables in the debug info have
+ // the DISubprogram of the parent function as the scope, the variables
+ // need to be updated at codegen time to avoid verification failures.
+
+ // This updating after the fact becomes more and more difficult when types
+ // are dependent on local variables like in the case of variable size arrays
+ // or string. We not only have to generate new variables but also new types.
+ // We can avoid this problem by generating a DISubprogramAttr here for the
+ // target op and make sure that all the variables inside the target region
+ // get the correct scope in the first place.
+ funcOp.walk([&](mlir::omp::TargetOp targetOp) {
+ unsigned line = getLineFromLoc(targetOp.getLoc());
+ mlir::StringAttr name =
+ getTargetFunctionName(context, targetOp.getLoc(), funcOp.getName());
+ mlir::LLVM::DISubprogramFlags flags =
+ mlir::LLVM::DISubprogramFlags::Definition |
+ mlir::LLVM::DISubprogramFlags::LocalToUnit;
+ if (isOptimized)
+ flags = flags | mlir::LLVM::DISubprogramFlags::Optimized;
+
+ mlir::DistinctAttr id =
+ mlir::DistinctAttr::create(mlir::UnitAttr::get(context));
+ llvm::SmallVector<mlir::LLVM::DITypeAttr> types;
+ types.push_back(mlir::LLVM::DINullTypeAttr::get(context));
+ for (auto arg : targetOp.getRegion().getArguments()) {
+ auto tyAttr = typeGen.convertType(fir::unwrapRefType(arg.getType()),
+ fileAttr, cuAttr, /*declOp=*/nullptr);
+ types.push_back(tyAttr);
+ }
+ CC = llvm::dwarf::getCallingConvention("DW_CC_normal");
+ mlir::LLVM::DISubroutineTypeAttr spTy =
+ mlir::LLVM::DISubroutineTypeAttr::get(context, CC, types);
+ if (lineTableOnly) {
+ auto spAttr = mlir::LLVM::DISubprogramAttr::get(
+ context, id, compilationUnit, Scope, name, name, funcFileAttr, line,
+ line, flags, spTy, /*retainedNodes=*/{}, /*annotations=*/{});
+ targetOp->setLoc(builder.getFusedLoc({targetOp.getLoc()}, spAttr));
+ return;
+ }
+ mlir::DistinctAttr recId =
+ mlir::DistinctAttr::create(mlir::UnitAttr::get(context));
+ auto spAttr = mlir::LLVM::DISubprogramAttr::get(
+ context, recId, /*isRecSelf=*/true, id, compilationUnit, Scope, name,
+ name, funcFileAttr, line, line, flags, spTy, /*retainedNodes=*/{},
+ /*annotations=*/{});
+
+ // Make sure that information about the imported modules is copied in the
+ // new function.
+ llvm::SmallVector<mlir::LLVM::DINodeAttr> opEntities;
+ for (mlir::LLVM::DINodeAttr N : entities) {
+ if (auto entity = mlir::dyn_cast<mlir::LLVM::DIImportedEntityAttr>(N)) {
+ auto importedEntity = mlir::LLVM::DIImportedEntityAttr::get(
+ context, llvm::dwarf::DW_TAG_imported_module, spAttr,
+ entity.getEntity(), fileAttr, /*line=*/1, /*name=*/nullptr,
+ /*elements*/ {});
+ opEntities.push_back(importedEntity);
+ }
+ }
+
+ id = mlir::DistinctAttr::create(mlir::UnitAttr::get(context));
+ spAttr = mlir::LLVM::DISubprogramAttr::get(
+ context, recId, /*isRecSelf=*/false, id, compilationUnit, Scope, name,
+ name, funcFileAttr, line, line, flags, spTy, opEntities,
+ /*annotations=*/{});
+ targetOp->setLoc(builder.getFusedLoc({targetOp.getLoc()}, spAttr));
+ });
+ };
+
// Don't process variables if user asked for line tables only.
if (debugLevel == mlir::LLVM::DIEmissionKind::LineTablesOnly) {
auto spAttr = mlir::LLVM::DISubprogramAttr::get(
context, id, compilationUnit, Scope, funcName, fullName, funcFileAttr,
line, line, subprogramFlags, subTypeAttr, /*retainedNodes=*/{},
/*annotations=*/{});
funcOp->setLoc(builder.getFusedLoc({l}, spAttr));
+ addTargetOpDISP(true, {});
----------------
skatrak wrote:
```suggestion
addTargetOpDISP(/*lineTableOnly=*/true, /*entities=*/{});
```
https://github.com/llvm/llvm-project/pull/138039
More information about the flang-commits
mailing list