1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 12:41:49 +01:00

Move LTO support library to a component, allowing it to be tested

more reliably across platforms.  Patch by Tom Roeder!

llvm-svn: 191343
This commit is contained in:
Peter Collingbourne 2013-09-24 23:52:22 +00:00
parent bdb3e2822e
commit 8969240f9e
20 changed files with 136 additions and 59 deletions

View File

@ -96,7 +96,7 @@ struct LTOCodeGenerator {
// single object file. Instead of returning the object-file-path to the caller // 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 // (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 // 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); const void *compile(size_t *length, std::string &errMsg);

View File

@ -76,7 +76,13 @@ public:
const char *triplePrefix); const char *triplePrefix);
/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership /// 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, static LTOModule *makeLTOModule(const char* path,
std::string &errMsg); std::string &errMsg);
static LTOModule *makeLTOModule(int fd, const char *path, static LTOModule *makeLTOModule(int fd, const char *path,

View File

@ -7,6 +7,7 @@ add_subdirectory(Bitcode)
add_subdirectory(Transforms) add_subdirectory(Transforms)
add_subdirectory(Linker) add_subdirectory(Linker)
add_subdirectory(Analysis) add_subdirectory(Analysis)
add_subdirectory(LTO)
add_subdirectory(MC) add_subdirectory(MC)
add_subdirectory(Object) add_subdirectory(Object)
add_subdirectory(Option) add_subdirectory(Option)

View File

@ -16,7 +16,7 @@
;===------------------------------------------------------------------------===; ;===------------------------------------------------------------------------===;
[common] [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] [component_0]
type = Group type = Group

4
lib/LTO/CMakeLists.txt Normal file
View File

@ -0,0 +1,4 @@
add_llvm_library(LLVMLTO
LTOModule.cpp
LTOCodeGenerator.cpp
)

22
lib/LTO/LLVMBuild.txt Normal file
View File

@ -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

View File

@ -12,8 +12,8 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "LTOCodeGenerator.h" #include "llvm/LTO/LTOCodeGenerator.h"
#include "LTOModule.h" #include "llvm/LTO/LTOModule.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/Analysis/Passes.h" #include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/Verifier.h" #include "llvm/Analysis/Verifier.h"
@ -40,10 +40,8 @@
#include "llvm/Support/TargetSelect.h" #include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ToolOutputFile.h" #include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/system_error.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/TargetRegisterInfo.h"
#include "llvm/Target/Mangler.h"
#include "llvm/Transforms/IPO.h" #include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h" #include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Transforms/ObjCARC.h" #include "llvm/Transforms/ObjCARC.h"
@ -73,9 +71,6 @@ LTOCodeGenerator::LTOCodeGenerator()
: Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)), : Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)),
TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false), TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false),
CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), NativeObjectFile(NULL) { CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), NativeObjectFile(NULL) {
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
initializeLTOPasses(); initializeLTOPasses();
} }
@ -452,7 +447,7 @@ void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
// ParseCommandLineOptions() expects argv[0] to be program name. Lazily add // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
// that. // that.
if (CodegenOptions.empty()) if (CodegenOptions.empty())
CodegenOptions.push_back(strdup("libLTO")); CodegenOptions.push_back(strdup("libLLVMLTO"));
CodegenOptions.push_back(strdup(o.first.str().c_str())); CodegenOptions.push_back(strdup(o.first.str().c_str()));
} }
} }

View File

@ -12,7 +12,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "LTOModule.h" #include "llvm/LTO/LTOModule.h"
#include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
#include "llvm/Bitcode/ReaderWriter.h" #include "llvm/Bitcode/ReaderWriter.h"
@ -248,14 +248,6 @@ void LTOModule::getTargetOptions(TargetOptions &Options) {
LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer, LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
std::string &errMsg) { std::string &errMsg) {
static bool Initialized = false;
if (!Initialized) {
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmParsers();
Initialized = true;
}
// parse bitcode buffer // parse bitcode buffer
OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(), OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
&errMsg)); &errMsg));

15
lib/LTO/Makefile Normal file
View File

@ -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

View File

@ -11,7 +11,7 @@ LEVEL = ..
include $(LEVEL)/Makefile.config include $(LEVEL)/Makefile.config
PARALLEL_DIRS := IR AsmParser Bitcode Analysis Transforms CodeGen \ PARALLEL_DIRS := IR AsmParser Bitcode Analysis Transforms CodeGen \
Target ExecutionEngine Linker MC Object Option DebugInfo \ Target ExecutionEngine Linker LTO MC Object Option DebugInfo \
IRReader IRReader
include $(LEVEL)/Makefile.common include $(LEVEL)/Makefile.common

View File

@ -16,7 +16,7 @@
;===------------------------------------------------------------------------===; ;===------------------------------------------------------------------------===;
[common] [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] [component_0]
type = Group type = Group

View File

@ -1,7 +1,6 @@
set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} lto support)
add_llvm_tool(llvm-lto add_llvm_tool(llvm-lto
llvm-lto.cpp llvm-lto.cpp
) )
target_link_libraries(llvm-lto LTO LLVMSupport)
add_dependencies(llvm-lto lto)

View File

@ -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

View File

@ -9,7 +9,7 @@
LEVEL := ../.. LEVEL := ../..
TOOLNAME := llvm-lto 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. # This tool has no plugins, optimize startup time.
TOOL_NO_EXPORTS := 1 TOOL_NO_EXPORTS := 1
@ -17,6 +17,3 @@ TOOL_NO_EXPORTS := 1
NO_INSTALL := 1 NO_INSTALL := 1
include $(LEVEL)/Makefile.common include $(LEVEL)/Makefile.common
LDFLAGS += -L$(LibDir)
LIBS += -lLTO

View File

@ -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/CommandLine.h"
#include "llvm/Support/ManagedStatic.h" #include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h" #include "llvm/Support/Signals.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Support/TargetSelect.h"
using namespace llvm; using namespace llvm;
@ -37,46 +39,48 @@ int main(int argc, char **argv) {
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n"); cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
// Initialize the configured targets.
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
InitializeAllAsmParsers();
unsigned BaseArg = 0; unsigned BaseArg = 0;
std::string ErrorMessage; std::string ErrorMessage;
lto_code_gen_t code_gen = lto_codegen_create(); LTOCodeGenerator CodeGen;
if (code_gen == NULL)
errs() << argv[0] << ": error creating a code generation module: "
<< lto_get_error_message() << "\n";
lto_codegen_set_pic_model(code_gen, LTO_CODEGEN_PIC_MODEL_DYNAMIC); CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF); CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) { for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
lto_module_t BitcodeModule = lto_module_create(InputFilenames[i].c_str()); std::string error;
if (BitcodeModule == NULL) { OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
error));
if (!error.empty()) {
errs() << argv[0] << ": error loading file '" << InputFilenames[i] errs() << argv[0] << ": error loading file '" << InputFilenames[i]
<< "': " << lto_get_error_message() << "\n"; << "': " << error << "\n";
return 1; return 1;
} }
if (lto_codegen_add_module(code_gen, BitcodeModule)) {
if (!CodeGen.addModule(Module.get(), error)) {
errs() << argv[0] << ": error adding file '" << InputFilenames[i] errs() << argv[0] << ": error adding file '" << InputFilenames[i]
<< "': " << lto_get_error_message() << "\n"; << "': " << error << "\n";
lto_module_dispose(BitcodeModule);
return 1; return 1;
} }
lto_module_dispose(BitcodeModule);
} }
if (!OutputFilename.empty()) { if (!OutputFilename.empty()) {
size_t len = 0; 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) { if (Code == NULL) {
errs() << argv[0] errs() << argv[0]
<< ": error compiling the code: " << lto_get_error_message() << ": error compiling the code: " << ErrorInfo << "\n";
<< "\n";
return 1; return 1;
} }
std::string ErrorInfo;
raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo); raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo);
if (!ErrorInfo.empty()) { if (!ErrorInfo.empty()) {
errs() << argv[0] << ": error opening the file '" << OutputFilename 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); FileStream.write(reinterpret_cast<const char *>(Code), len);
} else { } else {
std::string ErrorInfo;
const char *OutputName = NULL; const char *OutputName = NULL;
if (lto_codegen_compile_to_file(code_gen, &OutputName)) { if (!CodeGen.compile_to_file(&OutputName, ErrorInfo)) {
errs() << argv[0] errs() << argv[0]
<< ": error compiling the code: " << lto_get_error_message() << ": error compiling the code: " << ErrorInfo
<< "\n"; << "\n";
return 1; return 1;
} }
@ -97,7 +102,5 @@ int main(int argc, char **argv) {
outs() << "Wrote native object file '" << OutputName << "'\n"; outs() << "Wrote native object file '" << OutputName << "'\n";
} }
lto_codegen_dispose(code_gen);
return 0; return 0;
} }

View File

@ -1,14 +1,12 @@
set(LLVM_LINK_COMPONENTS set(LLVM_LINK_COMPONENTS
${LLVM_TARGETS_TO_BUILD} ${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}\" ) add_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" )
set(SOURCES set(SOURCES
LTOCodeGenerator.cpp
LTODisassembler.cpp LTODisassembler.cpp
lto.cpp lto.cpp
LTOModule.cpp
) )
if( NOT WIN32 AND LLVM_ENABLE_PIC ) if( NOT WIN32 AND LLVM_ENABLE_PIC )

View File

@ -10,7 +10,7 @@
LEVEL := ../.. LEVEL := ../..
LIBRARYNAME := LTO LIBRARYNAME := LTO
LINK_COMPONENTS := all-targets ipo scalaropts linker bitreader bitwriter \ LINK_COMPONENTS := all-targets ipo scalaropts linker bitreader bitwriter \
mcdisassembler vectorize lto mcdisassembler vectorize
LINK_LIBS_IN_SHARED := 1 LINK_LIBS_IN_SHARED := 1
SHARED_LIBRARY := 1 SHARED_LIBRARY := 1

View File

@ -13,15 +13,33 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm-c/lto.h" #include "llvm-c/lto.h"
#include "LTOCodeGenerator.h" #include "llvm/LTO/LTOCodeGenerator.h"
#include "LTOModule.h" #include "llvm/LTO/LTOModule.h"
#include "llvm-c/Core.h" #include "llvm-c/Core.h"
#include "llvm-c/Target.h"
// Holds most recent error string. // Holds most recent error string.
// *** Not thread safe *** // *** Not thread safe ***
static std::string sLastErrorString; 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. /// lto_get_version - Returns a printable string.
extern const char* lto_get_version() { extern const char* lto_get_version() {
return LTOCodeGenerator::getVersionString(); 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 /// lto_module_create - Loads an object file from disk. Returns NULL on error
/// (check lto_get_error_message() for details). /// (check lto_get_error_message() for details).
lto_module_t lto_module_create(const char* path) { lto_module_t lto_module_create(const char* path) {
lto_initialize();
return LTOModule::makeLTOModule(path, sLastErrorString); return LTOModule::makeLTOModule(path, sLastErrorString);
} }
/// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on /// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
/// error (check lto_get_error_message() for details). /// 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_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
lto_initialize();
return LTOModule::makeLTOModule(fd, path, size, sLastErrorString); 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 file_size,
size_t map_size, size_t map_size,
off_t offset) { off_t offset) {
lto_initialize();
return LTOModule::makeLTOModule(fd, path, map_size, offset, sLastErrorString); return LTOModule::makeLTOModule(fd, path, map_size, offset, sLastErrorString);
} }
/// lto_module_create_from_memory - Loads an object file from memory. Returns /// lto_module_create_from_memory - Loads an object file from memory. Returns
/// NULL on error (check lto_get_error_message() for details). /// 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_module_t lto_module_create_from_memory(const void* mem, size_t length) {
lto_initialize();
return LTOModule::makeLTOModule(mem, length, sLastErrorString); 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 /// lto_codegen_create - Instantiates a code generator. Returns NULL if there
/// is an error. /// is an error.
lto_code_gen_t lto_codegen_create(void) { lto_code_gen_t lto_codegen_create(void) {
lto_initialize();
return new LTOCodeGenerator(); return new LTOCodeGenerator();
} }