Skip to content

[mlir][debug] Allow global with local scope. #98358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,9 +1073,15 @@ LogicalResult ModuleTranslation::convertGlobals() {
// 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.
// Global variables are also used for things like static local variables
// in C and local variables with the save attribute in Fortran. The scope
// of the variable is the parent function. We use the compile unit of the
// parent function in this case.
llvm::DIScope *scope = diGlobalVar->getScope();
if (llvm::DIModule *mod = dyn_cast_if_present<llvm::DIModule>(scope))
if (auto *mod = dyn_cast_if_present<llvm::DIModule>(scope))
scope = mod->getScope();
else if (auto *sp = dyn_cast_if_present<llvm::DISubprogram>(scope))
scope = sp->getUnit();

// Get the compile unit (scope) of the the global variable.
if (llvm::DICompileUnit *compileUnit =
Expand Down
19 changes: 19 additions & 0 deletions mlir/test/Target/LLVMIR/llvmir-debug.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,25 @@ llvm.mlir.global external @module_global() {dbg_expr = #llvm.di_global_variable_

// -----

// CHECK: @func_global = external global i64, !dbg {{.*}}
// CHECK-DAG: ![[CU:.*]] = distinct !DICompileUnit({{.*}}globals: ![[GVALS:.*]])
// CHECK-DAG: ![[SP:.*]] = distinct !DISubprogram(name: "fn_with_gl"{{.*}}unit: ![[CU]])
// CHECK-DAG: ![[GVAR:.*]] = distinct !DIGlobalVariable(name: "func_global"{{.*}}, scope: ![[SP]]{{.*}})
// CHECK-DAG: ![[GEXPR:.*]] = !DIGlobalVariableExpression(var: ![[GVAR]], expr: !DIExpression())
// CHECK-DAG: ![[GVALS]] = !{![[GEXPR]]}

#file = #llvm.di_file<"test.f90" in "existence">
#cu = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_Fortran95, file = #file, producer = "MLIR", isOptimized = true, emissionKind = Full>
#ty1 = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "integer", sizeInBits = 64, encoding = DW_ATE_signed>
#sp = #llvm.di_subprogram<compileUnit = #cu, scope = #file, name = "fn_with_gl", file = #file, subprogramFlags = "Definition|Optimized">
llvm.mlir.global @func_global() {dbg_expr = #llvm.di_global_variable_expression<var = <scope = #sp, name = "func_global", linkageName = "func_global", file = #file, line = 121, type = #ty1, isLocalToUnit = true, isDefined = true>, expr = <>>} : i64

llvm.func @fn_with_gl() {
llvm.return
} loc(fused<#sp>["foo1.mlir":0:0])

// -----

// Nameless and scopeless global constant.

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