diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp index 669b95a9c6a5b..2e4b0feb19731 100644 --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -1030,10 +1030,21 @@ LogicalResult ModuleTranslation::convertGlobals() { llvm::DIGlobalVariable *diGlobalVar = diGlobalExpr->getVariable(); var->addDebugInfo(diGlobalExpr); + // There is no `globals` field in DICompileUnitAttr which can be directly + // assigned to DICompileUnit. We have to build the list by looking at the + // dbgExpr of all the GlobalOps. The scope of the variable is used to get + // the DICompileUnit in which to add it. But for the languages that + // support modules, the scope hierarchy can be + // variable -> module -> compile unit + // If a variable scope points to the module then we use the scope of the + // module to get the compile unit. + llvm::DIScope *scope = diGlobalVar->getScope(); + if (llvm::DIModule *mod = dyn_cast_if_present(scope)) + scope = mod->getScope(); + // Get the compile unit (scope) of the the global variable. if (llvm::DICompileUnit *compileUnit = - dyn_cast_if_present( - diGlobalVar->getScope())) { + dyn_cast_if_present(scope)) { // Update the compile unit with this incoming global variable expression // during the finalizing step later. allGVars[compileUnit].push_back(diGlobalExpr); diff --git a/mlir/test/Target/LLVMIR/llvmir-debug.mlir b/mlir/test/Target/LLVMIR/llvmir-debug.mlir index 1f0fc969364ac..15d4b8ccd88b6 100644 --- a/mlir/test/Target/LLVMIR/llvmir-debug.mlir +++ b/mlir/test/Target/LLVMIR/llvmir-debug.mlir @@ -311,6 +311,25 @@ llvm.mlir.global external @global_with_expr_2() {addr_space = 0 : i32, dbg_expr // ----- +// CHECK: @module_global = external global i64, !dbg {{.*}} +// CHECK: !llvm.module.flags = !{{{.*}}} +// CHECK: !llvm.dbg.cu = !{{{.*}}} +// CHECK-DAG: ![[FILE:.*]] = !DIFile(filename: "test.f90", directory: "existence") +// CHECK-DAG: ![[TYPE:.*]] = !DIBasicType(name: "integer", size: 64, encoding: DW_ATE_signed) +// CHECK-DAG: ![[SCOPE:.*]] = distinct !DICompileUnit(language: DW_LANG_Fortran95, file: ![[FILE]], producer: "MLIR", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: ![[GVALS:.*]]) +// CHECK-DAG: ![[SCOPE1:.*]] = !DIModule(scope: ![[SCOPE]], name: "module2", file: ![[FILE]], line: 120) +// CHECK-DAG: ![[GVAR:.*]] = distinct !DIGlobalVariable(name: "module_global", linkageName: "module_global", scope: ![[SCOPE1]], file: ![[FILE]], line: 121, type: ![[TYPE]], isLocal: false, isDefinition: true) +// CHECK-DAG: ![[GEXPR:.*]] = !DIGlobalVariableExpression(var: ![[GVAR]], expr: !DIExpression()) +// CHECK-DAG: ![[GVALS]] = !{![[GEXPR]]} + +#di_file = #llvm.di_file<"test.f90" in "existence"> +#di_compile_unit = #llvm.di_compile_unit, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "MLIR", isOptimized = true, emissionKind = Full> +#di_basic_type = #llvm.di_basic_type +#di_module = #llvm.di_module +llvm.mlir.global external @module_global() {dbg_expr = #llvm.di_global_variable_expression, expr = <>>} : i64 + +// ----- + // Nameless and scopeless global constant. // CHECK-LABEL: @.str.1 = external constant [10 x i8]