Skip to content

Commit a40a6e2

Browse files
committed
SIL: Fix assertion failure after deleting instructions that contain unresolved local archetypes
We maintained a counter of the number of pending local archetypes that had not yet been defined. However, if an instruction that references a pending local archetype was deleted before the local archetype was defined, the counter would never decrement. Before reading the counter value, garbage collect any inserted placeholders that have no uses. These correspond to pending local archetypes that are no longer in use and will never be defined.
1 parent 60615b8 commit a40a6e2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/SIL/IR/SILModule.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,23 @@ SILValue SILModule::getRootLocalArchetypeDef(CanLocalArchetypeType archetype,
688688
}
689689

690690
bool SILModule::hasUnresolvedLocalArchetypeDefinitions() {
691+
// Garbage collect dead placeholders first.
692+
llvm::DenseMap<LocalArchetypeKey, SILValue> newLocalArchetypeDefs;
693+
694+
for (auto pair : RootLocalArchetypeDefs) {
695+
if (auto *placeholder = dyn_cast<PlaceholderValue>(pair.second)) {
696+
if (placeholder->use_empty()) {
697+
--numUnresolvedLocalArchetypes;
698+
::delete placeholder;
699+
continue;
700+
}
701+
}
702+
703+
newLocalArchetypeDefs.insert(pair);
704+
}
705+
706+
std::swap(newLocalArchetypeDefs, RootLocalArchetypeDefs);
707+
691708
return numUnresolvedLocalArchetypes != 0;
692709
}
693710

@@ -753,6 +770,8 @@ void SILModule::notifyAddedInstruction(SILInstruction *inst) {
753770
auto *placeholder = cast<PlaceholderValue>(val);
754771
placeholder->replaceAllUsesWith(dependency);
755772
::delete placeholder;
773+
774+
assert(numUnresolvedLocalArchetypes > 0);
756775
numUnresolvedLocalArchetypes--;
757776
}
758777
val = dependency;

0 commit comments

Comments
 (0)