Skip to content

DWARF DW_tag_variable dropped for PHINode in DILexicalBlock #118883

Closed as not planned
@topolarity

Description

@topolarity
define i32 @square(i32, i32) local_unnamed_addr #0 !dbg !4 {
    %3 = alloca i32, align 4
    store i32 %0, i32* %3, align 4
    call void @llvm.dbg.declare(metadata i32* %3, metadata !12, metadata !DIExpression()), !dbg !9
    call void @llvm.dbg.value(metadata i32 %1, metadata !13, metadata !DIExpression()), !dbg !9
    %4 = icmp eq i32 %1, 0
    call void @llvm.dbg.value(metadata i1 %4, metadata !10, metadata !DIExpression()), !dbg !9
    br i1 %4, label %arg1, label %arg2

arg1:
    %5 = load i32, ptr %3, align 4, !dbg !9
    %6 = mul nsw i32 %5, %5
    br label %ret

arg2:
    %7 = mul nsw i32 %1, %1
    br label %ret

ret:
    %8 = phi i32 [%6, %arg1], [%7, %arg2], !dbg !15
    tail call void @llvm.dbg.value(metadata i32 %8, metadata !7, metadata !DIExpression()), !dbg !15
    ret i32 %8
}

declare void @llvm.dbg.value(metadata, metadata, metadata) #1
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1

attributes #1 = { nofree nosync nounwind readnone speculatable willreturn }

!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!0, !1}

!0 = !{i32 7, !"Dwarf Version", i32 5}
!1 = !{i32 2, !"Debug Info Version", i32 3}

!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "test", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "square.ll", directory: "")
!4 = distinct !DISubprogram(name: "square", linkageName: "square", scope: !3, file: !3, line: 1, type: !5, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !16)
!5 = !DISubroutineType(types: !6)
!6 = !{!8, !8, !8}
!7 = !DILocalVariable(name: "result", scope: !14, file: !3, line: 14, type: !8)
!8 = !DIBasicType(name: "i32", size: 32, encoding: DW_ATE_unsigned)
!9 = !DILocation(line: 2, column: 1, scope: !4)
!10 = !DILocalVariable(name: "branch", scope: !4, file: !3, line: 14, type: !11)
!11 = !DIBasicType(name: "i1", size: 1, encoding: DW_ATE_unsigned)
!12 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !3, line: 1, type: !8)
!13 = !DILocalVariable(name: "y", arg: 2, scope: !4, file: !3, line: 1, type: !8)
!14 = distinct !DILexicalBlock(scope: !4, file: !3, line: 100, column: 5)
!15 = !DILocation(line: 100, column: 50, scope: !14)
!16 = !{}

Checking with:

$ llc test.ll -o a.o -O0 --filetype=obj
$ ./usr/tools/llvm-dwarfdump ./a.o | grep AT_name
              DW_AT_name        ("square.ll")
                DW_AT_name      ("square")
                  DW_AT_name    ("x")
                  DW_AT_name    ("y")
                DW_AT_name      ("i32")

The debug information for "result" is missing

It seems to be related to the fact that the PHINode is the only user of the DILexicalBlock. It works if you either:

  1. Use the DILexicalBlock in more locations:
@@ -19,5 +19,5 @@
 ret:
     %8 = phi i32 [%6, %arg1], [%7, %arg2], !dbg !9
     tail call void @llvm.dbg.value(metadata i32 %8, metadata !7, metadata !DIExpression()), !dbg !15
-    ret i32 %8
+    ret i32 %8, !dbg !15
 }
  1. Use the DISubprogram as the scope of result:
@@ -38,7 +38,7 @@
 !4 = distinct !DISubprogram(name: "square", linkageName: "square", scope: !3, file: !3, line: 1, type: !5, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !16)
 !5 = !DISubroutineType(types: !6)
 !6 = !{!8, !8, !8}
-!7 = !DILocalVariable(name: "result", scope: !14, file: !3, line: 14, type: !8)
+!7 = !DILocalVariable(name: "result", scope: !4, file: !3, line: 14, type: !8)
 !8 = !DIBasicType(name: "i32", size: 32, encoding: DW_ATE_unsigned)
 !9 = !DILocation(line: 2, column: 1, scope: !4)
 !10 = !DILocalVariable(name: "branch", scope: !4, file: !3, line: 14, type: !11)
@@ -46,5 +46,5 @@
 !12 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !3, line: 1, type: !8)
 !13 = !DILocalVariable(name: "y", arg: 2, scope: !4, file: !3, line: 1, type: !8)
 !14 = distinct !DILexicalBlock(scope: !4, file: !3, line: 100, column: 5)
-!15 = !DILocation(line: 100, column: 50, scope: !14)
+!15 = !DILocation(line: 100, column: 50, scope: !4)
 !16 = !{}

Identified on LLVM 18.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    debuginfoquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions