diff options
Diffstat (limited to 'lib/LTO')
-rw-r--r-- | lib/LTO/CMakeLists.txt | 5 | ||||
-rw-r--r-- | lib/LTO/LLVMBuild.txt | 2 | ||||
-rw-r--r-- | lib/LTO/LTOCodeGenerator.cpp | 172 | ||||
-rw-r--r-- | lib/LTO/LTOModule.cpp | 66 |
4 files changed, 176 insertions, 69 deletions
diff --git a/lib/LTO/CMakeLists.txt b/lib/LTO/CMakeLists.txt index 8e00bcb..1c099bb 100644 --- a/lib/LTO/CMakeLists.txt +++ b/lib/LTO/CMakeLists.txt @@ -1,4 +1,9 @@ add_llvm_library(LLVMLTO LTOModule.cpp LTOCodeGenerator.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/LTO ) + +add_dependencies(LLVMLTO intrinsics_gen) diff --git a/lib/LTO/LLVMBuild.txt b/lib/LTO/LLVMBuild.txt index b9178e9..dfd424f 100644 --- a/lib/LTO/LLVMBuild.txt +++ b/lib/LTO/LLVMBuild.txt @@ -19,4 +19,4 @@ type = Library name = LTO parent = Libraries -required_libraries = BitReader BitWriter Core IPA IPO InstCombine Linker MC ObjCARC Object Scalar Support Target TransformUtils CodeGen +required_libraries = Analysis BitReader BitWriter CodeGen Core IPA IPO InstCombine Linker MC ObjCARC Object Scalar Support Target diff --git a/lib/LTO/LTOCodeGenerator.cpp b/lib/LTO/LTOCodeGenerator.cpp index c663d43..61c2749 100644 --- a/lib/LTO/LTOCodeGenerator.cpp +++ b/lib/LTO/LTOCodeGenerator.cpp @@ -15,6 +15,8 @@ #include "llvm/LTO/LTOCodeGenerator.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Analysis/Passes.h" +#include "llvm/Analysis/TargetLibraryInfo.h" +#include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/CodeGen/RuntimeLibcalls.h" #include "llvm/Config/config.h" @@ -24,6 +26,7 @@ #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" #include "llvm/IR/Verifier.h" @@ -33,7 +36,6 @@ #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/SubtargetFeature.h" -#include "llvm/PassManager.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormattedStream.h" @@ -44,7 +46,6 @@ #include "llvm/Support/TargetSelect.h" #include "llvm/Support/ToolOutputFile.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Target/TargetLibraryInfo.h" #include "llvm/Target/TargetLowering.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Target/TargetRegisterInfo.h" @@ -81,16 +82,27 @@ void LTOCodeGenerator::initialize() { CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT; DiagHandler = nullptr; DiagContext = nullptr; + OwnedModule = nullptr; 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(); + delete TargetMach; TargetMach = nullptr; - IRLinker.deleteModule(); - for (std::vector<char *>::iterator I = CodegenOptions.begin(), E = CodegenOptions.end(); I != E; ++I) @@ -108,7 +120,7 @@ void LTOCodeGenerator::initializeLTOPasses() { initializeGlobalOptPass(R); initializeConstantMergePass(R); initializeDAHPass(R); - initializeInstCombinerPass(R); + initializeInstructionCombiningPassPass(R); initializeSimpleInlinerPass(R); initializePruneEHPass(R); initializeGlobalDCEPass(R); @@ -140,6 +152,22 @@ bool LTOCodeGenerator::addModule(LTOModule *mod) { return !ret; } +void LTOCodeGenerator::setModule(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()); + + const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs(); + for (int I = 0, E = Undefs.size(); I != E; ++I) + AsmUndefinedRefs[Undefs[I]] = 1; +} + void LTOCodeGenerator::setTargetOptions(TargetOptions options) { Options = options; } @@ -201,12 +229,8 @@ bool LTOCodeGenerator::writeMergedModules(const char *path, return true; } -bool LTOCodeGenerator::compile_to_file(const char** name, - bool disableOpt, - bool disableInline, - bool disableGVNLoadPRE, - bool disableVectorization, - std::string& errMsg) { +bool LTOCodeGenerator::compileOptimizedToFile(const char **name, + std::string &errMsg) { // make unique temp .o file to put generated object file SmallString<128> Filename; int FD; @@ -220,9 +244,7 @@ bool LTOCodeGenerator::compile_to_file(const char** name, // generate object file tool_output_file objFile(Filename.c_str(), FD); - bool genResult = - generateObjectFile(objFile.os(), disableOpt, disableInline, - disableGVNLoadPRE, disableVectorization, errMsg); + bool genResult = compileOptimized(objFile.os(), errMsg); objFile.os().close(); if (objFile.os().has_error()) { objFile.os().clear_error(); @@ -241,15 +263,10 @@ bool LTOCodeGenerator::compile_to_file(const char** name, return true; } -const void* LTOCodeGenerator::compile(size_t* length, - bool disableOpt, - bool disableInline, - bool disableGVNLoadPRE, - bool disableVectorization, - std::string& errMsg) { +const void *LTOCodeGenerator::compileOptimized(size_t *length, + std::string &errMsg) { const char *name; - if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE, - disableVectorization, errMsg)) + if (!compileOptimizedToFile(&name, errMsg)) return nullptr; // read .o file into memory buffer @@ -272,6 +289,33 @@ const void* LTOCodeGenerator::compile(size_t* length, return NativeObjectFile->getBufferStart(); } + +bool LTOCodeGenerator::compile_to_file(const char **name, + bool disableOpt, + bool disableInline, + bool disableGVNLoadPRE, + bool disableVectorization, + std::string &errMsg) { + if (!optimize(disableOpt, disableInline, disableGVNLoadPRE, + disableVectorization, errMsg)) + return false; + + return compileOptimizedToFile(name, errMsg); +} + +const void* LTOCodeGenerator::compile(size_t *length, + bool disableOpt, + bool disableInline, + bool disableGVNLoadPRE, + bool disableVectorization, + std::string &errMsg) { + if (!optimize(disableOpt, disableInline, disableGVNLoadPRE, + disableVectorization, errMsg)) + return nullptr; + + return compileOptimized(length, errMsg); +} + bool LTOCodeGenerator::determineTarget(std::string &errMsg) { if (TargetMach) return true; @@ -368,10 +412,13 @@ static void findUsedValues(GlobalVariable *LLVMUsed, UsedValues.insert(GV); } +// Collect names of runtime library functions. User-defined functions with the +// same names are added to llvm.compiler.used to prevent them from being +// deleted by optimizations. static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls, const TargetLibraryInfo& TLI, - const TargetLowering *Lowering) -{ + const Module &Mod, + const TargetMachine &TM) { // TargetLibraryInfo has info on C runtime library calls on the current // target. for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs); @@ -381,14 +428,21 @@ static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls, Libcalls.push_back(TLI.getName(F)); } - // TargetLowering has info on library calls that CodeGen expects to be - // available, both from the C runtime and compiler-rt. - if (Lowering) - for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL); - I != E; ++I) - if (const char *Name - = Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I))) - Libcalls.push_back(Name); + SmallPtrSet<const TargetLowering *, 1> TLSet; + + for (const Function &F : Mod) { + const TargetLowering *Lowering = + TM.getSubtargetImpl(F)->getTargetLowering(); + + if (Lowering && TLSet.insert(Lowering).second) + // TargetLowering has info on library calls that CodeGen expects to be + // available, both from the C runtime and compiler-rt. + for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL); + I != E; ++I) + if (const char *Name = + Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I))) + Libcalls.push_back(Name); + } array_pod_sort(Libcalls.begin(), Libcalls.end()); Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()), @@ -401,18 +455,19 @@ void LTOCodeGenerator::applyScopeRestrictions() { Module *mergedModule = IRLinker.getModule(); // Start off with a verification pass. - PassManager passes; + legacy::PassManager passes; passes.add(createVerifierPass()); passes.add(createDebugInfoVerifierPass()); // mark which symbols can not be internalized - Mangler Mangler(TargetMach->getSubtargetImpl()->getDataLayout()); + Mangler Mangler(TargetMach->getDataLayout()); std::vector<const char*> MustPreserveList; SmallPtrSet<GlobalValue*, 8> AsmUsed; std::vector<StringRef> Libcalls; - TargetLibraryInfo TLI(Triple(TargetMach->getTargetTriple())); - accumulateAndSortLibcalls( - Libcalls, TLI, TargetMach->getSubtargetImpl()->getTargetLowering()); + TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple())); + TargetLibraryInfo TLI(TLII); + + accumulateAndSortLibcalls(Libcalls, TLI, *mergedModule, *TargetMach); for (Module::iterator f = mergedModule->begin(), e = mergedModule->end(); f != e; ++f) @@ -457,12 +512,11 @@ void LTOCodeGenerator::applyScopeRestrictions() { } /// Optimize merged modules using various IPO passes -bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, - bool DisableOpt, - bool DisableInline, - bool DisableGVNLoadPRE, - bool DisableVectorization, - std::string &errMsg) { +bool LTOCodeGenerator::optimize(bool DisableOpt, + bool DisableInline, + bool DisableGVNLoadPRE, + bool DisableVectorization, + std::string &errMsg) { if (!this->determineTarget(errMsg)) return false; @@ -472,10 +526,14 @@ bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, this->applyScopeRestrictions(); // Instantiate the pass manager to organize the passes. - PassManager passes; + legacy::PassManager passes; // Add an appropriate DataLayout instance for this module... - mergedModule->setDataLayout(TargetMach->getSubtargetImpl()->getDataLayout()); + mergedModule->setDataLayout(TargetMach->getDataLayout()); + + passes.add(new DataLayoutPass()); + passes.add( + createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis())); Triple TargetTriple(TargetMach->getTargetTriple()); PassManagerBuilder PMB; @@ -484,15 +542,30 @@ bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, PMB.SLPVectorize = !DisableVectorization; if (!DisableInline) PMB.Inliner = createFunctionInliningPass(); - PMB.LibraryInfo = new TargetLibraryInfo(TargetTriple); + PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple); if (DisableOpt) PMB.OptLevel = 0; PMB.VerifyInput = true; PMB.VerifyOutput = true; - PMB.populateLTOPassManager(passes, TargetMach); + PMB.populateLTOPassManager(passes); - PassManager codeGenPasses; + // Run our queue of passes all at once now, efficiently. + passes.run(*mergedModule); + + return true; +} + +bool LTOCodeGenerator::compileOptimized(raw_ostream &out, std::string &errMsg) { + if (!this->determineTarget(errMsg)) + return false; + + Module *mergedModule = IRLinker.getModule(); + + // Mark which symbols can not be internalized + this->applyScopeRestrictions(); + + legacy::PassManager codeGenPasses; codeGenPasses.add(new DataLayoutPass()); @@ -508,9 +581,6 @@ bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, return false; } - // Run our queue of passes all at once now, efficiently. - passes.run(*mergedModule); - // Run the code generator, and write assembly file codeGenPasses.run(*mergedModule); diff --git a/lib/LTO/LTOModule.cpp b/lib/LTO/LTOModule.cpp index 4108ef2..0d07791 100644 --- a/lib/LTO/LTOModule.cpp +++ b/lib/LTO/LTOModule.cpp @@ -17,6 +17,7 @@ #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/CodeGen/Analysis.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" @@ -146,6 +147,44 @@ LTOModule *LTOModule::createInContext(const void *mem, size_t length, return makeLTOModule(Buffer, options, errMsg, Context); } +static Module *parseBitcodeFileImpl(MemoryBufferRef Buffer, + LLVMContext &Context, bool ShouldBeLazy, + std::string &ErrMsg) { + + // Find the buffer. + ErrorOr<MemoryBufferRef> MBOrErr = + IRObjectFile::findBitcodeInMemBuffer(Buffer); + if (std::error_code EC = MBOrErr.getError()) { + ErrMsg = EC.message(); + return nullptr; + } + + std::function<void(const DiagnosticInfo &)> DiagnosticHandler = + [&ErrMsg](const DiagnosticInfo &DI) { + raw_string_ostream Stream(ErrMsg); + DiagnosticPrinterRawOStream DP(Stream); + DI.print(DP); + }; + + if (!ShouldBeLazy) { + // Parse the full file. + ErrorOr<Module *> M = + parseBitcodeFile(*MBOrErr, Context, DiagnosticHandler); + if (!M) + return nullptr; + return *M; + } + + // Parse lazily. + std::unique_ptr<MemoryBuffer> LightweightBuf = + MemoryBuffer::getMemBuffer(*MBOrErr, false); + ErrorOr<Module *> M = getLazyBitcodeModule(std::move(LightweightBuf), Context, + DiagnosticHandler); + if (!M) + return nullptr; + return *M; +} + LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer, TargetOptions options, std::string &errMsg, LLVMContext *Context) { @@ -155,18 +194,13 @@ LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer, Context = OwnedContext.get(); } - ErrorOr<MemoryBufferRef> MBOrErr = - IRObjectFile::findBitcodeInMemBuffer(Buffer); - if (std::error_code EC = MBOrErr.getError()) { - errMsg = EC.message(); + // If we own a context, we know this is being used only for symbol + // extraction, not linking. Be lazy in that case. + std::unique_ptr<Module> M(parseBitcodeFileImpl( + Buffer, *Context, + /* ShouldBeLazy */ static_cast<bool>(OwnedContext), errMsg)); + if (!M) return nullptr; - } - ErrorOr<Module *> MOrErr = parseBitcodeFile(*MBOrErr, *Context); - if (std::error_code EC = MOrErr.getError()) { - errMsg = EC.message(); - return nullptr; - } - std::unique_ptr<Module> M(MOrErr.get()); std::string TripleStr = M->getTargetTriple(); if (TripleStr.empty()) @@ -195,7 +229,7 @@ LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer, TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr, options); - M->setDataLayout(target->getSubtargetImpl()->getDataLayout()); + M->setDataLayout(target->getDataLayout()); std::unique_ptr<object::IRObjectFile> IRObj( new object::IRObjectFile(Buffer, std::move(M))); @@ -604,7 +638,7 @@ bool LTOModule::parseSymbols(std::string &errMsg) { /// parseMetadata - Parse metadata from the module void LTOModule::parseMetadata() { // Linker Options - if (Value *Val = getModule().getModuleFlag("Linker Options")) { + if (Metadata *Val = getModule().getModuleFlag("Linker Options")) { MDNode *LinkerOptions = cast<MDNode>(Val); for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); @@ -615,10 +649,8 @@ void LTOModule::parseMetadata() { // here. StringRef Op = _linkeropt_strings.insert(MDOption->getString()).first->first(); - StringRef DepLibName = _target->getSubtargetImpl() - ->getTargetLowering() - ->getObjFileLowering() - .getDepLibFromLinkerOpt(Op); + StringRef DepLibName = + _target->getObjFileLowering()->getDepLibFromLinkerOpt(Op); if (!DepLibName.empty()) _deplibs.push_back(DepLibName.data()); else if (!Op.empty()) |