Skip to content

Commit 1f72a03

Browse files
committed
Fix bug in getCompleteTypeIndex in codeview debug info
Summary: When there are multiple instances of a forward decl record type, only the first one is emitted with a type index, because the type is added to a map with a null type index. Avoid this by reordering so that forward decl types aren't added to the map. Reviewers: rnk Subscribers: aprantl, hiraditya, arphaman, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61460 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360101 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 69b042c commit 1f72a03

File tree

2 files changed

+78
-57
lines changed

2 files changed

+78
-57
lines changed

lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,11 +2497,7 @@ TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) {
24972497
return getTypeIndex(Ty);
24982498
}
24992499

2500-
// Check if we've already translated the complete record type.
25012500
const auto *CTy = cast<DICompositeType>(Ty);
2502-
auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
2503-
if (!InsertResult.second)
2504-
return InsertResult.first->second;
25052501

25062502
TypeLoweringScope S(*this);
25072503

@@ -2519,6 +2515,13 @@ TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) {
25192515
return FwdDeclTI;
25202516
}
25212517

2518+
// Check if we've already translated the complete record type.
2519+
// Insert the type with a null TypeIndex to signify that the type is currently
2520+
// being lowered.
2521+
auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
2522+
if (!InsertResult.second)
2523+
return InsertResult.first->second;
2524+
25222525
TypeIndex TI;
25232526
switch (CTy->getTag()) {
25242527
case dwarf::DW_TAG_class_type:

test/CodeGen/X86/label-heapallocsite.ll

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,77 @@
22
; FIXME: Add test for llc with optimizations once it is implemented.
33

44
; Source to regenerate:
5-
; $ clang --target=x86_64-windows-msvc -S heapallocsite.c -g -gcodeview -o t.ll \
5+
; $ clang --target=x86_64-windows-msvc -S heapallocsite.cpp -g -gcodeview -o t.ll \
66
; -emit-llvm -O0 -Xclang -disable-llvm-passes -fms-extensions
7-
; __declspec(allocator) char *myalloc(void);
8-
; void f() {
9-
; myalloc()
10-
; }
117
;
128
; struct Foo {
139
; __declspec(allocator) virtual void *alloc();
1410
; };
15-
; void use_alloc(void*);
16-
; void do_alloc(Foo *p) {
11+
;
12+
; extern "C" __declspec(allocator) Foo *alloc_foo();
13+
;
14+
; extern "C" void use_alloc(void*);
15+
; extern "C" void call_virtual(Foo *p) {
1716
; use_alloc(p->alloc());
1817
; }
18+
;
19+
; extern "C" void call_multiple() {
20+
; use_alloc(alloc_foo());
21+
; use_alloc(alloc_foo());
22+
; }
1923

2024
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
2125
target triple = "x86_64-unknown-windows-msvc"
2226

2327
%struct.Foo = type { i32 (...)** }
2428

2529
; Function Attrs: noinline optnone uwtable
26-
define dso_local void @f() #0 !dbg !8 {
30+
define dso_local void @call_virtual(%struct.Foo* %p) #0 !dbg !8 {
2731
entry:
28-
%call = call i8* @myalloc(), !dbg !11, !heapallocsite !2
29-
ret void, !dbg !12
32+
%p.addr = alloca %struct.Foo*, align 8
33+
store %struct.Foo* %p, %struct.Foo** %p.addr, align 8
34+
call void @llvm.dbg.declare(metadata %struct.Foo** %p.addr, metadata !13, metadata !DIExpression()), !dbg !14
35+
%0 = load %struct.Foo*, %struct.Foo** %p.addr, align 8, !dbg !15
36+
%1 = bitcast %struct.Foo* %0 to i8* (%struct.Foo*)***, !dbg !15
37+
%vtable = load i8* (%struct.Foo*)**, i8* (%struct.Foo*)*** %1, align 8, !dbg !15
38+
%vfn = getelementptr inbounds i8* (%struct.Foo*)*, i8* (%struct.Foo*)** %vtable, i64 0, !dbg !15
39+
%2 = load i8* (%struct.Foo*)*, i8* (%struct.Foo*)** %vfn, align 8, !dbg !15
40+
%call = call i8* %2(%struct.Foo* %0), !dbg !15, !heapallocsite !2
41+
call void @use_alloc(i8* %call), !dbg !15
42+
ret void, !dbg !16
3043
}
3144

32-
; CHECK-LABEL: f: # @f
33-
; CHECK: .Lheapallocsite0:
34-
; CHECK: callq myalloc
35-
; CHECK: .Lheapallocsite1:
36-
; CHECK: retq
45+
; Function Attrs: nounwind readnone speculatable
46+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
3747

38-
declare dso_local i8* @myalloc() #1
48+
declare dso_local void @use_alloc(i8*) #2
3949

4050
; Function Attrs: noinline optnone uwtable
41-
define dso_local void @do_alloc(%struct.Foo* %p) #0 !dbg !13 {
51+
define dso_local void @call_multiple() #0 !dbg !17 {
4252
entry:
43-
%p.addr = alloca %struct.Foo*, align 8
44-
store %struct.Foo* %p, %struct.Foo** %p.addr, align 8
45-
call void @llvm.dbg.declare(metadata %struct.Foo** %p.addr, metadata !18, metadata !DIExpression()), !dbg !19
46-
%0 = load %struct.Foo*, %struct.Foo** %p.addr, align 8, !dbg !20
47-
%1 = bitcast %struct.Foo* %0 to i8* (%struct.Foo*)***, !dbg !20
48-
%vtable = load i8* (%struct.Foo*)**, i8* (%struct.Foo*)*** %1, align 8, !dbg !20
49-
%vfn = getelementptr inbounds i8* (%struct.Foo*)*, i8* (%struct.Foo*)** %vtable, i64 0, !dbg !20
50-
%2 = load i8* (%struct.Foo*)*, i8* (%struct.Foo*)** %vfn, align 8, !dbg !20
51-
%call = call i8* %2(%struct.Foo* %0), !dbg !20, !heapallocsite !2
52-
call void @use_alloc(i8* %call), !dbg !20
53-
ret void, !dbg !21
53+
%call = call %struct.Foo* @alloc_foo(), !dbg !20, !heapallocsite !12
54+
%0 = bitcast %struct.Foo* %call to i8*, !dbg !20
55+
call void @use_alloc(i8* %0), !dbg !20
56+
%call1 = call %struct.Foo* @alloc_foo(), !dbg !21, !heapallocsite !12
57+
%1 = bitcast %struct.Foo* %call1 to i8*, !dbg !21
58+
call void @use_alloc(i8* %1), !dbg !21
59+
ret void, !dbg !22
5460
}
5561

56-
; CHECK-LABEL: do_alloc: # @do_alloc
57-
; CHECK: .Lheapallocsite2:
62+
declare dso_local %struct.Foo* @alloc_foo() #2
63+
64+
; CHECK-LABEL: call_virtual: # @call_virtual
65+
; CHECK: .Lheapallocsite0:
5866
; CHECK: callq *(%rax)
67+
; CHECK: .Lheapallocsite1:
68+
; CHECK: retq
69+
70+
; CHECK-LABEL: call_multiple: # @call_multiple
71+
; CHECK: .Lheapallocsite4:
72+
; CHECK: callq alloc_foo
73+
; CHECK: .Lheapallocsite5:
74+
; CHECK: .Lheapallocsite2:
75+
; CHECK: callq alloc_foo
5976
; CHECK: .Lheapallocsite3:
6077
; CHECK: retq
6178

@@ -73,39 +90,40 @@ entry:
7390
; CHECK-NEXT: .secrel32 .Lheapallocsite2
7491
; CHECK-NEXT: .secidx .Lheapallocsite2
7592
; CHECK-NEXT: .short .Lheapallocsite3-.Lheapallocsite2
76-
; CHECK-NEXT: .long 3
93+
; CHECK-NEXT: .long 4096
7794
; CHECK-NEXT: .p2align 2
78-
; CHECK-LABEL: .short 4431 # Record kind: S_PROC_ID_END
79-
80-
; Function Attrs: nounwind readnone speculatable
81-
declare void @llvm.dbg.declare(metadata, metadata, metadata) #2
8295

83-
declare dso_local void @use_alloc(i8*) #1
96+
; CHECK: .short 4446 # Record kind: S_HEAPALLOCSITE
97+
; CHECK-NEXT: .secrel32 .Lheapallocsite4
98+
; CHECK-NEXT: .secidx .Lheapallocsite4
99+
; CHECK-NEXT: .short .Lheapallocsite5-.Lheapallocsite4
100+
; CHECK-NEXT: .long 4096
101+
; CHECK-NEXT: .p2align 2
102+
; CHECK-LABEL: .short 4431 # Record kind: S_PROC_ID_END
84103

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

89-
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 9.0.0 (https://github.com/llvm/llvm-project.git 4eff3de99423a62fd6e833e29c71c1e62ba6140b)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
90-
!1 = !DIFile(filename: "heapallocsite.cpp", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "6d758cfa3834154a04ce8a55102772a9")
108+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 9.0.0 (https://github.com/llvm/llvm-project.git 9c8073f44f786fbf47335e53f20abe64429e8e47)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)!1 = !DIFile(filename: "filename", directory: "directory", checksumkind: CSK_MD5, checksum: "096443b661a0af36da9006330c08f97e")
91109
!2 = !{}
92110
!3 = !{i32 2, !"CodeView", i32 1}
93111
!4 = !{i32 2, !"Debug Info Version", i32 3}
94112
!5 = !{i32 1, !"wchar_size", i32 2}
95113
!6 = !{i32 7, !"PIC Level", i32 2}
96-
!7 = !{!"clang version 9.0.0 (https://github.com/llvm/llvm-project.git 4eff3de99423a62fd6e833e29c71c1e62ba6140b)"}
97-
!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 3, type: !9, scopeLine: 3, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
114+
!7 = !{!"clang version 9.0.0 (https://github.com/llvm/llvm-project.git 9c8073f44f786fbf47335e53f20abe64429e8e47)"}
115+
!8 = distinct !DISubprogram(name: "call_virtual", scope: !1, file: !1, line: 8, type: !9, scopeLine: 8, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
98116
!9 = !DISubroutineType(types: !10)
99-
!10 = !{null}
100-
!11 = !DILocation(line: 4, scope: !8)
101-
!12 = !DILocation(line: 5, scope: !8)
102-
!13 = distinct !DISubprogram(name: "do_alloc", scope: !1, file: !1, line: 11, type: !14, scopeLine: 11, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
103-
!14 = !DISubroutineType(types: !15)
104-
!15 = !{null, !16}
105-
!16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !17, size: 64)
106-
!17 = !DICompositeType(tag: DW_TAG_structure_type, name: "Foo", file: !1, line: 7, flags: DIFlagFwdDecl)
107-
!18 = !DILocalVariable(name: "p", arg: 1, scope: !13, file: !1, line: 11, type: !16)
108-
!19 = !DILocation(line: 11, scope: !13)
109-
!20 = !DILocation(line: 12, scope: !13)
110-
!21 = !DILocation(line: 13, scope: !13)
111-
117+
!10 = !{null, !11}
118+
!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64)
119+
!12 = !DICompositeType(tag: DW_TAG_structure_type, name: "Foo", file: !1, line: 1, flags: DIFlagFwdDecl, identifier: ".?AUFoo@@")
120+
!13 = !DILocalVariable(name: "p", arg: 1, scope: !8, file: !1, line: 8, type: !11)
121+
!14 = !DILocation(line: 8, scope: !8)
122+
!15 = !DILocation(line: 9, scope: !8)
123+
!16 = !DILocation(line: 10, scope: !8)
124+
!17 = distinct !DISubprogram(name: "call_multiple", scope: !1, file: !1, line: 12, type: !18, scopeLine: 12, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
125+
!18 = !DISubroutineType(types: !19)
126+
!19 = !{null}
127+
!20 = !DILocation(line: 13, scope: !17)
128+
!21 = !DILocation(line: 14, scope: !17)
129+
!22 = !DILocation(line: 15, scope: !17)

0 commit comments

Comments
 (0)