Skip to content

Commit c162194

Browse files
committed
[Coroutines] Part15b: Fix dbg information handling in coro-split.
Summary: Without the fix, if there was a function inlined into the coroutine with debug information, CloneFunctionInto(NewF, &F, VMap, /*ModuleLevelChanges=*/true, Returns); would duplicate all of the debug information including the DICompileUnit. We know use VMap to indicate that debug metadata for a File, Unit and FunctionType should not be duplicated when we creating clones that will become f.resume, f.destroy and f.cleanup. Reviewers: majnemer Subscribers: mehdi_amini, llvm-commits Differential Revision: https://reviews.llvm.org/D24417 llvm-svn: 282899
1 parent f9c622a commit c162194

File tree

3 files changed

+132
-6
lines changed

3 files changed

+132
-6
lines changed

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/Support/MathExtras.h"
2828
#include "llvm/Support/circular_raw_ostream.h"
2929
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
30+
#include "llvm/Transforms/Utils/Local.h"
3031

3132
using namespace llvm;
3233

@@ -637,6 +638,10 @@ static void splitAround(Instruction *I, const Twine &Name) {
637638
}
638639

639640
void coro::buildCoroutineFrame(Function &F, Shape &Shape) {
641+
// Lower coro.dbg.declare to coro.dbg.value, since we are going to rewrite
642+
// access to local variables.
643+
LowerDbgDeclare(F);
644+
640645
Shape.PromiseAlloca = Shape.CoroBegin->getId()->getPromise();
641646
if (Shape.PromiseAlloca) {
642647
Shape.CoroBegin->getId()->clearPromise();

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,16 @@ static Function *createClone(Function &F, Twine Suffix, coro::Shape &Shape,
200200

201201
SmallVector<ReturnInst *, 4> Returns;
202202

203+
if (DISubprogram *SP = F.getSubprogram()) {
204+
// If we have debug info, add mapping for the metadata nodes that should not
205+
// be cloned by CloneFunctionInfo.
206+
auto &MD = VMap.MD();
207+
MD[SP->getUnit()].reset(SP->getUnit());
208+
MD[SP->getType()].reset(SP->getType());
209+
MD[SP->getFile()].reset(SP->getFile());
210+
}
203211
CloneFunctionInto(NewF, &F, VMap, /*ModuleLevelChanges=*/true, Returns);
204212

205-
// If we have debug info, update it. ModuleLevelChanges = true above, does
206-
// the heavy lifting, we just need to repoint subprogram at the same
207-
// DICompileUnit as the original function F.
208-
if (DISubprogram *SP = F.getSubprogram())
209-
NewF->getSubprogram()->replaceUnit(SP->getUnit());
210-
211213
// Remove old returns.
212214
for (ReturnInst *Return : Returns)
213215
changeToUnreachable(Return, /*UseLLVMTrap=*/false);
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
; Make sure that coro-split correctly deals with debug information.
2+
; The test here is simply that it does not result in bad IR that will crash opt.
3+
; RUN: opt < %s -coro-split -disable-output
4+
source_filename = "coro.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: nounwind readnone
9+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
10+
11+
declare void @bar(...) local_unnamed_addr #2
12+
13+
; Function Attrs: nounwind uwtable
14+
define i8* @f() #3 !dbg !16 {
15+
entry:
16+
%0 = tail call token @llvm.coro.id(i32 0, i8* null, i8* bitcast (i8* ()* @f to i8*), i8* null), !dbg !26
17+
%1 = tail call i64 @llvm.coro.size.i64(), !dbg !26
18+
%call = tail call i8* @malloc(i64 %1), !dbg !26
19+
%2 = tail call i8* @llvm.coro.begin(token %0, i8* %call) #9, !dbg !26
20+
tail call void @llvm.dbg.value(metadata i8* %2, i64 0, metadata !21, metadata !12), !dbg !26
21+
br label %for.cond, !dbg !27
22+
23+
for.cond: ; preds = %for.cond, %entry
24+
tail call void @llvm.dbg.value(metadata i32 undef, i64 0, metadata !22, metadata !12), !dbg !28
25+
tail call void @llvm.dbg.value(metadata i32 undef, i64 0, metadata !11, metadata !12) #7, !dbg !29
26+
tail call void (...) @bar() #7, !dbg !33
27+
%3 = tail call token @llvm.coro.save(i8* null), !dbg !34
28+
%4 = tail call i8 @llvm.coro.suspend(token %3, i1 false), !dbg !34
29+
%conv = sext i8 %4 to i32, !dbg !34
30+
switch i32 %conv, label %coro_Suspend [
31+
i32 0, label %for.cond
32+
i32 1, label %coro_Cleanup
33+
], !dbg !34
34+
35+
coro_Cleanup: ; preds = %for.cond
36+
%5 = tail call i8* @llvm.coro.free(token %0, i8* %2), !dbg !35
37+
tail call void @free(i8* nonnull %5), !dbg !36
38+
br label %coro_Suspend, !dbg !36
39+
40+
coro_Suspend: ; preds = %for.cond, %if.then, %coro_Cleanup
41+
tail call void @llvm.coro.end(i8* null, i1 false) #9, !dbg !38
42+
ret i8* %2, !dbg !39
43+
}
44+
45+
; Function Attrs: argmemonly nounwind
46+
declare void @llvm.lifetime.start(i64, i8* nocapture) #4
47+
48+
; Function Attrs: argmemonly nounwind readonly
49+
declare token @llvm.coro.id(i32, i8* readnone, i8* nocapture readonly, i8*) #5
50+
51+
; Function Attrs: nounwind
52+
declare noalias i8* @malloc(i64) local_unnamed_addr #6
53+
declare i64 @llvm.coro.size.i64() #1
54+
declare i8* @llvm.coro.begin(token, i8* writeonly) #7
55+
declare token @llvm.coro.save(i8*) #7
56+
declare i8 @llvm.coro.suspend(token, i1) #7
57+
declare void @llvm.lifetime.end(i64, i8* nocapture) #4
58+
declare i8* @llvm.coro.free(token, i8* nocapture readonly) #5
59+
declare void @free(i8* nocapture) local_unnamed_addr #6
60+
declare void @llvm.coro.end(i8*, i1) #7
61+
declare i8* @llvm.coro.subfn.addr(i8* nocapture readonly, i8) #5
62+
63+
declare void @llvm.dbg.value(metadata, i64, metadata, metadata) #1
64+
65+
attributes #0 = { nounwind uwtable "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-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
66+
attributes #1 = { nounwind readnone }
67+
attributes #2 = { "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-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
68+
attributes #3 = { nounwind uwtable "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-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
69+
attributes #4 = { argmemonly nounwind }
70+
attributes #5 = { argmemonly nounwind readonly }
71+
attributes #6 = { nounwind "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-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
72+
attributes #7 = { nounwind }
73+
attributes #8 = { alwaysinline nounwind }
74+
attributes #9 = { noduplicate }
75+
76+
!llvm.dbg.cu = !{!0}
77+
!llvm.module.flags = !{!3, !4}
78+
!llvm.ident = !{!5}
79+
80+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 4.0.0 (http://llvm.org/git/clang.git 6c405f93921ac99ff5b8521bb1b3df4449deede4) (http://llvm.org/git/llvm.git 6e6d3247102a0f87ce14e906dcf6a3a5ed3faa65)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
81+
!1 = !DIFile(filename: "coro.c", directory: "/home/gor/build/bin")
82+
!2 = !{}
83+
!3 = !{i32 2, !"Dwarf Version", i32 4}
84+
!4 = !{i32 2, !"Debug Info Version", i32 3}
85+
!5 = !{!"clang version 4.0.0 (http://llvm.org/git/clang.git 6c405f93921ac99ff5b8521bb1b3df4449deede4) (http://llvm.org/git/llvm.git 6e6d3247102a0f87ce14e906dcf6a3a5ed3faa65)"}
86+
!6 = distinct !DISubprogram(name: "print", scope: !1, file: !1, line: 6, type: !7, isLocal: false, isDefinition: true, scopeLine: 6, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !10)
87+
!7 = !DISubroutineType(types: !8)
88+
!8 = !{null, !9}
89+
!9 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
90+
!10 = !{!11}
91+
!11 = !DILocalVariable(name: "v", arg: 1, scope: !6, file: !1, line: 6, type: !9)
92+
!12 = !DIExpression()
93+
!13 = !DILocation(line: 6, column: 16, scope: !6)
94+
!14 = !DILocation(line: 6, column: 19, scope: !6)
95+
!15 = !DILocation(line: 6, column: 25, scope: !6)
96+
!16 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 8, type: !17, isLocal: false, isDefinition: true, scopeLine: 8, isOptimized: true, unit: !0, variables: !20)
97+
!17 = !DISubroutineType(types: !18)
98+
!18 = !{!19}
99+
!19 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64, align: 64)
100+
!20 = !{!21, !22, !24}
101+
!21 = !DILocalVariable(name: "coro_hdl", scope: !16, file: !1, line: 9, type: !19)
102+
!22 = !DILocalVariable(name: "i", scope: !23, file: !1, line: 11, type: !9)
103+
!23 = distinct !DILexicalBlock(scope: !16, file: !1, line: 11, column: 3)
104+
!24 = !DILocalVariable(name: "coro_mem", scope: !25, file: !1, line: 16, type: !19)
105+
!25 = distinct !DILexicalBlock(scope: !16, file: !1, line: 16, column: 3)
106+
!26 = !DILocation(line: 9, column: 3, scope: !16)
107+
!27 = !DILocation(line: 11, column: 8, scope: !23)
108+
!28 = !DILocation(line: 11, column: 12, scope: !23)
109+
!29 = !DILocation(line: 6, column: 16, scope: !6, inlinedAt: !30)
110+
!30 = distinct !DILocation(line: 12, column: 5, scope: !31)
111+
!31 = distinct !DILexicalBlock(scope: !32, file: !1, line: 11, column: 25)
112+
!32 = distinct !DILexicalBlock(scope: !23, file: !1, line: 11, column: 3)
113+
!33 = !DILocation(line: 6, column: 19, scope: !6, inlinedAt: !30)
114+
!34 = !DILocation(line: 13, column: 5, scope: !31)
115+
!35 = !DILocation(line: 16, column: 3, scope: !25)
116+
!36 = !DILocation(line: 16, column: 3, scope: !37)
117+
!37 = distinct !DILexicalBlock(scope: !25, file: !1, line: 16, column: 3)
118+
!38 = !DILocation(line: 16, column: 3, scope: !16)
119+
!39 = !DILocation(line: 17, column: 1, scope: !16)

0 commit comments

Comments
 (0)