Skip to content

Commit 4d37ae0

Browse files
committed
[mlir][flang] Improve handling of fortran module variables.
Currently, only those global variables which are at compile unit scope are added to the 'Globals' list of the compile unit. This does not work for module variables of 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. Kiran pointed out that MLIR changes should go in separate PRs.
1 parent 45fed80 commit 4d37ae0

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,11 +1029,17 @@ LogicalResult ModuleTranslation::convertGlobals() {
10291029
debugTranslation->translateGlobalVariableExpression(op.getDbgExpr());
10301030
llvm::DIGlobalVariable *diGlobalVar = diGlobalExpr->getVariable();
10311031
var->addDebugInfo(diGlobalExpr);
1032+
// For fortran, the scope hierarchy can be
1033+
// variable -> module -> compile unit
1034+
// If a variable scope points to Module then we get its parent scope so
1035+
// that globals get added to the compile unit.
1036+
llvm::DIScope *scope = diGlobalVar->getScope();
1037+
if (llvm::DIModule *mod = dyn_cast_if_present<llvm::DIModule>(scope))
1038+
scope = mod->getScope();
10321039

10331040
// Get the compile unit (scope) of the the global variable.
10341041
if (llvm::DICompileUnit *compileUnit =
1035-
dyn_cast_if_present<llvm::DICompileUnit>(
1036-
diGlobalVar->getScope())) {
1042+
dyn_cast_if_present<llvm::DICompileUnit>(scope)) {
10371043
// Update the compile unit with this incoming global variable expression
10381044
// during the finalizing step later.
10391045
allGVars[compileUnit].push_back(diGlobalExpr);

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

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

312312
// -----
313313

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

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

0 commit comments

Comments
 (0)