1 //===- LTO.cpp ------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "LTO.h" 10 #include "Config.h" 11 #include "InputFiles.h" 12 #include "Symbols.h" 13 #include "lld/Common/Args.h" 14 #include "lld/Common/ErrorHandler.h" 15 #include "lld/Common/Strings.h" 16 #include "lld/Common/TargetOptionsCommandFlags.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/Twine.h" 21 #include "llvm/IR/DiagnosticPrinter.h" 22 #include "llvm/LTO/Caching.h" 23 #include "llvm/LTO/Config.h" 24 #include "llvm/LTO/LTO.h" 25 #include "llvm/Object/SymbolicFile.h" 26 #include "llvm/Support/CodeGen.h" 27 #include "llvm/Support/Error.h" 28 #include "llvm/Support/FileSystem.h" 29 #include "llvm/Support/MemoryBuffer.h" 30 #include "llvm/Support/raw_ostream.h" 31 #include <algorithm> 32 #include <cstddef> 33 #include <memory> 34 #include <string> 35 #include <system_error> 36 #include <vector> 37 38 using namespace llvm; 39 using namespace lld; 40 using namespace lld::wasm; 41 42 static std::unique_ptr<lto::LTO> createLTO() { 43 lto::Config C; 44 C.Options = initTargetOptionsFromCodeGenFlags(); 45 46 // Always emit a section per function/data with LTO. 47 C.Options.FunctionSections = true; 48 C.Options.DataSections = true; 49 50 C.DisableVerify = Config->DisableVerify; 51 C.DiagHandler = diagnosticHandler; 52 C.OptLevel = Config->LTOO; 53 C.MAttrs = getMAttrs(); 54 C.CGOptLevel = args::getCGOptLevel(Config->LTOO); 55 56 if (Config->Relocatable) 57 C.RelocModel = None; 58 else if (Config->Pic) 59 C.RelocModel = Reloc::PIC_; 60 else 61 C.RelocModel = Reloc::Static; 62 63 if (Config->SaveTemps) 64 checkError(C.addSaveTemps(Config->OutputFile.str() + ".", 65 /*UseInputModulePath*/ true)); 66 67 lto::ThinBackend Backend; 68 if (Config->ThinLTOJobs != -1U) 69 Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs); 70 return llvm::make_unique<lto::LTO>(std::move(C), Backend, 71 Config->LTOPartitions); 72 } 73 74 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {} 75 76 BitcodeCompiler::~BitcodeCompiler() = default; 77 78 static void undefine(Symbol *S) { 79 if (auto F = dyn_cast<DefinedFunction>(S)) 80 replaceSymbol<UndefinedFunction>(F, F->getName(), F->getName(), 81 DefaultModule, 0, 82 F->getFile(), F->Signature); 83 else if (isa<DefinedData>(S)) 84 replaceSymbol<UndefinedData>(S, S->getName(), 0, S->getFile()); 85 else 86 llvm_unreachable("unexpected symbol kind"); 87 } 88 89 void BitcodeCompiler::add(BitcodeFile &F) { 90 lto::InputFile &Obj = *F.Obj; 91 unsigned SymNum = 0; 92 ArrayRef<Symbol *> Syms = F.getSymbols(); 93 std::vector<lto::SymbolResolution> Resols(Syms.size()); 94 95 // Provide a resolution to the LTO API for each symbol. 96 for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) { 97 Symbol *Sym = Syms[SymNum]; 98 lto::SymbolResolution &R = Resols[SymNum]; 99 ++SymNum; 100 101 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 102 // reports two symbols for module ASM defined. Without this check, lld 103 // flags an undefined in IR with a definition in ASM as prevailing. 104 // Once IRObjectFile is fixed to report only one symbol this hack can 105 // be removed. 106 R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F; 107 R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj || 108 (R.Prevailing && Sym->isExported()); 109 if (R.Prevailing) 110 undefine(Sym); 111 112 // We tell LTO to not apply interprocedural optimization for wrapped 113 // (with --wrap) symbols because otherwise LTO would inline them while 114 // their values are still not final. 115 R.LinkerRedefined = !Sym->CanInline; 116 } 117 checkError(LTOObj->add(std::move(F.Obj), Resols)); 118 } 119 120 // Merge all the bitcode files we have seen, codegen the result 121 // and return the resulting objects. 122 std::vector<StringRef> BitcodeCompiler::compile() { 123 unsigned MaxTasks = LTOObj->getMaxTasks(); 124 Buf.resize(MaxTasks); 125 Files.resize(MaxTasks); 126 127 // The --thinlto-cache-dir option specifies the path to a directory in which 128 // to cache native object files for ThinLTO incremental builds. If a path was 129 // specified, configure LTO to use it as the cache directory. 130 lto::NativeObjectCache Cache; 131 if (!Config->ThinLTOCacheDir.empty()) 132 Cache = check( 133 lto::localCache(Config->ThinLTOCacheDir, 134 [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) { 135 Files[Task] = std::move(MB); 136 })); 137 138 checkError(LTOObj->run( 139 [&](size_t Task) { 140 return llvm::make_unique<lto::NativeObjectStream>( 141 llvm::make_unique<raw_svector_ostream>(Buf[Task])); 142 }, 143 Cache)); 144 145 if (!Config->ThinLTOCacheDir.empty()) 146 pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy); 147 148 std::vector<StringRef> Ret; 149 for (unsigned I = 0; I != MaxTasks; ++I) { 150 if (Buf[I].empty()) 151 continue; 152 if (Config->SaveTemps) { 153 if (I == 0) 154 saveBuffer(Buf[I], Config->OutputFile + ".lto.o"); 155 else 156 saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.o"); 157 } 158 Ret.emplace_back(Buf[I].data(), Buf[I].size()); 159 } 160 161 for (std::unique_ptr<MemoryBuffer> &File : Files) 162 if (File) 163 Ret.push_back(File->getBuffer()); 164 165 return Ret; 166 } 167