| Issue |
238918
|
| Summary |
[flang][debug] VOLATILE SAVE variable loses its procedure debug scope
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
SergeyShch01
|
With `-g`, adding `VOLATILE` to a saved local variable makes Flang describe the same storage with both a `DIGlobalVariable` and a `DILocalVariable` in LLVM IR. In the generated object, only one variable DIE remains, but it is scoped to the compile unit rather than to the source procedure.
The reproducer uses ordinary upstream Flang. It requires no OpenMP or out-of-tree changes.
### Reproducer
Save as `volatile_saved.f90`:
```fortran
program volatile_global
implicit none
integer, save, volatile :: x
x = 1
print *, x
end program volatile_global
```
Generate LLVM IR and an object:
```sh
flang -O0 -g -S -emit-llvm -x f95 - -o - \
< volatile_saved.f90 > volatile_saved.ll
flang -O0 -g -c -x f95 - -o - \
< volatile_saved.f90 > volatile_saved.o
grep -F -e 'DIGlobalVariable(name: "x"' \
-e 'DILocalVariable(name: "x"' volatile_saved.ll
readelf --debug-dump=info --wide volatile_saved.o
```
The stdin form above is the form used for verification.
### Actual LLVM IR
The relevant excerpt is:
```llvm
@_QFEx = internal global i32 0, !dbg !0
define void @_QQmain() #0 !dbg !13 {
#dbg_declare(ptr @_QFEx, !16, !DIExpression(), !17)
; ...
}
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "x", scope: !2, file: !5, line: 3, type: !6, isLocal: true, isDefinition: true)
!16 = !DILocalVariable(name: "x", scope: !13, file: !5, line: 3, type: !6)
```
Here `!2` is the `DICompileUnit`, while `!13` is the `DISubprogram` for the Fortran main program `volatile_global`. Metadata numbers may vary.
The local debug declaration points at the same global address that already carries global debug metadata.
### Object DWARF and controls
Compile two controls with the same commands, changing only the declaration:
- Saved, non-volatile control: `integer, save :: x`.
- Local volatile control: `integer, volatile :: x`.
The results on the tested build are:
| Declaration | LLVM debug records named `x` | Object DWARF |
| --- | --- | --- |
| `integer, save, volatile :: x` | One global and one local | One `DW_TAG_variable` at compile-unit scope |
| `integer, save :: x` | One global, no local | One `DW_TAG_variable` inside MAIN's `DW_TAG_subprogram` |
| `integer, volatile :: x` | No global, one local | One `DW_TAG_variable` inside MAIN's `DW_TAG_subprogram` |
Thus the duplicate LLVM metadata does **not** produce two variable DIEs in this object. The observable DWARF difference is the loss of the procedure scope. No debugger was run, so this report does not claim two debugger-visible variables or a particular debugger lookup failure.
### Expected behavior
Adding `VOLATILE` should not change the source scope of `x`. The saved variable should have one global debug description scoped to MAIN, as in the saved non-volatile control, without a separate local debug declaration for the same global storage.
### Relevant lowering path
The affected HLFIR contains:
```mlir
%1 = fir.address_of(@_QFEx) : !fir.ref<i32>
%2 = fir.volatile_cast %1 : (!fir.ref<i32>) -> !fir.ref<i32, volatile>
```
The declaration uses the result of `fir.volatile_cast`.
In [`AddDebugInfoPass::handleDeclareOp`](https://github.com/llvm/llvm-project/blob/0bbcb44259057f40f976c5b7977e693584a55da6/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp#L493), global recognition checks whether the declaration's immediate storage-defining operation is `fir::AddrOfOp`. The intervening `fir.volatile_cast` causes this check to fail, and the declaration goes through `handleLocalVariable`. The later pass over globals then describes the global without the declaration's procedure scope.
### Tested environment
- Upstream revision: [`0bbcb44259057f40f976c5b7977e693584a55da6`](https://github.com/llvm/llvm-project/commit/0bbcb44259057f40f976c5b7977e693584a55da6).
- Flang `24.0.0git`, target `aarch64-unknown-linux-gnu`, assertions enabled.
- GNU readelf `2.38`.
- The reproducer and both controls compiled successfully at `-O0 -g`. The LLVM metadata and object DWARF were inspected.
Prepared with AI assistance; the reproducer and control results above were verified on the specified build.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs