Skip to content

Commit 2603c84

Browse files
authored
Merge pull request #72476 from gottesmm/cleanups
[region-isolation] Some cleanups in preparation for later work
2 parents 4f6ed73 + 56a3270 commit 2603c84

File tree

10 files changed

+528
-406
lines changed

10 files changed

+528
-406
lines changed

include/swift/AST/ActorIsolation.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,18 @@ class ActorIsolation {
245245
return !(lhs == rhs);
246246
}
247247

248+
void Profile(llvm::FoldingSetNodeID &id) {
249+
id.AddInteger(getKind());
250+
id.AddPointer(pointer);
251+
id.AddBoolean(isolatedByPreconcurrency);
252+
id.AddBoolean(silParsed);
253+
id.AddInteger(parameterIndex);
254+
}
255+
248256
friend llvm::hash_code hash_value(const ActorIsolation &state) {
249-
return llvm::hash_combine(
250-
state.kind, state.pointer, state.isolatedByPreconcurrency,
251-
state.parameterIndex);
257+
return llvm::hash_combine(state.kind, state.pointer,
258+
state.isolatedByPreconcurrency, state.silParsed,
259+
state.parameterIndex);
252260
}
253261

254262
void print(llvm::raw_ostream &os) const {
@@ -278,7 +286,15 @@ class ActorIsolation {
278286
void printForDiagnostics(llvm::raw_ostream &os,
279287
StringRef openingQuotationMark = "'") const;
280288

281-
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
289+
SWIFT_DEBUG_DUMP {
290+
print(llvm::dbgs());
291+
llvm::dbgs() << '\n';
292+
}
293+
294+
// Defined out of line to prevent linker errors since libswiftBasic would
295+
// include this header exascerbating a layering violation where libswiftBasic
296+
// depends on libswiftAST.
297+
SWIFT_DEBUG_DUMPER(dumpForDiagnostics());
282298
};
283299

284300
/// Determine how the given value declaration is isolated.

include/swift/Basic/ImmutablePointerSet.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,55 @@ class ImmutablePointerSetFactory {
250250
return NewNode;
251251
}
252252

253+
template <typename U, typename... Args>
254+
struct IsTrivialTypedPointerAndHasProfile {
255+
constexpr static bool hasProfile =
256+
std::is_same_v<decltype(std::remove_pointer<U>::type::Profile(
257+
std::declval<Args>()...)),
258+
void>;
259+
constexpr static bool value =
260+
hasProfile && std::is_trivial_v<U> && std::is_pointer_v<U>;
261+
};
262+
263+
/// Emplace a new value with \p args if we do not yet have one. We allocate
264+
/// the object with our bump ptr allocator, so we require that the type be
265+
/// trivial.
266+
template <typename... Args>
267+
typename std::enable_if_t<IsTrivialTypedPointerAndHasProfile<
268+
T, llvm::FoldingSetNodeID &, Args...>::value,
269+
PtrSet> *
270+
emplace(Args... args) {
271+
llvm::FoldingSetNodeID ID;
272+
using NoPointerTy = typename std::remove_pointer<T>::type;
273+
NoPointerTy::Profile(ID, std::forward<Args>(args)...);
274+
275+
void *InsertPt;
276+
if (auto *PSet = Set.FindNodeOrInsertPos(ID, InsertPt)) {
277+
return PSet;
278+
}
279+
280+
size_t NumElts = 1;
281+
size_t MemSize = sizeof(PtrSet) + sizeof(NoPointerTy);
282+
283+
// Allocate the memory.
284+
auto *Mem =
285+
reinterpret_cast<PtrSet *>(Allocator.Allocate(MemSize, AllocAlignment));
286+
287+
// Copy in the pointers into the tail allocated memory. We do not need to do
288+
// any sorting/uniquing ourselves since we assume that our users perform
289+
// this task for us.
290+
llvm::MutableArrayRef<NoPointerTy *> DataMem(
291+
reinterpret_cast<NoPointerTy **>(&Mem[1]), NumElts);
292+
NoPointerTy *type =
293+
new (Allocator) NoPointerTy(std::forward<Args>(args)...);
294+
DataMem[0] = type;
295+
296+
// Allocate the new node and insert it into the Set.
297+
auto *NewNode = new (Mem) PtrSet(this, DataMem);
298+
Set.InsertNode(NewNode, InsertPt);
299+
return NewNode;
300+
}
301+
253302
PtrSet *get(T value) {
254303
llvm::FoldingSetNodeID ID;
255304
ID.AddPointer(PtrTraits::getAsVoidPointer(value));

include/swift/SILOptimizer/Analysis/RegionAnalysis.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ inline bool isNonSendableType(SILType type, SILFunction *fn) {
5252
return !type.isSendable(fn);
5353
}
5454

55+
/// Return the ApplyIsolationCrossing for a specific \p inst if it
56+
/// exists. Returns std::nullopt otherwise.
57+
std::optional<ApplyIsolationCrossing>
58+
getApplyIsolationCrossing(SILInstruction *inst);
59+
5560
// This is our PImpl type that we use to hide all of the internal details of
5661
// the computation.
5762
class PartitionOpTranslator;
@@ -135,7 +140,7 @@ using TrackedValueFlagSet = OptionSet<TrackableValueFlag>;
135140
class regionanalysisimpl::TrackableValueState {
136141
unsigned id;
137142
TrackedValueFlagSet flagSet = {TrackableValueFlag::isMayAlias};
138-
IsolationRegionInfo regionInfo = IsolationRegionInfo::getDisconnected();
143+
SILIsolationInfo regionInfo = SILIsolationInfo::getDisconnected();
139144

140145
public:
141146
TrackableValueState(unsigned newID) : id(newID) {}
@@ -152,19 +157,19 @@ class regionanalysisimpl::TrackableValueState {
152157

153158
bool isNonSendable() const { return !isSendable(); }
154159

155-
IsolationRegionInfo::Kind getIsolationRegionInfoKind() const {
160+
SILIsolationInfo::Kind getIsolationRegionInfoKind() const {
156161
return regionInfo.getKind();
157162
}
158163

159164
ActorIsolation getActorIsolation() const {
160165
return regionInfo.getActorIsolation().value();
161166
}
162167

163-
void mergeIsolationRegionInfo(IsolationRegionInfo newRegionInfo) {
168+
void mergeIsolationRegionInfo(SILIsolationInfo newRegionInfo) {
164169
regionInfo = regionInfo.merge(newRegionInfo);
165170
}
166171

167-
IsolationRegionInfo getIsolationRegionInfo() const { return regionInfo; }
172+
SILIsolationInfo getIsolationRegionInfo() const { return regionInfo; }
168173

169174
TrackableValueID getID() const { return TrackableValueID(id); }
170175

@@ -263,7 +268,7 @@ class regionanalysisimpl::TrackableValue {
263268

264269
bool isNonSendable() const { return !isSendable(); }
265270

266-
IsolationRegionInfo getIsolationRegionInfo() const {
271+
SILIsolationInfo getIsolationRegionInfo() const {
267272
return valueState.getIsolationRegionInfo();
268273
}
269274

@@ -333,8 +338,8 @@ class RegionAnalysisValueMap {
333338
/// exists. Returns nullptr otherwise.
334339
SILInstruction *maybeGetActorIntroducingInst(Element trackableValueID) const;
335340

336-
IsolationRegionInfo getIsolationRegion(Element trackableValueID) const;
337-
IsolationRegionInfo getIsolationRegion(SILValue trackableValueID) const;
341+
SILIsolationInfo getIsolationRegion(Element trackableValueID) const;
342+
SILIsolationInfo getIsolationRegion(SILValue trackableValueID) const;
338343

339344
void print(llvm::raw_ostream &os) const;
340345
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
@@ -357,8 +362,8 @@ class RegionAnalysisValueMap {
357362
std::optional<TrackableValue> tryToTrackValue(SILValue value) const;
358363
TrackableValue
359364
getActorIntroducingRepresentative(SILInstruction *introducingInst,
360-
IsolationRegionInfo isolation) const;
361-
bool mergeIsolationRegionInfo(SILValue value, IsolationRegionInfo isolation);
365+
SILIsolationInfo isolation) const;
366+
bool mergeIsolationRegionInfo(SILValue value, SILIsolationInfo isolation);
362367
bool valueHasID(SILValue value, bool dumpIfHasNoID = false);
363368
TrackableValueID lookupValueID(SILValue value);
364369
};

0 commit comments

Comments
 (0)