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
lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);
/**
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.
*
*
* \c cg and \c mod must both be in the same context.
*
bool addModule(struct LTOModule *);
// Set the destination module.
bool addModule(struct LTOModule *);
// Set the destination module.
- void setModule(struct LTOModule *);
+ void setModule(std::unique_ptr<LTOModule> M);
void setTargetOptions(TargetOptions options);
void setDebugInfo(lto_debug_model);
void setTargetOptions(TargetOptions options);
void setDebugInfo(lto_debug_model);
typedef StringMap<uint8_t> StringSet;
typedef StringMap<uint8_t> StringSet;
- void destroyMergedModule();
std::unique_ptr<LLVMContext> OwnedContext;
LLVMContext &Context;
std::unique_ptr<LLVMContext> OwnedContext;
LLVMContext &Context;
+ std::unique_ptr<Module> MergedModule;
Linker IRLinker;
std::unique_ptr<TargetMachine> TargetMach;
bool EmitDwarfDebugInfo = false;
Linker IRLinker;
std::unique_ptr<TargetMachine> TargetMach;
bool EmitDwarfDebugInfo = false;
unsigned OptLevel = 2;
lto_diagnostic_handler_t DiagHandler = nullptr;
void *DiagContext = nullptr;
unsigned OptLevel = 2;
lto_diagnostic_handler_t DiagHandler = nullptr;
void *DiagContext = nullptr;
- LTOModule *OwnedModule = nullptr;
bool ShouldInternalize = true;
bool ShouldEmbedUselists = false;
};
bool ShouldInternalize = true;
bool ShouldEmbedUselists = false;
};
return IRFile->getModule();
}
return IRFile->getModule();
}
+ std::unique_ptr<Module> takeModule() { return IRFile->takeModule(); }
+
/// Return the Module's target triple.
const std::string &getTargetTriple() {
return getModule().getTargetTriple();
/// Return the Module's target triple.
const std::string &getTargetTriple() {
return getModule().getTargetTriple();
}
LTOCodeGenerator::LTOCodeGenerator()
}
LTOCodeGenerator::LTOCodeGenerator()
- : Context(getGlobalContext()), IRLinker(new Module("ld-temp.o", Context)) {
+ : Context(getGlobalContext()),
+ MergedModule(new Module("ld-temp.o", Context)),
+ IRLinker(MergedModule.get()) {
initializeLTOPasses();
}
LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
: OwnedContext(std::move(Context)), Context(*OwnedContext),
initializeLTOPasses();
}
LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
: OwnedContext(std::move(Context)), Context(*OwnedContext),
- IRLinker(new Module("ld-temp.o", *OwnedContext)) {
+ MergedModule(new Module("ld-temp.o", *OwnedContext)),
+ IRLinker(MergedModule.get()) {
-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
// Initialize LTO passes. Please keep this funciton in sync with
// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
-void LTOCodeGenerator::setModule(LTOModule *Mod) {
+void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
assert(&Mod->getModule().getContext() == &Context &&
"Expected module in same context");
assert(&Mod->getModule().getContext() == &Context &&
"Expected module in same context");
- // Delete the old merged module.
- destroyMergedModule();
AsmUndefinedRefs.clear();
AsmUndefinedRefs.clear();
- OwnedModule = Mod;
- IRLinker.setModule(&Mod->getModule());
+ MergedModule = Mod->takeModule();
+ IRLinker.setModule(MergedModule.get());
const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
for (int I = 0, E = Undefs.size(); I != E; ++I)
const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
for (int I = 0, E = Undefs.size(); I != E; ++I)
- WriteBitcodeToFile(IRLinker.getModule(), Out.os(), ShouldEmbedUselists);
+ WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists);
Out.os().close();
if (Out.os().has_error()) {
Out.os().close();
if (Out.os().has_error()) {
if (TargetMach)
return true;
if (TargetMach)
return true;
- std::string TripleStr = IRLinker.getModule()->getTargetTriple();
+ std::string TripleStr = MergedModule->getTargetTriple();
if (TripleStr.empty()) {
TripleStr = sys::getDefaultTargetTriple();
if (TripleStr.empty()) {
TripleStr = sys::getDefaultTargetTriple();
- IRLinker.getModule()->setTargetTriple(TripleStr);
+ MergedModule->setTargetTriple(TripleStr);
}
llvm::Triple Triple(TripleStr);
}
llvm::Triple Triple(TripleStr);
void LTOCodeGenerator::applyScopeRestrictions() {
if (ScopeRestrictionsDone || !ShouldInternalize)
return;
void LTOCodeGenerator::applyScopeRestrictions() {
if (ScopeRestrictionsDone || !ShouldInternalize)
return;
- Module *MergedModule = IRLinker.getModule();
// Start off with a verification pass.
legacy::PassManager passes;
// Start off with a verification pass.
legacy::PassManager passes;
if (!this->determineTarget(errMsg))
return false;
if (!this->determineTarget(errMsg))
return false;
- Module *MergedModule = IRLinker.getModule();
-
// Mark which symbols can not be internalized
this->applyScopeRestrictions();
// Mark which symbols can not be internalized
this->applyScopeRestrictions();
if (!this->determineTarget(errMsg))
return false;
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,
legacy::PassManager codeGenPasses;
// If the bitcode files contain ARC code and were compiled with optimization,
// SetMergedModule is true.
if (SetMergedModule && i == BaseArg) {
// Transfer ownership to the code generator.
// 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;
}
} else if (!CodeGen.addModule(Module.get()))
return 1;
}
}
void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
}
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<LTOModule>(unwrap(mod)));
}
bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
}
bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {