Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit c60a982

Browse files
committed
Cloning: Fix debug info cloning
Summary: I believe https://reviews.llvm.org/rL302576 introduced two bugs: 1) it produces duplicate distinct variables for every: dbg.value describing the same variable. To fix the problme I switched form getDistinct() to get() in DebugLoc.cpp: auto reparentVar = [&](DILocalVariable *Var) { return DILocalVariable::getDistinct( 2) It passes NewFunction plain name as a linkagename parameter to Subprogram constructor. Breaks assert in: || DeclLinkageName.empty()) || LinkageName == DeclLinkageName) && "decl has a linkage name and it is different"' failed. #9 0x00007f5010261b75 llvm::DwarfUnit::applySubprogramDefinitionAttributes(llvm::DISubprogram const*, llvm::DIE&) /home/gor/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp:1173:3 # (Edit: reproducer added) Here how https://reviews.llvm.org/rL302576 broke coroutine debug info. Coroutine body of the original function is split into several parts by cloning and removing unneeded code. All parts describe the original function and variables present in the original function. For a simple case, prior to Split, original function has these two blocks: ``` PostSpill: ; preds = %AllocaSpillBB call void @llvm.dbg.value(metadata i32 %x, i64 0, metadata !14, metadata !15), !dbg !13 store i32 %x, i32* %x.addr, align 4 ... and sw.epilog: ; preds = %sw.bb %x.addr.reload.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0, i32 4, !dbg !20 %4 = load i32, i32* %x.addr.reload.addr, align 4, !dbg !20 call void @llvm.dbg.value(metadata i32 %4, i64 0, metadata !14, metadata !15), !dbg !13 !14 = !DILocalVariable(name: "x", arg: 1, scope: !6, file: !7, line: 55, type: !11) ``` Note that in two blocks different expression represent the same original user variable X. Before rL302576, for every cloned function there was exactly one cloned DILocalVariable(name: "x" as in: ``` define i8* @f(i32 %x) #0 !dbg !6 { ... !6 = distinct !DISubprogram(name: "f", scope: !7, file: !7, line: 55, type: !8, isLocal: false, isDefinition: true, scopeLine: 55, flags: DIFlagPrototyped, ... !14 = !DILocalVariable(name: "x", arg: 1, scope: !6, file: !7, line: 55, type: !11) define internal fastcc void @f.resume(%f.Frame* %FramePtr) #0 !dbg !25 { ... !25 = distinct !DISubprogram(name: "f", scope: !7, file: !7, line: 55, type: !8, isLocal: false, isDefinition: true, scopeLine: 55, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) !28 = !DILocalVariable(name: "x", arg: 1, scope: !25, file: !7, line: 55, type: !11) ``` After rL302576, for every cloned function there were as many DILocalVariable(name: "x" as there were "call void @llvm.dbg.value" for that variable. This was causing asserts in VerifyDebugInfo and AssemblyPrinter. Example: ``` !27 = distinct !DISubprogram(name: "f", linkageName: "f.resume", scope: !7, file: !7, line: 55, type: !8, isLocal: false, isDefinition: true, scopeLine: 55, !29 = distinct !DILocalVariable(name: "x", arg: 1, scope: !27, file: !7, line: 55, type: !11) !39 = distinct !DILocalVariable(name: "x", arg: 1, scope: !27, file: !7, line: 55, type: !11) !41 = distinct !DILocalVariable(name: "x", arg: 1, scope: !27, file: !7, line: 55, type: !11) ``` Second problem: Prior to rL302576, all clones were described by DISubprogram referring to original function. ``` define i8* @f(i32 %x) #0 !dbg !6 { ... !6 = distinct !DISubprogram(name: "f", scope: !7, file: !7, line: 55, type: !8, isLocal: false, isDefinition: true, scopeLine: 55, flags: DIFlagPrototyped, define internal fastcc void @f.resume(%f.Frame* %FramePtr) #0 !dbg !25 { ... !25 = distinct !DISubprogram(name: "f", scope: !7, file: !7, line: 55, type: !8, isLocal: false, isDefinition: true, scopeLine: 55, flags: DIFlagPrototyped, ``` After rL302576, DISubprogram for clones is of two minds, plain name refers to the original name, linkageName refers to plain name of the clone. ``` !27 = distinct !DISubprogram(name: "f", linkageName: "f.resume", scope: !7, file: !7, line: 55, type: !8, isLocal: false, isDefinition: true, scopeLine: 55, ``` I think the assumption in AsmPrinter is that both name and linkageName should refer to the same entity. It asserts here when they are not: ``` || DeclLinkageName.empty()) || LinkageName == DeclLinkageName) && "decl has a linkage name and it is different"' failed. #9 0x00007f5010261b75 llvm::DwarfUnit::applySubprogramDefinitionAttributes(llvm::DISubprogram const*, llvm::DIE&) /home/gor/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp:1173:3 ``` After this fix, behavior (with respect to coroutines) reverts to exactly as it was before and therefore making them debuggable again, or even more importantly, compilable, with "-g" Reviewers: dblaikie, echristo, aprantl Reviewed By: dblaikie Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D33614 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304079 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b5ac700 commit c60a982

File tree

4 files changed

+145
-11
lines changed

4 files changed

+145
-11
lines changed

lib/IR/DebugLoc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void DebugLoc::reparentDebugInfo(Instruction &I, DISubprogram *OrigSP,
163163

164164
// Fix up debug variables to point to NewSP.
165165
auto reparentVar = [&](DILocalVariable *Var) {
166-
return DILocalVariable::getDistinct(
166+
return DILocalVariable::get(
167167
Ctx,
168168
cast<DILocalScope>(
169169
reparentScope(Ctx, Var->getScope(), OrigSP, NewSP, Cache)),

lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,7 @@ static Function *createClone(Function &F, Twine Suffix, coro::Shape &Shape,
228228

229229
SmallVector<ReturnInst *, 4> Returns;
230230

231-
if (DISubprogram *SP = F.getSubprogram()) {
232-
// If we have debug info, add mapping for the metadata nodes that should not
233-
// be cloned by CloneFunctionInfo.
234-
auto &MD = VMap.MD();
235-
MD[SP->getUnit()].reset(SP->getUnit());
236-
MD[SP->getType()].reset(SP->getType());
237-
MD[SP->getFile()].reset(SP->getFile());
238-
}
239-
CloneFunctionInto(NewF, &F, VMap, /*ModuleLevelChanges=*/true, Returns);
231+
CloneFunctionInto(NewF, &F, VMap, /*ModuleLevelChanges=*/false, Returns);
240232

241233
// Remove old returns.
242234
for (ReturnInst *Return : Returns)

lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
133133
auto *SP = cast<DISubprogram>(MD.second);
134134
NewMD = DISubprogram::getDistinct(
135135
NewFunc->getContext(), SP->getScope(), SP->getName(),
136-
NewFunc->getName(), SP->getFile(), SP->getLine(), SP->getType(),
136+
SP->getLinkageName(), SP->getFile(), SP->getLine(), SP->getType(),
137137
SP->isLocalToUnit(), SP->isDefinition(), SP->getScopeLine(),
138138
SP->getContainingType(), SP->getVirtuality(), SP->getVirtualIndex(),
139139
SP->getThisAdjustment(), SP->getFlags(), SP->isOptimized(),
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
; Tests that debug information is sane after coro-split
2+
; RUN: opt < %s -coro-split -S | FileCheck %s
3+
4+
source_filename = "simple-repro.c"
5+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6+
target triple = "x86_64-unknown-linux-gnu"
7+
8+
; Function Attrs: noinline nounwind
9+
define i8* @f(i32 %x) #0 !dbg !6 {
10+
entry:
11+
%x.addr = alloca i32, align 4
12+
%coro_hdl = alloca i8*, align 8
13+
store i32 %x, i32* %x.addr, align 4
14+
call void @llvm.dbg.declare(metadata i32* %x.addr, metadata !12, metadata !13), !dbg !14
15+
call void @llvm.dbg.declare(metadata i8** %coro_hdl, metadata !15, metadata !13), !dbg !16
16+
%0 = call token @llvm.coro.id(i32 0, i8* null, i8* bitcast (i8* (i32)* @f to i8*), i8* null), !dbg !16
17+
%1 = call i64 @llvm.coro.size.i64(), !dbg !16
18+
%call = call i8* @malloc(i64 %1), !dbg !16
19+
%2 = call i8* @llvm.coro.begin(token %0, i8* %call) #7, !dbg !16
20+
store i8* %2, i8** %coro_hdl, align 8, !dbg !16
21+
%3 = call i8 @llvm.coro.suspend(token none, i1 false), !dbg !17
22+
%conv = sext i8 %3 to i32, !dbg !17
23+
call void @coro.devirt.trigger(i8* null)
24+
switch i32 %conv, label %sw.default [
25+
i32 0, label %sw.bb
26+
i32 1, label %sw.bb1
27+
], !dbg !17
28+
29+
sw.bb: ; preds = %entry
30+
br label %sw.epilog, !dbg !18
31+
32+
sw.bb1: ; preds = %entry
33+
br label %coro_Cleanup, !dbg !18
34+
35+
sw.default: ; preds = %entry
36+
br label %coro_Suspend, !dbg !18
37+
38+
sw.epilog: ; preds = %sw.bb
39+
%4 = load i32, i32* %x.addr, align 4, !dbg !20
40+
%add = add nsw i32 %4, 1, !dbg !21
41+
store i32 %add, i32* %x.addr, align 4, !dbg !22
42+
br label %coro_Cleanup, !dbg !23
43+
44+
coro_Cleanup: ; preds = %sw.epilog, %sw.bb1
45+
%5 = load i8*, i8** %coro_hdl, align 8, !dbg !24
46+
%6 = call i8* @llvm.coro.free(token %0, i8* %5), !dbg !24
47+
call void @free(i8* %6), !dbg !24
48+
br label %coro_Suspend, !dbg !24
49+
50+
coro_Suspend: ; preds = %coro_Cleanup, %sw.default
51+
%7 = call i1 @llvm.coro.end(i8* null, i1 false) #7, !dbg !24
52+
%8 = load i8*, i8** %coro_hdl, align 8, !dbg !24
53+
ret i8* %8, !dbg !24
54+
}
55+
56+
; Function Attrs: nounwind readnone speculatable
57+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
58+
59+
; Function Attrs: argmemonly nounwind readonly
60+
declare token @llvm.coro.id(i32, i8* readnone, i8* nocapture readonly, i8*) #2
61+
62+
declare i8* @malloc(i64) #3
63+
64+
; Function Attrs: nounwind readnone
65+
declare i64 @llvm.coro.size.i64() #4
66+
67+
; Function Attrs: nounwind
68+
declare i8* @llvm.coro.begin(token, i8* writeonly) #5
69+
70+
; Function Attrs: nounwind
71+
declare i8 @llvm.coro.suspend(token, i1) #5
72+
73+
declare void @free(i8*) #3
74+
75+
; Function Attrs: argmemonly nounwind readonly
76+
declare i8* @llvm.coro.free(token, i8* nocapture readonly) #2
77+
78+
; Function Attrs: nounwind
79+
declare i1 @llvm.coro.end(i8*, i1) #5
80+
81+
; Function Attrs: alwaysinline
82+
define private void @coro.devirt.trigger(i8*) #6 {
83+
entry:
84+
ret void
85+
}
86+
87+
; Function Attrs: argmemonly nounwind readonly
88+
declare i8* @llvm.coro.subfn.addr(i8* nocapture readonly, i8) #2
89+
90+
attributes #0 = { noinline nounwind "coroutine.presplit"="1" "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
91+
attributes #1 = { nounwind readnone speculatable }
92+
attributes #2 = { argmemonly nounwind readonly }
93+
attributes #3 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
94+
attributes #4 = { nounwind readnone }
95+
attributes #5 = { nounwind }
96+
attributes #6 = { alwaysinline }
97+
attributes #7 = { noduplicate }
98+
99+
!llvm.dbg.cu = !{!0}
100+
!llvm.module.flags = !{!3, !4}
101+
!llvm.ident = !{!5}
102+
103+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 5.0.0 (http://llvm.org/git/clang.git 97b002238b11ff30d94d0516d6a0515db5725fd8) (http://llvm.org/git/llvm.git 0cb060ba567f1aa5b4b04e86665f88e4632b528a)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
104+
!1 = !DIFile(filename: "<stdin>", directory: "C:\5CGitHub\5Cllvm\5Cbuild\5CDebug\5Cbin")
105+
!2 = !{}
106+
!3 = !{i32 2, !"Dwarf Version", i32 4}
107+
!4 = !{i32 2, !"Debug Info Version", i32 3}
108+
!5 = !{!"clang version 5.0.0 (http://llvm.org/git/clang.git 97b002238b11ff30d94d0516d6a0515db5725fd8) (http://llvm.org/git/llvm.git 0cb060ba567f1aa5b4b04e86665f88e4632b528a)"}
109+
!6 = distinct !DISubprogram(name: "f", linkageName: "flink", scope: !7, file: !7, line: 55, type: !8, isLocal: false, isDefinition: true, scopeLine: 55, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
110+
!7 = !DIFile(filename: "simple-repro.c", directory: "C:\5CGitHub\5Cllvm\5Cbuild\5CDebug\5Cbin")
111+
!8 = !DISubroutineType(types: !9)
112+
!9 = !{!10, !11}
113+
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
114+
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
115+
!12 = !DILocalVariable(name: "x", arg: 1, scope: !6, file: !7, line: 55, type: !11)
116+
!13 = !DIExpression()
117+
!14 = !DILocation(line: 55, column: 13, scope: !6)
118+
!15 = !DILocalVariable(name: "coro_hdl", scope: !6, file: !7, line: 56, type: !10)
119+
!16 = !DILocation(line: 56, column: 3, scope: !6)
120+
!17 = !DILocation(line: 58, column: 5, scope: !6)
121+
!18 = !DILocation(line: 58, column: 5, scope: !19)
122+
!19 = distinct !DILexicalBlock(scope: !6, file: !7, line: 58, column: 5)
123+
!20 = !DILocation(line: 59, column: 9, scope: !6)
124+
!21 = !DILocation(line: 59, column: 10, scope: !6)
125+
!22 = !DILocation(line: 59, column: 7, scope: !6)
126+
!23 = !DILocation(line: 59, column: 5, scope: !6)
127+
!24 = !DILocation(line: 62, column: 3, scope: !6)
128+
129+
; CHECK: define i8* @f(i32 %x) #0 !dbg ![[ORIG:[0-9]+]]
130+
; CHECK: define internal fastcc void @f.resume(%f.Frame* %FramePtr) #0 !dbg ![[RESUME:[0-9]+]]
131+
; CHECK: define internal fastcc void @f.destroy(%f.Frame* %FramePtr) #0 !dbg ![[DESTROY:[0-9]+]]
132+
; CHECK: define internal fastcc void @f.cleanup(%f.Frame* %FramePtr) #0 !dbg ![[CLEANUP:[0-9]+]]
133+
134+
; CHECK: ![[ORIG]] = distinct !DISubprogram(name: "f", linkageName: "flink"
135+
; CHECK: !DILocalVariable(name: "x", arg: 1, scope: ![[ORIG]]
136+
137+
; CHECK: ![[RESUME]] = distinct !DISubprogram(name: "f", linkageName: "flink"
138+
; CHECK: !DILocalVariable(name: "x", arg: 1, scope: ![[RESUME]]
139+
140+
; CHECK: ![[DESTROY]] = distinct !DISubprogram(name: "f", linkageName: "flink"
141+
142+
; CHECK: ![[CLEANUP]] = distinct !DISubprogram(name: "f", linkageName: "flink"

0 commit comments

Comments
 (0)