Skip to content

Commit 4c2dc5e

Browse files
authored
Merge pull request #73930 from gottesmm/nonisolatedunsafe-rdar128299305
[region-isolation] Add missing support for nonisolated(unsafe)
2 parents a664c02 + 06c32d7 commit 4c2dc5e

17 files changed

+3817
-1061
lines changed

include/swift/AST/ActorIsolation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ class ActorIsolation {
170170
return (kind == Nonisolated) || (kind == NonisolatedUnsafe);
171171
}
172172

173+
bool isNonisolatedUnsafe() const { return kind == NonisolatedUnsafe; }
174+
173175
/// Retrieve the parameter to which actor-instance isolation applies.
174176
///
175177
/// Parameter 0 is `self`.

include/swift/SILOptimizer/Analysis/RegionAnalysis.h

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace swift {
2525

2626
class RegionAnalysisFunctionInfo;
27+
class RegionAnalysisValueMap;
2728

2829
namespace regionanalysisimpl {
2930

@@ -121,9 +122,11 @@ using TrackedValueFlagSet = OptionSet<TrackableValueFlag>;
121122
} // namespace regionanalysisimpl
122123

123124
class regionanalysisimpl::TrackableValueState {
125+
friend RegionAnalysisValueMap;
126+
124127
unsigned id;
125128
TrackedValueFlagSet flagSet = {TrackableValueFlag::isMayAlias};
126-
SILIsolationInfo regionInfo = SILIsolationInfo::getDisconnected();
129+
std::optional<SILIsolationInfo> regionInfo = {};
127130

128131
public:
129132
TrackableValueState(unsigned newID) : id(newID) {}
@@ -140,20 +143,28 @@ class regionanalysisimpl::TrackableValueState {
140143

141144
bool isNonSendable() const { return !isSendable(); }
142145

146+
SILIsolationInfo getIsolationRegionInfo() const {
147+
if (!regionInfo) {
148+
return SILIsolationInfo::getDisconnected(false);
149+
}
150+
151+
return *regionInfo;
152+
}
153+
143154
SILIsolationInfo::Kind getIsolationRegionInfoKind() const {
144-
return regionInfo.getKind();
155+
return getIsolationRegionInfo().getKind();
145156
}
146157

147158
ActorIsolation getActorIsolation() const {
148-
return regionInfo.getActorIsolation();
159+
return getIsolationRegionInfo().getActorIsolation();
149160
}
150161

151-
void mergeIsolationRegionInfo(SILIsolationInfo newRegionInfo) {
152-
regionInfo = regionInfo.merge(newRegionInfo);
162+
void setDisconnectedNonisolatedUnsafe() {
163+
auto oldRegionInfo = getIsolationRegionInfo();
164+
assert(oldRegionInfo.isDisconnected());
165+
regionInfo = oldRegionInfo.withUnsafeNonIsolated();
153166
}
154167

155-
SILIsolationInfo getIsolationRegionInfo() const { return regionInfo; }
156-
157168
Element getID() const { return Element(id); }
158169

159170
void addFlag(TrackableValueFlag flag) { flagSet |= flag; }
@@ -165,11 +176,22 @@ class regionanalysisimpl::TrackableValueState {
165176
<< "][is_no_alias: " << (isNoAlias() ? "yes" : "no")
166177
<< "][is_sendable: " << (isSendable() ? "yes" : "no")
167178
<< "][region_value_kind: ";
168-
getIsolationRegionInfo().printForDiagnostics(os);
179+
getIsolationRegionInfo().printForOneLineLogging(os);
169180
os << "].";
170181
}
171182

172183
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
184+
185+
private:
186+
bool hasIsolationRegionInfo() const { return bool(regionInfo); }
187+
188+
/// Set the isolation region info for this TrackableValueState. Private so it
189+
/// can only be used by RegionAnalysisValueMap::getTrackableValue.
190+
void setIsolationRegionInfo(SILIsolationInfo newRegionInfo) {
191+
assert(!regionInfo.has_value() &&
192+
"Can only call setIsolationRegionInfo once!\n");
193+
regionInfo = newRegionInfo;
194+
}
173195
};
174196

175197
/// The representative value of the equivalence class that makes up a tracked
@@ -358,9 +380,17 @@ class RegionAnalysisValueMap {
358380
TrackableValue
359381
getActorIntroducingRepresentative(SILInstruction *introducingInst,
360382
SILIsolationInfo isolation) const;
361-
bool mergeIsolationRegionInfo(SILValue value, SILIsolationInfo isolation);
362383
bool valueHasID(SILValue value, bool dumpIfHasNoID = false);
363384
Element lookupValueID(SILValue value);
385+
386+
/// Initialize a TrackableValue with a SILIsolationInfo that we already know
387+
/// instead of inferring.
388+
///
389+
/// If we successfully initialize \p value with \p info, returns
390+
/// {TrackableValue(), true}. If we already had a TrackableValue, we return
391+
/// {TrackableValue(), false}.
392+
std::pair<TrackableValue, bool>
393+
initializeTrackableValue(SILValue value, SILIsolationInfo info) const;
364394
};
365395

366396
class RegionAnalysisFunctionInfo {

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ SILValue createEmptyAndUndefValue(SILType ty, SILInstruction *insertionPoint,
616616
/// Check if a struct or its fields can have unreferenceable storage.
617617
bool findUnreferenceableStorage(StructDecl *decl, SILType structType,
618618
SILFunction *func);
619+
620+
SILValue getInitOfTemporaryAllocStack(AllocStackInst *asi);
621+
619622
} // end namespace swift
620623

621624
#endif // SWIFT_SILOPTIMIZER_UTILS_INSTOPTUTILS_H

0 commit comments

Comments
 (0)