Skip to content
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
6 changes: 4 additions & 2 deletions lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ SILInstruction *SILCombiner::visitPartialApplyInst(PartialApplyInst *PAI) {
return nullptr;

// partial_apply without any substitutions or arguments is just a
// thin_to_thick_function.
if (!PAI->hasSubstitutions() && (PAI->getNumArguments() == 0)) {
// thin_to_thick_function. thin_to_thick_function supports only thin operands.
if (!PAI->hasSubstitutions() && (PAI->getNumArguments() == 0) &&
PAI->getSubstCalleeType()->getRepresentation() ==
SILFunctionTypeRepresentation::Thin) {
if (!PAI->isOnStack())
return Builder.createThinToThickFunction(PAI->getLoc(), PAI->getCallee(),
PAI->getType());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-build-swift -O %s

// SR-12548: SIL verification error regarding
// `CapturePropagation::rewritePartialApply` for `partial_apply` with
// `@convention(method)` callee.

import _Differentiation

protocol Protocol: Differentiable {
@differentiable
func method() -> Self
}

extension Protocol {
@differentiable
func method() -> Self { self }
}

struct Struct: Protocol {}

let _: @differentiable (Struct) -> Struct = { $0.method() }

// SIL verification failed: operand of thin_to_thick_function must be thin: opFTy->getRepresentation() == SILFunctionType::Representation::Thin
// Verifying instruction:
// // function_ref specialized Protocol.method()
// %5 = function_ref @$s7crasher8ProtocolPAAE6methodxyFAA6StructV_TG5 : $@convention(method) (@in_guaranteed Struct) -> @out Struct // user: %6
// -> %6 = thin_to_thick_function %5 : $@convention(method) (@in_guaranteed Struct) -> @out Struct to $@callee_guaranteed (@in_guaranteed Struct) -> @out Struct // user: %11
17 changes: 17 additions & 0 deletions test/SILOptimizer/sil_combine_apply.sil
Original file line number Diff line number Diff line change
Expand Up @@ -974,3 +974,20 @@ bb0(%0: $*FakeProtocol):
%r = tuple ()
return %r : $()
}

// Test `partial_apply` with a `@convention(method)` callee.
// Rewriting `partial_apply` to `thin_to_thick_function` is not okay because
// `thin_to_thick_function` supports only `@convention(thin)` operands.

sil @method : $@convention(method) (Int32) -> ()

sil @test_partial_apply_method : $@convention(thin) () -> @owned @callee_owned (Int32) -> () {
%1 = function_ref @method : $@convention(method) (Int32) -> ()
%2 = partial_apply %1() : $@convention(method) (Int32) -> ()
return %2 : $@callee_owned (Int32) -> ()
}

// CHECK-LABEL: sil @test_partial_apply_method
// CHECK: [[FN:%.*]] = function_ref @method
// CHECK-NEXT: partial_apply [[FN]]()
// CHECK-NEXT: return