Skip to content

Revert "Fix arg cleanup for objc continuations" #38652

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 0 additions & 18 deletions lib/SILGen/ManagedValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ ManagedValue ManagedValue::copy(SILGenFunction &SGF, SILLocation loc) const {
return SGF.emitManagedRValueWithCleanup(buf, lowering);
}

// Emit an unmanaged copy of this value
// WARNING: Callers of this API should manage the cleanup of this value!
ManagedValue ManagedValue::unmanagedCopy(SILGenFunction &SGF,
SILLocation loc) const {
auto &lowering = SGF.getTypeLowering(getType());
if (lowering.isTrivial())
return *this;

if (getType().isObject()) {
auto copy = SGF.B.emitCopyValueOperation(loc, getValue());
return ManagedValue::forUnmanaged(copy);
}

SILValue buf = SGF.emitTemporaryAllocation(loc, getType());
SGF.B.createCopyAddr(loc, getValue(), buf, IsNotTake, IsInitialization);
return ManagedValue::forUnmanaged(buf);
}

/// Emit a copy of this value with independent ownership.
ManagedValue ManagedValue::formalAccessCopy(SILGenFunction &SGF,
SILLocation loc) {
Expand Down
4 changes: 0 additions & 4 deletions lib/SILGen/ManagedValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,6 @@ class ManagedValue {
/// Emit a copy of this value with independent ownership.
ManagedValue copy(SILGenFunction &SGF, SILLocation loc) const;

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

/// Emit a copy of this value with independent ownership into the current
/// formal evaluation scope.
ManagedValue formalAccessCopy(SILGenFunction &SGF, SILLocation loc);
Expand Down
56 changes: 1 addition & 55 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4311,29 +4311,6 @@ bool SILGenModule::isNonMutatingSelfIndirect(SILDeclRef methodRef) {
return self.isFormalIndirect();
}

namespace {
/// Cleanup to insert fix_lifetime and destroy
class FixLifetimeDestroyCleanup : public Cleanup {
SILValue val;

public:
FixLifetimeDestroyCleanup(SILValue val) : val(val) {}

void emit(SILGenFunction &SGF, CleanupLocation l,
ForUnwind_t forUnwind) override {
SGF.B.emitFixLifetime(l, val);
SGF.B.emitDestroyOperation(l, val);
}

void dump(SILGenFunction &SGF) const override {
#ifndef NDEBUG
llvm::errs() << "FixLifetimeDestroyCleanup "
<< "State:" << getState() << " "
<< "Value: " << val << "\n";
#endif
}
};
} // end anonymous namespace

//===----------------------------------------------------------------------===//
// Top Level Entrypoints
Expand Down Expand Up @@ -4485,19 +4462,6 @@ RValue SILGenFunction::emitApply(
// hop back to the current executor
breadcrumb.emit(*this, loc);

// For objc async calls, lifetime extend the args until the result plan which
// generates `await_async_continuation`.
// Lifetime is extended by creating unmanaged copies here and by pushing the
// cleanups required just before the result plan is generated.
SmallVector<ManagedValue, 8> unmanagedCopies;
if (calleeTypeInfo.foreign.async) {
for (auto arg : args) {
if (arg.hasCleanup()) {
unmanagedCopies.push_back(arg.unmanagedCopy(*this, loc));
}
}
}

// Pop the argument scope.
argScope.pop();

Expand Down Expand Up @@ -4573,30 +4537,12 @@ RValue SILGenFunction::emitApply(
emitForeignErrorCheck(loc, directResults, errorTemp, doesNotThrow,
*foreignError);
}

// For objc async calls, push cleanup to be used on throw paths in the result
// planner.
for (unsigned i : indices(unmanagedCopies)) {
SILValue value = unmanagedCopies[i].getValue();
Cleanups.pushCleanup<FixLifetimeDestroyCleanup>(value);
unmanagedCopies[i] = ManagedValue(value, Cleanups.getTopCleanup());
}


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

// For objc async calls, generate cleanup on the resume path here and forward
// the previously pushed cleanups.
if (calleeTypeInfo.foreign.async) {
for (auto unmanagedCopy : unmanagedCopies) {
auto value = unmanagedCopy.forward(*this);
B.emitFixLifetime(loc, value);
B.emitDestroyOperation(loc, value);
}
}

return result;
}

Expand Down
8 changes: 1 addition & 7 deletions test/SILGen/objc_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import ObjCConcurrency
// CHECK-LABEL: sil {{.*}}@${{.*}}14testSlowServer
func testSlowServer(slowServer: SlowServer) async throws {
// CHECK: [[RESUME_BUF:%.*]] = alloc_stack $Int
// CHECK: [[STRINGINIT:%.*]] = function_ref @$sSS10FoundationE19_bridgeToObjectiveCSo8NSStringCyF :
// CHECK: [[ARG:%.*]] = apply [[STRINGINIT]]
// CHECK: [[METHOD:%.*]] = objc_method {{.*}} $@convention(objc_method) (NSString, @convention(block) (Int) -> (), SlowServer) -> ()
// CHECK: [[CONT:%.*]] = get_async_continuation_addr Int, [[RESUME_BUF]]
// CHECK: [[WRAPPED:%.*]] = struct $UnsafeContinuation<Int, Never> ([[CONT]] : $Builtin.RawUnsafeContinuation)
Expand All @@ -18,14 +16,10 @@ func testSlowServer(slowServer: SlowServer) async throws {
// CHECK: store [[WRAPPED]] to [trivial] [[CONT_SLOT]]
// CHECK: [[BLOCK_IMPL:%.*]] = function_ref @[[INT_COMPLETION_BLOCK:.*]] : $@convention(c) (@inout_aliasable @block_storage UnsafeContinuation<Int, Never>, Int) -> ()
// CHECK: [[BLOCK:%.*]] = init_block_storage_header [[BLOCK_STORAGE]] {{.*}}, invoke [[BLOCK_IMPL]]
// CHECK: apply [[METHOD]]([[ARG]], [[BLOCK]], %0)
// CHECK: [[COPY:%.*]] = copy_value [[ARG]]
// CHECK: destroy_value [[ARG]]
// CHECK: apply [[METHOD]]({{.*}}, [[BLOCK]], %0)
// CHECK: await_async_continuation [[CONT]] {{.*}}, resume [[RESUME:bb[0-9]+]]
// CHECK: [[RESUME]]:
// CHECK: [[RESULT:%.*]] = load [trivial] [[RESUME_BUF]]
// CHECK: fix_lifetime [[COPY]]
// CHECK: destroy_value [[COPY]]
// CHECK: dealloc_stack [[RESUME_BUF]]
let _: Int = await slowServer.doSomethingSlow("mail")

Expand Down