Index: lib/IR/AsmWriter.cpp =================================================================== --- lib/IR/AsmWriter.cpp +++ lib/IR/AsmWriter.cpp @@ -872,7 +872,8 @@ SmallVector, 4> MDs; I.getAllMetadata(MDs); for (auto &MD : MDs) - CreateMetadataSlot(MD.second); + if (MD.second) + CreateMetadataSlot(MD.second); } /// Clean up after incorporating a function. This is the only way to get out of @@ -1965,6 +1966,11 @@ TypePrinting *TypePrinter, SlotTracker *Machine, const Module *Context, bool FromValue) { + if (!MD) { + Out << ""; + return; + } + if (const MDNode *N = dyn_cast(MD)) { std::unique_ptr MachineStorage; if (!Machine) { @@ -3120,7 +3126,9 @@ if (MDs.empty()) return; - if (MDNames.empty()) + // Second should not be null in well formed, IR, but this is also + // called from the Verifier, so be permissive. + if (MDNames.empty() && MDs[0].second) MDs[0].second->getContext().getMDKindNames(MDNames); for (const auto &I : MDs) { Index: lib/IR/TypeFinder.cpp =================================================================== --- lib/IR/TypeFinder.cpp +++ lib/IR/TypeFinder.cpp @@ -71,7 +71,8 @@ // Incorporate types hiding in metadata. I.getAllMetadataOtherThanDebugLoc(MDForInst); for (unsigned i = 0, e = MDForInst.size(); i != e; ++i) - incorporateMDNode(MDForInst[i].second); + if (MDForInst[i].second) + incorporateMDNode(MDForInst[i].second); MDForInst.clear(); } Index: lib/IR/Verifier.cpp =================================================================== --- lib/IR/Verifier.cpp +++ lib/IR/Verifier.cpp @@ -3462,6 +3462,12 @@ } } + SmallVector, 4> InstMD; + I.getAllMetadata(InstMD); + for (auto MD : InstMD) { + Assert(MD.second, "Metadata attachment may not be null!", &I); + } + if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) { Assert(I.getType()->isFPOrFPVectorTy(), "fpmath requires a floating point result!", &I);