Skip to content

Commit 6daebfd

Browse files
committed
Add ossa lifetime analysis tests
1 parent 11ef752 commit 6daebfd

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// RUN: %target-sil-opt %s -ossa-lifetime-analysis -o /dev/null | %FileCheck %s
2+
//
3+
// These tests rely on "incomplete" OSSA lifetimes. Incomplete OSSA
4+
// lifetimes are invalid SIL, but the OSSA liveness utilities still
5+
// need to handle them! SILGen and textual SIL is allowed to produce
6+
// incomplete lifetimes. The OSSA liveness utilities need to be able
7+
// to fixup those incomplete lifetimes. So the utilities need to
8+
// handle invalid SIL in order to produce valid SIL.
9+
//
10+
// For each function, all values used by debug_value [trace] are
11+
// considered live range defs. Liveness is computed from their direct uses.
12+
13+
sil_stage canonical
14+
15+
import Builtin
16+
17+
enum FakeOptional<T> {
18+
case none
19+
case some(T)
20+
}
21+
22+
class C {}
23+
24+
// CHECK-LABEL: @testSelfLoop
25+
// CHECK: SSA lifetime analysis: [[V:%.*]] = copy_value %0 : $C
26+
// CHECK: bb1: LiveOut
27+
// CHECK: bb2: LiveWithin
28+
// CHECK: lifetime-ending user: br bb1([[V]] : $C)
29+
// CHECK: last user: br bb1([[V]] : $C)
30+
// CHECK-NOT: dead def
31+
sil [ossa] @testSelfLoop : $@convention(thin) (@guaranteed C) -> () {
32+
bb0(%0 : @guaranteed $C):
33+
br bb3
34+
35+
bb1(%1 : @owned $C):
36+
destroy_value %1 : $C
37+
%2 = copy_value %0 : $C
38+
debug_value [trace] %2 : $C
39+
br bb2
40+
41+
bb2:
42+
br bb1(%2 : $C)
43+
44+
bb3:
45+
%99 = tuple()
46+
return %99 : $()
47+
}
48+
49+
// CHECK-LABEL: @testSelfKill
50+
// CHECK:SSA lifetime analysis: [[V:%.*]] = move_value %1 : $C
51+
// CHECK: bb1: LiveOut
52+
// CHECK: bb2: LiveWithin
53+
// CHECK: lifetime-ending user: br bb1([[V]] : $C)
54+
// CHECK: last user: br bb1([[V]] : $C)
55+
// CHECK-NOT: dead def
56+
sil [ossa] @testSelfKill : $@convention(thin) () -> () {
57+
bb0:
58+
br bb3
59+
60+
bb1(%1 : @owned $C):
61+
%2 = move_value %1 : $C
62+
debug_value [trace] %2 : $C
63+
br bb2
64+
65+
bb2:
66+
br bb1(%2 : $C)
67+
68+
bb3:
69+
%99 = tuple()
70+
return %99 : $()
71+
}
72+
73+
// Test a live range that is extended through reborrows,
74+
// considering them new defs.
75+
// (e.g. BorrowedValue::visitTransitiveLifetimeEndingUses)
76+
//
77+
// This live range is not dominated by the original borrow.
78+
//
79+
// CHECK-LABEL: @testReborrow
80+
// CHECK: MultiDef lifetime analysis:
81+
// CHECK: def: [[B:%.*]] = begin_borrow %0 : $C
82+
// CHECK: def: [[RB:%.*]] = argument of bb3 : $C
83+
// CHECK-NEXT: bb2: LiveWithin
84+
// CHECK-NEXT: bb3: LiveWithin
85+
// CHECK-NEXT: lifetime-ending user: br bb3([[B]] : $C)
86+
// CHECK-NEXT: lifetime-ending user: end_borrow [[RB]] : $C
87+
// CHECK-NEXT: last user: br bb3([[B]] : $C)
88+
// CHECK-NEXT: last user: end_borrow [[RB]] : $C
89+
sil [ossa] @testReborrow : $@convention(thin) (@owned C) -> () {
90+
bb0(%0 : @owned $C):
91+
cond_br undef, bb1, bb2
92+
93+
bb1:
94+
%borrow1 = begin_borrow %0 : $C
95+
br bb3(%borrow1 : $C)
96+
97+
bb2:
98+
%borrow2 = begin_borrow %0 : $C
99+
debug_value [trace] %borrow2 : $C
100+
br bb3(%borrow2 : $C)
101+
102+
bb3(%reborrow : @guaranteed $C):
103+
debug_value [trace] %reborrow : $C
104+
end_borrow %reborrow : $C
105+
br bb4
106+
107+
bb4:
108+
destroy_value %0 : $C
109+
%99 = tuple()
110+
return %99 : $()
111+
}

0 commit comments

Comments
 (0)