LTO: Simplify merged module ownership.
authorPeter Collingbourne <peter@pcc.me.uk>
Mon, 24 Aug 2015 22:22:53 +0000 (22:22 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Mon, 24 Aug 2015 22:22:53 +0000 (22:22 +0000)
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

include/llvm-c/lto.h
include/llvm/LTO/LTOCodeGenerator.h
include/llvm/LTO/LTOModule.h
lib/LTO/LTOCodeGenerator.cpp
tools/llvm-lto/llvm-lto.cpp
tools/lto/lto.cpp

index cb3a69160454a29b464aea3c949ea7c05fa9c794..691a0cd3f55ce6b5c36304e389926706ab6c24fc 100644 (file)
@@ -374,8 +374,8 @@ extern lto_bool_t
 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 33ac009c796e6e522dd9be3ce3aa41b0749dbd8c..ca0578fdc7d2c3e847184b5563fa6f93dd1ebd51 100644 (file)
@@ -69,7 +69,7 @@ struct LTOCodeGenerator {
   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);
@@ -155,9 +155,9 @@ private:
 
   typedef StringMap<uint8_t> StringSet;
 
-  void destroyMergedModule();
   std::unique_ptr<LLVMContext> OwnedContext;
   LLVMContext &Context;
+  std::unique_ptr<Module> MergedModule;
   Linker IRLinker;
   std::unique_ptr<TargetMachine> TargetMach;
   bool EmitDwarfDebugInfo = false;
@@ -175,7 +175,6 @@ private:
   unsigned OptLevel = 2;
   lto_diagnostic_handler_t DiagHandler = nullptr;
   void *DiagContext = nullptr;
-  LTOModule *OwnedModule = nullptr;
   bool ShouldInternalize = true;
   bool ShouldEmbedUselists = false;
 };
index c4e2be6273997e8ff07948ea049d6d3df8b0ad34..eda5a3a09ce0ca4466faf493713cfc2626390fa9 100644 (file)
@@ -113,6 +113,8 @@ public:
     return IRFile->getModule();
   }
 
+  std::unique_ptr<Module> takeModule() { return IRFile->takeModule(); }
+
   /// Return the Module's target triple.
   const std::string &getTargetTriple() {
     return getModule().getTargetTriple();
index 3003113142a40b693a8e85030e8d9a37fea831e8..60ce4f67e034d29e7781004bfe47a4ba819acc8d 100644 (file)
@@ -64,29 +64,20 @@ const char* LTOCodeGenerator::getVersionString() {
 }
 
 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),
-      IRLinker(new Module("ld-temp.o", *OwnedContext)) {
+      MergedModule(new Module("ld-temp.o", *OwnedContext)),
+      IRLinker(MergedModule.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 @@ bool LTOCodeGenerator::addModule(LTOModule *mod) {
   return !ret;
 }
 
-void LTOCodeGenerator::setModule(LTOModule *Mod) {
+void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> 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());
+  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)
@@ -200,7 +189,7 @@ bool LTOCodeGenerator::writeMergedModules(const char *path,
   }
 
   // write bitcode to it
-  WriteBitcodeToFile(IRLinker.getModule(), Out.os(), ShouldEmbedUselists);
+  WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists);
   Out.os().close();
 
   if (Out.os().has_error()) {
@@ -296,10 +285,10 @@ bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
   if (TargetMach)
     return true;
 
-  std::string TripleStr = IRLinker.getModule()->getTargetTriple();
+  std::string TripleStr = MergedModule->getTargetTriple();
   if (TripleStr.empty()) {
     TripleStr = sys::getDefaultTargetTriple();
-    IRLinker.getModule()->setTargetTriple(TripleStr);
+    MergedModule->setTargetTriple(TripleStr);
   }
   llvm::Triple Triple(TripleStr);
 
@@ -412,7 +401,6 @@ static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
 void LTOCodeGenerator::applyScopeRestrictions() {
   if (ScopeRestrictionsDone || !ShouldInternalize)
     return;
-  Module *MergedModule = IRLinker.getModule();
 
   // Start off with a verification pass.
   legacy::PassManager passes;
@@ -475,8 +463,6 @@ bool LTOCodeGenerator::optimize(bool DisableInline,
   if (!this->determineTarget(errMsg))
     return false;
 
-  Module *MergedModule = IRLinker.getModule();
-
   // Mark which symbols can not be internalized
   this->applyScopeRestrictions();
 
@@ -514,8 +500,6 @@ bool LTOCodeGenerator::compileOptimized(raw_pwrite_stream &out,
   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,
index b17f1ea307bfb60fe6d27b431dacca30f46d47d6..52e45296126475d1fd5eb273a01e998f89d2edda 100644 (file)
@@ -210,7 +210,7 @@ int main(int argc, char **argv) {
     // 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 aebb1c64a319be18b76d8d32d37ec0f1d0d0f7b4..8f62929b326bb66dae91008b3d2ce8edd9fb2532 100644 (file)
@@ -260,7 +260,7 @@ bool lto_codegen_add_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) {