Issue 235237
Summary [SPARC] -O0: hole in the location list of an argument described by #dbg_declare on the incoming pointer
Labels new issue
Assignees
Reporter shalseth
    I ran into this through rustc's debuginfo tests on sparc64 (`tests/debuginfo/regression-bad-location-list-67992.rs`), where gdb prints `<optimized out>` for a large struct passed by reference. rustc describes an argument like that with `#dbg_declare` on the incoming pointer itself, without an alloca, which clang doesn't do for C, so I reduced it to IR.

foo.ll:

```llvm
target datalayout = "E-m:e-i64:64-i128:128-n32:64-S128"
target triple = "sparc64-unknown-linux-gnu"

@g = external global i64

define i64 @foo(ptr %a, i64 %i) !dbg !10 {
entry:
    #dbg_declare(ptr %a, !14, !DIExpression(), !16)
  %c = icmp ult i64 %i, 64, !dbg !17
  br i1 %c, label %in, label %out, !dbg !17

in:
  %p = getelementptr inbounds i64, ptr %a, i64 %i, !dbg !17
  %v = load i64, ptr %p, align 8, !dbg !17
  ret i64 %v, !dbg !18

out:
  %x = load i64, ptr @g, align 8, !dbg !17
  ret i64 %x, !dbg !18
}

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4}

!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "hand-written", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
!1 = !DIFile(filename: "foo.c", directory: "/tmp")
!3 = !{i32 7, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed)
!6 = !DICompositeType(tag: DW_TAG_array_type, baseType: !5, size: 4096, elements: !7)
!7 = !{!8}
!8 = !DISubrange(count: 64)
!9 = !DISubroutineType(types: !{!5, !6, !5})
!10 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 3, type: !9, scopeLine: 3, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !{})
!14 = !DILocalVariable(name: "a", arg: 1, scope: !10, file: !1, line: 3, type: !6)
!16 = !DILocation(line: 3, scope: !10)
!17 = !DILocation(line: 4, column: 9, scope: !10)
!18 = !DILocation(line: 4, column: 2, scope: !10)
```

main.c:

```c
long g;
long foo(long *a, long i);

int main(void)
{
	long a[64] = { 0 };
	a[3] = 42;
	return foo(a, 3) != 42;
}
```

```
$ llc -O0 -relocation-model=pic -filetype=obj foo.ll -o foo.o
$ gcc -g -O0 -o gap main.c foo.o
$ gdb -batch -ex 'break foo.c:4' -ex run -ex 'print a' -ex 'x/gx $fp+2031' ./gap
Breakpoint 1, foo (a=...) at foo.c:4
$1 = <optimized out>
0x7fefffff1a0:	0x000007fefffff268
```

The location list for `a` has a hole right where line 4 starts:

```
DW_AT_location	(0x00000000:
   [0x0000000000000000, 0x000000000000001c): DW_OP_breg24 I0+0
   [0x0000000000000020, 0x0000000000000064): DW_OP_breg30 I6+2031, DW_OP_deref)

0x0000000000000008 3      0      1   0             0       0  is_stmt prologue_end
0x000000000000000c      0      0      1   0             0 0
0x000000000000001c      4      9      1   0             0       0 is_stmt
```

The pointer is stored to `[%fp+2031]` at 0x8, but the stack range only starts at the branch at 0x20, and in between %i0 is reused for the GOT base:

```
       0:      	save %sp, -0xa0, %sp
       4:      	stx %i1, [%fp+0x7e7]
       8:      	stx %i0, [%fp+0x7ef]
       c:      	rd %pc, %o7
      10:      	sethi 0x0, %i0
      14:      	or %i0, 0x0, %i0
      18:      	add %i0, %o7, %i0
      1c:      	cmp	%i1, 0x3f
 20:      	bgu %xcc, 0x48
```

For comparison, the same IR built for x86_64 doesn't show it. With FastISel the location covers the whole function. With `-fast-isel=false` the stack range also only starts at the branch rather than at the store, but %rdi stays untouched until then so there's no hole:

```
   [0x0000000000000000, 0x000000000000000e): DW_OP_breg5 RDI+0
   [0x000000000000000e, 0x000000000000002c): DW_OP_breg7 RSP-8, DW_OP_deref)

       0:      	movq	%rsi, -0x10(%rsp)
       5: 	movq	%rdi, -0x8(%rsp)
       a:      	subq	$0x3f, %rsi
       e: 	ja	0x21 <foo+0x21>
```

So it looks like the late start of the stack range isn't SPARC-specific, SPARC just happens to clobber the register before the branch. I might be reading that wrong though.

LLVM 23.1.4, GCC 16.2.1, gdb 18.1, UltraSPARC T4-1, Linux sparc64.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to