From 0b5745abd8e46ac60cd16f949a2fd5a581fc8fd4 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 19 Jun 2014 21:14:13 +0000 Subject: Use lib/LTO directly in the gold plugin. The tools/lto API is not the best choice for implementing a gold plugin. Among other issues: * It is an stable ABI. Old errors stay and we have to be really careful before adding new features. * It has to support two fairly different linkers: gold and ld64. * We end up with a plugin that depends on a shared lib, something quiet unusual in LLVM land. * It hides LLVM. For some features in the gold plugin it would be really nice to be able to just get a Module or a GlobalValue. This change is intended to be a very direct translation from the C API. It will just enable other fixes and cleanups. Tested with a LTO bootstrap on linux. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211315 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/gold/CMakeLists.txt | 7 ++-- tools/gold/gold-plugin.cpp | 93 ++++++++++++++++++++++++++++++---------------- 2 files changed, 65 insertions(+), 35 deletions(-) (limited to 'tools/gold') diff --git a/tools/gold/CMakeLists.txt b/tools/gold/CMakeLists.txt index 07a1e2849b..3864e15454 100644 --- a/tools/gold/CMakeLists.txt +++ b/tools/gold/CMakeLists.txt @@ -14,13 +14,14 @@ else() # ABI compatibility. add_definitions( -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 ) - set(LLVM_LINK_COMPONENTS support) + set(LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} + LTO + ) add_llvm_loadable_module(LLVMgold gold-plugin.cpp ) - target_link_libraries(LLVMgold ${cmake_2_8_12_PRIVATE} LTO) - endif() diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index a056c61817..f4b5caeeff 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -15,11 +15,15 @@ #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H #include "llvm-c/lto.h" #include "llvm/ADT/StringSet.h" +#include "llvm/CodeGen/CommandFlags.h" +#include "llvm/LTO/LTOCodeGenerator.h" +#include "llvm/LTO/LTOModule.h" #include "llvm/Support/Errno.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" +#include "llvm/Support/TargetSelect.h" #include "llvm/Support/ToolOutputFile.h" #include #include @@ -72,9 +76,10 @@ namespace { std::string output_name = ""; std::list Modules; std::vector Cleanup; - lto_code_gen_t code_gen = NULL; + LTOCodeGenerator *CodeGen = nullptr; StringSet<> CannotBeHidden; } +static llvm::TargetOptions TargetOpts; namespace options { enum generate_bc { BC_NO, BC_ALSO, BC_ONLY }; @@ -142,6 +147,7 @@ ld_plugin_status onload(ld_plugin_tv *tv) { // for services. bool registeredClaimFile = false; + bool RegisteredAllSymbolsRead = false; for (; tv->tv_tag != LDPT_NULL; ++tv) { switch (tv->tv_tag) { @@ -189,7 +195,7 @@ ld_plugin_status onload(ld_plugin_tv *tv) { if ((*callback)(all_symbols_read_hook) != LDPS_OK) return LDPS_ERR; - code_gen = lto_codegen_create(); + RegisteredAllSymbolsRead = true; } break; case LDPT_REGISTER_CLEANUP_HOOK: { ld_plugin_register_cleanup callback; @@ -233,6 +239,28 @@ ld_plugin_status onload(ld_plugin_tv *tv) { return LDPS_ERR; } + if (RegisteredAllSymbolsRead) { + InitializeAllTargetInfos(); + InitializeAllTargets(); + InitializeAllTargetMCs(); + InitializeAllAsmParsers(); + InitializeAllAsmPrinters(); + InitializeAllDisassemblers(); + TargetOpts = InitTargetOptionsFromCodeGenFlags(); + CodeGen = new LTOCodeGenerator(); + CodeGen->setTargetOptions(TargetOpts); + if (MAttrs.size()) { + std::string Attrs; + for (unsigned I = 0; I < MAttrs.size(); ++I) { + if (I > 0) + Attrs.append(","); + Attrs.append(MAttrs[I]); + } + + CodeGen->setAttr(Attrs.c_str()); + } + } + return LDPS_OK; } @@ -241,7 +269,7 @@ ld_plugin_status onload(ld_plugin_tv *tv) { /// with add_symbol if possible. static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, int *claimed) { - lto_module_t M; + LTOModule *M; const void *view; std::unique_ptr buffer; if (get_view) { @@ -264,17 +292,15 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, view = buffer->getBufferStart(); } - if (!lto_module_is_object_file_in_memory(view, file->filesize)) + if (!LTOModule::isBitcodeFile(view, file->filesize)) return LDPS_OK; - M = lto_module_create_from_memory(view, file->filesize); + std::string Error; + M = LTOModule::makeLTOModule(view, file->filesize, TargetOpts, Error); if (!M) { - if (const char* msg = lto_get_error_message()) { - (*message)(LDPL_ERROR, - "LLVM gold plugin has failed to create LTO module: %s", - msg); - return LDPS_ERR; - } + (*message)(LDPL_ERROR, + "LLVM gold plugin has failed to create LTO module: %s", + Error.c_str()); return LDPS_OK; } @@ -283,20 +309,20 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, claimed_file &cf = Modules.back(); if (!options::triple.empty()) - lto_module_set_target_triple(M, options::triple.c_str()); + M->setTargetTriple(options::triple.c_str()); cf.handle = file->handle; - unsigned sym_count = lto_module_get_num_symbols(M); + unsigned sym_count = M->getSymbolCount(); cf.syms.reserve(sym_count); for (unsigned i = 0; i != sym_count; ++i) { - lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i); + lto_symbol_attributes attrs = M->getSymbolAttributes(i); if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL) continue; cf.syms.push_back(ld_plugin_symbol()); ld_plugin_symbol &sym = cf.syms.back(); - sym.name = const_cast(lto_module_get_symbol_name(M, i)); + sym.name = const_cast(M->getSymbolName(i)); sym.name = strdup(sym.name); sym.version = NULL; @@ -359,15 +385,15 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, } } - if (code_gen) { - if (lto_codegen_add_module(code_gen, M)) { - (*message)(LDPL_ERROR, "Error linking module: %s", - lto_get_error_message()); + if (CodeGen) { + std::string Error; + if (!CodeGen->addModule(M, Error)) { + (*message)(LDPL_ERROR, "Error linking module: %s", Error.c_str()); return LDPS_ERR; } } - lto_module_dispose(M); + delete M; return LDPS_OK; } @@ -386,7 +412,7 @@ static bool mustPreserve(const claimed_file &F, int i) { /// codegen. static ld_plugin_status all_symbols_read_hook(void) { std::ofstream api_file; - assert(code_gen); + assert(CodeGen); if (options::generate_api_file) { api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc); @@ -403,7 +429,7 @@ static ld_plugin_status all_symbols_read_hook(void) { (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]); for (unsigned i = 0, e = I->syms.size(); i != e; i++) { if (mustPreserve(*I, i)) { - lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name); + CodeGen->addMustPreserveSymbol(I->syms[i].name); if (options::generate_api_file) api_file << I->syms[i].name << "\n"; @@ -414,16 +440,16 @@ static ld_plugin_status all_symbols_read_hook(void) { if (options::generate_api_file) api_file.close(); - lto_codegen_set_pic_model(code_gen, output_type); - lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF); + CodeGen->setCodePICModel(output_type); + CodeGen->setDebugInfo(LTO_DEBUG_MODEL_DWARF); if (!options::mcpu.empty()) - lto_codegen_set_cpu(code_gen, options::mcpu.c_str()); + CodeGen->setCpu(options::mcpu.c_str()); // Pass through extra options to the code generator. if (!options::extra.empty()) { for (std::vector::iterator it = options::extra.begin(); it != options::extra.end(); ++it) { - lto_codegen_debug_options(code_gen, (*it).c_str()); + CodeGen->setCodeGenDebugOptions((*it).c_str()); } } @@ -435,11 +461,12 @@ static ld_plugin_status all_symbols_read_hook(void) { path = options::bc_path; else path = output_name + ".bc"; - bool err = lto_codegen_write_merged_modules(code_gen, path.c_str()); - if (err) + CodeGen->parseCodeGenDebugOptions(); + std::string Error; + if (!CodeGen->writeMergedModules(path.c_str(), Error)) (*message)(LDPL_FATAL, "Failed to write the output file."); if (options::generate_bc_file == options::BC_ONLY) { - lto_codegen_dispose(code_gen); + delete CodeGen; exit(0); } } @@ -447,13 +474,15 @@ static ld_plugin_status all_symbols_read_hook(void) { std::string ObjPath; { const char *Temp; - if (lto_codegen_compile_to_file(code_gen, &Temp)) { + CodeGen->parseCodeGenDebugOptions(); + std::string Error; + if (!CodeGen->compile_to_file(&Temp, /*DisableOpt*/ false, /*DisableInline*/ + false, /*DisableGVNLoadPRE*/ false, Error)) (*message)(LDPL_ERROR, "Could not produce a combined object file\n"); - } ObjPath = Temp; } - lto_codegen_dispose(code_gen); + delete CodeGen; for (std::list::iterator I = Modules.begin(), E = Modules.end(); I != E; ++I) { for (unsigned i = 0; i != I->syms.size(); ++i) { -- cgit v1.2.3