Skip to content

[SIL] Fix bridged begin_apply results. #80555

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
merged 1 commit into from
Apr 7, 2025
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
5 changes: 3 additions & 2 deletions SwiftCompilerSources/Sources/SIL/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1532,14 +1532,15 @@ final public class EndUnpairedAccessInst : Instruction {}

final public class BeginApplyInst : MultipleValueInstruction, FullApplySite {
public var numArguments: Int { bridged.BeginApplyInst_numArguments() }
public var isCalleeAllocated: Bool { bridged.BeginApplyInst_isCalleeAllocated() }

public var singleDirectResult: Value? { nil }
public var singleDirectErrorResult: Value? { nil }

public var token: Value { getResult(index: resultCount - 1) }
public var token: Value { getResult(index: resultCount - (isCalleeAllocated ? 2 : 1)) }

public var yieldedValues: Results {
Results(inst: self, numResults: resultCount - 1)
Results(inst: self, numResults: resultCount - (isCalleeAllocated ? 2 : 1))
}
}

Expand Down
1 change: 1 addition & 0 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ struct BridgedInstruction {
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSILTypeArray AllocRefInstBase_getTailAllocatedTypes() const;
BRIDGED_INLINE bool AllocRefDynamicInst_isDynamicTypeDeinitAndSizeKnownEquivalentToBaseType() const;
BRIDGED_INLINE SwiftInt BeginApplyInst_numArguments() const;
BRIDGED_INLINE bool BeginApplyInst_isCalleeAllocated() const;
BRIDGED_INLINE SwiftInt TryApplyInst_numArguments() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock BranchInst_getTargetBlock() const;
BRIDGED_INLINE SwiftInt SwitchEnumInst_getNumCases() const;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/SIL/SILBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,10 @@ SwiftInt BridgedInstruction::BeginApplyInst_numArguments() const {
return getAs<swift::BeginApplyInst>()->getNumArguments();
}

bool BridgedInstruction::BeginApplyInst_isCalleeAllocated() const {
return getAs<swift::BeginApplyInst>()->isCalleeAllocated();
}

SwiftInt BridgedInstruction::TryApplyInst_numArguments() const {
return getAs<swift::TryApplyInst>()->getNumArguments();
}
Expand Down
18 changes: 18 additions & 0 deletions test/SILOptimizer/copy-to-borrow-optimization.sil
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ sil @use_inguaranteed : $@convention(thin) (@in_guaranteed Klass) -> ()
sil @guaranteed_fakeoptional_klass_user : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> ()
sil @guaranteed_fakeoptional_classlet_user : $@convention(thin) (@guaranteed FakeOptional<ClassLet>) -> ()
sil @closure : $@convention(thin) (@inout_aliasable Klass) -> ()
sil @lendC : $@yield_once_2 @convention(thin) () -> @yields @guaranteed C
sil @useC : $@convention(thin) (@guaranteed C) -> () {
[global:]
}
Expand Down Expand Up @@ -2202,3 +2203,20 @@ bb3:
%r = tuple ()
return %r
}


// CHECK-LABEL: sil [ossa] @keep_yield2ed_copy : {{.*}} {
// CHECK: copy_value
// CHECK: } // end sil function 'keep_yield2ed_copy'
sil [ossa] @keep_yield2ed_copy : $@convention(thin) () -> () {
%lendC = function_ref @lendC : $@yield_once_2 @convention(thin) () -> @yields @guaranteed C
(%c, %token, %alloc) = begin_apply %lendC() : $@yield_once_2 @convention(thin) () -> @yields @guaranteed C
%copy = copy_value %c : $C
end_apply %token as $()
dealloc_stack %alloc : $*Builtin.SILToken
%useC = function_ref @useC : $@convention(thin) (@guaranteed C) -> ()
apply %useC(%copy) : $@convention(thin) (@guaranteed C) -> ()
destroy_value %copy : $C
%retval = tuple ()
return %retval : $()
}