Skip to content

Commit db0725c

Browse files
committed
Add some optimizer tests
1 parent ce128e7 commit db0725c

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed

test/SILOptimizer/copy_propagation.sil

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,3 +1094,58 @@ bb0:
10941094
return %3
10951095
}
10961096

1097+
class Klass {}
1098+
1099+
public struct Wrapper {
1100+
var _k: Klass
1101+
var i: Int
1102+
}
1103+
1104+
sil @get_wrapper : $@convention(thin) () -> @owned Wrapper
1105+
sil @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1106+
1107+
sil [ossa] [noinline] @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass {
1108+
bb0(%0 : @guaranteed $Wrapper):
1109+
%2 = struct_extract %0, #Wrapper._k
1110+
return %2
1111+
}
1112+
1113+
// CHECK-LABEL: sil [ossa] @test_borrow_accessor_opt1 :
1114+
// CHECK-NOT: copy_value
1115+
// CHECK-LABEL: } // end sil function 'test_borrow_accessor_opt1'
1116+
sil [ossa] @test_borrow_accessor_opt1 : $@convention(thin) (@owned Wrapper) -> () {
1117+
bb0(%0 : @owned $Wrapper):
1118+
%copy = copy_value %0
1119+
%1 = begin_borrow %copy
1120+
%2 = function_ref @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
1121+
%3 = apply %2(%1) : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
1122+
%4 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1123+
%5 = apply %4(%3) : $@convention(thin) (@guaranteed Klass) -> ()
1124+
end_borrow %1
1125+
destroy_value %copy
1126+
destroy_value %0
1127+
%6 = tuple ()
1128+
return %6
1129+
}
1130+
1131+
// SemanticArcOpts removes the copy_value in this test
1132+
// CHECK-LABEL: sil [ossa] @test_borrow_accessor_opt2 :
1133+
// TODO-CHECK-NOT: copy_value
1134+
// CHECK-LABEL: } // end sil function 'test_borrow_accessor_opt2'
1135+
sil [ossa] @test_borrow_accessor_opt2 : $@convention(thin) () -> () {
1136+
bb0:
1137+
%1 = function_ref @get_wrapper : $@convention(thin) () -> @owned Wrapper
1138+
%2 = apply %1() : $@convention(thin) () -> @owned Wrapper
1139+
%3 = begin_borrow %2
1140+
%4 = function_ref @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
1141+
%5 = apply %4(%3) : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
1142+
%copy = copy_value %5
1143+
%6 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1144+
%7 = apply %6(%copy) : $@convention(thin) (@guaranteed Klass) -> ()
1145+
destroy_value %copy
1146+
end_borrow %3
1147+
destroy_value %2
1148+
%10 = tuple ()
1149+
return %10
1150+
}
1151+

test/SILOptimizer/destroy-hoisting.sil

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,58 @@ bb2:
395395
unreachable
396396
}
397397

398+
399+
public struct Wrapper {
400+
var _k: Klass
401+
var i: Int
402+
}
403+
404+
sil @get_wrapper : $@convention(thin) () -> @owned Wrapper
405+
sil @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
406+
407+
sil [ossa] @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass {
408+
bb0(%0 : @guaranteed $Wrapper):
409+
%2 = struct_extract %0, #Wrapper._k
410+
return %2
411+
}
412+
413+
// CHECK-LABEL: sil [ossa] @test_borrow_accessor_noopt1 :
414+
// CHECK: [[F:%.*]] = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
415+
// CHECK: [[A:%.*]] = apply [[F]]({{.*}}) : $@convention(thin) (@guaranteed Klass) -> ()
416+
// CHECK: end_borrow {{.*}}
417+
// CHECK: destroy_value %0
418+
// CHECK-LABEL: } // end sil function 'test_borrow_accessor_noopt1'
419+
sil [ossa] @test_borrow_accessor_noopt1 : $@convention(thin) (@owned Wrapper) -> () {
420+
bb0(%0 : @owned $Wrapper):
421+
%1 = begin_borrow %0
422+
%2 = function_ref @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
423+
%3 = apply %2(%1) : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
424+
%4 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
425+
%5 = apply %4(%3) : $@convention(thin) (@guaranteed Klass) -> ()
426+
end_borrow %1
427+
destroy_value %0
428+
%6 = tuple ()
429+
return %6
430+
}
431+
432+
// CHECK-LABEL: sil [ossa] @test_borrow_accessor_noopt2 :
433+
// CHECK: [[F:%.*]] = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
434+
// CHECK: [[A:%.*]] = apply [[F]]({{.*}}) : $@convention(thin) (@guaranteed Klass) -> ()
435+
// CHECK: end_borrow {{.*}}
436+
// CHECK: destroy_value {{.*}}
437+
// CHECK-LABEL: } // end sil function 'test_borrow_accessor_noopt2'
438+
sil [ossa] @test_borrow_accessor_noopt2 : $@convention(thin) () -> () {
439+
bb0:
440+
%1 = function_ref @get_wrapper : $@convention(thin) () -> @owned Wrapper
441+
%2 = apply %1() : $@convention(thin) () -> @owned Wrapper
442+
%3 = begin_borrow %2
443+
%4 = function_ref @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
444+
%5 = apply %4(%3) : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
445+
%6 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
446+
%7 = apply %6(%5) : $@convention(thin) (@guaranteed Klass) -> ()
447+
end_borrow %3
448+
destroy_value %2
449+
%10 = tuple ()
450+
return %10
451+
}
452+

test/SILOptimizer/ossa_lifetime_completion.sil

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,3 +1033,52 @@ bb0(%0 : @owned $Wrapper, %1 : $*Klass):
10331033
%4 = mark_dependence [nonescaping] %1 : $*Klass on %0 : $Wrapper
10341034
unreachable
10351035
}
1036+
1037+
sil @get_wrapper : $@convention(thin) () -> @owned Wrapper
1038+
sil @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1039+
1040+
sil [ossa] [noinline] @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass {
1041+
bb0(%0 : @guaranteed $Wrapper):
1042+
%2 = struct_extract %0, #Wrapper.c
1043+
return %2
1044+
}
1045+
1046+
// CHECK-LABEL: sil [ossa] @test_borrow_accessor1 :
1047+
// CHECK: [[F:%.*]] = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1048+
// CHECK: [[A:%.*]] = apply [[F]]({{.*}}) : $@convention(thin) (@guaranteed Klass) -> ()
1049+
// CHECK: end_borrow {{.*}}
1050+
// CHECK: destroy_value {{.*}}
1051+
// CHECK: unreachable
1052+
// CHECK-LABEL: } // end sil function 'test_borrow_accessor1'
1053+
sil [ossa] @test_borrow_accessor1 : $@convention(thin) (@owned Wrapper) -> () {
1054+
bb0(%0 : @owned $Wrapper):
1055+
specify_test "ossa_complete_lifetime @instruction[0] liveness"
1056+
%copy = copy_value %0
1057+
%1 = begin_borrow %copy
1058+
%2 = function_ref @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
1059+
%3 = apply %2(%1) : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
1060+
%4 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1061+
%5 = apply %4(%3) : $@convention(thin) (@guaranteed Klass) -> ()
1062+
unreachable
1063+
}
1064+
1065+
// CHECK-LABEL: sil [ossa] @test_borrow_accessor2 :
1066+
// CHECK: [[F:%.*]] = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1067+
// CHECK: [[A:%.*]] = apply [[F]]({{.*}}) : $@convention(thin) (@guaranteed Klass) -> ()
1068+
// CHECK: end_borrow {{.*}}
1069+
// CHECK: destroy_value {{.*}}
1070+
// CHECK: unreachable
1071+
// CHECK-LABEL: } // end sil function 'test_borrow_accessor2'
1072+
sil [ossa] @test_borrow_accessor2 : $@convention(thin) () -> () {
1073+
bb0:
1074+
specify_test "ossa_complete_lifetime @instruction[1] liveness"
1075+
%1 = function_ref @get_wrapper : $@convention(thin) () -> @owned Wrapper
1076+
%2 = apply %1() : $@convention(thin) () -> @owned Wrapper
1077+
%3 = begin_borrow %2
1078+
%4 = function_ref @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
1079+
%5 = apply %4(%3) : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
1080+
%6 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1081+
%7 = apply %6(%5) : $@convention(thin) (@guaranteed Klass) -> ()
1082+
unreachable
1083+
}
1084+

test/SILOptimizer/semantic-arc-opts-redundantcopyopts.sil

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,37 @@ bb0(%1 : $*ContainsKlass):
204204
destroy_value %131
205205
unreachable
206206
}
207+
208+
public struct Wrapper {
209+
var _k: Klass
210+
}
211+
212+
sil @get_wrapper : $@convention(thin) () -> @owned Wrapper
213+
sil @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
214+
215+
sil [ossa] @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass {
216+
bb0(%0 : @guaranteed $Wrapper):
217+
%2 = struct_extract %0, #Wrapper._k
218+
return %2
219+
}
220+
221+
// CHECK-LABEL: sil [ossa] @test_borrow_accessor_opt :
222+
// CHECK-NOT: copy_value
223+
// CHECK-LABEL: } // end sil function 'test_borrow_accessor_opt'
224+
sil [ossa] @test_borrow_accessor_opt : $@convention(thin) () -> () {
225+
bb0:
226+
%1 = function_ref @get_wrapper : $@convention(thin) () -> @owned Wrapper
227+
%2 = apply %1() : $@convention(thin) () -> @owned Wrapper
228+
%3 = begin_borrow %2
229+
%4 = function_ref @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
230+
%5 = apply %4(%3) : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
231+
%copy = copy_value %5
232+
%6 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
233+
%7 = apply %6(%copy) : $@convention(thin) (@guaranteed Klass) -> ()
234+
destroy_value %copy
235+
end_borrow %3
236+
destroy_value %2
237+
%10 = tuple ()
238+
return %10
239+
}
240+

test/SILOptimizer/shrink_borrow_scope.unit.sil

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,37 @@ exit:
6767
%retval = tuple ()
6868
return %retval : $()
6969
}
70+
71+
class Klass {}
72+
73+
public struct Wrapper {
74+
var _k: Klass
75+
}
76+
77+
sil @get_wrapper : $@convention(thin) () -> @owned Wrapper
78+
sil @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
79+
80+
sil [ossa] @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass {
81+
bb0(%0 : @guaranteed $Wrapper):
82+
%2 = struct_extract %0, #Wrapper._k
83+
return %2
84+
}
85+
86+
// CHECK-LABEL: begin running test {{.*}} on test_borrow_accessor_noopt
87+
// CHECK: Result: did not shrink
88+
// CHECK-LABEL: end running test {{.*}} on test_borrow_accessor_noopt
89+
sil [ossa] @test_borrow_accessor_noopt : $@convention(thin) () -> () {
90+
bb0:
91+
%1 = function_ref @get_wrapper : $@convention(thin) () -> @owned Wrapper
92+
%2 = apply %1() : $@convention(thin) () -> @owned Wrapper
93+
%3 = begin_borrow %2
94+
specify_test "shrink_borrow_scope %3"
95+
%4 = function_ref @borrow_loadable_prop : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
96+
%5 = apply %4(%3) : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass
97+
%6 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
98+
%7 = apply %6(%5) : $@convention(thin) (@guaranteed Klass) -> ()
99+
end_borrow %3
100+
destroy_value %2
101+
%10 = tuple ()
102+
return %10
103+
}

0 commit comments

Comments
 (0)