Skip to content

[region-isolation] Do not allow for a disconnected value passed as an explicitly sent parameter to be reused. #76234

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
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
19 changes: 14 additions & 5 deletions include/swift/SILOptimizer/Utils/PartitionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1226,11 +1226,17 @@ struct PartitionOpEvaluator {
}

// Next see if we are disconnected and have the same isolation. In such a
// case, we do not transfer since the disconnected value is allowed to be
// resued after we return.
if (transferredRegionIsolation.isDisconnected() && calleeIsolationInfo &&
transferredRegionIsolation.hasSameIsolation(calleeIsolationInfo))
return;
// case, if we are not marked explicitly as sending, we do not transfer
// since the disconnected value is allowed to be resued after we
// return. If we are passed as a sending parameter, we cannot do this.
if (auto *sourceInst = Impl::getSourceInst(op)) {
if (auto fas = FullApplySite::isa(sourceInst);
(!fas || !fas.isSending(*op.getSourceOp())) &&
transferredRegionIsolation.isDisconnected() &&
calleeIsolationInfo &&
transferredRegionIsolation.hasSameIsolation(calleeIsolationInfo))
return;
}

// Mark op.getOpArgs()[0] as transferred.
TransferringOperandState &state = operandToStateMap.get(op.getSourceOp());
Expand Down Expand Up @@ -1578,6 +1584,9 @@ struct PartitionOpEvaluatorBaseImpl : PartitionOpEvaluator<Subclass> {

static SILLocation getLoc(SILInstruction *inst) { return inst->getLoc(); }
static SILLocation getLoc(Operand *op) { return op->getUser()->getLoc(); }
static SILInstruction *getSourceInst(const PartitionOp &partitionOp) {
return partitionOp.getSourceInst();
}
static SILIsolationInfo getIsolationInfo(const PartitionOp &partitionOp) {
return SILIsolationInfo::get(partitionOp.getSourceInst());
}
Expand Down
60 changes: 59 additions & 1 deletion test/Concurrency/transfernonsendable_sending_params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
// MARK: Declarations //
////////////////////////

class NonSendableKlass {}
class NonSendableKlass {
func use() {}
}

struct NonSendableStruct {
var first = NonSendableKlass()
Expand Down Expand Up @@ -55,6 +57,9 @@ func throwingFunction() throws { fatalError() }
func transferArg(_ x: sending NonSendableKlass) {
}

func transferArgAsync(_ x: sending NonSendableKlass) async {
}

func transferArgWithOtherParam(_ x: sending NonSendableKlass, _ y: NonSendableKlass) {
}

Expand Down Expand Up @@ -559,3 +564,56 @@ extension MyActor {
}
}
}

// We would normally not error here since transferArg is nonisolated and c is
// disconnected. Since c is passed as sending, we shouldn't squelch this.
func disconnectedPassedSendingToNonIsolatedCallee(
) async -> Void {
let c = NonSendableKlass()
transferArg(c) // expected-warning {{sending 'c' risks causing data races}}
// expected-note @-1 {{'c' used after being passed as a 'sending' parameter}}
c.use() // expected-note {{access can happen concurrently}}
}

// We would normally not error here since transferArg is nonisolated and c is
// disconnected. Since c is passed as sending, we shouldn't squelch this.
func disconnectedPassedSendingToAsyncNonIsolatedCallee(
) async -> Void {
let c = NonSendableKlass()
await transferArgAsync(c) // expected-warning {{sending 'c' risks causing data races}}
// expected-note @-1 {{'c' used after being passed as a 'sending' parameter}}
c.use() // expected-note {{access can happen concurrently}}
}

// We would normally not error here since transferArg is nonisolated and c is
// disconnected. Since c is passed as sending, we shouldn't squelch this.
func disconnectedPassedSendingToNonIsolatedCalleeIsolatedParam2(
isolation: isolated (any Actor)? = nil
) async -> Void {
let c = NonSendableKlass()
transferArg(c) // expected-warning {{sending 'c' risks causing data races}}
// expected-note @-1 {{'c' used after being passed as a 'sending' parameter}}
c.use() // expected-note {{access can happen concurrently}}
}

// We would normally not error here since transferArg is nonisolated and c is
// disconnected. Since c is passed as sending, we shouldn't squelch this.
func disconnectedPassedSendingToAsyncNonIsolatedCalleeIsolatedParam2(
isolation: isolated (any Actor)? = nil
) async -> Void {
let c = NonSendableKlass()
await transferArgAsync(c) // expected-warning {{sending 'c' risks causing data races}}
// expected-note @-1 {{'c' used after being passed as a 'sending' parameter}}
c.use() // expected-note {{access can happen concurrently}}
}

// We would normally not error here since transferArg is nonisolated and c is
// disconnected. Since c is passed as sending, we shouldn't squelch this.
func disconnectedPassedSendingToNonIsolatedCalleeIsolatedParam3(
isolation: isolated (any Actor)? = nil
) -> Void {
let c = NonSendableKlass()
transferArg(c) // expected-warning {{sending 'c' risks causing data races}}
// expected-note @-1 {{'c' used after being passed as a 'sending' parameter}}
c.use() // expected-note {{access can happen concurrently}}
}
8 changes: 8 additions & 0 deletions unittests/SILOptimizer/PartitionUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ struct MockedPartitionOpEvaluator final
return {};
}

static SILInstruction *getSourceInst(const PartitionOp &partitionOp) {
return nullptr;
}

static bool doesFunctionHaveSendingResult(const PartitionOp &partitionOp) {
return false;
}
Expand Down Expand Up @@ -107,6 +111,10 @@ struct MockedPartitionOpEvaluatorWithFailureCallback final
static SILIsolationInfo getIsolationInfo(const PartitionOp &partitionOp) {
return {};
}

static SILInstruction *getSourceInst(const PartitionOp &partitionOp) {
return nullptr;
}
};

} // namespace
Expand Down