Skip to content

Commit 9298918

Browse files
authored
Merge pull request #79611 from gottesmm/pr-1af1f4e53db03b01babcd0470c532fe00f6a7a11
[rbi] Respect nonisolated(unsafe) when assigning or merging into a sending out parameter.
2 parents 5c15952 + dab3240 commit 9298918

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

include/swift/SILOptimizer/Utils/PartitionUtils.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,12 +1323,16 @@ struct PartitionOpEvaluator {
13231323
if (auto value = instance.maybeGetValue()) {
13241324
if (auto *fArg = dyn_cast<SILFunctionArgument>(value)) {
13251325
if (fArg->getArgumentConvention().isIndirectOutParameter()) {
1326+
auto staticRegionIsolation =
1327+
getIsolationRegionInfo(op.getOpArgs()[1]);
13261328
Region srcRegion = p.getRegion(op.getOpArgs()[1]);
13271329
auto dynamicRegionIsolation = getIsolationRegionInfo(srcRegion);
1330+
13281331
// We can unconditionally getValue here since we can never
13291332
// assign an actor introducing inst.
13301333
auto rep = getRepresentativeValue(op.getOpArgs()[1]).getValue();
1331-
if (!dynamicRegionIsolation.isDisconnected()) {
1334+
if (!dynamicRegionIsolation.isDisconnected() &&
1335+
!staticRegionIsolation.isUnsafeNonIsolated()) {
13321336
handleError(AssignNeverSendableIntoSendingResultError(
13331337
op, op.getOpArgs()[0], fArg, op.getOpArgs()[1], rep,
13341338
dynamicRegionIsolation));
@@ -1451,12 +1455,15 @@ struct PartitionOpEvaluator {
14511455
if (auto value = instance.maybeGetValue()) {
14521456
if (auto *fArg = dyn_cast<SILFunctionArgument>(value)) {
14531457
if (fArg->getArgumentConvention().isIndirectOutParameter()) {
1458+
auto staticRegionIsolation =
1459+
getIsolationRegionInfo(op.getOpArgs()[1]);
14541460
Region srcRegion = p.getRegion(op.getOpArgs()[1]);
14551461
auto dynamicRegionIsolation = getIsolationRegionInfo(srcRegion);
14561462
// We can unconditionally getValue here since we can never
14571463
// assign an actor introducing inst.
14581464
auto rep = getRepresentativeValue(op.getOpArgs()[1]).getValue();
1459-
if (!dynamicRegionIsolation.isDisconnected()) {
1465+
if (!dynamicRegionIsolation.isDisconnected() &&
1466+
!staticRegionIsolation.isUnsafeNonIsolated()) {
14601467
handleError(AssignNeverSendableIntoSendingResultError(
14611468
op, op.getOpArgs()[0], fArg, op.getOpArgs()[1], rep,
14621469
dynamicRegionIsolation));

lib/SILOptimizer/Analysis/RegionAnalysis.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ static bool isLookThroughIfOperandAndResultNonSendable(SILInstruction *inst) {
320320
case SILInstructionKind::StructElementAddrInst:
321321
case SILInstructionKind::TupleElementAddrInst:
322322
case SILInstructionKind::UncheckedTakeEnumDataAddrInst:
323+
case SILInstructionKind::ConvertEscapeToNoEscapeInst:
324+
case SILInstructionKind::ConvertFunctionInst:
323325
return true;
324326
}
325327
}
@@ -2985,8 +2987,6 @@ CONSTANT_TRANSLATION(LoadUnownedInst, Assign)
29852987
// getUnderlyingTrackedObject.
29862988
CONSTANT_TRANSLATION(AddressToPointerInst, Assign)
29872989
CONSTANT_TRANSLATION(BaseAddrForOffsetInst, Assign)
2988-
CONSTANT_TRANSLATION(ConvertEscapeToNoEscapeInst, Assign)
2989-
CONSTANT_TRANSLATION(ConvertFunctionInst, Assign)
29902990
CONSTANT_TRANSLATION(ThunkInst, Assign)
29912991
CONSTANT_TRANSLATION(CopyBlockInst, Assign)
29922992
CONSTANT_TRANSLATION(CopyBlockWithoutEscapingInst, Assign)
@@ -3314,6 +3314,8 @@ LOOKTHROUGH_IF_NONSENDABLE_RESULT_AND_OPERAND(UncheckedValueCastInst)
33143314
LOOKTHROUGH_IF_NONSENDABLE_RESULT_AND_OPERAND(TupleElementAddrInst)
33153315
LOOKTHROUGH_IF_NONSENDABLE_RESULT_AND_OPERAND(StructElementAddrInst)
33163316
LOOKTHROUGH_IF_NONSENDABLE_RESULT_AND_OPERAND(UncheckedTakeEnumDataAddrInst)
3317+
LOOKTHROUGH_IF_NONSENDABLE_RESULT_AND_OPERAND(ConvertEscapeToNoEscapeInst)
3318+
LOOKTHROUGH_IF_NONSENDABLE_RESULT_AND_OPERAND(ConvertFunctionInst)
33173319

33183320
#undef LOOKTHROUGH_IF_NONSENDABLE_RESULT_AND_OPERAND
33193321

test/Concurrency/transfernonsendable.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,3 +1944,14 @@ func testFunctionIsNotEmpty(input: SendableKlass) async throws {
19441944
}
19451945
}
19461946
}
1947+
1948+
func unsafeNonIsolatedAppliesToAssignToOutParam(ns: NonSendableKlass) -> sending NonSendableKlass {
1949+
func withUnsafeValue<T>(_ block: (NonSendableKlass) throws -> sending T) rethrows -> sending T {
1950+
fatalError()
1951+
}
1952+
1953+
return withUnsafeValue {
1954+
nonisolated(unsafe) let obj = $0
1955+
return obj
1956+
}
1957+
}

0 commit comments

Comments
 (0)