Skip to content

Commit 4d90cdd

Browse files
committed
[sil-combine] When simplifying convert functions, base whether or not an apply has nothrow on the actual function_ref.
Previously, we based it off of whether or not the original apply had a nothrow bit. This is incorrect in the case where we added an error result to a function without an error result. In such a case, we need to /not/ put on the nothrow bit. This commit generalizes this idea slightly by assuming that if we are asked to perform this transformation we should just match what the underlying function_ref (i.e. setting nothrow if the underlying function type has an error result and not setting nothrow if the underlying function type does not have an error result). rdar://47828439
1 parent b00b18b commit 4d90cdd

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
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+
}

0 commit comments

Comments
 (0)