mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[sil-cse] Fix a bug in the CSE of open_existential_ref instructions
When performing a CSE of open_existential_ref instructions, we replace the new archetype by the old archetype by cloning the uses and re-mapping the archetypes. But we also need to consider that some of the uses of a open_existential_ref instruction (e.g. loads) may produce results depending on the opened archetype being replaced. Therefore, for every such use its own uses (and their uses) should be eventually recursively cloned and type-remapped as well if they depend on the opened archetype being replaced. Fixes rdar://28136015 and https://bugs.swift.org/browse/SR-2545
This commit is contained in:
@@ -681,13 +681,48 @@ bool CSE::processOpenExistentialRef(SILInstruction *Inst, ValueBase *V,
|
||||
auto &Builder = Cloner.getBuilder();
|
||||
Builder.setOpenedArchetypesTracker(&OpenedArchetypesTracker);
|
||||
|
||||
llvm::SmallPtrSet<SILInstruction *, 16> Processed;
|
||||
// Now clone each candidate and replace the opened archetype
|
||||
// by a dominating one.
|
||||
for (auto Candidate : Candidates) {
|
||||
while (!Candidates.empty()) {
|
||||
auto Candidate = Candidates.pop_back_val();
|
||||
if (Processed.count(Candidate))
|
||||
continue;
|
||||
// True if a candidate depends on the old opened archetype.
|
||||
bool DependsOnOldOpenedArchetype = !Candidate->getTypeDependentOperands().empty();
|
||||
if (!Candidate->use_empty() &&
|
||||
Candidate->getType().getSwiftRValueType()->hasOpenedExistential()) {
|
||||
// Check if the result type of the candidate depends on the opened
|
||||
// existential in question.
|
||||
auto ResultDependsOnOldOpenedArchetype =
|
||||
Candidate->getType().getSwiftRValueType().findIf(
|
||||
[&OldOpenedArchetype](Type t) -> bool {
|
||||
if (t.getCanonicalTypeOrNull() == OldOpenedArchetype)
|
||||
return true;
|
||||
return false;
|
||||
});
|
||||
if (ResultDependsOnOldOpenedArchetype) {
|
||||
DependsOnOldOpenedArchetype |= ResultDependsOnOldOpenedArchetype;
|
||||
// We need to update uses of this candidate, because their types
|
||||
// may be affected.
|
||||
for (auto Use : Candidate->getUses()) {
|
||||
Candidates.insert(Use->getUser());
|
||||
}
|
||||
}
|
||||
}
|
||||
// Remember that this candidate was processed already.
|
||||
Processed.insert(Candidate);
|
||||
|
||||
// No need to clone if there is no dependency on the old opened archetype.
|
||||
if (!DependsOnOldOpenedArchetype)
|
||||
continue;
|
||||
|
||||
Builder.getOpenedArchetypes().addOpenedArchetypeOperands(
|
||||
Candidate->getTypeDependentOperands());
|
||||
Builder.setInsertionPoint(Candidate);
|
||||
auto NewI = Cloner.clone(Candidate);
|
||||
// Result types of candidate's uses instructions may be using this archetype.
|
||||
// Thus, we need to try to replace it there.
|
||||
Candidate->replaceAllUsesWith(NewI);
|
||||
if (I == Candidate->getIterator())
|
||||
I = NewI->getIterator();
|
||||
|
||||
Reference in New Issue
Block a user