Adrian Vogelsgesang de3c8410d8
[debuginfo][coro] Emit debug info labels for coroutine resume points (#141937)
RFC on discourse:
https://discourse.llvm.org/t/rfc-debug-info-for-coroutine-suspension-locations-take-2/86606

With this commit, we add `DILabel` debug infos to the resume points of a
coroutine. Those labels can be used by debugging scripts to figure out
the exact line and column at which a coroutine was suspended by looking
up current `__coro_index` value inside the coroutines frame, and then
searching for the corresponding label inside the coroutine's resume
function.

The DWARF information generated for such a label looks like:

```
0x00000f71:     DW_TAG_label
                  DW_AT_name    ("__coro_resume_1")
                  DW_AT_decl_file       ("generator-example.cpp")
                  DW_AT_decl_line       (5)
                  DW_AT_decl_column     (3)
                  DW_AT_artificial      (true)
                  DW_AT_LLVM_coro_suspend_idx   (0x01)
                  DW_AT_low_pc  (0x00000000000019be)
```

The labels can be mapped to their corresponding `__coro_idx` values
either via their naming convention `__coro_resume_<N>` or using the new
`DW_AT_LLVM_coro_suspend_idx` attribute. In gdb, those line numebrs can
be looked up using `info line -function my_coroutine -label
__coro_resume_1`. LLDB unfortunately does not understand DW_TAG_label
debug information, yet.

Given this is an artificial compiler-generated label, I did apply the
DW_AT_artificial tag to it. The DWARFv5 standard only allows that tag on
type and variable definitions, but this is a natural extension and was
also blessed in the RFC on discourse.

Also, this commit adds `DW_AT_decl_column` to labels, not only for
coroutines but also for normal C and C++ labels. While not strictly
necessary, I am doing so now because it would be harder to do so later
without breaking the binary LLVM-IR format

Drive-by fixes: While reading the existing test cases to understand how
to write my own test case, I did a couple of small typo fixes and
comment improvements
2025-07-04 10:44:35 +02:00

79 lines
2.8 KiB
LLVM

; RUN: llc -O0 -filetype=obj -o - %s | llvm-dwarfdump -v - | FileCheck %s
;
; CHECK: .debug_info contents:
; CHECK: DW_TAG_label
; CHECK-NEXT: DW_AT_name {{.*}}"top"
; CHECK-NEXT: DW_AT_decl_file [DW_FORM_data1] {{.*}}debug-label.c
; CHECK-NEXT: DW_AT_decl_line [DW_FORM_data1] {{.*}}4
; CHECK-NEXT: DW_AT_decl_column [DW_FORM_data1] {{.*}}9
; CHECK-NEXT: DW_AT_low_pc [DW_FORM_addr] {{.*}}{{0x[0-9a-f]+}}
; CHECK: DW_TAG_label
; CHECK-NEXT: DW_AT_name {{.*}}"done"
; CHECK-NEXT: DW_AT_decl_file [DW_FORM_data1] {{.*}}debug-label.c
; CHECK-NEXT: DW_AT_decl_line [DW_FORM_data1] {{.*}}7
; CHECK-NEXT: DW_AT_low_pc [DW_FORM_addr] {{.*}}{{0x[0-9a-f]+}}
; CHECK-NOT: DW_AT_name {{.*}}"top"
;
; RUN: llc -O0 -o - %s | FileCheck %s -check-prefix=ASM
;
; ASM: [[TOP_LOW_PC:[.0-9a-zA-Z]+]]:{{[[:space:]].*}}DEBUG_LABEL: foo:top
; ASM: [[DONE_LOW_PC:[.0-9a-zA-Z]+]]:{{[[:space:]].*}}DEBUG_LABEL: foo:done
; ASM-LABEL: {{debug_info|dwinfo}}
; ASM: DW_TAG_label
; ASM-NEXT: DW_AT_name
; ASM: 1 {{.*}} DW_AT_decl_file
; ASM-NEXT: 4 {{.*}} DW_AT_decl_line
; ASM-NEXT: 9 {{.*}} DW_AT_decl_column
; ASM-NEXT: [[TOP_LOW_PC]]{{.*}} DW_AT_low_pc
; ASM: DW_TAG_label
; ASM-NEXT: DW_AT_name
; ASM: 1 {{.*}} DW_AT_decl_file
; ASM-NEXT: 7 {{.*}} DW_AT_decl_line
; ASM-NEXT: [[DONE_LOW_PC]]{{.*}} DW_AT_low_pc
source_filename = "debug-label.c"
define dso_local i32 @foo(i32 %a, i32 %b) !dbg !6 {
entry:
%a.addr = alloca i32, align 4
%b.addr = alloca i32, align 4
%sum = alloca i32, align 4
store i32 %a, ptr %a.addr, align 4
store i32 %b, ptr %b.addr, align 4
br label %top
top:
call void @llvm.dbg.label(metadata !10), !dbg !11
%0 = load i32, ptr %a.addr, align 4
%1 = load i32, ptr %b.addr, align 4
%add = add nsw i32 %0, %1
store i32 %add, ptr %sum, align 4
br label %done
done:
call void @llvm.dbg.label(metadata !12), !dbg !13
%2 = load i32, ptr %sum, align 4
ret i32 %2, !dbg !14
}
declare void @llvm.dbg.label(metadata)
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!4}
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "debug-label.c", directory: "./")
!2 = !{}
!3 = !{!10}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!6 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 2, isOptimized: false, unit: !0, retainedNodes: !3)
!7 = !DISubroutineType(types: !8)
!8 = !{!9, !9, !9}
!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!10 = !DILabel(scope: !6, name: "top", file: !1, line: 4, column: 9)
!11 = !DILocation(line: 4, column: 1, scope: !6)
!12 = !DILabel(scope: !15, name: "done", file: !1, line: 7)
!13 = !DILocation(line: 7, column: 1, scope: !6)
!14 = !DILocation(line: 8, column: 3, scope: !6)
!15 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !6)