Index: lib/IR/LLVMContextImpl.cpp =================================================================== --- lib/IR/LLVMContextImpl.cpp +++ lib/IR/LLVMContextImpl.cpp @@ -48,6 +48,15 @@ while (!OwnedModules.empty()) delete *OwnedModules.begin(); + // Drop metadata references from leaked Instructions to prevent random crashes +#ifndef NDEBUG + for (auto &Pair : InstructionMetadata) + Pair.first->dump(); + assert(InstructionMetadata.empty() && + "Instructions with metadata have been leaked"); +#endif + InstructionMetadata.clear(); + // Drop references for MDNodes. Do this before Values get deleted to avoid // unnecessary RAUW when nodes are still unresolved. for (auto *I : DistinctMDNodes)