Skip to content

[arc] An apply of a callee_guaranteed thick function is a "guaranteed… #14833

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
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
14 changes: 11 additions & 3 deletions lib/SILOptimizer/Analysis/ARCAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,16 +441,24 @@ mayGuaranteedUseValue(SILInstruction *User, SILValue Ptr, AliasAnalysis *AA) {

FullApplySite FAS(User);

// Ok, we have a full apply site. If the apply has no arguments, we don't need
// to worry about any guaranteed parameters.
// Ok, we have a full apply site. Check if the callee is callee_guaranteed. In
// such a case, if we can not prove no alias, we need to be conservative and
// return true.
CanSILFunctionType FType = FAS.getSubstCalleeType();
if (FType->isCalleeGuaranteed() && !AA->isNoAlias(FAS.getCallee(), Ptr)) {
return true;
}

// Ok, we have a full apply site and our callee is a normal use. Thus if the
// apply does not have any normal arguments, we don't need to worry about any
// guaranteed parameters and return early.
if (!FAS.getNumArguments())
return false;

// Ok, we have an apply site with arguments. Look at the function type and
// iterate through the function parameters. If any of the parameters are
// guaranteed, attempt to prove that the passed in parameter cannot alias
// Ptr. If we fail, return true.
CanSILFunctionType FType = FAS.getSubstCalleeType();
auto Params = FType->getParameters();
for (unsigned i : indices(Params)) {
if (!Params[i].isGuaranteed())
Expand Down
17 changes: 17 additions & 0 deletions test/SILOptimizer/arcsequenceopts.sil
Original file line number Diff line number Diff line change
Expand Up @@ -2146,3 +2146,20 @@ bb0(%0 : $*Builtin.NativeObject):
strong_release %1 : $Builtin.NativeObject
return undef : $()
}

// Make sure that we treat applications of callee_guaranteed functions as a
// guaranteed use of the function object.
//
// CHECK-LABEL: sil @test_callee_guaranteed_is_treated_as_guaranteed : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> () {
// CHECK: strong_retain
// CHECK: apply
// CHECK: strong_release
// CHECK: } // end sil function 'test_callee_guaranteed_is_treated_as_guaranteed'
sil @test_callee_guaranteed_is_treated_as_guaranteed : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> () {
bb0(%0 : $@callee_guaranteed () -> ()):
strong_retain %0 : $@callee_guaranteed () -> ()
apply %0() : $@callee_guaranteed () -> ()
strong_release %0 : $@callee_guaranteed () -> ()
%9999 = tuple()
return %9999 : $()
}