Index: lib/CodeGen/CodeGenAction.cpp =================================================================== --- lib/CodeGen/CodeGenAction.cpp +++ lib/CodeGen/CodeGenAction.cpp @@ -73,7 +73,6 @@ std::unique_ptr Gen; - std::unique_ptr TheModule; SmallVector>, 4> LinkModules; @@ -97,7 +96,10 @@ this->LinkModules.push_back( std::make_pair(I.first, std::unique_ptr(I.second))); } - std::unique_ptr takeModule() { return std::move(TheModule); } + llvm::Module *getModule() const { return Gen->GetModule(); } + std::unique_ptr takeModule() { + return std::unique_ptr(Gen->ReleaseModule()); + } void releaseLinkModules() { for (auto &I : LinkModules) I.second.release(); @@ -117,8 +119,6 @@ Gen->Initialize(Ctx); - TheModule.reset(Gen->GetModule()); - if (llvm::TimePassesIsEnabled) LLVMIRGeneration.stopTimer(); } @@ -165,27 +165,14 @@ } // Silently ignore if we weren't initialized for some reason. - if (!TheModule) - return; - - // Make sure IR generation is happy with the module. This is released by - // the module provider. - llvm::Module *M = Gen->ReleaseModule(); - if (!M) { - // The module has been released by IR gen on failures, do not double - // free. - TheModule.release(); + if (!getModule()) return; - } - - assert(TheModule.get() == M && - "Unexpected module change during IR generation"); // Link LinkModule into this module if present, preserving its validity. for (auto &I : LinkModules) { unsigned LinkFlags = I.first; llvm::Module *LinkModule = I.second.get(); - if (Linker::linkModules(*M, *LinkModule, + if (Linker::linkModules(*getModule(), *LinkModule, [=](const DiagnosticInfo &DI) { linkerDiagnosticHandler(DI, LinkModule, Diags); }, @@ -195,7 +182,7 @@ // Install an inline asm handler so that diagnostics get printed through // our diagnostics hooks. - LLVMContext &Ctx = TheModule->getContext(); + LLVMContext &Ctx = getModule()->getContext(); LLVMContext::InlineAsmDiagHandlerTy OldHandler = Ctx.getInlineAsmDiagnosticHandler(); void *OldContext = Ctx.getInlineAsmDiagnosticContext(); @@ -208,7 +195,7 @@ EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, C.getTargetInfo().getDataLayoutString(), - TheModule.get(), Action, AsmOutStream); + getModule(), Action, AsmOutStream); Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);