From cc48854d5d51a2d7557f1040a61f160ad86c9729 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 24 Sep 2013 23:52:22 +0000 Subject: [PATCH] Move LTO support library to a component, allowing it to be tested 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 --- .../llvm/LTO}/LTOCodeGenerator.h | 2 +- {tools/lto => include/llvm/LTO}/LTOModule.h | 8 ++- lib/CMakeLists.txt | 1 + lib/LLVMBuild.txt | 2 +- lib/LTO/CMakeLists.txt | 4 ++ lib/LTO/LLVMBuild.txt | 22 +++++++++ {tools/lto => lib/LTO}/LTOCodeGenerator.cpp | 13 ++--- {tools/lto => lib/LTO}/LTOModule.cpp | 10 +--- lib/LTO/Makefile | 15 ++++++ lib/Makefile | 2 +- test/{tools/lto => LTO}/cfi_endproc.ll | 0 test/{tools/lto => LTO}/lit.local.cfg | 0 tools/LLVMBuild.txt | 2 +- tools/llvm-lto/CMakeLists.txt | 5 +- tools/llvm-lto/LLVMBuild.txt | 22 +++++++++ tools/llvm-lto/Makefile | 5 +- tools/llvm-lto/llvm-lto.cpp | 49 ++++++++++--------- tools/lto/CMakeLists.txt | 4 +- tools/lto/Makefile | 2 +- tools/lto/lto.cpp | 27 +++++++++- 20 files changed, 136 insertions(+), 59 deletions(-) rename {tools/lto => include/llvm/LTO}/LTOCodeGenerator.h (98%) rename {tools/lto => include/llvm/LTO}/LTOModule.h (96%) create mode 100644 lib/LTO/CMakeLists.txt create mode 100644 lib/LTO/LLVMBuild.txt rename {tools/lto => lib/LTO}/LTOCodeGenerator.cpp (98%) rename {tools/lto => lib/LTO}/LTOModule.cpp (99%) create mode 100644 lib/LTO/Makefile rename test/{tools/lto => LTO}/cfi_endproc.ll (100%) rename test/{tools/lto => LTO}/lit.local.cfg (100%) create mode 100644 tools/llvm-lto/LLVMBuild.txt diff --git a/tools/lto/LTOCodeGenerator.h b/include/llvm/LTO/LTOCodeGenerator.h similarity index 98% rename from tools/lto/LTOCodeGenerator.h rename to include/llvm/LTO/LTOCodeGenerator.h index 02633629f98..3d151b93afc 100644 --- a/tools/lto/LTOCodeGenerator.h +++ b/include/llvm/LTO/LTOCodeGenerator.h @@ -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); diff --git a/tools/lto/LTOModule.h b/include/llvm/LTO/LTOModule.h similarity index 96% rename from tools/lto/LTOModule.h rename to include/llvm/LTO/LTOModule.h index 902e9c54389..973466cc0ea 100644 --- a/tools/lto/LTOModule.h +++ b/include/llvm/LTO/LTOModule.h @@ -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, diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 4d9aebcd0a2..7fbf12339d7 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -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) diff --git a/lib/LLVMBuild.txt b/lib/LLVMBuild.txt index ff288bc78b7..00280c86032 100644 --- a/lib/LLVMBuild.txt +++ b/lib/LLVMBuild.txt @@ -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 index 00000000000..8e00bcb6551 --- /dev/null +++ b/lib/LTO/CMakeLists.txt @@ -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 index 00000000000..38c1170b9e2 --- /dev/null +++ b/lib/LTO/LLVMBuild.txt @@ -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 diff --git a/tools/lto/LTOCodeGenerator.cpp b/lib/LTO/LTOCodeGenerator.cpp similarity index 98% rename from tools/lto/LTOCodeGenerator.cpp rename to lib/LTO/LTOCodeGenerator.cpp index 19e8c4c1491..22e9754bcfa 100644 --- a/tools/lto/LTOCodeGenerator.cpp +++ b/lib/LTO/LTOCodeGenerator.cpp @@ -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" @@ -40,10 +40,8 @@ #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())); } } diff --git a/tools/lto/LTOModule.cpp b/lib/LTO/LTOModule.cpp similarity index 99% rename from tools/lto/LTOModule.cpp rename to lib/LTO/LTOModule.cpp index 87e3573de90..1457f2f5a21 100644 --- a/tools/lto/LTOModule.cpp +++ b/lib/LTO/LTOModule.cpp @@ -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 m(getLazyBitcodeModule(buffer, getGlobalContext(), &errMsg)); diff --git a/lib/LTO/Makefile b/lib/LTO/Makefile new file mode 100644 index 00000000000..7ca125ab14b --- /dev/null +++ b/lib/LTO/Makefile @@ -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 + diff --git a/lib/Makefile b/lib/Makefile index 0a4435ef64d..cfe992668de 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -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 diff --git a/test/tools/lto/cfi_endproc.ll b/test/LTO/cfi_endproc.ll similarity index 100% rename from test/tools/lto/cfi_endproc.ll rename to test/LTO/cfi_endproc.ll diff --git a/test/tools/lto/lit.local.cfg b/test/LTO/lit.local.cfg similarity index 100% rename from test/tools/lto/lit.local.cfg rename to test/LTO/lit.local.cfg diff --git a/tools/LLVMBuild.txt b/tools/LLVMBuild.txt index 9ec89f3aa24..fa10b997a2c 100644 --- a/tools/LLVMBuild.txt +++ b/tools/LLVMBuild.txt @@ -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 diff --git a/tools/llvm-lto/CMakeLists.txt b/tools/llvm-lto/CMakeLists.txt index b253b69c1bc..348976cb811 100644 --- a/tools/llvm-lto/CMakeLists.txt +++ b/tools/llvm-lto/CMakeLists.txt @@ -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 index 00000000000..c1613a34c0e --- /dev/null +++ b/tools/llvm-lto/LLVMBuild.txt @@ -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 diff --git a/tools/llvm-lto/Makefile b/tools/llvm-lto/Makefile index 1b1a1f8b389..f1801b4b20c 100644 --- a/tools/llvm-lto/Makefile +++ b/tools/llvm-lto/Makefile @@ -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 diff --git a/tools/llvm-lto/llvm-lto.cpp b/tools/llvm-lto/llvm-lto.cpp index f25037c37aa..82a2c8288f1 100644 --- a/tools/llvm-lto/llvm-lto.cpp +++ b/tools/llvm-lto/llvm-lto.cpp @@ -12,12 +12,14 @@ // //===----------------------------------------------------------------------===// -#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 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(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; } diff --git a/tools/lto/CMakeLists.txt b/tools/lto/CMakeLists.txt index c71aac1eeca..8b26ddd57e8 100644 --- a/tools/lto/CMakeLists.txt +++ b/tools/lto/CMakeLists.txt @@ -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 ) diff --git a/tools/lto/Makefile b/tools/lto/Makefile index 56c67df3739..672fef4c64f 100644 --- a/tools/lto/Makefile +++ b/tools/lto/Makefile @@ -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 diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp index db7147c2bc7..441bc77352c 100644 --- a/tools/lto/lto.cpp +++ b/tools/lto/lto.cpp @@ -13,15 +13,33 @@ //===----------------------------------------------------------------------===// #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(); } -- 2.34.1