Skip to content

[sil-combine] When simplifying convert functions, base whether or not… #22692

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
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
24 changes: 13 additions & 11 deletions lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,18 +531,20 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
}

// Create the new apply inst.
SILInstruction *NAI;
if (auto *TAI = dyn_cast<TryApplyInst>(AI))
NAI = Builder.createTryApply(AI.getLoc(), FRI,
SubstitutionMap(), Args,
TAI->getNormalBB(), TAI->getErrorBB());
else {
NAI = Builder.createApply(AI.getLoc(), FRI, SubstitutionMap(), Args,
cast<ApplyInst>(AI)->isNonThrowing());
assert(FullApplySite::isa(NAI).getSubstCalleeType()->getAllResultsType() ==
AI.getSubstCalleeType()->getAllResultsType() &&
"Function types should be the same");
if (auto *TAI = dyn_cast<TryApplyInst>(AI)) {
return Builder.createTryApply(AI.getLoc(), FRI, SubstitutionMap(), Args,
TAI->getNormalBB(), TAI->getErrorBB());
}

// Match the throwing bit of the underlying function_ref. We assume that if
// we got this far it is legal to perform the transformation (since
// otherwise, we would be creating malformed SIL).
bool setNonThrowing = FRI->getFunctionType()->hasErrorResult();
SILInstruction *NAI = Builder.createApply(AI.getLoc(), FRI, SubstitutionMap(),
Args, setNonThrowing);
assert(FullApplySite::isa(NAI).getSubstCalleeType()->getAllResultsType() ==
AI.getSubstCalleeType()->getAllResultsType() &&
"Function types should be the same");
return NAI;
}

Expand Down
36 changes: 36 additions & 0 deletions test/SILOptimizer/sil_combine_apply.sil
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ protocol SwiftP {
func foo()
}

class Klass {}

/////////////////////////////////
// Tests for SILCombinerApply. //
/////////////////////////////////
Expand Down Expand Up @@ -636,3 +638,37 @@ bb99:
%t = tuple()
return %t : $()
}

sil @convert_function_simplification_callee : $@convention(thin) (@guaranteed Klass) -> () {
bb0(%0 : $Klass):
%9999 = tuple()
return %9999 : $()
}

sil @convert_function_simplification_callee_with_error : $@convention(thin) (@guaranteed Klass) -> @error Error {
bb0(%0 : $Klass):
%9999 = tuple()
return %9999 : $()
}

// CHECK-LABEL: sil @convert_function_simplification_caller : $@convention(thin) (@guaranteed Klass) -> () {
// CHECK: [[FUNC:%.*]] = function_ref @convert_function_simplification_callee : $@convention(thin) (@guaranteed Klass) -> ()
// CHECK: apply [[FUNC]]({{.*}}) : $@convention(thin) (@guaranteed Klass) -> ()
// CHECK: [[FUNC:%.*]] = function_ref @convert_function_simplification_callee_with_error : $@convention(thin) (@guaranteed Klass) -> @error Error
// CHECK: apply [nothrow] [[FUNC]]({{.*}}) : $@convention(thin) (@guaranteed Klass) -> @error Error
// CHECK: } // end sil function 'convert_function_simplification_caller'
sil @convert_function_simplification_caller : $@convention(thin) (@guaranteed Klass) -> () {
bb0(%0 : $Klass):
%1 = function_ref @convert_function_simplification_callee : $@convention(thin) (@guaranteed Klass) -> ()
%2 = thin_to_thick_function %1 : $@convention(thin) (@guaranteed Klass) -> () to $@callee_guaranteed (@guaranteed Klass) -> ()
%3 = convert_function %2 : $@callee_guaranteed (@guaranteed Klass) -> () to $@callee_guaranteed (@guaranteed Klass) -> @error Error
%4 = apply [nothrow] %3(%0) : $@callee_guaranteed (@guaranteed Klass) -> @error Error

%5 = function_ref @convert_function_simplification_callee_with_error : $@convention(thin) (@guaranteed Klass) -> @error Error
%6 = thin_to_thick_function %5 : $@convention(thin) (@guaranteed Klass) -> @error Error to $@callee_guaranteed (@guaranteed Klass) -> @error Error
%7 = convert_function %6 : $@callee_guaranteed (@guaranteed Klass) -> @error Error to $@callee_guaranteed (@guaranteed Klass) -> @error Error
%8 = apply [nothrow] %7(%0) : $@callee_guaranteed (@guaranteed Klass) -> @error Error

%9999 = tuple()
return %9999 : $()
}
2 changes: 1 addition & 1 deletion test/SILOptimizer/stack_promotion.sil
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ bb0(%0 : $Int, %closure: $@callee_owned (@inout Int) -> (@out (), @error Error))
// pass the array to the withUnsafeMutableBufferPointer closure.
%21 = function_ref @withUnsafeMutableBufferPointer : $@convention(method) (@owned @callee_owned (@inout Int) -> (@out (), @error Error), @inout Array<Int>) -> (@out (), @error Error)
%22 = alloc_stack $()
%23 = apply [nothrow]%21(%22, %closure, %the_array) : $@convention(method) (@owned @callee_owned (@inout Int) -> (@out (), @error Error), @inout Array<Int>) -> (@out (), @error Error)
%23 = apply [nothrow] %21(%22, %closure, %the_array) : $@convention(method) (@owned @callee_owned (@inout Int) -> (@out (), @error Error), @inout Array<Int>) -> (@out (), @error Error)
dealloc_stack %22 : $*()

dealloc_stack %the_array: $*Array<Int>
Expand Down