Skip to content

Commit a54ddae

Browse files
committed
[region-isolation] Move RepresentativeValue from RegionAnalysis.h -> PartitionUtils.h and add APIs for mapping an ElementID -> Representative.
This is just moving up the declaration in the chain of dependencies so that I can write logic in PartitionUtils.h using it. I also added entrypoints to lookup the ReprensetativeValue for our various emitters. (cherry picked from commit ace94b0)
1 parent caf944e commit a54ddae

File tree

4 files changed

+104
-82
lines changed

4 files changed

+104
-82
lines changed

include/swift/SILOptimizer/Analysis/RegionAnalysis.h

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class BlockPartitionState {
114114

115115
class TrackableValue;
116116
class TrackableValueState;
117-
class RepresentativeValue;
118117

119118
enum class TrackableValueFlag {
120119
/// Base value that says a value is uniquely represented and is
@@ -208,53 +207,6 @@ class regionanalysisimpl::TrackableValueState {
208207
}
209208
};
210209

211-
/// The representative value of the equivalence class that makes up a tracked
212-
/// value.
213-
///
214-
/// We use a wrapper struct here so that we can inject "fake" actor isolated
215-
/// values into the regions of values that become merged into an actor by
216-
/// calling a function without a non-sendable result.
217-
class regionanalysisimpl::RepresentativeValue {
218-
friend llvm::DenseMapInfo<RepresentativeValue>;
219-
220-
using InnerType = PointerUnion<SILValue, SILInstruction *>;
221-
222-
/// If this is set to a SILValue then it is the actual represented value. If
223-
/// it is set to a SILInstruction, then this is a "fake" representative value
224-
/// used to inject actor isolatedness. The instruction stored is the
225-
/// instruction that introduced the actor isolated-ness.
226-
InnerType value;
227-
228-
public:
229-
RepresentativeValue() : value() {}
230-
RepresentativeValue(SILValue value) : value(value) {}
231-
RepresentativeValue(SILInstruction *actorRegionInst)
232-
: value(actorRegionInst) {}
233-
234-
operator bool() const { return bool(value); }
235-
236-
void print(llvm::raw_ostream &os) const {
237-
if (auto *inst = value.dyn_cast<SILInstruction *>()) {
238-
os << "ActorRegionIntroducingInst: " << *inst;
239-
return;
240-
}
241-
242-
os << *value.get<SILValue>();
243-
}
244-
245-
SILValue getValue() const { return value.get<SILValue>(); }
246-
SILValue maybeGetValue() const { return value.dyn_cast<SILValue>(); }
247-
bool hasRegionIntroducingInst() const { return value.is<SILInstruction *>(); }
248-
SILInstruction *getActorRegionIntroducingInst() const {
249-
return value.get<SILInstruction *>();
250-
}
251-
252-
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
253-
254-
private:
255-
RepresentativeValue(InnerType value) : value(value) {}
256-
};
257-
258210
/// A tuple consisting of a base value and its value state.
259211
///
260212
/// DISCUSSION: We are computing regions among equivalence classes of values
@@ -336,7 +288,6 @@ class RegionAnalysisValueMap {
336288
using Region = PartitionPrimitives::Region;
337289
using TrackableValue = regionanalysisimpl::TrackableValue;
338290
using TrackableValueState = regionanalysisimpl::TrackableValueState;
339-
using RepresentativeValue = regionanalysisimpl::RepresentativeValue;
340291

341292
private:
342293
/// A map from the representative of an equivalence class of values to their
@@ -365,6 +316,10 @@ class RegionAnalysisValueMap {
365316
/// value" returns an empty SILValue.
366317
SILValue maybeGetRepresentative(Element trackableValueID) const;
367318

319+
/// Returns the value for this instruction if it isn't a fake "represenative
320+
/// value" to inject actor isolatedness. Asserts in such a case.
321+
RepresentativeValue getRepresentativeValue(Element trackableValueID) const;
322+
368323
/// Returns the fake "representative value" for this element if it
369324
/// exists. Returns nullptr otherwise.
370325
SILInstruction *maybeGetActorIntroducingInst(Element trackableValueID) const;
@@ -576,37 +531,4 @@ class RegionAnalysis final
576531

577532
} // namespace swift
578533

579-
namespace llvm {
580-
581-
inline llvm::raw_ostream &
582-
operator<<(llvm::raw_ostream &os,
583-
const swift::regionanalysisimpl::RepresentativeValue &value) {
584-
value.print(os);
585-
return os;
586-
}
587-
588-
template <>
589-
struct DenseMapInfo<swift::regionanalysisimpl::RepresentativeValue> {
590-
using RepresentativeValue = swift::regionanalysisimpl::RepresentativeValue;
591-
using InnerType = RepresentativeValue::InnerType;
592-
using InnerDenseMapInfo = DenseMapInfo<InnerType>;
593-
594-
static RepresentativeValue getEmptyKey() {
595-
return RepresentativeValue(InnerDenseMapInfo::getEmptyKey());
596-
}
597-
static RepresentativeValue getTombstoneKey() {
598-
return RepresentativeValue(InnerDenseMapInfo::getTombstoneKey());
599-
}
600-
601-
static unsigned getHashValue(RepresentativeValue value) {
602-
return InnerDenseMapInfo::getHashValue(value.value);
603-
}
604-
605-
static bool isEqual(RepresentativeValue LHS, RepresentativeValue RHS) {
606-
return InnerDenseMapInfo::isEqual(LHS.value, RHS.value);
607-
}
608-
};
609-
610-
} // namespace llvm
611-
612534
#endif

include/swift/SILOptimizer/Utils/PartitionUtils.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,53 @@ namespace swift {
100100
class Partition;
101101
class TransferringOperandToStateMap;
102102

103+
/// The representative value of the equivalence class that makes up a tracked
104+
/// value.
105+
///
106+
/// We use a wrapper struct here so that we can inject "fake" actor isolated
107+
/// values into the regions of values that become merged into an actor by
108+
/// calling a function without a non-sendable result.
109+
class RepresentativeValue {
110+
friend llvm::DenseMapInfo<RepresentativeValue>;
111+
112+
using InnerType = PointerUnion<SILValue, SILInstruction *>;
113+
114+
/// If this is set to a SILValue then it is the actual represented value. If
115+
/// it is set to a SILInstruction, then this is a "fake" representative value
116+
/// used to inject actor isolatedness. The instruction stored is the
117+
/// instruction that introduced the actor isolated-ness.
118+
InnerType value;
119+
120+
public:
121+
RepresentativeValue() : value() {}
122+
RepresentativeValue(SILValue value) : value(value) {}
123+
RepresentativeValue(SILInstruction *actorRegionInst)
124+
: value(actorRegionInst) {}
125+
126+
operator bool() const { return bool(value); }
127+
128+
void print(llvm::raw_ostream &os) const {
129+
if (auto *inst = value.dyn_cast<SILInstruction *>()) {
130+
os << "ActorRegionIntroducingInst: " << *inst;
131+
return;
132+
}
133+
134+
os << *value.get<SILValue>();
135+
}
136+
137+
SILValue getValue() const { return value.get<SILValue>(); }
138+
SILValue maybeGetValue() const { return value.dyn_cast<SILValue>(); }
139+
bool hasRegionIntroducingInst() const { return value.is<SILInstruction *>(); }
140+
SILInstruction *getActorRegionIntroducingInst() const {
141+
return value.get<SILInstruction *>();
142+
}
143+
144+
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
145+
146+
private:
147+
RepresentativeValue(InnerType value) : value(value) {}
148+
};
149+
103150
/// A persistent data structure that is used to "rewind" partition history so
104151
/// that we can discover when values become part of the same region.
105152
///
@@ -1029,6 +1076,10 @@ struct PartitionOpEvaluator {
10291076
return asImpl().getRepresentative(value);
10301077
}
10311078

1079+
RepresentativeValue getRepresentativeValue(Element element) const {
1080+
return asImpl().getRepresentativeValue(element);
1081+
}
1082+
10321083
/// Apply \p op to the partition op.
10331084
void apply(const PartitionOp &op) const {
10341085
if (shouldEmitVerboseLogging()) {
@@ -1409,6 +1460,10 @@ struct PartitionOpEvaluatorBaseImpl : PartitionOpEvaluator<Subclass> {
14091460
/// have one.
14101461
SILValue getRepresentative(SILValue value) const { return SILValue(); }
14111462

1463+
RepresentativeValue getRepresentativeValue(Element element) const {
1464+
return RepresentativeValue();
1465+
}
1466+
14121467
/// Check if the representative value of \p elt is closure captured at \p
14131468
/// op.
14141469
///
@@ -1441,4 +1496,36 @@ struct PartitionOpEvaluatorBasic final
14411496

14421497
} // namespace swift
14431498

1499+
namespace llvm {
1500+
1501+
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
1502+
const swift::RepresentativeValue &value) {
1503+
value.print(os);
1504+
return os;
1505+
}
1506+
1507+
template <>
1508+
struct DenseMapInfo<swift::RepresentativeValue> {
1509+
using RepresentativeValue = swift::RepresentativeValue;
1510+
using InnerType = RepresentativeValue::InnerType;
1511+
using InnerDenseMapInfo = DenseMapInfo<InnerType>;
1512+
1513+
static RepresentativeValue getEmptyKey() {
1514+
return RepresentativeValue(InnerDenseMapInfo::getEmptyKey());
1515+
}
1516+
static RepresentativeValue getTombstoneKey() {
1517+
return RepresentativeValue(InnerDenseMapInfo::getTombstoneKey());
1518+
}
1519+
1520+
static unsigned getHashValue(RepresentativeValue value) {
1521+
return InnerDenseMapInfo::getHashValue(value.value);
1522+
}
1523+
1524+
static bool isEqual(RepresentativeValue LHS, RepresentativeValue RHS) {
1525+
return InnerDenseMapInfo::isEqual(LHS.value, RHS.value);
1526+
}
1527+
};
1528+
1529+
} // namespace llvm
1530+
14441531
#endif // SWIFT_PARTITIONUTILS_H

lib/SILOptimizer/Analysis/RegionAnalysis.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,6 +3264,10 @@ bool BlockPartitionState::recomputeExitFromEntry(
32643264
.maybeGetValue();
32653265
}
32663266

3267+
RepresentativeValue getRepresentativeValue(Element element) const {
3268+
return translator.getValueMap().getRepresentativeValue(element);
3269+
}
3270+
32673271
bool isClosureCaptured(Element elt, Operand *op) const {
32683272
auto iter = translator.getValueForId(elt);
32693273
if (!iter)
@@ -3502,6 +3506,11 @@ RegionAnalysisValueMap::maybeGetRepresentative(Element trackableValueID) const {
35023506
return getValueForId(trackableValueID)->getRepresentative().maybeGetValue();
35033507
}
35043508

3509+
RepresentativeValue
3510+
RegionAnalysisValueMap::getRepresentativeValue(Element trackableValueID) const {
3511+
return getValueForId(trackableValueID)->getRepresentative();
3512+
}
3513+
35053514
SILIsolationInfo
35063515
RegionAnalysisValueMap::getIsolationRegion(Element trackableValueID) const {
35073516
auto iter = getValueForId(trackableValueID);

lib/SILOptimizer/Mandatory/TransferNonSendable.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,10 @@ struct DiagnosticEvaluator final
20292029
.maybeGetValue();
20302030
}
20312031

2032+
RepresentativeValue getRepresentativeValue(Element element) const {
2033+
return info->getValueMap().getRepresentativeValue(element);
2034+
}
2035+
20322036
bool isClosureCaptured(Element element, Operand *op) const {
20332037
auto value = info->getValueMap().maybeGetRepresentative(element);
20342038
if (!value)

0 commit comments

Comments
 (0)