Skip to content

Commit 677b897

Browse files
authored
Merge pull request #82555 from gottesmm/pr-b4de2f5d58d852cd3e9e606101e1b0ff42ce6092
[nonisolated-nonsending] Make the AST not consider nonisolated(nonsending) to be an actor isolation crossing point.
2 parents 31408fe + 010fa39 commit 677b897

File tree

39 files changed

+953
-443
lines changed

39 files changed

+953
-443
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -998,23 +998,23 @@ GROUPED_ERROR(regionbasedisolation_type_send_yields_race, SendingRisksDataRace,
998998
(Type))
999999
NOTE(regionbasedisolation_type_use_after_send, none,
10001000
"sending value of non-Sendable type %0 to %1 callee risks causing data races between %1 and local %2 uses",
1001-
(Type, ActorIsolation, ActorIsolation))
1001+
(Type, StringRef, StringRef))
10021002
NOTE(regionbasedisolation_type_use_after_send_callee, none,
10031003
"sending value of non-Sendable type %0 to %1 %kind2 risks causing data "
10041004
"races between %1 and local %3 uses",
1005-
(Type, ActorIsolation, const ValueDecl *, ActorIsolation))
1005+
(Type, StringRef, const ValueDecl *, StringRef))
10061006

10071007
NOTE(regionbasedisolation_named_info_send_yields_race, none,
1008-
"sending %1%0 to %2 callee risks causing data races between %2 and local %3 uses",
1009-
(Identifier, StringRef, ActorIsolation, ActorIsolation))
1008+
"sending %select{%2 |}0%1 to %3 callee risks causing data races between %3 and local %4 uses",
1009+
(bool, Identifier, StringRef, StringRef, StringRef))
10101010
NOTE(regionbasedisolation_named_info_send_yields_race_callee, none,
1011-
"sending %1%0 to %2 %kind3 risks causing data races between %2 and local %4 uses",
1012-
(Identifier, StringRef, ActorIsolation, const ValueDecl *, ActorIsolation))
1011+
"sending %select{%2 |}0%1 to %3 %kind4 risks causing data races between %3 and local %5 uses",
1012+
(bool, Identifier, StringRef, StringRef, const ValueDecl *, StringRef))
10131013

10141014
// Use after send closure.
10151015
NOTE(regionbasedisolation_type_isolated_capture_yields_race, none,
10161016
"sending value of non-Sendable type %0 to %1 closure due to closure capture risks causing races in between %1 and %2 uses",
1017-
(Type, ActorIsolation, ActorIsolation))
1017+
(Type, StringRef, StringRef))
10181018

10191019
// Value captured in async let and reused.
10201020
NOTE(regionbasedisolation_named_nonisolated_asynclet_name, none,
@@ -1025,8 +1025,8 @@ NOTE(regionbasedisolation_named_value_used_after_explicit_sending, none,
10251025
"%0 used after being passed as a 'sending' parameter; Later uses could race",
10261026
(Identifier))
10271027
NOTE(regionbasedisolation_named_isolated_closure_yields_race, none,
1028-
"%0%1 is captured by a %2 closure. %2 uses in closure may race against later %3 uses",
1029-
(StringRef, Identifier, ActorIsolation, ActorIsolation))
1028+
"%select{%1 |}0%2 is captured by a %3 closure. %3 uses in closure may race against later %4 uses",
1029+
(bool, StringRef, Identifier, StringRef, StringRef))
10301030

10311031
NOTE(regionbasedisolation_typed_use_after_sending, none,
10321032
"Passing value of non-Sendable type %0 as a 'sending' argument risks causing races in between local and caller code",
@@ -1039,19 +1039,19 @@ NOTE(regionbasedisolation_typed_use_after_sending_callee, none,
10391039
// Sending Never Sendable Emitter
10401040

10411041
NOTE(regionbasedisolation_named_send_never_sendable, none,
1042-
"sending %1%0 to %2 callee risks causing data races between %2 and %3 uses",
1043-
(Identifier, StringRef, ActorIsolation, StringRef))
1042+
"sending %select{%2 |}0%1 to %3 callee risks causing data races between %3 and %4 uses",
1043+
(bool, Identifier, StringRef, StringRef, StringRef))
10441044
NOTE(regionbasedisolation_named_send_never_sendable_callee, none,
1045-
"sending %1%0 to %2 %kind3 risks causing data races between %2 and %4 uses",
1046-
(Identifier, StringRef, ActorIsolation, const ValueDecl *, StringRef))
1045+
"sending %select{%2 |}0%1 to %3 %kind4 risks causing data races between %3 and %5 uses",
1046+
(bool, Identifier, StringRef, StringRef, const ValueDecl *, StringRef))
10471047

10481048
NOTE(regionbasedisolation_named_send_into_sending_param, none,
1049-
"%0%1 is passed as a 'sending' parameter; Uses in callee may race with "
1050-
"later %0uses",
1051-
(StringRef, Identifier))
1049+
"%select{%1 |}0%2 is passed as a 'sending' parameter; Uses in callee may race with "
1050+
"later %1 uses",
1051+
(bool, StringRef, Identifier))
10521052
NOTE(regionbasedisolation_named_nosend_send_into_result, none,
1053-
"%0%1 cannot be a 'sending' result. %2 uses may race with caller uses",
1054-
(StringRef, Identifier, StringRef))
1053+
"%select{%1 |}0%2 cannot be a 'sending' result. %3 uses may race with caller uses",
1054+
(bool, StringRef, Identifier, StringRef))
10551055
NOTE(regionbasedisolation_typed_tns_passed_to_sending, none,
10561056
"Passing %0 value of non-Sendable type %1 as a 'sending' parameter risks "
10571057
"causing races inbetween %0 uses and uses reachable from the callee",
@@ -1092,10 +1092,10 @@ NOTE(regionbasedisolation_named_send_nt_asynclet_capture, none,
10921092
(Identifier, StringRef))
10931093
NOTE(regionbasedisolation_typed_sendneversendable_via_arg, none,
10941094
"sending %0 value of non-Sendable type %1 to %2 callee risks causing races in between %0 and %2 uses",
1095-
(StringRef, Type, ActorIsolation))
1095+
(StringRef, Type, StringRef))
10961096
NOTE(regionbasedisolation_typed_sendneversendable_via_arg_callee, none,
10971097
"sending %0 value of non-Sendable type %1 to %2 %kind3 risks causing races in between %0 and %2 uses",
1098-
(StringRef, Type, ActorIsolation, const ValueDecl *))
1098+
(StringRef, Type, StringRef, const ValueDecl *))
10991099

11001100
// Error that is only used when the send non sendable emitter cannot discover any
11011101
// information to give a better diagnostic.
@@ -1109,10 +1109,10 @@ NOTE(regionbasedisolation_inout_sending_must_be_reinitialized, none,
11091109
"'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value",
11101110
())
11111111
ERROR(regionbasedisolation_inout_sending_cannot_be_actor_isolated, none,
1112-
"'inout sending' parameter %0 cannot be %1at end of function",
1112+
"'inout sending' parameter %0 cannot be %1 at end of function",
11131113
(Identifier, StringRef))
11141114
NOTE(regionbasedisolation_inout_sending_cannot_be_actor_isolated_note, none,
1115-
"%1%0 risks causing races in between %1uses and caller uses since caller assumes value is not actor isolated",
1115+
"%1 %0 risks causing races in between %1 uses and caller uses since caller assumes value is not actor isolated",
11161116
(Identifier, StringRef))
11171117

11181118
//===
@@ -1137,10 +1137,10 @@ NOTE(regionbasedisolation_out_sending_cannot_be_actor_isolated_note_named, none,
11371137
// Example: returning main-actor isolated result to a custom-actor isolated context risks causing data races
11381138
ERROR(rbi_isolation_crossing_result, none,
11391139
"non-Sendable %0-typed result can not be returned from %1 %kind2 to %3 context",
1140-
(Type, ActorIsolation, const ValueDecl *, ActorIsolation))
1140+
(Type, StringRef, const ValueDecl *, StringRef))
11411141
ERROR(rbi_isolation_crossing_result_no_decl, none,
11421142
"non-Sendable %0-typed result can not be returned from %1 function to %2 context",
1143-
(Type, ActorIsolation, ActorIsolation))
1143+
(Type, StringRef, StringRef))
11441144
NOTE(rbi_non_sendable_nominal,none,
11451145
"%kind0 does not conform to the 'Sendable' protocol",
11461146
(const ValueDecl *))

include/swift/AST/Identifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class Identifier {
230230
private:
231231
bool isOperatorSlow() const;
232232
};
233-
233+
234234
class DeclName;
235235
class DeclNameRef;
236236
class ObjCSelector;

include/swift/SILOptimizer/Analysis/RegionAnalysis.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,16 @@ class regionanalysisimpl::TrackableValueState {
179179

180180
void removeFlag(TrackableValueFlag flag) { flagSet -= flag; }
181181

182-
void print(llvm::raw_ostream &os) const {
182+
void print(SILFunction *fn, llvm::raw_ostream &os) const {
183183
os << "TrackableValueState[id: " << id
184184
<< "][is_no_alias: " << (isNoAlias() ? "yes" : "no")
185185
<< "][is_sendable: " << (isSendable() ? "yes" : "no")
186186
<< "][region_value_kind: ";
187-
getIsolationRegionInfo().printForOneLineLogging(os);
187+
getIsolationRegionInfo().printForOneLineLogging(fn, os);
188188
os << "].";
189189
}
190190

191-
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
191+
SWIFT_DEBUG_DUMPER(dump(SILFunction *fn)) { print(fn, llvm::dbgs()); }
192192

193193
private:
194194
bool hasIsolationRegionInfo() const { return bool(regionInfo); }
@@ -249,26 +249,26 @@ class regionanalysisimpl::TrackableValue {
249249
/// parameter.
250250
bool isSendingParameter() const;
251251

252-
void printIsolationInfo(SmallString<64> &outString) const {
252+
void printIsolationInfo(SILFunction *fn, SmallString<64> &outString) const {
253253
llvm::raw_svector_ostream os(outString);
254-
getIsolationRegionInfo().printForDiagnostics(os);
254+
getIsolationRegionInfo().printForDiagnostics(fn, os);
255255
}
256256

257-
void print(llvm::raw_ostream &os) const {
257+
void print(SILFunction *fn, llvm::raw_ostream &os) const {
258258
os << "TrackableValue. State: ";
259-
valueState.print(os);
259+
valueState.print(fn, os);
260260
os << "\n Rep Value: ";
261261
getRepresentative().print(os);
262262
}
263263

264-
void printVerbose(llvm::raw_ostream &os) const {
264+
void printVerbose(SILFunction *fn, llvm::raw_ostream &os) const {
265265
os << "TrackableValue. State: ";
266-
valueState.print(os);
266+
valueState.print(fn, os);
267267
os << "\n Rep Value: " << getRepresentative();
268268
}
269269

270-
SWIFT_DEBUG_DUMP {
271-
print(llvm::dbgs());
270+
SWIFT_DEBUG_DUMPER(dump(SILFunction *fn)) {
271+
print(fn, llvm::dbgs());
272272
llvm::dbgs() << '\n';
273273
}
274274
};
@@ -288,8 +288,8 @@ struct regionanalysisimpl::TrackableValueLookupResult {
288288
/// TrackableValue.
289289
std::optional<TrackableValue> base;
290290

291-
void print(llvm::raw_ostream &os) const;
292-
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
291+
void print(SILFunction *fn, llvm::raw_ostream &os) const;
292+
SWIFT_DEBUG_DUMPER(dumper(SILFunction *fn)) { print(fn, llvm::dbgs()); }
293293
};
294294

295295
class RegionAnalysis;
@@ -390,6 +390,8 @@ class RegionAnalysisValueMap {
390390
return getUnderlyingTrackedValue(value).value;
391391
}
392392

393+
SILFunction *getFunction() const { return fn; }
394+
393395
/// Returns the value for this instruction if it isn't a fake "represenative
394396
/// value" to inject actor isolation. Asserts in such a case.
395397
SILValue getRepresentative(Element trackableValueID) const;

include/swift/SILOptimizer/Utils/SILIsolationInfo.h

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ class SILIsolationInfo {
185185
/// parameter and should be allowed to merge into a self parameter.
186186
UnappliedIsolatedAnyParameter = 0x2,
187187

188+
/// If set, this was a TaskIsolated value from a nonisolated(nonsending)
189+
/// parameter.
190+
NonisolatedNonsendingTaskIsolated = 0x4,
191+
188192
/// The maximum number of bits used by a Flag.
189193
MaxNumBits = 2,
190194
};
@@ -269,6 +273,25 @@ class SILIsolationInfo {
269273
return self;
270274
}
271275

276+
bool isNonisolatedNonsendingTaskIsolated() const {
277+
return getOptions().contains(Flag::NonisolatedNonsendingTaskIsolated);
278+
}
279+
280+
SILIsolationInfo
281+
withNonisolatedNonsendingTaskIsolated(bool newValue = true) const {
282+
assert(*this && "Cannot be unknown");
283+
assert(isTaskIsolated() && "Can only be task isolated");
284+
auto self = *this;
285+
if (newValue) {
286+
self.options =
287+
(self.getOptions() | Flag::NonisolatedNonsendingTaskIsolated).toRaw();
288+
} else {
289+
self.options = self.getOptions().toRaw() &
290+
~Options(Flag::NonisolatedNonsendingTaskIsolated).toRaw();
291+
}
292+
return self;
293+
}
294+
272295
/// Returns true if this actor isolation is derived from an unapplied
273296
/// isolation parameter. When merging, we allow for this to be merged with a
274297
/// more specific isolation kind.
@@ -289,16 +312,16 @@ class SILIsolationInfo {
289312
return self;
290313
}
291314

292-
void print(llvm::raw_ostream &os) const;
315+
void print(SILFunction *fn, llvm::raw_ostream &os) const;
293316

294317
/// Print a textual representation of the text info that is meant to be
295318
/// included in other logging output for types that compose with
296319
/// SILIsolationInfo. As a result, we only print state that can fit on
297320
/// one line.
298-
void printForOneLineLogging(llvm::raw_ostream &os) const;
321+
void printForOneLineLogging(SILFunction *fn, llvm::raw_ostream &os) const;
299322

300-
SWIFT_DEBUG_DUMP {
301-
print(llvm::dbgs());
323+
SWIFT_DEBUG_DUMPER(dump(SILFunction *fn)) {
324+
print(fn, llvm::dbgs());
302325
llvm::dbgs() << '\n';
303326
}
304327

@@ -307,12 +330,20 @@ class SILIsolationInfo {
307330
///
308331
/// We do this programatically since task-isolated code needs a very different
309332
/// form of diagnostic than other cases.
310-
void printForCodeDiagnostic(llvm::raw_ostream &os) const;
333+
void printForCodeDiagnostic(SILFunction *fn, llvm::raw_ostream &os) const;
334+
335+
/// Overload of printForCodeDiagnostics that returns an interned StringRef
336+
/// owned by the AST.
337+
StringRef printForCodeDiagnostic(SILFunction *fn) const;
311338

312-
void printForDiagnostics(llvm::raw_ostream &os) const;
339+
void printForDiagnostics(SILFunction *fn, llvm::raw_ostream &os) const;
313340

314-
SWIFT_DEBUG_DUMPER(dumpForDiagnostics()) {
315-
printForDiagnostics(llvm::dbgs());
341+
/// Overload of printForDiagnostics that returns an interned StringRef owned
342+
/// by the AST.
343+
StringRef printForDiagnostics(SILFunction *fn) const;
344+
345+
SWIFT_DEBUG_DUMPER(dumpForDiagnostics(SILFunction *fn)) {
346+
printForDiagnostics(fn, llvm::dbgs());
316347
llvm::dbgs() << '\n';
317348
}
318349

@@ -494,6 +525,19 @@ class SILIsolationInfo {
494525
/// that the isolation and the isolated value match.
495526
bool isEqual(const SILIsolationInfo &other) const;
496527

528+
/// A helper function that prints ActorIsolation like we normally do except
529+
/// that it prints nonisolated(nonsending) as nonisolated. This is needed in
530+
/// certain cases when talking about use-after-free uses in send non sendable.
531+
static void printActorIsolationForDiagnostics(
532+
SILFunction *fn, ActorIsolation iso, llvm::raw_ostream &os,
533+
StringRef openingQuotationMark = "'", bool asNoun = false);
534+
535+
/// Overload for printActorIsolationForDiagnostics that produces a StringRef.
536+
static StringRef
537+
printActorIsolationForDiagnostics(SILFunction *fn, ActorIsolation iso,
538+
StringRef openingQuotationMark = "'",
539+
bool asNoun = false);
540+
497541
void Profile(llvm::FoldingSetNodeID &id) const;
498542

499543
private:
@@ -557,43 +601,33 @@ class SILDynamicMergedIsolationInfo {
557601
SILIsolationInfo::getDisconnected(isUnsafeNonIsolated));
558602
}
559603

560-
SWIFT_DEBUG_DUMP { innerInfo.dump(); }
604+
SWIFT_DEBUG_DUMPER(dump(SILFunction *fn)) { innerInfo.dump(fn); }
561605

562-
void printForDiagnostics(llvm::raw_ostream &os) const {
563-
innerInfo.printForDiagnostics(os);
606+
void printForDiagnostics(SILFunction *fn, llvm::raw_ostream &os) const {
607+
innerInfo.printForDiagnostics(fn, os);
564608
}
565609

566-
SWIFT_DEBUG_DUMPER(dumpForDiagnostics()) {
567-
innerInfo.dumpForDiagnostics();
610+
StringRef printForDiagnostics(SILFunction *fn) const {
611+
return innerInfo.printForDiagnostics(fn);
568612
}
569613

570-
void printForCodeDiagnostic(llvm::raw_ostream &os) const {
571-
innerInfo.printForCodeDiagnostic(os);
614+
SWIFT_DEBUG_DUMPER(dumpForDiagnostics(SILFunction *fn)) {
615+
innerInfo.dumpForDiagnostics(fn);
572616
}
573617

574-
void printForOneLineLogging(llvm::raw_ostream &os) const {
575-
innerInfo.printForOneLineLogging(os);
618+
void printForCodeDiagnostic(SILFunction *fn, llvm::raw_ostream &os) const {
619+
innerInfo.printForCodeDiagnostic(fn, os);
576620
}
577-
};
578621

579-
} // namespace swift
580-
581-
namespace llvm {
582-
583-
inline llvm::raw_ostream &
584-
operator<<(llvm::raw_ostream &os,
585-
const swift::SILIsolationInfo &isolationInfo) {
586-
isolationInfo.printForOneLineLogging(os);
587-
return os;
588-
}
622+
StringRef printForCodeDiagnostic(SILFunction *fn) const {
623+
return innerInfo.printForCodeDiagnostic(fn);
624+
}
589625

590-
inline llvm::raw_ostream &
591-
operator<<(llvm::raw_ostream &os,
592-
const swift::SILDynamicMergedIsolationInfo &isolationInfo) {
593-
isolationInfo.printForOneLineLogging(os);
594-
return os;
595-
}
626+
void printForOneLineLogging(SILFunction *fn, llvm::raw_ostream &os) const {
627+
innerInfo.printForOneLineLogging(fn, os);
628+
}
629+
};
596630

597-
} // namespace llvm
631+
} // namespace swift
598632

599633
#endif

0 commit comments

Comments
 (0)