Skip to content

Commit cbf1f3b

Browse files
committed
[Debuginfo][SROA] Need to handle dbg.value in SROA pass.
SROA pass processes debug info incorrecly if applied twice. Specifically, after SROA works first time, instcombine converts dbg.declare intrinsics into dbg.value. Inlining creates new opportunities for SROA, so it is called again. This time it does not handle correctly previously inserted dbg.value intrinsics. Differential Revision: https://reviews.llvm.org/D64595 llvm-svn: 370906
1 parent c86d47b commit cbf1f3b

File tree

4 files changed

+232
-156
lines changed

4 files changed

+232
-156
lines changed

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,12 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
13781378
/// Determine whether this alloca is either a VLA or an array.
13791379
static bool isArray(AllocaInst *AI) {
13801380
return AI->isArrayAllocation() ||
1381-
AI->getType()->getElementType()->isArrayTy();
1381+
(AI->getAllocatedType() && AI->getAllocatedType()->isArrayTy());
1382+
}
1383+
1384+
/// Determine whether this alloca is a structure.
1385+
static bool isStructure(AllocaInst *AI) {
1386+
return AI->getAllocatedType() && AI->getAllocatedType()->isStructTy();
13821387
}
13831388

13841389
/// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
@@ -1403,7 +1408,7 @@ bool llvm::LowerDbgDeclare(Function &F) {
14031408
// stored on the stack, while the dbg.declare can only describe
14041409
// the stack slot (and at a lexical-scope granularity). Later
14051410
// passes will attempt to elide the stack slot.
1406-
if (!AI || isArray(AI))
1411+
if (!AI || isArray(AI) || isStructure(AI))
14071412
continue;
14081413

14091414
// A volatile load/store means that the alloca can't be elided anyway.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
; RUN: opt %s -sroa -instcombine -inline -instcombine -sroa -verify -S -o - | FileCheck %s
2+
;
3+
; This test checks that SROA pass processes debug info correctly if applied twice.
4+
; Specifically, after SROA works first time, instcombine converts dbg.declare
5+
; intrinsics into dbg.value. Inlining creates new opportunities for SROA,
6+
; so it is called again. This time it does not handle correctly previously
7+
; inserted dbg.value intrinsics: current SROA implementation while doing
8+
; "Migrate debug information from the old alloca to the new alloca(s)" handles
9+
; only dbg.declare intrinsic. In this case, original dbg.declare was lowered by
10+
; instcombine pass into dbg.value. When it comes into SROA second time, all dbg.value
11+
; intrinsics, inserted by instcombine pass before second SROA, just not updated
12+
; (though SROA was done). The fix is to not lower dbg.declare for structures.
13+
14+
;
15+
; Hand-reduced from this example (-g -O -mllvm -disable-llvm-optzns -gno-column-info):
16+
;
17+
; struct S1 {
18+
; int p1;
19+
;
20+
; bool IsNull ( ) { return p1 == 0; }
21+
; };
22+
;
23+
; S1 foo ( void );
24+
;
25+
; int bar ( ) {
26+
; S1 result = foo();
27+
;
28+
; if (result.IsNull())
29+
; return 0;
30+
;
31+
; return result.p1 + 1;
32+
; }
33+
34+
; CHECK: _Z3barv
35+
; CHECK: %[[RESULT:.*]] = call i32 @_Z3foov
36+
; CHECK: llvm.dbg.value(metadata i32 %[[RESULT]], metadata [[METADATA_IDX1:![0-9]+]]
37+
; CHECK: ret
38+
; CHECK: DICompileUnit
39+
; CHECK: [[METADATA_IDX1]] = !DILocalVariable(name: "result"
40+
41+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
42+
target triple = "x86_64-unknown-linux-gnu"
43+
44+
%struct.S1 = type { i32 }
45+
46+
$_ZN2S16IsNullEv = comdat any
47+
48+
define dso_local i32 @_Z3barv() !dbg !7 {
49+
entry:
50+
%retval = alloca i32, align 4
51+
%result = alloca %struct.S1, align 4
52+
%cleanup.dest.slot = alloca i32, align 4
53+
%0 = bitcast %struct.S1* %result to i8*, !dbg !21
54+
call void @llvm.lifetime.start.p0i8(i64 4, i8* %0) #5, !dbg !21
55+
call void @llvm.dbg.declare(metadata %struct.S1* %result, metadata !12, metadata !DIExpression()), !dbg !21
56+
%call = call i32 @_Z3foov(), !dbg !21
57+
%coerce.dive = getelementptr inbounds %struct.S1, %struct.S1* %result, i32 0, i32 0, !dbg !21
58+
store i32 %call, i32* %coerce.dive, align 4, !dbg !21
59+
%call1 = call zeroext i1 @_ZN2S16IsNullEv(%struct.S1* %result), !dbg !22
60+
br i1 %call1, label %if.then, label %if.end, !dbg !24
61+
62+
if.then: ; preds = %entry
63+
store i32 0, i32* %retval, align 4, !dbg !25
64+
store i32 1, i32* %cleanup.dest.slot, align 4
65+
br label %cleanup, !dbg !25
66+
67+
if.end: ; preds = %entry
68+
%p1 = getelementptr inbounds %struct.S1, %struct.S1* %result, i32 0, i32 0, !dbg !26
69+
%1 = load i32, i32* %p1, align 4, !dbg !26
70+
%add = add nsw i32 %1, 1, !dbg !26
71+
store i32 %add, i32* %retval, align 4, !dbg !26
72+
store i32 1, i32* %cleanup.dest.slot, align 4
73+
br label %cleanup, !dbg !26
74+
75+
cleanup: ; preds = %if.end, %if.then
76+
%2 = bitcast %struct.S1* %result to i8*, !dbg !32
77+
call void @llvm.lifetime.end.p0i8(i64 4, i8* %2) #5, !dbg !32
78+
%3 = load i32, i32* %retval, align 4, !dbg !32
79+
ret i32 %3, !dbg !32
80+
}
81+
82+
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)
83+
84+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
85+
86+
declare dso_local i32 @_Z3foov()
87+
88+
define linkonce_odr dso_local zeroext i1 @_ZN2S16IsNullEv(%struct.S1* %this) #4 comdat align 2 !dbg !33 {
89+
entry:
90+
%this.addr = alloca %struct.S1*, align 8
91+
store %struct.S1* %this, %struct.S1** %this.addr, align 8
92+
call void @llvm.dbg.declare(metadata %struct.S1** %this.addr, metadata !35, metadata !DIExpression()), !dbg !39
93+
%this1 = load %struct.S1*, %struct.S1** %this.addr, align 8
94+
%p1 = getelementptr inbounds %struct.S1, %struct.S1* %this1, i32 0, i32 0, !dbg !40
95+
%0 = load i32, i32* %p1, align 4, !dbg !40
96+
%cmp = icmp eq i32 %0, 0, !dbg !40
97+
ret i1 %cmp, !dbg !40
98+
}
99+
100+
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)
101+
102+
!llvm.dbg.cu = !{!0}
103+
!llvm.module.flags = !{!3, !4, !5}
104+
!llvm.ident = !{!6}
105+
106+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
107+
!1 = !DIFile(filename: "sroa-after-inlining.cpp", directory: "")
108+
!2 = !{}
109+
!3 = !{i32 2, !"Dwarf Version", i32 4}
110+
!4 = !{i32 2, !"Debug Info Version", i32 3}
111+
!5 = !{i32 1, !"wchar_size", i32 4}
112+
!6 = !{!"clang"}
113+
!7 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !1, file: !1, line: 9, type: !8, scopeLine: 9, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
114+
!8 = !DISubroutineType(types: !9)
115+
!9 = !{!10}
116+
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
117+
!11 = !{!12}
118+
!12 = !DILocalVariable(name: "result", scope: !7, file: !1, line: 10, type: !13)
119+
!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S1", file: !1, line: 1, size: 32, flags: DIFlagTypePassByValue, elements: !14, identifier: "_ZTS2S1")
120+
!14 = !{!15, !16}
121+
!15 = !DIDerivedType(tag: DW_TAG_member, name: "p1", scope: !13, file: !1, line: 2, baseType: !10, size: 32)
122+
!16 = !DISubprogram(name: "IsNull", linkageName: "_ZN2S16IsNullEv", scope: !13, file: !1, line: 4, type: !17, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)
123+
!17 = !DISubroutineType(types: !18)
124+
!18 = !{!19, !20}
125+
!19 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean)
126+
!20 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
127+
!21 = !DILocation(line: 10, scope: !7)
128+
!22 = !DILocation(line: 12, scope: !23)
129+
!23 = distinct !DILexicalBlock(scope: !7, file: !1, line: 12)
130+
!24 = !DILocation(line: 12, scope: !7)
131+
!25 = !DILocation(line: 13, scope: !23)
132+
!26 = !DILocation(line: 15, scope: !7)
133+
!32 = !DILocation(line: 16, scope: !7)
134+
!33 = distinct !DISubprogram(name: "IsNull", linkageName: "_ZN2S16IsNullEv", scope: !13, file: !1, line: 4, type: !17, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, declaration: !16, retainedNodes: !34)
135+
!34 = !{!35}
136+
!35 = !DILocalVariable(name: "this", arg: 1, scope: !33, type: !36, flags: DIFlagArtificial | DIFlagObjectPointer)
137+
!36 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64)
138+
!39 = !DILocation(line: 0, scope: !33)
139+
!40 = !DILocation(line: 4, scope: !33)

0 commit comments

Comments
 (0)