llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

DWARF debug info for 32-bit variable has a 64-bit composite location description

Open dstenb opened this issue 2 years ago • 3 comments

LLVM commit: https://github.com/llvm/llvm-project/commit/ef35da825f38a86e4621c13a2898d561da88991c

When compiling the following program:

volatile int a;
int b;
long c;
char d;
long long e;
void main() {
  int f = c < 6;
  long long g = f;
  d = g ^= e;
  b = 0;
  for (; b < 10; b++)
    for (; a;) // break here and print f
      for (;;)
        ;
}

for i386 using:

clang -m32 -O3 -g reduce30841.c

GDB 13.2 will emit an error when trying to print f:

(gdb) b 12
Breakpoint 1 at 0x11f6: file reduce30841.c, line 12.
(gdb) run
Starting program: reduce30841.out 

Breakpoint 1, main () at reduce30841.c:12
12          for (; a;)
(gdb) p f
access outside bounds of object referenced via synthetic pointer

The DW_AT_location in question looks like:

0x0000008a:     DW_TAG_variable
                  DW_AT_location        (indexed (0x1) loclist = 0x00000025: 
                     [0x000011e8, 0x000011ee): DW_OP_reg1 ECX, DW_OP_piece 0x4
                     [0x000011f6, 0x00001204): DW_OP_piece 0x4, DW_OP_reg1 ECX, DW_OP_piece 0x4)
                  DW_AT_name    ("f")
                  DW_AT_decl_file       ("reduce30841.c")
                  DW_AT_decl_line       (7)
                  DW_AT_type    (0x00000037 "int")

As seen, a 64-bit composite location description is emitted for the range [0x000011f6, 0x00001204).

The llvm.dbg.value for the 32-bit variable is changed to a 64-bit value by InstCombine:

-; *** IR Dump Before InstCombinePass on main ***
+; *** IR Dump After InstCombinePass on main ***
 ; ModuleID = 'reduce30841.c'
 source_filename = "reduce30841.c"
 target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128"
@@ -15,39 +15,37 @@
 entry:
   %0 = load i32, ptr @c, align 4, !dbg !33, !tbaa !34
   %cmp = icmp slt i32 %0, 6, !dbg !38
-  %conv = zext i1 %cmp to i32, !dbg !38
-  tail call void @llvm.dbg.value(metadata i32 %conv, metadata !31, metadata !DIExpression()), !dbg !39
-  %conv1 = zext nneg i32 %conv to i64
+  %conv1 = zext i1 %cmp to i64
+  tail call void @llvm.dbg.value(metadata i64 %conv1, metadata !31, metadata !DIExpression()), !dbg !39

[...]
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!31 = !DILocalVariable(name: "f", scope: !27, file: !3, line: 7, type: !8)

but I don't know if that is an issue, or if this should be handled in later steps.

dstenb avatar Dec 14 '23 10:12 dstenb

@llvm/issue-subscribers-debuginfo

Author: David Stenberg (dstenb)

LLVM commit: https://github.com/llvm/llvm-project/commit/ef35da825f38a86e4621c13a2898d561da88991c

When compiling the following program:

volatile int a;
int b;
long c;
char d;
long long e;
void main() {
  int f = c &lt; 6;
  long long g = f;
  d = g ^= e;
  b = 0;
  for (; b &lt; 10; b++)
    for (; a;) // break here and print f
      for (;;)
        ;
}

for i386 using:

clang -m32 -O3 -g reduce30841.c

GDB 13.2 will emit an error when trying to print f:

(gdb) b 12
Breakpoint 1 at 0x11f6: file reduce30841.c, line 12.
(gdb) run
Starting program: reduce30841.out 

Breakpoint 1, main () at reduce30841.c:12
12          for (; a;)
(gdb) p f
access outside bounds of object referenced via synthetic pointer

The DW_AT_location in question looks like:

0x0000008a:     DW_TAG_variable
                  DW_AT_location        (indexed (0x1) loclist = 0x00000025: 
                     [0x000011e8, 0x000011ee): DW_OP_reg1 ECX, DW_OP_piece 0x4
                     [0x000011f6, 0x00001204): DW_OP_piece 0x4, DW_OP_reg1 ECX, DW_OP_piece 0x4)
                  DW_AT_name    ("f")
                  DW_AT_decl_file       ("reduce30841.c")
                  DW_AT_decl_line       (7)
                  DW_AT_type    (0x00000037 "int")

As seen, a 64-bit composite location description is emitted for the range [0x000011f6, 0x00001204).

The llvm.dbg.value for the 32-bit variable is changed to a 64-bit value by InstCombine:

-; *** IR Dump Before InstCombinePass on main ***
+; *** IR Dump After InstCombinePass on main ***
 ; ModuleID = 'reduce30841.c'
 source_filename = "reduce30841.c"
 target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128"
@@ -15,39 +15,37 @@
 entry:
   %0 = load i32, ptr @<!-- -->c, align 4, !dbg !33, !tbaa !34
   %cmp = icmp slt i32 %0, 6, !dbg !38
-  %conv = zext i1 %cmp to i32, !dbg !38
-  tail call void @<!-- -->llvm.dbg.value(metadata i32 %conv, metadata !31, metadata !DIExpression()), !dbg !39
-  %conv1 = zext nneg i32 %conv to i64
+  %conv1 = zext i1 %cmp to i64
+  tail call void @<!-- -->llvm.dbg.value(metadata i64 %conv1, metadata !31, metadata !DIExpression()), !dbg !39

[...]
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!31 = !DILocalVariable(name: "f", scope: !27, file: !3, line: 7, type: !8)

but I don't know if that is an issue, or if this should be handled in later steps.

llvmbot avatar Dec 14 '23 12:12 llvmbot

(Skimming over some older bug reports...)

The debug record gets split in two during isel when the zext to i64 is converted into a zext to i32 (lower bits) and constant<0> (upper bits). The function responsible is SelectionDAG::transferDbgValues. We don't' need to split the debug record here since the variable itself is fully contained in the zext to i32.

Applying the following patch "fixes" it, sort of. This omits the spurious creation of a DBG_VALUE for the (non-existent) upper 32 bits. But we still get a fragment description (!DIExpression(DW_OP_LLVM_fragment, 0, 32)) for the lower bits, which is unnecessary/redundant. Ideally no fragment would be generated because the variable itself is 32 bits.

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 7f57b6db40ef..087229ac31e9 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -11174,9 +11174,13 @@ void SelectionDAG::transferDbgValues(SDValue From, SDValue To,
       // When splitting a larger (e.g., sign-extended) value whose
       // lower bits are described with an SDDbgValue, do not attempt
       // to transfer the SDDbgValue to the upper bits.
-      if (auto FI = Expr->getFragmentInfo())
-        if (OffsetInBits + SizeInBits > FI->SizeInBits)
+      std::optional<uint64_t> VarSizeInBits = Var->getSizeInBits();
+      auto FragInfo = Expr->getFragmentInfo();
+      if (FragInfo || VarSizeInBits) {
+        uint64_t VarSize = FragInfo? FragInfo->SizeInBits : *VarSizeInBits;
+        if (OffsetInBits + SizeInBits > VarSize)
           continue;
+      }
       auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
                                                              SizeInBits);
       if (!Fragment)

I figured it would still be useful to post in case anyone else has time to fix it properly.

but I don't know if that is an issue, or if this should be handled in later steps.

Even though the code above is responsible, I can't help but feel we should filter out spurious fragments like this at DWARF emission time too...

OCHyams avatar Nov 19 '24 11:11 OCHyams

@scott-linder potentially relevant to your interests as something that more type information in expressions could solve.

jmorse avatar Jan 17 '25 16:01 jmorse