Skip to content

Commit bfa910d

Browse files
authored
Merge pull request swiftlang#72959 from gottesmm/tns-upstream-2
[region-isolation] Do not look through begin_borrow or move_value if they are marked as a var_decl.
2 parents ec19901 + 3c29997 commit bfa910d

19 files changed

+1213
-454
lines changed

include/swift/SIL/SILType.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,16 @@ class SILType {
917917

918918
bool isMarkedAsImmortal() const;
919919

920+
/// Returns true if this type is an actor type. Returns false if this is any
921+
/// other type. This includes distributed actors. To check for distributed
922+
/// actors and actors, use isAnyActor().
920923
bool isActor() const { return getASTType()->isActorType(); }
921924

925+
bool isDistributedActor() const { return getASTType()->isDistributedActor(); }
926+
927+
/// Returns true if this type is an actor or a distributed actor.
928+
bool isAnyActor() const { return getASTType()->isAnyActorType(); }
929+
922930
/// Returns true if this function conforms to the Sendable protocol.
923931
bool isSendable(SILFunction *fn) const;
924932

include/swift/SILOptimizer/Analysis/RegionAnalysis.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class RegionAnalysisFunctionInfo;
2828
namespace regionanalysisimpl {
2929

3030
using TransferringOperandSetFactory = Partition::TransferringOperandSetFactory;
31-
using TrackableValueID = PartitionPrimitives::Element;
31+
using Element = PartitionPrimitives::Element;
3232
using Region = PartitionPrimitives::Region;
3333

3434
/// Check if the passed in type is NonSendable.
@@ -175,7 +175,7 @@ class regionanalysisimpl::TrackableValueState {
175175

176176
SILIsolationInfo getIsolationRegionInfo() const { return regionInfo; }
177177

178-
TrackableValueID getID() const { return TrackableValueID(id); }
178+
Element getID() const { return Element(id); }
179179

180180
void addFlag(TrackableValueFlag flag) { flagSet |= flag; }
181181

@@ -186,7 +186,7 @@ class regionanalysisimpl::TrackableValueState {
186186
<< "][is_no_alias: " << (isNoAlias() ? "yes" : "no")
187187
<< "][is_sendable: " << (isSendable() ? "yes" : "no")
188188
<< "][region_value_kind: ";
189-
getIsolationRegionInfo().print(os);
189+
getIsolationRegionInfo().printForDiagnostics(os);
190190
os << "].";
191191
}
192192

@@ -276,9 +276,7 @@ class regionanalysisimpl::TrackableValue {
276276
return valueState.getIsolationRegionInfo();
277277
}
278278

279-
TrackableValueID getID() const {
280-
return TrackableValueID(valueState.getID());
281-
}
279+
Element getID() const { return Element(valueState.getID()); }
282280

283281
/// Return the representative value of this equivalence class of values.
284282
RepresentativeValue getRepresentative() const { return representativeValue; }
@@ -289,6 +287,11 @@ class regionanalysisimpl::TrackableValue {
289287
/// parameter.
290288
bool isTransferringParameter() const;
291289

290+
void printIsolationInfo(SmallString<64> &outString) const {
291+
llvm::raw_svector_ostream os(outString);
292+
getIsolationRegionInfo().printForDiagnostics(os);
293+
}
294+
292295
void print(llvm::raw_ostream &os) const {
293296
os << "TrackableValue. State: ";
294297
valueState.print(os);
@@ -315,7 +318,6 @@ class RegionAnalysisValueMap {
315318
using Region = PartitionPrimitives::Region;
316319
using TrackableValue = regionanalysisimpl::TrackableValue;
317320
using TrackableValueState = regionanalysisimpl::TrackableValueState;
318-
using TrackableValueID = Element;
319321
using RepresentativeValue = regionanalysisimpl::RepresentativeValue;
320322

321323
private:
@@ -369,14 +371,14 @@ class RegionAnalysisValueMap {
369371
SILInstruction *introducingInst) const;
370372

371373
private:
372-
std::optional<TrackableValue> getValueForId(TrackableValueID id) const;
374+
std::optional<TrackableValue> getValueForId(Element id) const;
373375
std::optional<TrackableValue> tryToTrackValue(SILValue value) const;
374376
TrackableValue
375377
getActorIntroducingRepresentative(SILInstruction *introducingInst,
376378
SILIsolationInfo isolation) const;
377379
bool mergeIsolationRegionInfo(SILValue value, SILIsolationInfo isolation);
378380
bool valueHasID(SILValue value, bool dumpIfHasNoID = false);
379-
TrackableValueID lookupValueID(SILValue value);
381+
Element lookupValueID(SILValue value);
380382
};
381383

382384
class RegionAnalysisFunctionInfo {

include/swift/SILOptimizer/Utils/PartitionUtils.h

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,24 @@ class SILIsolationInfo {
118118

119119
/// This is the value that we got isolation from if we were able to find
120120
/// one. Used for isolation history.
121-
SILValue isolationSource;
121+
SILValue isolatedValue;
122122

123-
SILIsolationInfo(ActorIsolation actorIsolation, SILValue isolationSource)
123+
/// If set this is the SILValue that represents the actor instance that we
124+
/// derived isolatedValue from.
125+
SILValue actorInstance;
126+
127+
SILIsolationInfo(ActorIsolation actorIsolation, SILValue isolatedValue,
128+
SILValue actorInstance)
124129
: kind(Actor), actorIsolation(actorIsolation),
125-
isolationSource(isolationSource) {}
130+
isolatedValue(isolatedValue), actorInstance(actorInstance) {
131+
assert((!actorInstance ||
132+
(actorIsolation.getKind() == ActorIsolation::ActorInstance &&
133+
actorInstance->getType().isAnyActor())) &&
134+
"actorInstance must be an actor if it is non-empty");
135+
}
126136

127-
SILIsolationInfo(Kind kind, SILValue isolationSource)
128-
: kind(kind), actorIsolation(), isolationSource(isolationSource) {}
137+
SILIsolationInfo(Kind kind, SILValue isolatedValue)
138+
: kind(kind), actorIsolation(), isolatedValue(isolatedValue) {}
129139

130140
SILIsolationInfo(Kind kind) : kind(kind), actorIsolation() {}
131141

@@ -151,51 +161,68 @@ class SILIsolationInfo {
151161

152162
void printForDiagnostics(llvm::raw_ostream &os) const;
153163

164+
SWIFT_DEBUG_DUMPER(dumpForDiagnostics()) {
165+
printForDiagnostics(llvm::dbgs());
166+
llvm::dbgs() << '\n';
167+
}
168+
154169
ActorIsolation getActorIsolation() const {
155170
assert(kind == Actor);
156171
return actorIsolation;
157172
}
158173

159-
// If we are actor or task isolated and could find a specific value that
160-
// caused the isolation, put it here. Used for isolation history.
174+
/// If we are actor or task isolated and could find a specific value that
175+
/// caused the isolation, put it here. Used for isolation history.
161176
SILValue getIsolatedValue() const {
162177
assert(kind == Task || kind == Actor);
163-
return isolationSource;
178+
return isolatedValue;
179+
}
180+
181+
/// Return the specific SILValue for the actor that our isolated value is
182+
/// isolated to if one exists.
183+
SILValue getActorInstance() const {
184+
assert(kind == Actor);
185+
return actorInstance;
164186
}
165187

166188
bool hasActorIsolation() const { return kind == Actor; }
167189

168190
bool hasIsolatedValue() const {
169-
return (kind == Task || kind == Actor) && bool(isolationSource);
191+
return (kind == Task || kind == Actor) && bool(isolatedValue);
170192
}
171193

172194
[[nodiscard]] SILIsolationInfo merge(SILIsolationInfo other) const;
173195

174196
SILIsolationInfo withActorIsolated(SILValue isolatedValue,
197+
SILValue actorInstance,
175198
ActorIsolation isolation) {
176-
return SILIsolationInfo::getActorIsolated(isolatedValue, isolation);
199+
return SILIsolationInfo::getActorIsolated(isolatedValue, actorInstance,
200+
isolation);
177201
}
178202

179203
static SILIsolationInfo getDisconnected() { return {Kind::Disconnected}; }
180204

181205
static SILIsolationInfo getActorIsolated(SILValue isolatedValue,
206+
SILValue actorInstance,
182207
ActorIsolation actorIsolation) {
183-
return {actorIsolation, isolatedValue};
208+
return {actorIsolation, isolatedValue, actorInstance};
184209
}
185210

186211
static SILIsolationInfo getActorIsolated(SILValue isolatedValue,
212+
SILValue actorInstance,
187213
NominalTypeDecl *typeDecl) {
188-
if (typeDecl->isActor())
189-
return {ActorIsolation::forActorInstanceSelf(typeDecl), isolatedValue};
214+
if (typeDecl->isAnyActor())
215+
return {ActorIsolation::forActorInstanceSelf(typeDecl), isolatedValue,
216+
actorInstance};
190217
auto isolation = swift::getActorIsolation(typeDecl);
191218
if (isolation.isGlobalActor())
192-
return {isolation, isolatedValue};
219+
return {isolation, isolatedValue, actorInstance};
193220
return {};
194221
}
195222

196223
static SILIsolationInfo getGlobalActorIsolated(SILValue value,
197224
Type globalActorType) {
198-
return getActorIsolated(value,
225+
return getActorIsolated(value, SILValue() /*no actor instance*/,
199226
ActorIsolation::forGlobalActor(globalActorType));
200227
}
201228

@@ -207,7 +234,15 @@ class SILIsolationInfo {
207234
static SILIsolationInfo get(SILInstruction *inst);
208235

209236
/// Attempt to infer the isolation region info for \p arg.
210-
static SILIsolationInfo get(SILFunctionArgument *arg);
237+
static SILIsolationInfo get(SILArgument *arg);
238+
239+
static SILIsolationInfo get(SILValue value) {
240+
if (auto *arg = dyn_cast<SILArgument>(value))
241+
return get(arg);
242+
if (auto *inst = dyn_cast<SingleValueInstruction>(value))
243+
return get(inst);
244+
return {};
245+
}
211246

212247
bool hasSameIsolation(ActorIsolation actorIsolation) const;
213248

@@ -670,7 +705,7 @@ class Partition {
670705

671706
/// Track a label that is guaranteed to be strictly larger than all in use,
672707
/// and therefore safe for use as a fresh label.
673-
Region fresh_label = Region(0);
708+
Region freshLabel = Region(0);
674709

675710
/// An immutable data structure that we use to push/pop isolation history.
676711
IsolationHistory history;
@@ -841,7 +876,7 @@ class Partition {
841876
llvm::dbgs() << "Partition";
842877
if (canonical)
843878
llvm::dbgs() << "(canonical)";
844-
llvm::dbgs() << "(fresh=" << fresh_label << "){";
879+
llvm::dbgs() << "(fresh=" << freshLabel << "){";
845880
for (const auto &[i, label] : elementToRegionMap)
846881
llvm::dbgs() << "[" << i << ": " << label << "] ";
847882
llvm::dbgs() << "}\n";

include/swift/SILOptimizer/Utils/VariableNameUtils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ class VariableNameInferrer {
192192

193193
StringRef getName() const { return resultingString; }
194194

195+
/// Given a specific SILValue, construct a VariableNameInferrer and use it to
196+
/// attempt to infer an identifier for the value.
197+
static std::optional<Identifier> inferName(SILValue value);
198+
199+
/// Given a specific SILValue, construct a VariableNameInferrer and use it to
200+
/// attempt to infer an identifier for the value and a named value.
201+
static std::optional<std::pair<Identifier, SILValue>>
202+
inferNameAndRoot(SILValue value);
203+
195204
private:
196205
void drainVariableNamePath();
197206
void popSingleVariableName();

0 commit comments

Comments
 (0)