Index: lib/CodeGen/GlobalISel/IRTranslator.cpp =================================================================== --- lib/CodeGen/GlobalISel/IRTranslator.cpp +++ lib/CodeGen/GlobalISel/IRTranslator.cpp @@ -1129,6 +1129,17 @@ ValToVReg.clear(); FrameIndices.clear(); MachinePreds.clear(); + // MachineIRBuilder holds a DebugLoc which can hold on to a TrackingMDRef + // that can outlive the context. + // When the LLVMContext is destroyed, it first releases all MDNode references + // and then destroys the passes. If we don't clear the Builders(which may + // hold some ref), the DebugLoc will again get destroyed leading to + // undefined behavior. + // We do it here - but we could probably also do it in + // releaseMemory. + // This also clears various pointers that the Builders hold on to. + EntryBuilder = MachineIRBuilder(); + CurBuilder = MachineIRBuilder(); } bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {