Skip to content

Commit

Permalink
Reapply [Assignment Tracking][13/*] Account for assignment tracking i…
Browse files Browse the repository at this point in the history
…n SROA

The Assignment Tracking debug-info feature is outlined in this RFC:

https://discourse.llvm.org/t/
rfc-assignment-tracking-a-better-way-of-specifying-variable-locations-in-ir

Split dbg.assign intrinsics into fragments similarly to what SROA already does
for dbg.declares, except that there's many more intrinsics to split. The
function migrateDebugInfo generates new dbg.assigns intrinsic for each part of
a split store.

Reviewed By: jmorse

Differential Revision: https://reviews.llvm.org/D133296
  • Loading branch information
OCHyams committed Dec 13, 2022
1 parent 54e72dd commit f354716
Show file tree
Hide file tree
Showing 15 changed files with 2,203 additions and 15 deletions.
217 changes: 202 additions & 15 deletions llvm/lib/Transforms/Scalar/SROA.cpp

Large diffs are not rendered by default.

144 changes: 144 additions & 0 deletions llvm/test/DebugInfo/Generic/assignment-tracking/sroa/after-inlining.ll
@@ -0,0 +1,144 @@
; RUN: opt %s -S -passes=sroa -o - -experimental-assignment-tracking | FileCheck %s

;; Check that SROA preserves the InlinedAt status of new dbg.assign intriniscs
;; it inserts.

;; $cat test.c
;; typedef struct {
;; int a;
;; int b[];
;; } c;
;; int d, e, f;
;; void g(c *h) {
;; if (d)
;; h->a = 1;
;; }
;; void i(c *h) {
;; long j = f = 0;
;; for (; f < h->a; f++)
;; j += h->b[f];
;; e = j;
;; }
;; void k() {
;; c j;
;; g(&j);
;; i(&j);
;; }
;; void l() { k(); }
;;
;; $ clang test.c -Xclang -fexperimental-assignment-tracking -O2 -g

; CHECK: call void @llvm.dbg.assign(metadata i1 undef, metadata !{{.+}}, metadata !DIExpression(), metadata !{{.+}}, metadata ptr undef, metadata !DIExpression()), !dbg ![[DBG:[0-9]+]]

; CHECK-DAG: ![[DBG]] = !DILocation(line: 0, scope: ![[INL_SC:[0-9]+]], inlinedAt: ![[IA:[0-9]+]])
; CHECK-DAG: ![[IA]] = distinct !DILocation(line: 21, column: 12, scope: ![[SC:[0-9]+]])
; CHECK-DAG: ![[SC]] = distinct !DISubprogram(name: "l",
; CHECK-DAG: ![[INL_SC]] = distinct !DISubprogram(name: "k"

%struct.c = type { i32, [0 x i32] }

@f = dso_local local_unnamed_addr global i32 0, align 4, !dbg !9
@e = dso_local local_unnamed_addr global i32 0, align 4, !dbg !6

declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata) #1
declare void @llvm.lifetime.start.p0i8(i64 immarg, ptr nocapture) #2
declare void @llvm.lifetime.end.p0i8(i64 immarg, ptr nocapture) #2

define dso_local void @l() local_unnamed_addr #4 !dbg !73 {
entry:
%j.i = alloca %struct.c, align 4, !DIAssignID !74
call void @llvm.dbg.assign(metadata i1 undef, metadata !64, metadata !DIExpression(), metadata !74, metadata ptr %j.i, metadata !DIExpression()) #5, !dbg !75
%0 = bitcast ptr %j.i to ptr, !dbg !77
call void @llvm.lifetime.start.p0i8(i64 4, ptr nonnull %0) #5, !dbg !77
%arrayidx.i.i = getelementptr inbounds %struct.c, ptr %j.i, i64 0, i32 1, i64 0, !dbg !78
%1 = load i32, ptr %arrayidx.i.i, align 4, !dbg !78
store i32 1, ptr @f, align 4, !dbg !80
store i32 %1, ptr @e, align 4, !dbg !81
call void @llvm.lifetime.end.p0i8(i64 4, ptr nonnull %0) #5, !dbg !82
ret void, !dbg !83
}

!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!11, !12, !13}
!llvm.ident = !{!14}

!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "d", scope: !2, file: !3, line: 5, type: !8, isLocal: false, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 12.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "test.c", directory: "/")
!4 = !{}
!5 = !{!0, !6, !9}
!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression())
!7 = distinct !DIGlobalVariable(name: "e", scope: !2, file: !3, line: 5, type: !8, isLocal: false, isDefinition: true)
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!9 = !DIGlobalVariableExpression(var: !10, expr: !DIExpression())
!10 = distinct !DIGlobalVariable(name: "f", scope: !2, file: !3, line: 5, type: !8, isLocal: false, isDefinition: true)
!11 = !{i32 7, !"Dwarf Version", i32 4}
!12 = !{i32 2, !"Debug Info Version", i32 3}
!13 = !{i32 1, !"wchar_size", i32 4}
!14 = !{!"clang version 12.0.0)"}
!15 = distinct !DISubprogram(name: "g", scope: !3, file: !3, line: 6, type: !16, scopeLine: 6, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !27)
!16 = !DISubroutineType(types: !17)
!17 = !{null, !18}
!18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !19, size: 64)
!19 = !DIDerivedType(tag: DW_TAG_typedef, name: "c", file: !3, line: 4, baseType: !20)
!20 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !3, line: 1, size: 32, elements: !21)
!21 = !{!22, !23}
!22 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !20, file: !3, line: 2, baseType: !8, size: 32)
!23 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !20, file: !3, line: 3, baseType: !24, offset: 32)
!24 = !DICompositeType(tag: DW_TAG_array_type, baseType: !8, elements: !25)
!25 = !{!26}
!26 = !DISubrange(count: -1)
!27 = !{!28}
!28 = !DILocalVariable(name: "h", arg: 1, scope: !15, file: !3, line: 6, type: !18)
!29 = !DILocation(line: 7, column: 7, scope: !30)
!30 = distinct !DILexicalBlock(scope: !15, file: !3, line: 7, column: 7)
!35 = !DILocation(line: 7, column: 7, scope: !15)
!36 = !DILocation(line: 8, column: 8, scope: !30)
!37 = !DILocation(line: 8, column: 10, scope: !30)
!38 = !DILocation(line: 8, column: 5, scope: !30)
!39 = !DILocation(line: 9, column: 1, scope: !15)
!40 = distinct !DISubprogram(name: "i", scope: !3, file: !3, line: 10, type: !16, scopeLine: 10, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !41)
!41 = !{!42, !43}
!42 = !DILocalVariable(name: "h", arg: 1, scope: !40, file: !3, line: 10, type: !18)
!43 = !DILocalVariable(name: "j", scope: !40, file: !3, line: 11, type: !44)
!44 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed)
!45 = !DILocation(line: 0, scope: !40)
!46 = !DILocation(line: 12, column: 17, scope: !47)
!47 = distinct !DILexicalBlock(scope: !48, file: !3, line: 12, column: 3)
!48 = distinct !DILexicalBlock(scope: !40, file: !3, line: 12, column: 3)
!49 = !DILocation(line: 12, column: 12, scope: !47)
!50 = !DILocation(line: 12, column: 3, scope: !48)
!51 = !DILocation(line: 13, column: 10, scope: !47)
!52 = !DILocation(line: 13, column: 7, scope: !47)
!53 = !DILocation(line: 12, column: 21, scope: !47)
!54 = distinct !{!54, !50, !55, !56}
!55 = !DILocation(line: 13, column: 16, scope: !48)
!56 = !{!"llvm.loop.mustprogress"}
!57 = !DILocation(line: 14, column: 7, scope: !40)
!58 = !DILocation(line: 14, column: 5, scope: !40)
!59 = !DILocation(line: 15, column: 1, scope: !40)
!60 = distinct !DISubprogram(name: "k", scope: !3, file: !3, line: 16, type: !61, scopeLine: 16, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !63)
!61 = !DISubroutineType(types: !62)
!62 = !{null}
!63 = !{!64}
!64 = !DILocalVariable(name: "j", scope: !60, file: !3, line: 17, type: !19)
!65 = distinct !DIAssignID()
!66 = !DILocation(line: 0, scope: !60)
!67 = !DILocation(line: 17, column: 3, scope: !60)
!68 = !DILocation(line: 13, column: 10, scope: !47, inlinedAt: !69)
!69 = distinct !DILocation(line: 19, column: 3, scope: !60)
!70 = !DILocation(line: 0, scope: !40, inlinedAt: !69)
!71 = !DILocation(line: 14, column: 5, scope: !40, inlinedAt: !69)
!72 = !DILocation(line: 20, column: 1, scope: !60)
!73 = distinct !DISubprogram(name: "l", scope: !3, file: !3, line: 21, type: !61, scopeLine: 21, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !4)
!74 = distinct !DIAssignID()
!75 = !DILocation(line: 0, scope: !60, inlinedAt: !76)
!76 = distinct !DILocation(line: 21, column: 12, scope: !73)
!77 = !DILocation(line: 17, column: 3, scope: !60, inlinedAt: !76)
!78 = !DILocation(line: 13, column: 10, scope: !47, inlinedAt: !79)
!79 = distinct !DILocation(line: 19, column: 3, scope: !60, inlinedAt: !76)
!80 = !DILocation(line: 0, scope: !40, inlinedAt: !79)
!81 = !DILocation(line: 14, column: 5, scope: !40, inlinedAt: !79)
!82 = !DILocation(line: 20, column: 1, scope: !60, inlinedAt: !76)
!83 = !DILocation(line: 21, column: 17, scope: !73)
@@ -0,0 +1,82 @@
; RUN: opt -passes=sroa,verify -S %s -o - -experimental-assignment-tracking \
; RUN: | FileCheck %s --implicit-check-not="call void @llvm.dbg"

; Check that single sliced allocas retain their assignment tracking debug info.

;; $ cat test.c
;; struct a {
;; char b[8];
;; };
;; int c;
;; void d() {
;; struct a a;
;; memcpy(a.b, 0, c);
;; }
;; $ clang test.c -Xclang -disable-llvm-passes -O2 -g -c -S -emit-llvm -o - \
;; | opt -passes=declare-to-assign -S -o -

; CHECK: entry:
; CHECK-NEXT: %a.sroa.0 = alloca i64, align 8, !DIAssignID ![[ID_1:[0-9]+]]
; CHECK-NEXT: call void @llvm.dbg.assign(metadata i1 undef, metadata ![[VAR:[0-9]+]], metadata !DIExpression(), metadata ![[ID_1]], metadata ptr %a.sroa.0, metadata !DIExpression()), !dbg

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

%struct.a = type { [8 x i8] }

@c = dso_local global i32 0, align 4, !dbg !0

define dso_local void @d() !dbg !11 {
entry:
%a = alloca %struct.a, align 1, !DIAssignID !23
call void @llvm.dbg.assign(metadata i1 undef, metadata !15, metadata !DIExpression(), metadata !23, metadata ptr %a, metadata !DIExpression()), !dbg !24
%0 = bitcast ptr %a to ptr, !dbg !25
call void @llvm.lifetime.start.p0i8(i64 8, ptr %0), !dbg !25
%b = getelementptr inbounds %struct.a, ptr %a, i32 0, i32 0, !dbg !26
%arraydecay = getelementptr inbounds [8 x i8], ptr %b, i64 0, i64 0, !dbg !27
%1 = load i32, ptr @c, align 4, !dbg !28
%conv = sext i32 %1 to i64, !dbg !28
call void @llvm.memcpy.p0i8.p0i8.i64(ptr align 1 %arraydecay, ptr align 1 null, i64 %conv, i1 false), !dbg !27
%2 = bitcast ptr %a to ptr, !dbg !33
call void @llvm.lifetime.end.p0i8(i64 8, ptr %2), !dbg !33
ret void, !dbg !33
}

declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)
declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg)
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)

!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!7, !8, !9}
!llvm.ident = !{!10}

!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "c", scope: !2, file: !3, line: 4, type: !6, isLocal: false, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 12.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "test.c", directory: "/")
!4 = !{}
!5 = !{!0}
!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!7 = !{i32 7, !"Dwarf Version", i32 4}
!8 = !{i32 2, !"Debug Info Version", i32 3}
!9 = !{i32 1, !"wchar_size", i32 4}
!10 = !{!"clang version 12.0.0"}
!11 = distinct !DISubprogram(name: "d", scope: !3, file: !3, line: 5, type: !12, scopeLine: 5, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !14)
!12 = !DISubroutineType(types: !13)
!13 = !{null}
!14 = !{!15}
!15 = !DILocalVariable(name: "a", scope: !11, file: !3, line: 6, type: !16)
!16 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "a", file: !3, line: 1, size: 64, elements: !17)
!17 = !{!18}
!18 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !16, file: !3, line: 2, baseType: !19, size: 64)
!19 = !DICompositeType(tag: DW_TAG_array_type, baseType: !20, size: 64, elements: !21)
!20 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!21 = !{!22}
!22 = !DISubrange(count: 8)
!23 = distinct !DIAssignID()
!24 = !DILocation(line: 0, scope: !11)
!25 = !DILocation(line: 6, column: 3, scope: !11)
!26 = !DILocation(line: 7, column: 12, scope: !11)
!27 = !DILocation(line: 7, column: 3, scope: !11)
!28 = !DILocation(line: 7, column: 18, scope: !11)
!33 = !DILocation(line: 8, column: 1, scope: !11)
69 changes: 69 additions & 0 deletions llvm/test/DebugInfo/Generic/assignment-tracking/sroa/complex.ll
@@ -0,0 +1,69 @@
; RUN: opt -passes=sroa -S -o - %s -experimental-assignment-tracking | FileCheck %s
;
;; Based on llvm/test/DebugInfo/ARM/sroa-complex.ll
;; generated from:
;; $ cat test.c
;; void f(_Complex double c) { c = 0; }
;; $ clang test.c -g -O2 -c -Xclang -disable-llvm-passes -S \
;; -emit-llvm -o - --target="thumbv7-apple-unknown"
;;
;; Commented out some parts of the function that are not relevant to the test.
;;
;; Check that a split store gets dbg.assigns fragments. Ensure that only the
;; value-expression gets fragment info; that the address-expression remains
;; untouched.

;; dbg.assigns for the split (then promoted) stores.
; CHECK: %c.coerce.fca.0.extract = extractvalue [2 x i64] %c.coerce, 0
; CHECK: %c.coerce.fca.1.extract = extractvalue [2 x i64] %c.coerce, 1
; CHECK: call void @llvm.dbg.assign(metadata i64 %c.coerce.fca.0.extract,{{.+}}, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 64),{{.+}}, metadata ptr undef, metadata !DIExpression())
; CHECK: call void @llvm.dbg.assign(metadata i64 %c.coerce.fca.1.extract,{{.+}}, metadata !DIExpression(DW_OP_LLVM_fragment, 64, 64),{{.+}}, metadata ptr undef, {{.+}})

target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "armv7-apple-unknown"

define dso_local arm_aapcscc void @f([2 x i64] %c.coerce) #0 !dbg !8 {
entry:
%c = alloca { double, double }, align 8, !DIAssignID !14
call void @llvm.dbg.assign(metadata i1 undef, metadata !13, metadata !DIExpression(), metadata !14, metadata ptr %c, metadata !DIExpression()), !dbg !15
%0 = bitcast ptr %c to [2 x i64]*
store [2 x i64] %c.coerce, [2 x i64]* %0, align 8, !DIAssignID !16
call void @llvm.dbg.assign(metadata [2 x i64] %c.coerce, metadata !13, metadata !DIExpression(), metadata !16, metadata [2 x i64]* %0, metadata !DIExpression()), !dbg !15
; --- The rest of this function isn't useful for the test ---
;%c.realp = getelementptr inbounds { double, double }, ptr %c, i32 0, i32 0, !dbg !17
;%c.imagp = getelementptr inbounds { double, double }, ptr %c, i32 0, i32 1, !dbg !17
;store double 0.000000e+00, ptr %c.realp, align 8, !dbg !17, !DIAssignID !18
;call void @llvm.dbg.assign(metadata double 0.000000e+00, metadata !13, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 64), metadata !18, metadata ptr %c.realp, metadata !DIExpression()), !dbg !15
;store double 0.000000e+00, ptr %c.imagp, align 8, !dbg !17, !DIAssignID !19
;call void @llvm.dbg.assign(metadata double 0.000000e+00, metadata !13, metadata !DIExpression(DW_OP_LLVM_fragment, 64, 64), metadata !19, metadata ptr %c.imagp, metadata !DIExpression()), !dbg !15
ret void, !dbg !20
}

declare void @llvm.dbg.declare(metadata, metadata, metadata)
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4, !5, !6}
!llvm.ident = !{!7}

!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "test.c", directory: "/")
!2 = !{}
!3 = !{i32 7, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = !{i32 1, !"wchar_size", i32 4}
!6 = !{i32 1, !"min_enum_size", i32 4}
!7 = !{!"clang version 12.0.0"}
!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 2, type: !9, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
!9 = !DISubroutineType(types: !10)
!10 = !{null, !11}
!11 = !DIBasicType(name: "complex", size: 128, encoding: DW_ATE_complex_float)
!12 = !{!13}
!13 = !DILocalVariable(name: "c", arg: 1, scope: !8, file: !1, line: 2, type: !11)
!14 = distinct !DIAssignID()
!15 = !DILocation(line: 0, scope: !8)
!16 = distinct !DIAssignID()
!17 = !DILocation(line: 2, column: 31, scope: !8)
!18 = distinct !DIAssignID()
!19 = distinct !DIAssignID()
!20 = !DILocation(line: 2, column: 36, scope: !8)

0 comments on commit f354716

Please sign in to comment.