llvm.org GIT mirror llvm / 8c1602d
LTO: Simplify merged module ownership. This change moves LTOCodeGenerator's ownership of the merged module to a field of type std::unique_ptr<Module>. This helps simplify parts of the code and clears the way for the module to be consumed by LLVM CodeGen (see D12132 review comments). Differential Revision: http://reviews.llvm.org/D12205 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245891 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 4 years ago
6 changed file(s) with 20 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
6868 bool addModule(struct LTOModule *);
6969
7070 // Set the destination module.
71 void setModule(struct LTOModule *);
71 void setModule(std::unique_ptr M);
7272
7373 void setTargetOptions(TargetOptions options);
7474 void setDebugInfo(lto_debug_model);
154154
155155 typedef StringMap StringSet;
156156
157 void destroyMergedModule();
158157 std::unique_ptr OwnedContext;
159158 LLVMContext &Context;
159 std::unique_ptr MergedModule;
160160 Linker IRLinker;
161161 std::unique_ptr TargetMach;
162162 bool EmitDwarfDebugInfo = false;
174174 unsigned OptLevel = 2;
175175 lto_diagnostic_handler_t DiagHandler = nullptr;
176176 void *DiagContext = nullptr;
177 LTOModule *OwnedModule = nullptr;
178177 bool ShouldInternalize = true;
179178 bool ShouldEmbedUselists = false;
180179 };
112112 return IRFile->getModule();
113113 }
114114
115 std::unique_ptr takeModule() { return IRFile->takeModule(); }
116
115117 /// Return the Module's target triple.
116118 const std::string &getTargetTriple() {
117119 return getModule().getTargetTriple();
373373 lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);
374374
375375 /**
376 * Sets the object module for code generation. This will transfer the ownship of
377 * the module to code generator.
376 * Sets the object module for code generation. This will transfer the ownership
377 * of the module to the code generator.
378378 *
379379 * \c cg and \c mod must both be in the same context.
380380 *
6363 }
6464
6565 LTOCodeGenerator::LTOCodeGenerator()
66 : Context(getGlobalContext()), IRLinker(new Module("ld-temp.o", Context)) {
66 : Context(getGlobalContext()),
67 MergedModule(new Module("ld-temp.o", Context)),
68 IRLinker(MergedModule.get()) {
6769 initializeLTOPasses();
6870 }
6971
7072 LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr Context)
7173 : OwnedContext(std::move(Context)), Context(*OwnedContext),
72 IRLinker(new Module("ld-temp.o", *OwnedContext)) {
74 MergedModule(new Module("ld-temp.o", *OwnedContext)),
75 IRLinker(MergedModule.get()) {
7376 initializeLTOPasses();
7477 }
7578
76 void LTOCodeGenerator::destroyMergedModule() {
77 if (OwnedModule) {
78 assert(IRLinker.getModule() == &OwnedModule->getModule() &&
79 "The linker's module should be the same as the owned module");
80 delete OwnedModule;
81 OwnedModule = nullptr;
82 } else if (IRLinker.getModule())
83 IRLinker.deleteModule();
84 }
85
86 LTOCodeGenerator::~LTOCodeGenerator() {
87 destroyMergedModule();
88 }
79 LTOCodeGenerator::~LTOCodeGenerator() {}
8980
9081 // Initialize LTO passes. Please keep this funciton in sync with
9182 // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
130121 return !ret;
131122 }
132123
133 void LTOCodeGenerator::setModule(LTOModule *Mod) {
124 void LTOCodeGenerator::setModule(std::unique_ptr Mod) {
134125 assert(&Mod->getModule().getContext() == &Context &&
135126 "Expected module in same context");
136127
137 // Delete the old merged module.
138 destroyMergedModule();
139128 AsmUndefinedRefs.clear();
140129
141 OwnedModule = Mod;
142 IRLinker.setModule(&Mod->getModule());
130 MergedModule = Mod->takeModule();
131 IRLinker.setModule(MergedModule.get());
143132
144133 const std::vector &Undefs = Mod->getAsmUndefinedRefs();
145134 for (int I = 0, E = Undefs.size(); I != E; ++I)
199188 }
200189
201190 // write bitcode to it
202 WriteBitcodeToFile(IRLinker.getModule(), Out.os(), ShouldEmbedUselists);
191 WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists);
203192 Out.os().close();
204193
205194 if (Out.os().has_error()) {
295284 if (TargetMach)
296285 return true;
297286
298 std::string TripleStr = IRLinker.getModule()->getTargetTriple();
287 std::string TripleStr = MergedModule->getTargetTriple();
299288 if (TripleStr.empty()) {
300289 TripleStr = sys::getDefaultTargetTriple();
301 IRLinker.getModule()->setTargetTriple(TripleStr);
290 MergedModule->setTargetTriple(TripleStr);
302291 }
303292 llvm::Triple Triple(TripleStr);
304293
411400 void LTOCodeGenerator::applyScopeRestrictions() {
412401 if (ScopeRestrictionsDone || !ShouldInternalize)
413402 return;
414 Module *MergedModule = IRLinker.getModule();
415403
416404 // Start off with a verification pass.
417405 legacy::PassManager passes;
473461 std::string &errMsg) {
474462 if (!this->determineTarget(errMsg))
475463 return false;
476
477 Module *MergedModule = IRLinker.getModule();
478464
479465 // Mark which symbols can not be internalized
480466 this->applyScopeRestrictions();
513499 if (!this->determineTarget(errMsg))
514500 return false;
515501
516 Module *MergedModule = IRLinker.getModule();
517
518502 legacy::PassManager codeGenPasses;
519503
520504 // If the bitcode files contain ARC code and were compiled with optimization,
209209 // SetMergedModule is true.
210210 if (SetMergedModule && i == BaseArg) {
211211 // Transfer ownership to the code generator.
212 CodeGen.setModule(Module.release());
212 CodeGen.setModule(std::move(Module));
213213 } else if (!CodeGen.addModule(Module.get()))
214214 return 1;
215215 }
259259 }
260260
261261 void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
262 unwrap(cg)->setModule(unwrap(mod));
262 unwrap(cg)->setModule(std::unique_ptr(unwrap(mod)));
263263 }
264264
265265 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {