Skip to content

Commit a2bb947

Browse files
authored
Merge pull request #22692 from gottesmm/pr-68be16c4231a0b3388186514417e7c2f4dc8ca27
[sil-combine] When simplifying convert functions, base whether or not…
2 parents bf909ca + 4d90cdd commit a2bb947

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -531,18 +531,20 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
531531
}
532532

533533
// Create the new apply inst.
534-
SILInstruction *NAI;
535-
if (auto *TAI = dyn_cast<TryApplyInst>(AI))
536-
NAI = Builder.createTryApply(AI.getLoc(), FRI,
537-
SubstitutionMap(), Args,
538-
TAI->getNormalBB(), TAI->getErrorBB());
539-
else {
540-
NAI = Builder.createApply(AI.getLoc(), FRI, SubstitutionMap(), Args,
541-
cast<ApplyInst>(AI)->isNonThrowing());
542-
assert(FullApplySite::isa(NAI).getSubstCalleeType()->getAllResultsType() ==
543-
AI.getSubstCalleeType()->getAllResultsType() &&
544-
"Function types should be the same");
534+
if (auto *TAI = dyn_cast<TryApplyInst>(AI)) {
535+
return Builder.createTryApply(AI.getLoc(), FRI, SubstitutionMap(), Args,
536+
TAI->getNormalBB(), TAI->getErrorBB());
545537
}
538+
539+
// Match the throwing bit of the underlying function_ref. We assume that if
540+
// we got this far it is legal to perform the transformation (since
541+
// otherwise, we would be creating malformed SIL).
542+
bool setNonThrowing = FRI->getFunctionType()->hasErrorResult();
543+
SILInstruction *NAI = Builder.createApply(AI.getLoc(), FRI, SubstitutionMap(),
544+
Args, setNonThrowing);
545+
assert(FullApplySite::isa(NAI).getSubstCalleeType()->getAllResultsType() ==
546+
AI.getSubstCalleeType()->getAllResultsType() &&
547+
"Function types should be the same");
546548
return NAI;
547549
}
548550

test/SILOptimizer/sil_combine_apply.sil

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ protocol SwiftP {
1515
func foo()
1616
}
1717

18+
class Klass {}
19+
1820
/////////////////////////////////
1921
// Tests for SILCombinerApply. //
2022
/////////////////////////////////
@@ -636,3 +638,37 @@ bb99:
636638
%t = tuple()
637639
return %t : $()
638640
}
641+
642+
sil @convert_function_simplification_callee : $@convention(thin) (@guaranteed Klass) -> () {
643+
bb0(%0 : $Klass):
644+
%9999 = tuple()
645+
return %9999 : $()
646+
}
647+
648+
sil @convert_function_simplification_callee_with_error : $@convention(thin) (@guaranteed Klass) -> @error Error {
649+
bb0(%0 : $Klass):
650+
%9999 = tuple()
651+
return %9999 : $()
652+
}
653+
654+
// CHECK-LABEL: sil @convert_function_simplification_caller : $@convention(thin) (@guaranteed Klass) -> () {
655+
// CHECK: [[FUNC:%.*]] = function_ref @convert_function_simplification_callee : $@convention(thin) (@guaranteed Klass) -> ()
656+
// CHECK: apply [[FUNC]]({{.*}}) : $@convention(thin) (@guaranteed Klass) -> ()
657+
// CHECK: [[FUNC:%.*]] = function_ref @convert_function_simplification_callee_with_error : $@convention(thin) (@guaranteed Klass) -> @error Error
658+
// CHECK: apply [nothrow] [[FUNC]]({{.*}}) : $@convention(thin) (@guaranteed Klass) -> @error Error
659+
// CHECK: } // end sil function 'convert_function_simplification_caller'
660+
sil @convert_function_simplification_caller : $@convention(thin) (@guaranteed Klass) -> () {
661+
bb0(%0 : $Klass):
662+
%1 = function_ref @convert_function_simplification_callee : $@convention(thin) (@guaranteed Klass) -> ()
663+
%2 = thin_to_thick_function %1 : $@convention(thin) (@guaranteed Klass) -> () to $@callee_guaranteed (@guaranteed Klass) -> ()
664+
%3 = convert_function %2 : $@callee_guaranteed (@guaranteed Klass) -> () to $@callee_guaranteed (@guaranteed Klass) -> @error Error
665+
%4 = apply [nothrow] %3(%0) : $@callee_guaranteed (@guaranteed Klass) -> @error Error
666+
667+
%5 = function_ref @convert_function_simplification_callee_with_error : $@convention(thin) (@guaranteed Klass) -> @error Error
668+
%6 = thin_to_thick_function %5 : $@convention(thin) (@guaranteed Klass) -> @error Error to $@callee_guaranteed (@guaranteed Klass) -> @error Error
669+
%7 = convert_function %6 : $@callee_guaranteed (@guaranteed Klass) -> @error Error to $@callee_guaranteed (@guaranteed Klass) -> @error Error
670+
%8 = apply [nothrow] %7(%0) : $@callee_guaranteed (@guaranteed Klass) -> @error Error
671+
672+
%9999 = tuple()
673+
return %9999 : $()
674+
}

test/SILOptimizer/stack_promotion.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ bb0(%0 : $Int, %closure: $@callee_owned (@inout Int) -> (@out (), @error Error))
624624
// pass the array to the withUnsafeMutableBufferPointer closure.
625625
%21 = function_ref @withUnsafeMutableBufferPointer : $@convention(method) (@owned @callee_owned (@inout Int) -> (@out (), @error Error), @inout Array<Int>) -> (@out (), @error Error)
626626
%22 = alloc_stack $()
627-
%23 = apply [nothrow]%21(%22, %closure, %the_array) : $@convention(method) (@owned @callee_owned (@inout Int) -> (@out (), @error Error), @inout Array<Int>) -> (@out (), @error Error)
627+
%23 = apply [nothrow] %21(%22, %closure, %the_array) : $@convention(method) (@owned @callee_owned (@inout Int) -> (@out (), @error Error), @inout Array<Int>) -> (@out (), @error Error)
628628
dealloc_stack %22 : $*()
629629

630630
dealloc_stack %the_array: $*Array<Int>

0 commit comments

Comments
 (0)