Skip to content

[region-isolation] Convert more diagnostics from type isolation form to named variable form #72435

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
3 changes: 0 additions & 3 deletions lib/SILOptimizer/Mandatory/TransferNonSendable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,6 @@ class UseAfterTransferDiagnosticEmitter {
diag::regionbasedisolation_named_info_transfer_yields_race,
name, isolationCrossing.getCallerIsolation(),
isolationCrossing.getCalleeIsolation());
// Then emit the note about where the variable is defined.
diagnoseNote(variableDefinedLoc, diag::variable_defined_here,
false /*variable*/);
emitRequireInstDiagnostics();
}

Expand Down
75 changes: 73 additions & 2 deletions lib/SILOptimizer/Utils/VariableNameUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ findRootValueForNonTupleTempAllocation(AllocationInst *allocInst,
}
}

if (auto *sbi = dyn_cast<StoreBorrowInst>(&inst)) {
if (sbi->getDest() == allocInst)
return sbi->getSrc();
}

// If we do not identify the write... return SILValue(). We weren't able
// to understand the write.
break;
Expand Down Expand Up @@ -128,6 +133,57 @@ static SILValue findRootValueForTupleTempAllocation(AllocationInst *allocInst,

if (auto *si = dyn_cast<StoreInst>(&inst)) {
if (si->getOwnershipQualifier() != StoreOwnershipQualifier::Assign) {
// Check if we are updating the entire tuple value.
if (si->getDest() == allocInst) {
// If we already found a root address (meaning we were processing
// tuple_elt_addr), bail. We have some sort of unhandled mix of
// copy_addr and store.
if (foundRootAddress)
return SILValue();

// If we already found a destructure, return SILValue(). We are
// initializing twice.
if (foundDestructure)
return SILValue();

// We are looking for a pattern where we construct a tuple from
// destructured parts.
if (auto *ti = dyn_cast<TupleInst>(si->getSrc())) {
for (auto p : llvm::enumerate(ti->getOperandValues())) {
SILValue value = lookThroughOwnershipInsts(p.value());
if (auto *dti = dyn_cast_or_null<DestructureTupleInst>(
value->getDefiningInstruction())) {
// We should always go through the same dti.
if (foundDestructure && foundDestructure != dti)
return SILValue();
if (!foundDestructure)
foundDestructure = dti;

// If we have a mixmatch of indices, we cannot look through.
if (p.index() != dti->getIndexOfResult(value))
return SILValue();
if (tupleValues[p.index()])
return SILValue();
tupleValues[p.index()] = value;

// If we have completely covered the tuple, break.
--numEltsLeft;
if (!numEltsLeft)
break;
}
}

// If we haven't completely covered the tuple, return SILValue(). We
// should completely cover the tuple.
if (numEltsLeft)
return SILValue();

// Otherwise, break since we are done.
break;
}
}

// If we store to a tuple_element_addr, update for a single value.
if (auto *tei = dyn_cast<TupleElementAddrInst>(si->getDest())) {
if (tei->getOperand() == allocInst) {
unsigned i = tei->getFieldIndex();
Expand Down Expand Up @@ -183,7 +239,7 @@ static SILValue findRootValueForTupleTempAllocation(AllocationInst *allocInst,

SILValue VariableNameInferrer::getRootValueForTemporaryAllocation(
AllocationInst *allocInst) {
struct AddressWalker : public TransitiveAddressWalker<AddressWalker> {
struct AddressWalker final : public TransitiveAddressWalker<AddressWalker> {
AddressWalkerState &state;

AddressWalker(AddressWalkerState &state) : state(state) {}
Expand All @@ -194,6 +250,12 @@ SILValue VariableNameInferrer::getRootValueForTemporaryAllocation(
return true;
}

TransitiveUseVisitation visitTransitiveUseAsEndPointUse(Operand *use) {
if (auto *sbi = dyn_cast<StoreBorrowInst>(use->getUser()))
return TransitiveUseVisitation::OnlyUser;
return TransitiveUseVisitation::OnlyUses;
}

void onError(Operand *use) { state.foundError = true; }
};

Expand Down Expand Up @@ -288,6 +350,13 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
return allocInst;
}

// If we have a store_borrow, always look at the dest. We are going to see
// if we can determine if dest is a temporary alloc_stack.
if (auto *sbi = dyn_cast<StoreBorrowInst>(searchValue)) {
searchValue = sbi->getDest();
continue;
}

if (auto *globalAddrInst = dyn_cast<GlobalAddrInst>(searchValue)) {
variableNamePath.push_back(globalAddrInst);
return globalAddrInst;
Expand Down Expand Up @@ -487,7 +556,9 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
isa<ConvertFunctionInst>(searchValue) ||
isa<MarkUninitializedInst>(searchValue) ||
isa<CopyableToMoveOnlyWrapperAddrInst>(searchValue) ||
isa<MoveOnlyWrapperToCopyableAddrInst>(searchValue)) {
isa<MoveOnlyWrapperToCopyableAddrInst>(searchValue) ||
isa<MoveOnlyWrapperToCopyableValueInst>(searchValue) ||
isa<CopyableToMoveOnlyWrapperValueInst>(searchValue)) {
searchValue = cast<SingleValueInstruction>(searchValue)->getOperand(0);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Test2: TestProtocol { // expected-warning{{conformance of 'C2' to 'Sendab

@MainActor
func iterate(stream: AsyncStream<Int>) async {
nonisolated(unsafe) var it = stream.makeAsyncIterator() // expected-region-isolation-note {{variable defined here}}
nonisolated(unsafe) var it = stream.makeAsyncIterator()
// FIXME: Region isolation should consider a value from a 'nonisolated(unsafe)'
// declaration to be in a disconnected region

Expand Down
2 changes: 1 addition & 1 deletion test/Concurrency/sendable_checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ final class NonSendable {

@available(SwiftStdlib 5.1, *)
func testNonSendableBaseArg() async {
let t = NonSendable() // expected-tns-note {{variable defined here}}
let t = NonSendable()
await t.update()
// expected-targeted-and-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into main actor-isolated context may introduce data races}}
// expected-tns-warning @-2 {{transferring 't' may cause a race}}
Expand Down
Loading