Skip to content

Commit 9cfe91a

Browse files
authored
Merge pull request #30877 from dan-zheng/SR-12548-fix
[SILOptimizer] Disable `SILCombiner::visitPartialApplyInst` for method callees.
2 parents 0340e9a + 606c070 commit 9cfe91a

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ SILInstruction *SILCombiner::visitPartialApplyInst(PartialApplyInst *PAI) {
8383
return nullptr;
8484

8585
// partial_apply without any substitutions or arguments is just a
86-
// thin_to_thick_function.
87-
if (!PAI->hasSubstitutions() && (PAI->getNumArguments() == 0)) {
86+
// thin_to_thick_function. thin_to_thick_function supports only thin operands.
87+
if (!PAI->hasSubstitutions() && (PAI->getNumArguments() == 0) &&
88+
PAI->getSubstCalleeType()->getRepresentation() ==
89+
SILFunctionTypeRepresentation::Thin) {
8890
if (!PAI->isOnStack())
8991
return Builder.createThinToThickFunction(PAI->getLoc(), PAI->getCallee(),
9092
PAI->getType());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-build-swift -O %s
2+
3+
// SR-12548: SIL verification error regarding
4+
// `CapturePropagation::rewritePartialApply` for `partial_apply` with
5+
// `@convention(method)` callee.
6+
7+
import _Differentiation
8+
9+
protocol Protocol: Differentiable {
10+
@differentiable
11+
func method() -> Self
12+
}
13+
14+
extension Protocol {
15+
@differentiable
16+
func method() -> Self { self }
17+
}
18+
19+
struct Struct: Protocol {}
20+
21+
let _: @differentiable (Struct) -> Struct = { $0.method() }
22+
23+
// SIL verification failed: operand of thin_to_thick_function must be thin: opFTy->getRepresentation() == SILFunctionType::Representation::Thin
24+
// Verifying instruction:
25+
// // function_ref specialized Protocol.method()
26+
// %5 = function_ref @$s7crasher8ProtocolPAAE6methodxyFAA6StructV_TG5 : $@convention(method) (@in_guaranteed Struct) -> @out Struct // user: %6
27+
// -> %6 = thin_to_thick_function %5 : $@convention(method) (@in_guaranteed Struct) -> @out Struct to $@callee_guaranteed (@in_guaranteed Struct) -> @out Struct // user: %11

test/SILOptimizer/sil_combine_apply.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,3 +974,20 @@ bb0(%0: $*FakeProtocol):
974974
%r = tuple ()
975975
return %r : $()
976976
}
977+
978+
// Test `partial_apply` with a `@convention(method)` callee.
979+
// Rewriting `partial_apply` to `thin_to_thick_function` is not okay because
980+
// `thin_to_thick_function` supports only `@convention(thin)` operands.
981+
982+
sil @method : $@convention(method) (Int32) -> ()
983+
984+
sil @test_partial_apply_method : $@convention(thin) () -> @owned @callee_owned (Int32) -> () {
985+
%1 = function_ref @method : $@convention(method) (Int32) -> ()
986+
%2 = partial_apply %1() : $@convention(method) (Int32) -> ()
987+
return %2 : $@callee_owned (Int32) -> ()
988+
}
989+
990+
// CHECK-LABEL: sil @test_partial_apply_method
991+
// CHECK: [[FN:%.*]] = function_ref @method
992+
// CHECK-NEXT: partial_apply [[FN]]()
993+
// CHECK-NEXT: return

0 commit comments

Comments
 (0)