Skip to content

Commit c49fb33

Browse files
committed
[region-isolation] Change look through check to use a switch instead of an isa list.
I am doing this because I discovered that mark_dependence was being misclassified as assigned even though we wanted to treat it as being look through in its first operand and since I kept on hitting merge conflicts with the isa list option. With this commit: 1. I am converting it to a switch in a utility function. That should prevent the merge conflicts. 2. I also fixed mark_dependence's semantics. 3. I added an assert into CONSTANT_TRANSLATION(..., LookThrough) to make sure that the switch and the CONSTANT_TRANSLATION code stays in sync.
1 parent e7475c8 commit c49fb33

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

lib/SILOptimizer/Analysis/RegionAnalysis.cpp

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,45 @@ struct UseDefChainVisitor
177177

178178
} // namespace
179179

180+
/// Classify an instructions as look through when we are looking through
181+
/// values. We assert that all instructions that are CONSTANT_TRANSLATION
182+
/// LookThrough to make sure they stay in sync.
183+
static bool isStaticallyLookThroughInst(SILInstruction *inst) {
184+
switch (inst->getKind()) {
185+
default:
186+
return false;
187+
case SILInstructionKind::BeginAccessInst:
188+
case SILInstructionKind::BeginBorrowInst:
189+
case SILInstructionKind::BeginDeallocRefInst:
190+
case SILInstructionKind::BridgeObjectToRefInst:
191+
case SILInstructionKind::CopyValueInst:
192+
case SILInstructionKind::CopyableToMoveOnlyWrapperAddrInst:
193+
case SILInstructionKind::CopyableToMoveOnlyWrapperValueInst:
194+
case SILInstructionKind::DestructureStructInst:
195+
case SILInstructionKind::DestructureTupleInst:
196+
case SILInstructionKind::EndCOWMutationInst:
197+
case SILInstructionKind::EndInitLetRefInst:
198+
case SILInstructionKind::ExplicitCopyValueInst:
199+
case SILInstructionKind::InitEnumDataAddrInst:
200+
case SILInstructionKind::MarkDependenceInst:
201+
case SILInstructionKind::MarkUninitializedInst:
202+
case SILInstructionKind::MarkUnresolvedNonCopyableValueInst:
203+
case SILInstructionKind::MarkUnresolvedReferenceBindingInst:
204+
case SILInstructionKind::MoveOnlyWrapperToCopyableAddrInst:
205+
case SILInstructionKind::MoveOnlyWrapperToCopyableBoxInst:
206+
case SILInstructionKind::MoveOnlyWrapperToCopyableValueInst:
207+
case SILInstructionKind::MoveValueInst:
208+
case SILInstructionKind::OpenExistentialAddrInst:
209+
case SILInstructionKind::ProjectBlockStorageInst:
210+
case SILInstructionKind::ProjectBoxInst:
211+
case SILInstructionKind::RefToBridgeObjectInst:
212+
case SILInstructionKind::UncheckedRefCastInst:
213+
case SILInstructionKind::UncheckedTakeEnumDataAddrInst:
214+
case SILInstructionKind::UpcastInst:
215+
return true;
216+
}
217+
}
218+
180219
static SILValue getUnderlyingTrackedObjectValue(SILValue value) {
181220
auto *fn = value->getFunction();
182221
SILValue result = value;
@@ -189,14 +228,16 @@ static SILValue getUnderlyingTrackedObjectValue(SILValue value) {
189228
temp = lookThroughOwnershipInsts(temp);
190229

191230
if (auto *svi = dyn_cast<SingleValueInstruction>(temp)) {
192-
if (isa<ExplicitCopyValueInst, CopyableToMoveOnlyWrapperValueInst,
193-
MoveOnlyWrapperToCopyableValueInst,
194-
MoveOnlyWrapperToCopyableBoxInst, BeginAccessInst,
195-
MarkDependenceInst>(svi) ||
196-
isIdentityPreservingRefCast(svi)) {
231+
if (isStaticallyLookThroughInst(svi)) {
197232
temp = svi->getOperand(0);
198233
}
199234

235+
if (auto cast = SILDynamicCastInst::getAs(svi)) {
236+
if (cast.isRCIdentityPreserving()) {
237+
temp = svi->getOperand(0);
238+
}
239+
}
240+
200241
// If we have a cast and our operand and result are non-Sendable, treat it
201242
// as a look through.
202243
if (isa<UncheckedTrivialBitCastInst, UncheckedBitwiseCastInst,
@@ -223,13 +264,10 @@ static SILValue getUnderlyingTrackedObjectValue(SILValue value) {
223264
}
224265
}
225266

226-
if (auto *dsi = dyn_cast_or_null<DestructureStructInst>(
227-
temp->getDefiningInstruction())) {
228-
temp = dsi->getOperand();
229-
}
230-
if (auto *dti = dyn_cast_or_null<DestructureTupleInst>(
231-
temp->getDefiningInstruction())) {
232-
temp = dti->getOperand();
267+
if (auto *inst = temp->getDefiningInstruction()) {
268+
if (isStaticallyLookThroughInst(inst)) {
269+
temp = inst->getOperand(0);
270+
}
233271
}
234272

235273
if (temp != result) {
@@ -1895,6 +1933,12 @@ void PartitionOpBuilder::print(llvm::raw_ostream &os) const {
18951933

18961934
#define CONSTANT_TRANSLATION(INST, Kind) \
18971935
TranslationSemantics PartitionOpTranslator::visit##INST(INST *inst) { \
1936+
assert((TranslationSemantics::Kind != TranslationSemantics::LookThrough || \
1937+
isStaticallyLookThroughInst(inst)) && \
1938+
"Out of sync?!"); \
1939+
assert((TranslationSemantics::Kind == TranslationSemantics::LookThrough || \
1940+
!isStaticallyLookThroughInst(inst)) && \
1941+
"Out of sync?!"); \
18981942
return TranslationSemantics::Kind; \
18991943
}
19001944

@@ -2297,7 +2341,7 @@ PartitionOpTranslator::visitRefToRawPointerInst(RefToRawPointerInst *r) {
22972341

22982342
TranslationSemantics
22992343
PartitionOpTranslator::visitMarkDependenceInst(MarkDependenceInst *mdi) {
2300-
translateSILAssign(mdi, mdi->getValue());
2344+
translateSILLookThrough(mdi->getResults(), mdi->getValue());
23012345
translateSILRequire(mdi->getBase());
23022346
return TranslationSemantics::Special;
23032347
}

0 commit comments

Comments
 (0)