Skip to content

Commit b795228

Browse files
authored
Merge pull request #79644 from meg-gupta/arraypropoptfix
Fix hoisting array semantics call with non-dominant self in ossa
2 parents b52d154 + 199482a commit b795228

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,35 @@ bb0(%0: @owned $MyArray<MyClass>):
2626
unreachable
2727
}
2828

29+
// CHECK-LABEL: sil [ossa] @load_within_loop :
30+
// CHECK: bb1:
31+
// CHECK: [[FUNC1:%.*]] = function_ref @arrayPropertyIsNative
32+
// CHECK: apply [[FUNC1]]
33+
// CHECK: cond_br {{.*}}
34+
// CHECK: bb2:
35+
// CHECK: br bb3
36+
// CHECK: bb3:
37+
// CHECK: [[FUNC2:%.*]] = function_ref @arrayPropertyIsNative
38+
// CHECK: apply [[FUNC2]]
39+
// CHECK-LABEL: } // end sil function 'load_within_loop'
40+
sil [ossa] @load_within_loop : $@convention(thin) (@inout MyArray<MyClass>, @inout MyBool) -> MyBool {
41+
bb0(%0 : $*MyArray<MyClass>, %1 : $*MyBool):
42+
br bb1
43+
44+
bb1:
45+
%3 = load [copy] %0 : $*MyArray<MyClass>
46+
%4 = load [trivial] %1 : $*MyBool
47+
%2 = function_ref @arrayPropertyIsNative : $@convention(method) (@owned MyArray<MyClass>) -> Bool
48+
%5 = apply %2(%3) : $@convention(method) (@owned MyArray<MyClass>) -> Bool
49+
cond_br undef, bb2, bb3
50+
51+
bb2:
52+
br bb1
53+
54+
bb3:
55+
return %4 : $MyBool
56+
}
57+
2958
// CHECK-LABEL: sil [ossa] @load_and_copy_within_loop :
3059
// CHECK: bb1:
3160
// CHECK: [[FUNC1:%.*]] = function_ref @arrayPropertyIsNative

0 commit comments

Comments
 (0)