Skip to content

Fix hoisting array semantics call with non-dominant self in ossa #79644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/SILOptimizer/Analysis/ArraySemantic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ static bool canHoistArrayArgument(ApplyInst *SemanticsCall, SILValue Arr,
if (DT->dominates(SelfBB, InsertBefore->getParent()))
return true;

// If the self value does not dominate the new insertion point,
// we have to clone the self value as well.
// If we have a semantics call that does not consume the self value, then
// there will be consuming users within the loop, since we don't have support
// for creating the consume for the self value in the new insertion point,
// bailout hoisiting in this case.
if (SemanticsCall->getFunction()->hasOwnership() &&
Convention == ParameterConvention::Direct_Guaranteed) {
return false;
}

if (auto *Copy = dyn_cast<CopyValueInst>(SelfVal)) {
// look through one level
SelfVal = Copy->getOperand();
Expand Down
32 changes: 32 additions & 0 deletions test/SILOptimizer/array_property_opt.sil
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ bb0(%0: $MyArray<MyClass>):
unreachable
}

sil public_external [_semantics "array.props.isNativeTypeChecked"] @arrayPropertyIsNativeGuaranteed : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool {
bb0(%0: $MyArray<MyClass>):
unreachable
}

// Make sure we can handle try_apply when splitting edges. This test used to crash.

sil @throwing_fun : $@convention(thin) () -> (MyBool, @error any Error)
Expand Down Expand Up @@ -409,3 +414,30 @@ bb12(%69 : $Builtin.Int64):
%70 = struct $MyInt (%69 : $Builtin.Int64)
return %70 : $MyInt
}

// CHECK-LABEL: sil @load_copy_within_loop :
// CHECK: [[FUNC1:%.*]] = function_ref @arrayPropertyIsNativeGuaranteed : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
// CHECK: apply [[FUNC1]]
// CHECK: [[FUNC2:%.*]] = function_ref @arrayPropertyIsNativeGuaranteed : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
// CHECK: apply [[FUNC2]]
// CHECK-LABEL: } // end sil function 'load_copy_within_loop'
sil @load_copy_within_loop : $@convention(thin) (@inout MyArray<MyClass>, @inout MyBool) -> MyBool {
bb0(%0 : $*MyArray<MyClass>, %1 : $*MyBool):
br bb1

bb1:
%3 = load %0
retain_value %3
%5 = load %1
%6 = function_ref @arrayPropertyIsNativeGuaranteed : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
%7 = apply %6(%3) : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
release_value %3
cond_br undef, bb2, bb3

bb2:
br bb1

bb3:
return %5
}

50 changes: 50 additions & 0 deletions test/SILOptimizer/array_property_opt_ossa_guaranteed.sil
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,56 @@ bb0(%0: @guaranteed $MyArray<MyClass>):
unreachable
}

// CHECK-LABEL: sil [ossa] @load_copy_within_loop :
// CHECK: [[FUNC:%.*]] = function_ref @arrayPropertyIsNative
// CHECK: apply [[FUNC]]
// CHECK-NOT: function_ref @arrayPropertyIsNative
// CHECK-NOT: apply [[FUNC]]
// CHECK-LABEL: } // end sil function 'load_copy_within_loop'
sil [ossa] @load_copy_within_loop : $@convention(thin) (@inout MyArray<MyClass>, @inout MyBool) -> MyBool {
bb0(%0 : $*MyArray<MyClass>, %1 : $*MyBool):
br bb1

bb1:
%3 = load [copy] %0 : $*MyArray<MyClass>
%4 = load [trivial] %1 : $*MyBool
%2 = function_ref @arrayPropertyIsNative : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
%5 = apply %2(%3) : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
destroy_value %3 : $MyArray<MyClass>
cond_br undef, bb2, bb3

bb2:
br bb1

bb3:
return %4 : $MyBool
}

// CHECK-LABEL: sil [ossa] @load_borrow_within_loop :
// CHECK: [[FUNC:%.*]] = function_ref @arrayPropertyIsNative
// CHECK: apply [[FUNC]]
// CHECK-NOT: function_ref @arrayPropertyIsNative
// CHECK-NOT: apply [[FUNC]]
// CHECK-LABEL: } // end sil function 'load_borrow_within_loop'
sil [ossa] @load_borrow_within_loop : $@convention(thin) (@inout MyArray<MyClass>, @inout MyBool) -> MyBool {
bb0(%0 : $*MyArray<MyClass>, %1 : $*MyBool):
br bb1

bb1:
%3 = load_borrow %0 : $*MyArray<MyClass>
%4 = load [trivial] %1 : $*MyBool
%2 = function_ref @arrayPropertyIsNative : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
%5 = apply %2(%3) : $@convention(method) (@guaranteed MyArray<MyClass>) -> Bool
end_borrow %3 : $MyArray<MyClass>
cond_br undef, bb2, bb3

bb2:
br bb1

bb3:
return %4 : $MyBool
}

// CHECK-LABEL: sil [ossa] @load_and_copy_within_loop :
// CHECK: bb1:
// CHECK: [[FUNC1:%.*]] = function_ref @arrayPropertyIsNative
Expand Down
29 changes: 29 additions & 0 deletions test/SILOptimizer/array_property_opt_ossa_owned.sil
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ bb0(%0: @owned $MyArray<MyClass>):
unreachable
}

// CHECK-LABEL: sil [ossa] @load_within_loop :
// CHECK: bb1:
// CHECK: [[FUNC1:%.*]] = function_ref @arrayPropertyIsNative
// CHECK: apply [[FUNC1]]
// CHECK: cond_br {{.*}}
// CHECK: bb2:
// CHECK: br bb3
// CHECK: bb3:
// CHECK: [[FUNC2:%.*]] = function_ref @arrayPropertyIsNative
// CHECK: apply [[FUNC2]]
// CHECK-LABEL: } // end sil function 'load_within_loop'
sil [ossa] @load_within_loop : $@convention(thin) (@inout MyArray<MyClass>, @inout MyBool) -> MyBool {
bb0(%0 : $*MyArray<MyClass>, %1 : $*MyBool):
br bb1

bb1:
%3 = load [copy] %0 : $*MyArray<MyClass>
%4 = load [trivial] %1 : $*MyBool
%2 = function_ref @arrayPropertyIsNative : $@convention(method) (@owned MyArray<MyClass>) -> Bool
%5 = apply %2(%3) : $@convention(method) (@owned MyArray<MyClass>) -> Bool
cond_br undef, bb2, bb3

bb2:
br bb1

bb3:
return %4 : $MyBool
}

// CHECK-LABEL: sil [ossa] @load_and_copy_within_loop :
// CHECK: bb1:
// CHECK: [[FUNC1:%.*]] = function_ref @arrayPropertyIsNative
Expand Down