Skip to content

Commit 2fc3de9

Browse files
committed
[flang][debug] Use DICompileUnit as scope.
This PR is required for llvm#96316. For assumed size arrays, we don't have the size of the array in the last dimension. This is represented in metadata by having a DISubrange with no entry for count or upper bound. LLVM verifier allows a DISubrange with no count or upper bound only for Fortran. This language is set when DICompileUnit is processed. If a type is processed before DICompileUnit then Verifier will not allow DISubrange with not count or upper bound entry. It will fail with error "Subrange must contain count or upperBound". Above scenario easily happens if a subroutine at global scope takes assumed size parameter. The metadata on DISubprogram is such that type is processed before unit so we hit Verifier::visitDISubrange before language has been set to Fortran. I have worked around this issue by setting the scope of such functions to be DICompileUnit instead of DIFile. As scope metadata in DISubprogram is processed before type so this problem does not happen. Some other observations about this change: 1. It does not make any difference to the generated DWARF. 2. It bring subroutine inline with module as modules already set their scope to DiCompileUnit. 3. I also looked at the classic flang compiler and it also uses DICompileUnit as scope for such subroutines probably for the same reasons.
1 parent c2a22f1 commit 2fc3de9

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

flang/lib/Optimizer/Transforms/AddDebugInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ void AddDebugInfoPass::runOnOperation() {
266266

267267
// Only definitions need a distinct identifier and a compilation unit.
268268
mlir::DistinctAttr id;
269-
mlir::LLVM::DIScopeAttr Scope = fileAttr;
269+
mlir::LLVM::DIScopeAttr Scope = cuAttr;
270270
mlir::LLVM::DICompileUnitAttr compilationUnit;
271271
mlir::LLVM::DISubprogramFlags subprogramFlags =
272272
mlir::LLVM::DISubprogramFlags{};

flang/test/Transforms/debug-line-table-inc-file.fir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
3131
// CHECK: #[[LOC_INC_FILE:.*]] = loc("{{.*}}inc.f90":1:1)
3232
// CHECK: #[[LOC_FILE:.*]] = loc("{{.*}}simple.f90":3:1)
3333
// CHECK: #[[DI_CU:.*]] = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #[[DI_FILE]], producer = "{{.*}}flang{{.*}}", isOptimized = false, emissionKind = LineTablesOnly>
34-
// CHECK: #[[DI_SP_INC:.*]] = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #[[DI_CU]], scope = #[[DI_FILE]], name = "sinc", linkageName = "_QPsinc", file = #[[DI_INC_FILE]], {{.*}}>
35-
// CHECK: #[[DI_SP:.*]] = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #[[DI_CU]], scope = #[[DI_FILE]], name = "_QQmain", linkageName = "_QQmain", file = #[[DI_FILE]], {{.*}}>
34+
// CHECK: #[[DI_SP_INC:.*]] = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #[[DI_CU]], scope = #[[DI_CU]], name = "sinc", linkageName = "_QPsinc", file = #[[DI_INC_FILE]], {{.*}}>
35+
// CHECK: #[[DI_SP:.*]] = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #[[DI_CU]], scope = #[[DI_CU]], name = "_QQmain", linkageName = "_QQmain", file = #[[DI_FILE]], {{.*}}>
3636
// CHECK: #[[FUSED_LOC_INC_FILE]] = loc(fused<#[[DI_SP_INC]]>[#[[LOC_INC_FILE]]])
3737
// CHECK: #[[FUSED_LOC_FILE]] = loc(fused<#[[DI_SP]]>[#[[LOC_FILE]]])

0 commit comments

Comments
 (0)