Skip to content

[region-isolation] Some cleanups in preparation for later work #72476

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 16 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
24 changes: 20 additions & 4 deletions include/swift/AST/ActorIsolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,18 @@ class ActorIsolation {
return !(lhs == rhs);
}

void Profile(llvm::FoldingSetNodeID &id) {
id.AddInteger(getKind());
id.AddPointer(pointer);
id.AddBoolean(isolatedByPreconcurrency);
id.AddBoolean(silParsed);
id.AddInteger(parameterIndex);
}

friend llvm::hash_code hash_value(const ActorIsolation &state) {
return llvm::hash_combine(
state.kind, state.pointer, state.isolatedByPreconcurrency,
state.parameterIndex);
return llvm::hash_combine(state.kind, state.pointer,
state.isolatedByPreconcurrency, state.silParsed,
state.parameterIndex);
}

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

SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
SWIFT_DEBUG_DUMP {
print(llvm::dbgs());
llvm::dbgs() << '\n';
}

// Defined out of line to prevent linker errors since libswiftBasic would
// include this header exascerbating a layering violation where libswiftBasic
// depends on libswiftAST.
SWIFT_DEBUG_DUMPER(dumpForDiagnostics());
};

/// Determine how the given value declaration is isolated.
Expand Down
49 changes: 49 additions & 0 deletions include/swift/Basic/ImmutablePointerSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,55 @@ class ImmutablePointerSetFactory {
return NewNode;
}

template <typename U, typename... Args>
struct IsTrivialTypedPointerAndHasProfile {
constexpr static bool hasProfile =
std::is_same_v<decltype(std::remove_pointer<U>::type::Profile(
std::declval<Args>()...)),
void>;
constexpr static bool value =
hasProfile && std::is_trivial_v<U> && std::is_pointer_v<U>;
};

/// Emplace a new value with \p args if we do not yet have one. We allocate
/// the object with our bump ptr allocator, so we require that the type be
/// trivial.
template <typename... Args>
typename std::enable_if_t<IsTrivialTypedPointerAndHasProfile<
T, llvm::FoldingSetNodeID &, Args...>::value,
PtrSet> *
emplace(Args... args) {
llvm::FoldingSetNodeID ID;
using NoPointerTy = typename std::remove_pointer<T>::type;
NoPointerTy::Profile(ID, std::forward<Args>(args)...);

void *InsertPt;
if (auto *PSet = Set.FindNodeOrInsertPos(ID, InsertPt)) {
return PSet;
}

size_t NumElts = 1;
size_t MemSize = sizeof(PtrSet) + sizeof(NoPointerTy);

// Allocate the memory.
auto *Mem =
reinterpret_cast<PtrSet *>(Allocator.Allocate(MemSize, AllocAlignment));

// Copy in the pointers into the tail allocated memory. We do not need to do
// any sorting/uniquing ourselves since we assume that our users perform
// this task for us.
llvm::MutableArrayRef<NoPointerTy *> DataMem(
reinterpret_cast<NoPointerTy **>(&Mem[1]), NumElts);
NoPointerTy *type =
new (Allocator) NoPointerTy(std::forward<Args>(args)...);
DataMem[0] = type;

// Allocate the new node and insert it into the Set.
auto *NewNode = new (Mem) PtrSet(this, DataMem);
Set.InsertNode(NewNode, InsertPt);
return NewNode;
}

PtrSet *get(T value) {
llvm::FoldingSetNodeID ID;
ID.AddPointer(PtrTraits::getAsVoidPointer(value));
Expand Down
23 changes: 14 additions & 9 deletions include/swift/SILOptimizer/Analysis/RegionAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ inline bool isNonSendableType(SILType type, SILFunction *fn) {
return !type.isSendable(fn);
}

/// Return the ApplyIsolationCrossing for a specific \p inst if it
/// exists. Returns std::nullopt otherwise.
std::optional<ApplyIsolationCrossing>
getApplyIsolationCrossing(SILInstruction *inst);

// This is our PImpl type that we use to hide all of the internal details of
// the computation.
class PartitionOpTranslator;
Expand Down Expand Up @@ -135,7 +140,7 @@ using TrackedValueFlagSet = OptionSet<TrackableValueFlag>;
class regionanalysisimpl::TrackableValueState {
unsigned id;
TrackedValueFlagSet flagSet = {TrackableValueFlag::isMayAlias};
IsolationRegionInfo regionInfo = IsolationRegionInfo::getDisconnected();
SILIsolationInfo regionInfo = SILIsolationInfo::getDisconnected();

public:
TrackableValueState(unsigned newID) : id(newID) {}
Expand All @@ -152,19 +157,19 @@ class regionanalysisimpl::TrackableValueState {

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

IsolationRegionInfo::Kind getIsolationRegionInfoKind() const {
SILIsolationInfo::Kind getIsolationRegionInfoKind() const {
return regionInfo.getKind();
}

ActorIsolation getActorIsolation() const {
return regionInfo.getActorIsolation().value();
}

void mergeIsolationRegionInfo(IsolationRegionInfo newRegionInfo) {
void mergeIsolationRegionInfo(SILIsolationInfo newRegionInfo) {
regionInfo = regionInfo.merge(newRegionInfo);
}

IsolationRegionInfo getIsolationRegionInfo() const { return regionInfo; }
SILIsolationInfo getIsolationRegionInfo() const { return regionInfo; }

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

Expand Down Expand Up @@ -263,7 +268,7 @@ class regionanalysisimpl::TrackableValue {

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

IsolationRegionInfo getIsolationRegionInfo() const {
SILIsolationInfo getIsolationRegionInfo() const {
return valueState.getIsolationRegionInfo();
}

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

IsolationRegionInfo getIsolationRegion(Element trackableValueID) const;
IsolationRegionInfo getIsolationRegion(SILValue trackableValueID) const;
SILIsolationInfo getIsolationRegion(Element trackableValueID) const;
SILIsolationInfo getIsolationRegion(SILValue trackableValueID) const;

void print(llvm::raw_ostream &os) const;
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
Expand All @@ -357,8 +362,8 @@ class RegionAnalysisValueMap {
std::optional<TrackableValue> tryToTrackValue(SILValue value) const;
TrackableValue
getActorIntroducingRepresentative(SILInstruction *introducingInst,
IsolationRegionInfo isolation) const;
bool mergeIsolationRegionInfo(SILValue value, IsolationRegionInfo isolation);
SILIsolationInfo isolation) const;
bool mergeIsolationRegionInfo(SILValue value, SILIsolationInfo isolation);
bool valueHasID(SILValue value, bool dumpIfHasNoID = false);
TrackableValueID lookupValueID(SILValue value);
};
Expand Down
Loading