Skip to content

Commit 77b80bd

Browse files
authored
[mlir][flang] Improve handling of fortran module variables. (llvm#91604)
Currently, only those global variables which are at compile unit scope are added to the 'globals' list of the DICompileUnit. This does not work for languages which support modules (e.g. Fortran) where hierarchy can be variable -> module -> compile unit. To fix this, if a variable scope points to a module, we walk one level up and see if module is in the compile unit scope. This was initially part of llvm#91582 which adds debug information for Fortran module variables. @kiranchandramohan pointed out that MLIR changes should go in separate PRs.
1 parent 1355dcb commit 77b80bd

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,10 +1030,21 @@ LogicalResult ModuleTranslation::convertGlobals() {
10301030
llvm::DIGlobalVariable *diGlobalVar = diGlobalExpr->getVariable();
10311031
var->addDebugInfo(diGlobalExpr);
10321032

1033+
// There is no `globals` field in DICompileUnitAttr which can be directly
1034+
// assigned to DICompileUnit. We have to build the list by looking at the
1035+
// dbgExpr of all the GlobalOps. The scope of the variable is used to get
1036+
// the DICompileUnit in which to add it. But for the languages that
1037+
// support modules, the scope hierarchy can be
1038+
// variable -> module -> compile unit
1039+
// If a variable scope points to the module then we use the scope of the
1040+
// module to get the compile unit.
1041+
llvm::DIScope *scope = diGlobalVar->getScope();
1042+
if (llvm::DIModule *mod = dyn_cast_if_present<llvm::DIModule>(scope))
1043+
scope = mod->getScope();
1044+
10331045
// Get the compile unit (scope) of the the global variable.
10341046
if (llvm::DICompileUnit *compileUnit =
1035-
dyn_cast_if_present<llvm::DICompileUnit>(
1036-
diGlobalVar->getScope())) {
1047+
dyn_cast_if_present<llvm::DICompileUnit>(scope)) {
10371048
// Update the compile unit with this incoming global variable expression
10381049
// during the finalizing step later.
10391050
allGVars[compileUnit].push_back(diGlobalExpr);

mlir/test/Target/LLVMIR/llvmir-debug.mlir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,25 @@ llvm.mlir.global external @global_with_expr_2() {addr_space = 0 : i32, dbg_expr
311311

312312
// -----
313313

314+
// CHECK: @module_global = external global i64, !dbg {{.*}}
315+
// CHECK: !llvm.module.flags = !{{{.*}}}
316+
// CHECK: !llvm.dbg.cu = !{{{.*}}}
317+
// CHECK-DAG: ![[FILE:.*]] = !DIFile(filename: "test.f90", directory: "existence")
318+
// CHECK-DAG: ![[TYPE:.*]] = !DIBasicType(name: "integer", size: 64, encoding: DW_ATE_signed)
319+
// CHECK-DAG: ![[SCOPE:.*]] = distinct !DICompileUnit(language: DW_LANG_Fortran95, file: ![[FILE]], producer: "MLIR", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: ![[GVALS:.*]])
320+
// CHECK-DAG: ![[SCOPE1:.*]] = !DIModule(scope: ![[SCOPE]], name: "module2", file: ![[FILE]], line: 120)
321+
// CHECK-DAG: ![[GVAR:.*]] = distinct !DIGlobalVariable(name: "module_global", linkageName: "module_global", scope: ![[SCOPE1]], file: ![[FILE]], line: 121, type: ![[TYPE]], isLocal: false, isDefinition: true)
322+
// CHECK-DAG: ![[GEXPR:.*]] = !DIGlobalVariableExpression(var: ![[GVAR]], expr: !DIExpression())
323+
// CHECK-DAG: ![[GVALS]] = !{![[GEXPR]]}
324+
325+
#di_file = #llvm.di_file<"test.f90" in "existence">
326+
#di_compile_unit = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "MLIR", isOptimized = true, emissionKind = Full>
327+
#di_basic_type = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "integer", sizeInBits = 64, encoding = DW_ATE_signed>
328+
#di_module = #llvm.di_module<file = #di_file, scope = #di_compile_unit, name = "module2", configMacros = "", includePath = "", apinotes = "", line = 120, isDecl = false >
329+
llvm.mlir.global external @module_global() {dbg_expr = #llvm.di_global_variable_expression<var = <scope = #di_module, name = "module_global", linkageName = "module_global", file = #di_file, line = 121, type = #di_basic_type, isLocalToUnit = false, isDefined = true>, expr = <>>} : i64
330+
331+
// -----
332+
314333
// Nameless and scopeless global constant.
315334

316335
// CHECK-LABEL: @.str.1 = external constant [10 x i8]

0 commit comments

Comments
 (0)