Index: include/llvm-c/lto.h =================================================================== --- include/llvm-c/lto.h +++ include/llvm-c/lto.h @@ -374,8 +374,8 @@ lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod); /** - * Sets the object module for code generation. This will transfer the ownship of - * the module to code generator. + * Sets the object module for code generation. This will transfer the ownership + * of the module to the code generator. * * \c cg and \c mod must both be in the same context. * Index: include/llvm/LTO/LTOCodeGenerator.h =================================================================== --- include/llvm/LTO/LTOCodeGenerator.h +++ include/llvm/LTO/LTOCodeGenerator.h @@ -69,7 +69,7 @@ bool addModule(struct LTOModule *); // Set the destination module. - void setModule(struct LTOModule *); + void setModule(std::unique_ptr M); void setTargetOptions(TargetOptions options); void setDebugInfo(lto_debug_model); @@ -155,9 +155,9 @@ typedef StringMap StringSet; - void destroyMergedModule(); std::unique_ptr OwnedContext; LLVMContext &Context; + std::unique_ptr OwnedModule; Linker IRLinker; std::unique_ptr TargetMach; bool EmitDwarfDebugInfo = false; @@ -173,7 +173,6 @@ unsigned OptLevel = 2; lto_diagnostic_handler_t DiagHandler = nullptr; void *DiagContext = nullptr; - LTOModule *OwnedModule = nullptr; bool ShouldInternalize = true; bool ShouldEmbedUselists = false; }; Index: include/llvm/LTO/LTOModule.h =================================================================== --- include/llvm/LTO/LTOModule.h +++ include/llvm/LTO/LTOModule.h @@ -113,6 +113,8 @@ return IRFile->getModule(); } + std::unique_ptr takeModule() { return IRFile->takeModule(); } + /// Return the Module's target triple. const std::string &getTargetTriple() { return getModule().getTargetTriple(); Index: lib/LTO/LTOCodeGenerator.cpp =================================================================== --- lib/LTO/LTOCodeGenerator.cpp +++ lib/LTO/LTOCodeGenerator.cpp @@ -64,29 +64,20 @@ } LTOCodeGenerator::LTOCodeGenerator() - : Context(getGlobalContext()), IRLinker(new Module("ld-temp.o", Context)) { + : Context(getGlobalContext()), + OwnedModule(new Module("ld-temp.o", Context)), + IRLinker(OwnedModule.get()) { initializeLTOPasses(); } LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr Context) : OwnedContext(std::move(Context)), Context(*OwnedContext), - IRLinker(new Module("ld-temp.o", *OwnedContext)) { + OwnedModule(new Module("ld-temp.o", *OwnedContext)), + IRLinker(OwnedModule.get()) { initializeLTOPasses(); } -void LTOCodeGenerator::destroyMergedModule() { - if (OwnedModule) { - assert(IRLinker.getModule() == &OwnedModule->getModule() && - "The linker's module should be the same as the owned module"); - delete OwnedModule; - OwnedModule = nullptr; - } else if (IRLinker.getModule()) - IRLinker.deleteModule(); -} - -LTOCodeGenerator::~LTOCodeGenerator() { - destroyMergedModule(); -} +LTOCodeGenerator::~LTOCodeGenerator() {} // Initialize LTO passes. Please keep this funciton in sync with // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO @@ -131,16 +122,14 @@ return !ret; } -void LTOCodeGenerator::setModule(LTOModule *Mod) { +void LTOCodeGenerator::setModule(std::unique_ptr Mod) { assert(&Mod->getModule().getContext() == &Context && "Expected module in same context"); - // Delete the old merged module. - destroyMergedModule(); AsmUndefinedRefs.clear(); - OwnedModule = Mod; - IRLinker.setModule(&Mod->getModule()); + OwnedModule = Mod->takeModule(); + IRLinker.setModule(OwnedModule.get()); const std::vector &Undefs = Mod->getAsmUndefinedRefs(); for (int I = 0, E = Undefs.size(); I != E; ++I) @@ -182,7 +171,7 @@ } // write bitcode to it - WriteBitcodeToFile(IRLinker.getModule(), Out.os(), ShouldEmbedUselists); + WriteBitcodeToFile(OwnedModule.get(), Out.os(), ShouldEmbedUselists); Out.os().close(); if (Out.os().has_error()) { @@ -278,7 +267,7 @@ if (TargetMach) return true; - std::string TripleStr = IRLinker.getModule()->getTargetTriple(); + std::string TripleStr = OwnedModule->getTargetTriple(); if (TripleStr.empty()) TripleStr = sys::getDefaultTargetTriple(); llvm::Triple Triple(TripleStr); @@ -408,7 +397,6 @@ void LTOCodeGenerator::applyScopeRestrictions() { if (ScopeRestrictionsDone || !ShouldInternalize) return; - Module *mergedModule = IRLinker.getModule(); // Start off with a verification pass. legacy::PassManager passes; @@ -422,20 +410,17 @@ TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple())); TargetLibraryInfo TLI(TLII); - accumulateAndSortLibcalls(Libcalls, TLI, *mergedModule, *TargetMach); + accumulateAndSortLibcalls(Libcalls, TLI, *OwnedModule, *TargetMach); - for (Module::iterator f = mergedModule->begin(), - e = mergedModule->end(); f != e; ++f) - applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler); - for (Module::global_iterator v = mergedModule->global_begin(), - e = mergedModule->global_end(); v != e; ++v) - applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler); - for (Module::alias_iterator a = mergedModule->alias_begin(), - e = mergedModule->alias_end(); a != e; ++a) - applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler); + for (Function &f : *OwnedModule) + applyRestriction(f, Libcalls, MustPreserveList, AsmUsed, Mangler); + for (GlobalVariable &v : OwnedModule->globals()) + applyRestriction(v, Libcalls, MustPreserveList, AsmUsed, Mangler); + for (GlobalAlias &a : OwnedModule->aliases()) + applyRestriction(a, Libcalls, MustPreserveList, AsmUsed, Mangler); GlobalVariable *LLVMCompilerUsed = - mergedModule->getGlobalVariable("llvm.compiler.used"); + OwnedModule->getGlobalVariable("llvm.compiler.used"); findUsedValues(LLVMCompilerUsed, AsmUsed); if (LLVMCompilerUsed) LLVMCompilerUsed->eraseFromParent(); @@ -450,7 +435,7 @@ llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size()); LLVMCompilerUsed = - new llvm::GlobalVariable(*mergedModule, ATy, false, + new llvm::GlobalVariable(*OwnedModule, ATy, false, llvm::GlobalValue::AppendingLinkage, llvm::ConstantArray::get(ATy, asmUsed2), "llvm.compiler.used"); @@ -461,7 +446,7 @@ passes.add(createInternalizePass(MustPreserveList)); // apply scope restrictions - passes.run(*mergedModule); + passes.run(*OwnedModule); ScopeRestrictionsDone = true; } @@ -474,8 +459,6 @@ if (!this->determineTarget(errMsg)) return false; - Module *mergedModule = IRLinker.getModule(); - // Mark which symbols can not be internalized this->applyScopeRestrictions(); @@ -483,7 +466,7 @@ legacy::PassManager passes; // Add an appropriate DataLayout instance for this module... - mergedModule->setDataLayout(TargetMach->createDataLayout()); + OwnedModule->setDataLayout(TargetMach->createDataLayout()); passes.add( createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis())); @@ -503,7 +486,7 @@ PMB.populateLTOPassManager(passes); // Run our queue of passes all at once now, efficiently. - passes.run(*mergedModule); + passes.run(*OwnedModule); return true; } @@ -513,8 +496,6 @@ if (!this->determineTarget(errMsg)) return false; - Module *mergedModule = IRLinker.getModule(); - legacy::PassManager codeGenPasses; // If the bitcode files contain ARC code and were compiled with optimization, @@ -527,8 +508,8 @@ return false; } - // Run the code generator, and write assembly file - codeGenPasses.run(*mergedModule); + // Run the code generator, and write object file + codeGenPasses.run(*OwnedModule); return true; } Index: tools/llvm-lto/llvm-lto.cpp =================================================================== --- tools/llvm-lto/llvm-lto.cpp +++ tools/llvm-lto/llvm-lto.cpp @@ -210,7 +210,7 @@ // SetMergedModule is true. if (SetMergedModule && i == BaseArg) { // Transfer ownership to the code generator. - CodeGen.setModule(Module.release()); + CodeGen.setModule(std::move(Module)); } else if (!CodeGen.addModule(Module.get())) return 1; } Index: tools/lto/lto.cpp =================================================================== --- tools/lto/lto.cpp +++ tools/lto/lto.cpp @@ -260,7 +260,7 @@ } void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) { - unwrap(cg)->setModule(unwrap(mod)); + unwrap(cg)->setModule(std::unique_ptr(unwrap(mod))); } bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {