OSDN Git Service

Move LTO support library to a component, allowing it to be tested
authorPeter Collingbourne <peter@pcc.me.uk>
Tue, 24 Sep 2013 23:52:22 +0000 (23:52 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Tue, 24 Sep 2013 23:52:22 +0000 (23:52 +0000)
more reliably across platforms.  Patch by Tom Roeder!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191343 91177308-0d34-0410-b5e6-96231b3b80d8

20 files changed:
include/llvm/LTO/LTOCodeGenerator.h [moved from tools/lto/LTOCodeGenerator.h with 98% similarity]
include/llvm/LTO/LTOModule.h [moved from tools/lto/LTOModule.h with 96% similarity]
lib/CMakeLists.txt
lib/LLVMBuild.txt
lib/LTO/CMakeLists.txt [new file with mode: 0644]
lib/LTO/LLVMBuild.txt [new file with mode: 0644]
lib/LTO/LTOCodeGenerator.cpp [moved from tools/lto/LTOCodeGenerator.cpp with 98% similarity]
lib/LTO/LTOModule.cpp [moved from tools/lto/LTOModule.cpp with 99% similarity]
lib/LTO/Makefile [new file with mode: 0644]
lib/Makefile
test/LTO/cfi_endproc.ll [moved from test/tools/lto/cfi_endproc.ll with 100% similarity]
test/LTO/lit.local.cfg [moved from test/tools/lto/lit.local.cfg with 100% similarity]
tools/LLVMBuild.txt
tools/llvm-lto/CMakeLists.txt
tools/llvm-lto/LLVMBuild.txt [new file with mode: 0644]
tools/llvm-lto/Makefile
tools/llvm-lto/llvm-lto.cpp
tools/lto/CMakeLists.txt
tools/lto/Makefile
tools/lto/lto.cpp

similarity index 98%
rename from tools/lto/LTOCodeGenerator.h
rename to include/llvm/LTO/LTOCodeGenerator.h
index 0263362..3d151b9 100644 (file)
@@ -96,7 +96,7 @@ struct LTOCodeGenerator {
   // single object file. Instead of returning the object-file-path to the caller
   // (linker), it brings the object to a buffer, and return the buffer to the
   // caller. This function should delete intermediate object file once its content
-  // is brought to memory. Return NULL is the compilation was not successful. 
+  // is brought to memory. Return NULL if the compilation was not successful. 
   //
   const void *compile(size_t *length, std::string &errMsg);
 
similarity index 96%
rename from tools/lto/LTOModule.h
rename to include/llvm/LTO/LTOModule.h
index 902e9c5..973466c 100644 (file)
@@ -76,7 +76,13 @@ public:
                                      const char *triplePrefix);
 
   /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership
-  /// of the buffer.
+  /// of the buffer. The caller must have initialized the Targets, the
+  /// TargetMCs, the AsmPrinters, and the AsmParsers by calling:
+  ///
+  /// InitializeAllTargets();
+  /// InitializeAllTargetMCs();
+  /// InitializeAllAsmPrinters();
+  /// InitializeAllAsmParsers();
   static LTOModule *makeLTOModule(const char* path,
                                   std::string &errMsg);
   static LTOModule *makeLTOModule(int fd, const char *path,
index 4d9aebc..7fbf123 100644 (file)
@@ -7,6 +7,7 @@ add_subdirectory(Bitcode)
 add_subdirectory(Transforms)
 add_subdirectory(Linker)
 add_subdirectory(Analysis)
+add_subdirectory(LTO)
 add_subdirectory(MC)
 add_subdirectory(Object)
 add_subdirectory(Option)
index ff288bc..00280c8 100644 (file)
@@ -16,7 +16,7 @@
 ;===------------------------------------------------------------------------===;
 
 [common]
-subdirectories = Analysis AsmParser Bitcode CodeGen DebugInfo ExecutionEngine Linker IR IRReader MC Object Option Support TableGen Target Transforms
+subdirectories = Analysis AsmParser Bitcode CodeGen DebugInfo ExecutionEngine Linker IR IRReader LTO MC Object Option Support TableGen Target Transforms
 
 [component_0]
 type = Group
diff --git a/lib/LTO/CMakeLists.txt b/lib/LTO/CMakeLists.txt
new file mode 100644 (file)
index 0000000..8e00bcb
--- /dev/null
@@ -0,0 +1,4 @@
+add_llvm_library(LLVMLTO
+  LTOModule.cpp
+  LTOCodeGenerator.cpp
+  )
diff --git a/lib/LTO/LLVMBuild.txt b/lib/LTO/LLVMBuild.txt
new file mode 100644 (file)
index 0000000..38c1170
--- /dev/null
@@ -0,0 +1,22 @@
+;===- ./lib/LTO/LLVMBuild.txt ----------------------------------*- Conf -*--===;
+;
+;                     The LLVM Compiler Infrastructure
+;
+; This file is distributed under the University of Illinois Open Source
+; License. See LICENSE.TXT for details.
+;
+;===------------------------------------------------------------------------===;
+;
+; This is an LLVMBuild description file for the components in this subdirectory.
+;
+; For more information on the LLVMBuild system, please see:
+;
+;   http://llvm.org/docs/LLVMBuild.html
+;
+;===------------------------------------------------------------------------===;
+
+[component_0]
+type = Library
+name = LTO
+parent = Libraries
+required_libraries = Analysis BitReader BitWriter Core IPO Linker MC MCParser Scalar Support Target Vectorize
\ No newline at end of file
similarity index 98%
rename from tools/lto/LTOCodeGenerator.cpp
rename to lib/LTO/LTOCodeGenerator.cpp
index 19e8c4c..22e9754 100644 (file)
@@ -12,8 +12,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "LTOCodeGenerator.h"
-#include "LTOModule.h"
+#include "llvm/LTO/LTOCodeGenerator.h"
+#include "llvm/LTO/LTOModule.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Analysis/Passes.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/system_error.h"
-#include "llvm/Target/Mangler.h"
-#include "llvm/Target/TargetMachine.h"
-#include "llvm/Target/TargetOptions.h"
 #include "llvm/Target/TargetRegisterInfo.h"
+#include "llvm/Target/Mangler.h"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
 #include "llvm/Transforms/ObjCARC.h"
@@ -73,9 +71,6 @@ LTOCodeGenerator::LTOCodeGenerator()
     : Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)),
       TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false),
       CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), NativeObjectFile(NULL) {
-  InitializeAllTargets();
-  InitializeAllTargetMCs();
-  InitializeAllAsmPrinters();
   initializeLTOPasses();
 }
 
@@ -452,7 +447,7 @@ void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
     // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
     // that.
     if (CodegenOptions.empty())
-      CodegenOptions.push_back(strdup("libLTO"));
+      CodegenOptions.push_back(strdup("libLLVMLTO"));
     CodegenOptions.push_back(strdup(o.first.str().c_str()));
   }
 }
similarity index 99%
rename from tools/lto/LTOModule.cpp
rename to lib/LTO/LTOModule.cpp
index 87e3573..1457f2f 100644 (file)
@@ -12,7 +12,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "LTOModule.h"
+#include "llvm/LTO/LTOModule.h"
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/Bitcode/ReaderWriter.h"
@@ -248,14 +248,6 @@ void LTOModule::getTargetOptions(TargetOptions &Options) {
 
 LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
                                     std::string &errMsg) {
-  static bool Initialized = false;
-  if (!Initialized) {
-    InitializeAllTargets();
-    InitializeAllTargetMCs();
-    InitializeAllAsmParsers();
-    Initialized = true;
-  }
-
   // parse bitcode buffer
   OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
                                            &errMsg));
diff --git a/lib/LTO/Makefile b/lib/LTO/Makefile
new file mode 100644 (file)
index 0000000..7ca125a
--- /dev/null
@@ -0,0 +1,15 @@
+##===- lib/Linker/Makefile ---------------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+LIBRARYNAME = LLVMLTO
+BUILD_ARCHIVE := 1
+
+include $(LEVEL)/Makefile.common
+
index 0a4435e..cfe9926 100644 (file)
@@ -11,7 +11,7 @@ LEVEL = ..
 include $(LEVEL)/Makefile.config
 
 PARALLEL_DIRS := IR AsmParser Bitcode Analysis Transforms CodeGen \
-                 Target ExecutionEngine Linker MC Object Option DebugInfo \
+                 Target ExecutionEngine Linker LTO MC Object Option DebugInfo \
                                                                 IRReader
 
 include $(LEVEL)/Makefile.common
index 9ec89f3..fa10b99 100644 (file)
@@ -16,7 +16,7 @@
 ;===------------------------------------------------------------------------===;
 
 [common]
-subdirectories = bugpoint llc lli llvm-ar llvm-as llvm-bcanalyzer llvm-cov llvm-diff llvm-dis llvm-dwarfdump llvm-extract llvm-jitlistener llvm-link llvm-mc llvm-nm llvm-objdump llvm-prof llvm-rtdyld llvm-size macho-dump opt llvm-mcmarkup
+subdirectories = bugpoint llc lli llvm-ar llvm-as llvm-bcanalyzer llvm-cov llvm-diff llvm-dis llvm-dwarfdump llvm-extract llvm-jitlistener llvm-link llvm-lto llvm-mc llvm-nm llvm-objdump llvm-prof llvm-rtdyld llvm-size macho-dump opt llvm-mcmarkup
 
 [component_0]
 type = Group
index b253b69..348976c 100644 (file)
@@ -1,7 +1,6 @@
+set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} lto support)
+
 add_llvm_tool(llvm-lto
   llvm-lto.cpp
   )
 
-target_link_libraries(llvm-lto LTO LLVMSupport)
-
-add_dependencies(llvm-lto lto)
diff --git a/tools/llvm-lto/LLVMBuild.txt b/tools/llvm-lto/LLVMBuild.txt
new file mode 100644 (file)
index 0000000..c1613a3
--- /dev/null
@@ -0,0 +1,22 @@
+;===- ./tools/llvm-lto/LLVMBuild.txt ----------------------------*- Conf -*--===;
+;
+;                     The LLVM Compiler Infrastructure
+;
+; This file is distributed under the University of Illinois Open Source
+; License. See LICENSE.TXT for details.
+;
+;===------------------------------------------------------------------------===;
+;
+; This is an LLVMBuild description file for the components in this subdirectory.
+;
+; For more information on the LLVMBuild system, please see:
+;
+;   http://llvm.org/docs/LLVMBuild.html
+;
+;===------------------------------------------------------------------------===;
+
+[component_0]
+type = Tool
+name = llvm-lto
+parent = Tools
+required_libraries = LTO Support all-targets
index 1b1a1f8..f1801b4 100644 (file)
@@ -9,7 +9,7 @@
 
 LEVEL := ../..
 TOOLNAME := llvm-lto
-LINK_COMPONENTS := support
+LINK_COMPONENTS := lto ipo scalaropts linker bitreader bitwriter mcdisassembler support target vectorize all-targets
 
 # This tool has no plugins, optimize startup time.
 TOOL_NO_EXPORTS := 1
@@ -17,6 +17,3 @@ TOOL_NO_EXPORTS := 1
 NO_INSTALL := 1
 
 include $(LEVEL)/Makefile.common
-
-LDFLAGS += -L$(LibDir)
-LIBS += -lLTO
index f25037c..82a2c82 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm-c/lto.h"
+#include "llvm/LTO/LTOCodeGenerator.h"
+#include "llvm/LTO/LTOModule.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/TargetSelect.h"
 
 using namespace llvm;
 
@@ -37,46 +39,48 @@ int main(int argc, char **argv) {
   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
   cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
 
+  // Initialize the configured targets.
+  InitializeAllTargets();
+  InitializeAllTargetMCs();
+  InitializeAllAsmPrinters();
+  InitializeAllAsmParsers();
+
   unsigned BaseArg = 0;
   std::string ErrorMessage;
 
-  lto_code_gen_t code_gen = lto_codegen_create();
-  if (code_gen == NULL)
-    errs() << argv[0] << ": error creating a code generation module: "
-           << lto_get_error_message() << "\n";
+  LTOCodeGenerator CodeGen;
 
-  lto_codegen_set_pic_model(code_gen, LTO_CODEGEN_PIC_MODEL_DYNAMIC);
-  lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF);
+  CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
+  CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
 
   for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
-    lto_module_t BitcodeModule = lto_module_create(InputFilenames[i].c_str());
-    if (BitcodeModule == NULL) {
+    std::string error;
+    OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
+                                                         error));
+    if (!error.empty()) {
       errs() << argv[0] << ": error loading file '" << InputFilenames[i]
-             << "': " << lto_get_error_message() << "\n";
+             << "': " << error << "\n";
       return 1;
     }
 
-    if (lto_codegen_add_module(code_gen, BitcodeModule)) {
+
+    if (!CodeGen.addModule(Module.get(), error)) {
       errs() << argv[0] << ": error adding file '" << InputFilenames[i]
-             << "': " << lto_get_error_message() << "\n";
-      lto_module_dispose(BitcodeModule);
+             << "': " << error << "\n";
       return 1;
     }
-
-    lto_module_dispose(BitcodeModule);
   }
 
   if (!OutputFilename.empty()) {
     size_t len = 0;
-    const void *Code = lto_codegen_compile(code_gen, &len);
+    std::string ErrorInfo;
+    const void *Code = CodeGen.compile(&len, ErrorInfo);
     if (Code == NULL) {
       errs() << argv[0]
-             << ": error compiling the code: " << lto_get_error_message()
-             << "\n";
+             << ": error compiling the code: " << ErrorInfo << "\n";
       return 1;
     }
 
-    std::string ErrorInfo;
     raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo);
     if (!ErrorInfo.empty()) {
       errs() << argv[0] << ": error opening the file '" << OutputFilename
@@ -86,10 +90,11 @@ int main(int argc, char **argv) {
 
     FileStream.write(reinterpret_cast<const char *>(Code), len);
   } else {
+    std::string ErrorInfo;
     const char *OutputName = NULL;
-    if (lto_codegen_compile_to_file(code_gen, &OutputName)) {
+    if (!CodeGen.compile_to_file(&OutputName, ErrorInfo)) {
       errs() << argv[0]
-             << ": error compiling the code: " << lto_get_error_message()
+             << ": error compiling the code: " << ErrorInfo
              << "\n";
       return 1;
     }
@@ -97,7 +102,5 @@ int main(int argc, char **argv) {
     outs() << "Wrote native object file '" << OutputName << "'\n";
   }
 
-  lto_codegen_dispose(code_gen);
-
   return 0;
 }
index c71aac1..8b26ddd 100644 (file)
@@ -1,14 +1,12 @@
 set(LLVM_LINK_COMPONENTS
   ${LLVM_TARGETS_TO_BUILD}
-  ipo scalaropts linker bitreader bitwriter mcdisassembler vectorize)
+  ipo scalaropts linker bitreader bitwriter lto mcdisassembler vectorize)
 
 add_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" )
 
 set(SOURCES
-  LTOCodeGenerator.cpp
   LTODisassembler.cpp
   lto.cpp
-  LTOModule.cpp
   )
 
 if( NOT WIN32 AND LLVM_ENABLE_PIC )
index 56c67df..672fef4 100644 (file)
@@ -10,7 +10,7 @@
 LEVEL := ../..
 LIBRARYNAME := LTO
 LINK_COMPONENTS := all-targets ipo scalaropts linker bitreader bitwriter \
-                   mcdisassembler vectorize
+                   lto mcdisassembler vectorize
 LINK_LIBS_IN_SHARED := 1
 SHARED_LIBRARY := 1
 
index db7147c..441bc77 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm-c/lto.h"
-#include "LTOCodeGenerator.h"
-#include "LTOModule.h"
+#include "llvm/LTO/LTOCodeGenerator.h"
+#include "llvm/LTO/LTOModule.h"
 #include "llvm-c/Core.h"
+#include "llvm-c/Target.h"
 
 
 // Holds most recent error string.
 // *** Not thread safe ***
 static std::string sLastErrorString;
 
+// Holds the initialization state of the LTO module.
+// *** Not thread safe ***
+static bool initialized = false;
+
+// Initialize the configured targets if they have not been initialized.
+static void lto_initialize() {
+  if (!initialized) {
+    LLVMInitializeAllTargetInfos();
+    LLVMInitializeAllTargets();
+    LLVMInitializeAllTargetMCs();
+    LLVMInitializeAllAsmParsers();
+    LLVMInitializeAllAsmPrinters();
+    LLVMInitializeAllDisassemblers();
+    initialized = true;
+  }
+}
+
 /// lto_get_version - Returns a printable string.
 extern const char* lto_get_version() {
   return LTOCodeGenerator::getVersionString();
@@ -63,12 +81,14 @@ lto_module_is_object_file_in_memory_for_target(const void* mem,
 /// lto_module_create - Loads an object file from disk. Returns NULL on error
 /// (check lto_get_error_message() for details).
 lto_module_t lto_module_create(const char* path) {
+  lto_initialize();
   return LTOModule::makeLTOModule(path, sLastErrorString);
 }
 
 /// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
 /// error (check lto_get_error_message() for details).
 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
+  lto_initialize();
   return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
 }
 
@@ -78,12 +98,14 @@ lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
                                                  size_t file_size,
                                                  size_t map_size,
                                                  off_t offset) {
+  lto_initialize();
   return LTOModule::makeLTOModule(fd, path, map_size, offset, sLastErrorString);
 }
 
 /// lto_module_create_from_memory - Loads an object file from memory. Returns
 /// NULL on error (check lto_get_error_message() for details).
 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
+  lto_initialize();
   return LTOModule::makeLTOModule(mem, length, sLastErrorString);
 }
 
@@ -127,6 +149,7 @@ lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
 /// lto_codegen_create - Instantiates a code generator. Returns NULL if there
 /// is an error.
 lto_code_gen_t lto_codegen_create(void) {
+  lto_initialize();
   return new LTOCodeGenerator();
 }