Skip to content

Commit 318cd6c

Browse files
committed
Fix hoisting array semantics call with non-dominant self
If we have a self value that does not dominate loop preheader, and the array semantics call does not consume the self value, that means there will be instructions that consume the self value with the loop. In ossa, we cannot hoist such semantic calls because there is no support for creating destroys for them in the preheader. Add a bailout to avoid the ownership error. rdar://145673368
1 parent 7aa1970 commit 318cd6c

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

lib/SILOptimizer/Analysis/ArraySemantic.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ static bool canHoistArrayArgument(ApplyInst *SemanticsCall, SILValue Arr,
297297
if (DT->dominates(SelfBB, InsertBefore->getParent()))
298298
return true;
299299

300+
// If the self value does not dominate the new insertion point,
301+
// we have to clone the self value as well.
302+
// If we have a semantics call that does not consume the self value, then
303+
// there will be consuming users within the loop, since we don't have support
304+
// for creating the consume for the self value in the new insertion point,
305+
// bailout hoisiting in this case.
306+
if (SemanticsCall->getFunction()->hasOwnership() &&
307+
Convention == ParameterConvention::Direct_Guaranteed) {
308+
return false;
309+
}
310+
300311
if (auto *Copy = dyn_cast<CopyValueInst>(SelfVal)) {
301312
// look through one level
302313
SelfVal = Copy->getOperand();

test/SILOptimizer/array_property_opt.sil

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ bb0(%0: $MyArray<MyClass>):
118118
unreachable
119119
}
120120

121+
sil public_external [_semantics "array.props.isNativeTypeChecked"] @arrayPropertyIsNativeGuaranteed : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool {
122+
bb0(%0: $MyArray<MyClass>):
123+
unreachable
124+
}
125+
121126
// Make sure we can handle try_apply when splitting edges. This test used to crash.
122127

123128
sil @throwing_fun : $@convention(thin) () -> (MyBool, @error any Error)
@@ -409,3 +414,30 @@ bb12(%69 : $Builtin.Int64):
409414
%70 = struct $MyInt (%69 : $Builtin.Int64)
410415
return %70 : $MyInt
411416
}
417+
418+
// CHECK-LABEL: sil @load_copy_within_loop :
419+
// CHECK: [[FUNC1:%.*]] = function_ref @arrayPropertyIsNativeGuaranteed : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
420+
// CHECK: apply [[FUNC1]]
421+
// CHECK: [[FUNC2:%.*]] = function_ref @arrayPropertyIsNativeGuaranteed : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
422+
// CHECK: apply [[FUNC2]]
423+
// CHECK-LABEL: } // end sil function 'load_copy_within_loop'
424+
sil @load_copy_within_loop : $@convention(thin) (@inout MyArray<MyClass>, @inout MyBool) -> MyBool {
425+
bb0(%0 : $*MyArray<MyClass>, %1 : $*MyBool):
426+
br bb1
427+
428+
bb1:
429+
%3 = load %0
430+
retain_value %3
431+
%5 = load %1
432+
%6 = function_ref @arrayPropertyIsNativeGuaranteed : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
433+
%7 = apply %6(%3) : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
434+
release_value %3
435+
cond_br undef, bb2, bb3
436+
437+
bb2:
438+
br bb1
439+
440+
bb3:
441+
return %5
442+
}
443+

test/SILOptimizer/array_property_opt_ossa_guaranteed.sil

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,56 @@ bb0(%0: @guaranteed $MyArray<MyClass>):
2626
unreachable
2727
}
2828

29+
// CHECK-LABEL: sil [ossa] @load_copy_within_loop :
30+
// CHECK: [[FUNC:%.*]] = function_ref @arrayPropertyIsNative
31+
// CHECK: apply [[FUNC]]
32+
// CHECK-NOT: function_ref @arrayPropertyIsNative
33+
// CHECK-NOT: apply [[FUNC]]
34+
// CHECK-LABEL: } // end sil function 'load_copy_within_loop'
35+
sil [ossa] @load_copy_within_loop : $@convention(thin) (@inout MyArray<MyClass>, @inout MyBool) -> MyBool {
36+
bb0(%0 : $*MyArray<MyClass>, %1 : $*MyBool):
37+
br bb1
38+
39+
bb1:
40+
%3 = load [copy] %0 : $*MyArray<MyClass>
41+
%4 = load [trivial] %1 : $*MyBool
42+
%2 = function_ref @arrayPropertyIsNative : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
43+
%5 = apply %2(%3) : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
44+
destroy_value %3 : $MyArray<MyClass>
45+
cond_br undef, bb2, bb3
46+
47+
bb2:
48+
br bb1
49+
50+
bb3:
51+
return %4 : $MyBool
52+
}
53+
54+
// CHECK-LABEL: sil [ossa] @load_borrow_within_loop :
55+
// CHECK: [[FUNC:%.*]] = function_ref @arrayPropertyIsNative
56+
// CHECK: apply [[FUNC]]
57+
// CHECK-NOT: function_ref @arrayPropertyIsNative
58+
// CHECK-NOT: apply [[FUNC]]
59+
// CHECK-LABEL: } // end sil function 'load_borrow_within_loop'
60+
sil [ossa] @load_borrow_within_loop : $@convention(thin) (@inout MyArray<MyClass>, @inout MyBool) -> MyBool {
61+
bb0(%0 : $*MyArray<MyClass>, %1 : $*MyBool):
62+
br bb1
63+
64+
bb1:
65+
%3 = load_borrow %0 : $*MyArray<MyClass>
66+
%4 = load [trivial] %1 : $*MyBool
67+
%2 = function_ref @arrayPropertyIsNative : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
68+
%5 = apply %2(%3) : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
69+
end_borrow %3 : $MyArray<MyClass>
70+
cond_br undef, bb2, bb3
71+
72+
bb2:
73+
br bb1
74+
75+
bb3:
76+
return %4 : $MyBool
77+
}
78+
2979
// CHECK-LABEL: sil [ossa] @load_and_copy_within_loop :
3080
// CHECK: bb1:
3181
// CHECK: [[FUNC1:%.*]] = function_ref @arrayPropertyIsNative

test/SILOptimizer/array_property_opt_ossa_owned.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ bb0(%0: @owned $MyArray<MyClass>):
2626
unreachable
2727
}
2828

29+
sil [ossa] @load_within_loop : $@convention(thin) (@inout MyArray<MyClass>, @inout MyBool) -> MyBool {
30+
bb0(%0 : $*MyArray<MyClass>, %1 : $*MyBool):
31+
br bb1
32+
33+
bb1:
34+
%3 = load [copy] %0 : $*MyArray<MyClass>
35+
%4 = load [trivial] %1 : $*MyBool
36+
%2 = function_ref @arrayPropertyIsNative : $@convention(method) (@owned MyArray<MyClass>) -> Bool
37+
%5 = apply %2(%3) : $@convention(method) (@owned MyArray<MyClass>) -> Bool
38+
cond_br undef, bb2, bb3
39+
40+
bb2:
41+
br bb1
42+
43+
bb3:
44+
return %4 : $MyBool
45+
}
46+
2947
// CHECK-LABEL: sil [ossa] @load_and_copy_within_loop :
3048
// CHECK: bb1:
3149
// CHECK: [[FUNC1:%.*]] = function_ref @arrayPropertyIsNative

0 commit comments

Comments
 (0)