Skip to content

Commit 88fb10e

Browse files
Merge pull request #80589 from nate-chandler/cherrypick/release/6.2/rdar148744816
6.2: [SIL] Fix bridged begin_apply results.
2 parents 8975d1e + b7ca567 commit 88fb10e

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

SwiftCompilerSources/Sources/SIL/Instruction.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -1532,14 +1532,15 @@ final public class EndUnpairedAccessInst : Instruction {}
15321532

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

15361537
public var singleDirectResult: Value? { nil }
15371538
public var singleDirectErrorResult: Value? { nil }
15381539

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

15411542
public var yieldedValues: Results {
1542-
Results(inst: self, numResults: resultCount - 1)
1543+
Results(inst: self, numResults: resultCount - (isCalleeAllocated ? 2 : 1))
15431544
}
15441545
}
15451546

include/swift/SIL/SILBridging.h

+1
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ struct BridgedInstruction {
780780
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSILTypeArray AllocRefInstBase_getTailAllocatedTypes() const;
781781
BRIDGED_INLINE bool AllocRefDynamicInst_isDynamicTypeDeinitAndSizeKnownEquivalentToBaseType() const;
782782
BRIDGED_INLINE SwiftInt BeginApplyInst_numArguments() const;
783+
BRIDGED_INLINE bool BeginApplyInst_isCalleeAllocated() const;
783784
BRIDGED_INLINE SwiftInt TryApplyInst_numArguments() const;
784785
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock BranchInst_getTargetBlock() const;
785786
BRIDGED_INLINE SwiftInt SwitchEnumInst_getNumCases() const;

include/swift/SIL/SILBridgingImpl.h

+4
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,10 @@ SwiftInt BridgedInstruction::BeginApplyInst_numArguments() const {
13381338
return getAs<swift::BeginApplyInst>()->getNumArguments();
13391339
}
13401340

1341+
bool BridgedInstruction::BeginApplyInst_isCalleeAllocated() const {
1342+
return getAs<swift::BeginApplyInst>()->isCalleeAllocated();
1343+
}
1344+
13411345
SwiftInt BridgedInstruction::TryApplyInst_numArguments() const {
13421346
return getAs<swift::TryApplyInst>()->getNumArguments();
13431347
}

test/SILOptimizer/copy-to-borrow-optimization.sil

+18
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ sil @use_inguaranteed : $@convention(thin) (@in_guaranteed Klass) -> ()
8787
sil @guaranteed_fakeoptional_klass_user : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> ()
8888
sil @guaranteed_fakeoptional_classlet_user : $@convention(thin) (@guaranteed FakeOptional<ClassLet>) -> ()
8989
sil @closure : $@convention(thin) (@inout_aliasable Klass) -> ()
90+
sil @lendC : $@yield_once_2 @convention(thin) () -> @yields @guaranteed C
9091
sil @useC : $@convention(thin) (@guaranteed C) -> () {
9192
[global:]
9293
}
@@ -2202,3 +2203,20 @@ bb3:
22022203
%r = tuple ()
22032204
return %r
22042205
}
2206+
2207+
2208+
// CHECK-LABEL: sil [ossa] @keep_yield2ed_copy : {{.*}} {
2209+
// CHECK: copy_value
2210+
// CHECK: } // end sil function 'keep_yield2ed_copy'
2211+
sil [ossa] @keep_yield2ed_copy : $@convention(thin) () -> () {
2212+
%lendC = function_ref @lendC : $@yield_once_2 @convention(thin) () -> @yields @guaranteed C
2213+
(%c, %token, %alloc) = begin_apply %lendC() : $@yield_once_2 @convention(thin) () -> @yields @guaranteed C
2214+
%copy = copy_value %c : $C
2215+
end_apply %token as $()
2216+
dealloc_stack %alloc : $*Builtin.SILToken
2217+
%useC = function_ref @useC : $@convention(thin) (@guaranteed C) -> ()
2218+
apply %useC(%copy) : $@convention(thin) (@guaranteed C) -> ()
2219+
destroy_value %copy : $C
2220+
%retval = tuple ()
2221+
return %retval : $()
2222+
}

0 commit comments

Comments
 (0)