Skip to content

Commit df1fd1f

Browse files
committed
[Test] Unit test lexical destroy folding.
Adapt a preexisting test to be a unit test that runs the utility.
1 parent 24ae5d7 commit df1fd1f

File tree

3 files changed

+69
-45
lines changed

3 files changed

+69
-45
lines changed

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,23 @@ struct TestSpecificationTest : UnitTest {
270270
// MARK: OSSA Lifetime Unit Tests
271271
//===----------------------------------------------------------------------===//
272272

273+
// Arguments:
274+
// - the lexical borrow to fold
275+
// Dumpts:
276+
// - the function
277+
struct LexicalDestroyFoldingTest : UnitTest {
278+
LexicalDestroyFoldingTest(UnitTestRunner *pass) : UnitTest(pass) {}
279+
void invoke(Arguments &arguments) override {
280+
auto *dominanceAnalysis = getAnalysis<DominanceAnalysis>();
281+
DominanceInfo *domTree = dominanceAnalysis->get(getFunction());
282+
auto value = arguments.takeValue();
283+
auto *bbi = cast<BeginBorrowInst>(value);
284+
InstructionDeleter deleter;
285+
foldDestroysOfCopiedLexicalBorrow(bbi, *domTree, deleter);
286+
getFunction()->dump();
287+
}
288+
};
289+
273290
// Arguments:
274291
// - variadic list of - instruction: a last user
275292
// Dumps:

test/SILOptimizer/lexical_destroy_folding.sil

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -519,51 +519,6 @@ exit:
519519
return %retval : $()
520520
}
521521

522-
// Fold apply when guaranteed lexical value used in one but not two branches
523-
// and the lexical scope ends before the use on the non-lexical branch.
524-
//
525-
// CHECK-LABEL: sil [ossa] @nofold_two_parallel_owned_uses_one_lexical___scope_ends_before_use : {{.*}} {
526-
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] :
527-
// CHECK: [[COPY:%[^,]+]] = copy_value [[INSTANCE]]
528-
// CHECK: [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
529-
// CHECK: [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
530-
// CHECK: cond_br undef, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
531-
// CHECK: [[LEFT]]:
532-
// CHECK: apply [[CALLEE_OWNED]]([[MOVE]])
533-
// CHECK: destroy_value [[COPY]]
534-
// CHECK: br [[EXIT:bb[0-9]+]]
535-
// CHECK: [[RIGHT]]:
536-
// CHECK: destroy_value [[MOVE]]
537-
// CHECK: apply [[CALLEE_OWNED]]([[COPY]])
538-
// CHECK: br [[EXIT]]
539-
// CHECK: [[EXIT]]:
540-
// CHECK-LABEL: } // end sil function 'nofold_two_parallel_owned_uses_one_lexical___scope_ends_before_use'
541-
sil [ossa] @nofold_two_parallel_owned_uses_one_lexical___scope_ends_before_use : $@convention(thin) (@owned C) -> () {
542-
entry(%instance : @owned $C):
543-
%copy_2 = copy_value %instance : $C
544-
%lifetime = begin_borrow [lexical] %instance : $C
545-
%callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
546-
cond_br undef, left, right
547-
548-
left:
549-
%copy_1 = copy_value %lifetime : $C
550-
apply %callee_owned(%copy_1) : $@convention(thin) (@owned C) -> ()
551-
end_borrow %lifetime : $C
552-
destroy_value %instance : $C
553-
destroy_value %copy_2 : $C
554-
br exit
555-
556-
right:
557-
end_borrow %lifetime : $C
558-
destroy_value %instance : $C
559-
apply %callee_owned(%copy_2) : $@convention(thin) (@owned C) -> ()
560-
br exit
561-
562-
exit:
563-
%retval = tuple ()
564-
return %retval : $()
565-
}
566-
567522
// Fold even if there is a noncanonicalized copy which is otherwise used.
568523
// CHECK-LABEL: sil [ossa] @fold_noncanonical_copy_with_other_use : {{.*}} {
569524
// CHECK: {{bb[0-9]+}}:
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// RUN: %target-sil-opt -unit-test-runner %s -o /dev/null 2>&1 | %FileCheck %s
2+
3+
class C {}
4+
sil [ossa] @callee_owned : $@convention(thin) (@owned C) -> ()
5+
6+
// Fold apply when guaranteed lexical value used in one but not two branches
7+
// and the lexical scope ends before the use on the non-lexical branch.
8+
//
9+
// CHECK-LABEL: sil [ossa] @nofold_two_parallel_owned_uses_one_lexical___scope_ends_before_use : {{.*}} {
10+
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] :
11+
// CHECK: [[COPY:%[^,]+]] = copy_value [[INSTANCE]]
12+
// CHECK: [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
13+
// CHECK: [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
14+
// CHECK: cond_br undef, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
15+
// CHECK: [[LEFT]]:
16+
// CHECK: apply [[CALLEE_OWNED]]([[MOVE]])
17+
// CHECK: destroy_value [[COPY]]
18+
// CHECK: br [[EXIT:bb[0-9]+]]
19+
// CHECK: [[RIGHT]]:
20+
// CHECK: destroy_value [[MOVE]]
21+
// CHECK: apply [[CALLEE_OWNED]]([[COPY]])
22+
// CHECK: br [[EXIT]]
23+
// CHECK: [[EXIT]]:
24+
// CHECK-LABEL: } // end sil function 'nofold_two_parallel_owned_uses_one_lexical___scope_ends_before_use'
25+
sil [ossa] @nofold_two_parallel_owned_uses_one_lexical___scope_ends_before_use : $@convention(thin) (@owned C) -> () {
26+
entry(%instance : @owned $C):
27+
test_specification "lexical-destroy-folding @trace[0]"
28+
%copy_2 = copy_value %instance : $C
29+
%lifetime = begin_borrow [lexical] %instance : $C
30+
debug_value [trace] %lifetime : $C
31+
%callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
32+
cond_br undef, left, right
33+
34+
left:
35+
%copy_1 = copy_value %lifetime : $C
36+
apply %callee_owned(%copy_1) : $@convention(thin) (@owned C) -> ()
37+
end_borrow %lifetime : $C
38+
destroy_value %instance : $C
39+
destroy_value %copy_2 : $C
40+
br exit
41+
42+
right:
43+
end_borrow %lifetime : $C
44+
destroy_value %instance : $C
45+
apply %callee_owned(%copy_2) : $@convention(thin) (@owned C) -> ()
46+
br exit
47+
48+
exit:
49+
%retval = tuple ()
50+
return %retval : $()
51+
}
52+

0 commit comments

Comments
 (0)