Skip to content

Commit 77a9433

Browse files
committed
Revert "Revert "Simplify lifetime extension for async ObjC calls.""
This reverts commit 8441e07.
1 parent 3e8d153 commit 77a9433

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

lib/SILGen/ManagedValue.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ ManagedValue ManagedValue::copy(SILGenFunction &SGF, SILLocation loc) const {
5858

5959
// Emit an unmanaged copy of this value
6060
// WARNING: Callers of this API should manage the cleanup of this value!
61-
ManagedValue ManagedValue::unmanagedCopy(SILGenFunction &SGF,
61+
SILValue ManagedValue::unmanagedCopy(SILGenFunction &SGF,
6262
SILLocation loc) const {
6363
auto &lowering = SGF.getTypeLowering(getType());
6464
if (lowering.isTrivial())
65-
return *this;
65+
return getValue();
6666

6767
if (getType().isObject()) {
6868
auto copy = SGF.B.emitCopyValueOperation(loc, getValue());
69-
return ManagedValue::forUnmanaged(copy);
69+
return copy;
7070
}
7171

7272
SILValue buf = SGF.emitTemporaryAllocation(loc, getType());
7373
SGF.B.createCopyAddr(loc, getValue(), buf, IsNotTake, IsInitialization);
74-
return ManagedValue::forUnmanaged(buf);
74+
return buf;
7575
}
7676

7777
/// Emit a copy of this value with independent ownership.

lib/SILGen/ManagedValue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class ManagedValue {
280280

281281
/// Returns an unmanaged copy of this value.
282282
/// WARNING: Callers of this API should manage the cleanup of this value!
283-
ManagedValue unmanagedCopy(SILGenFunction &SGF, SILLocation loc) const;
283+
SILValue unmanagedCopy(SILGenFunction &SGF, SILLocation loc) const;
284284

285285
/// Emit a copy of this value with independent ownership into the current
286286
/// formal evaluation scope.

lib/SILGen/SILGenApply.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4586,7 +4586,7 @@ RValue SILGenFunction::emitApply(
45864586
// generates `await_async_continuation`.
45874587
// Lifetime is extended by creating unmanaged copies here and by pushing the
45884588
// cleanups required just before the result plan is generated.
4589-
SmallVector<ManagedValue, 8> unmanagedCopies;
4589+
SmallVector<SILValue, 8> unmanagedCopies;
45904590
if (calleeTypeInfo.foreign.async) {
45914591
for (auto arg : args) {
45924592
if (arg.hasCleanup()) {
@@ -4673,29 +4673,22 @@ RValue SILGenFunction::emitApply(
46734673
*foreignError, calleeTypeInfo.foreign.async);
46744674
}
46754675

4676-
// For objc async calls, push cleanup to be used on throw paths in the result
4677-
// planner.
4678-
for (unsigned i : indices(unmanagedCopies)) {
4679-
SILValue value = unmanagedCopies[i].getValue();
4680-
Cleanups.pushCleanup<FixLifetimeDestroyCleanup>(value);
4681-
unmanagedCopies[i] = ManagedValue(value, Cleanups.getTopCleanup());
4676+
// For objc async calls, push cleanup to be used on
4677+
// both result and throw paths prior to finishing the result plan.
4678+
if (calleeTypeInfo.foreign.async) {
4679+
for (auto unmanagedCopy : unmanagedCopies) {
4680+
Cleanups.pushCleanup<FixLifetimeDestroyCleanup>(unmanagedCopy);
4681+
}
4682+
} else {
4683+
assert(unmanagedCopies.empty());
46824684
}
46834685

46844686
auto directResultsArray = makeArrayRef(directResults);
46854687
RValue result = resultPlan->finish(*this, loc, substResultType,
46864688
directResultsArray, bridgedForeignError);
46874689
assert(directResultsArray.empty() && "didn't claim all direct results");
46884690

4689-
// For objc async calls, generate cleanup on the resume path here and forward
4690-
// the previously pushed cleanups.
46914691
if (calleeTypeInfo.foreign.async) {
4692-
for (auto unmanagedCopy : unmanagedCopies) {
4693-
auto value = unmanagedCopy.forward(*this);
4694-
B.emitFixLifetime(loc, value);
4695-
B.emitDestroyOperation(loc, value);
4696-
}
4697-
4698-
// hop back to the current executor
46994692
breadcrumb.emit(*this, loc);
47004693
}
47014694

test/SILGen/objc_async.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ func testSlowServerFromMain(slowServer: SlowServer) async throws {
195195
// CHECK: await_async_continuation [[CONT]] {{.*}}, resume [[RESUME:bb[0-9]+]]
196196
// CHECK: [[RESUME]]:
197197
// CHECK: [[RESULT:%.*]] = load [trivial] [[RESUME_BUF]]
198+
// CHECK: hop_to_executor %6 : $MainActor
198199
// CHECK: fix_lifetime [[COPY]]
199200
// CHECK: destroy_value [[COPY]]
200-
// CHECK: hop_to_executor %6 : $MainActor
201201
// CHECK: dealloc_stack [[RESUME_BUF]]
202202
let _: Int = await slowServer.doSomethingSlow("mail")
203203
}

0 commit comments

Comments
 (0)