Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 1 | //===- LTO.cpp ------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "LTO.h" |
| 11 | #include "Config.h" |
| 12 | #include "Error.h" |
| 13 | #include "InputFiles.h" |
| 14 | #include "Symbols.h" |
| 15 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 16 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 17 | #include "llvm/Bitcode/ReaderWriter.h" |
Davide Italiano | 8eca282 | 2016-04-01 00:35:29 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/CommandFlags.h" |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/ParallelCG.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LegacyPassManager.h" |
| 21 | #include "llvm/Linker/IRMover.h" |
| 22 | #include "llvm/Support/StringSaver.h" |
| 23 | #include "llvm/Support/TargetRegistry.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Transforms/IPO.h" |
Davide Italiano | 86f2bd5 | 2016-03-29 21:46:35 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | using namespace llvm::object; |
| 31 | using namespace llvm::ELF; |
| 32 | |
| 33 | using namespace lld; |
| 34 | using namespace lld::elf; |
| 35 | |
| 36 | // This is for use when debugging LTO. |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 37 | static void saveLtoObjectFile(StringRef Buffer, unsigned I, bool Many) { |
| 38 | SmallString<128> Filename = Config->OutputFile; |
| 39 | if (Many) |
| 40 | Filename += utostr(I); |
| 41 | Filename += ".lto.o"; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 42 | std::error_code EC; |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 43 | raw_fd_ostream OS(Filename, EC, sys::fs::OpenFlags::F_None); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 44 | check(EC); |
| 45 | OS << Buffer; |
| 46 | } |
| 47 | |
| 48 | // This is for use when debugging LTO. |
| 49 | static void saveBCFile(Module &M, StringRef Suffix) { |
| 50 | std::error_code EC; |
| 51 | raw_fd_ostream OS(Config->OutputFile.str() + Suffix.str(), EC, |
| 52 | sys::fs::OpenFlags::F_None); |
| 53 | check(EC); |
| 54 | WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true); |
| 55 | } |
| 56 | |
| 57 | // Run LTO passes. |
Rui Ueyama | 4e62db4 | 2016-03-29 19:19:03 +0000 | [diff] [blame] | 58 | // Note that the gold plugin has a similar piece of code, so |
| 59 | // it is probably better to move this code to a common place. |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 60 | static void runLTOPasses(Module &M, TargetMachine &TM) { |
| 61 | legacy::PassManager LtoPasses; |
| 62 | LtoPasses.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis())); |
| 63 | PassManagerBuilder PMB; |
| 64 | PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())); |
| 65 | PMB.Inliner = createFunctionInliningPass(); |
Davide Italiano | 842fa53 | 2016-04-03 03:39:09 +0000 | [diff] [blame] | 66 | PMB.VerifyInput = PMB.VerifyOutput = !Config->DisableVerify; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 67 | PMB.LoopVectorize = true; |
| 68 | PMB.SLPVectorize = true; |
Peter Collingbourne | ed22f9b | 2016-03-31 21:00:27 +0000 | [diff] [blame] | 69 | PMB.OptLevel = Config->LtoO; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 70 | PMB.populateLTOPassManager(LtoPasses); |
| 71 | LtoPasses.run(M); |
| 72 | |
| 73 | if (Config->SaveTemps) |
| 74 | saveBCFile(M, ".lto.opt.bc"); |
| 75 | } |
| 76 | |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame^] | 77 | static bool shouldInternalize(const SmallPtrSet<GlobalValue *, 8> &Used, |
| 78 | SymbolBody &B, GlobalValue *GV) { |
| 79 | if (B.isUsedInRegularObj()) |
| 80 | return false; |
| 81 | |
| 82 | if (Used.count(GV)) |
| 83 | return false; |
| 84 | |
| 85 | return !B.includeInDynsym(); |
| 86 | } |
| 87 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 88 | void BitcodeCompiler::add(BitcodeFile &F) { |
| 89 | std::unique_ptr<IRObjectFile> Obj = |
| 90 | check(IRObjectFile::create(F.MB, Context)); |
| 91 | std::vector<GlobalValue *> Keep; |
| 92 | unsigned BodyIndex = 0; |
| 93 | ArrayRef<SymbolBody *> Bodies = F.getSymbols(); |
| 94 | |
Davide Italiano | 86f2bd5 | 2016-03-29 21:46:35 +0000 | [diff] [blame] | 95 | Module &M = Obj->getModule(); |
Davide Italiano | 493b683 | 2016-04-16 01:33:33 +0000 | [diff] [blame] | 96 | if (M.getDataLayoutStr().empty()) |
| 97 | fatal("invalid bitcode file: " + F.getName() + " has no datalayout"); |
Davide Italiano | 49fe4ed | 2016-03-29 23:57:22 +0000 | [diff] [blame] | 98 | |
| 99 | // If a symbol appears in @llvm.used, the linker is required |
| 100 | // to treat the symbol as there is a reference to the symbol |
| 101 | // that it cannot see. Therefore, we can't internalize. |
Davide Italiano | 86f2bd5 | 2016-03-29 21:46:35 +0000 | [diff] [blame] | 102 | SmallPtrSet<GlobalValue *, 8> Used; |
| 103 | collectUsedGlobalVariables(M, Used, /* CompilerUsed */ false); |
| 104 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 105 | for (const BasicSymbolRef &Sym : Obj->symbols()) { |
| 106 | GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl()); |
Peter Collingbourne | 7cf73ec | 2016-04-11 16:39:43 +0000 | [diff] [blame] | 107 | // Ignore module asm symbols. |
| 108 | if (!GV) |
| 109 | continue; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 110 | if (GV->hasAppendingLinkage()) { |
| 111 | Keep.push_back(GV); |
| 112 | continue; |
| 113 | } |
Davide Italiano | 1460e9f | 2016-03-26 18:33:09 +0000 | [diff] [blame] | 114 | if (BitcodeFile::shouldSkip(Sym)) |
| 115 | continue; |
| 116 | SymbolBody *B = Bodies[BodyIndex++]; |
| 117 | if (!B || &B->repl() != B || !isa<DefinedBitcode>(B)) |
| 118 | continue; |
| 119 | switch (GV->getLinkage()) { |
| 120 | default: |
| 121 | break; |
| 122 | case llvm::GlobalValue::LinkOnceAnyLinkage: |
| 123 | GV->setLinkage(GlobalValue::WeakAnyLinkage); |
| 124 | break; |
| 125 | case llvm::GlobalValue::LinkOnceODRLinkage: |
| 126 | GV->setLinkage(GlobalValue::WeakODRLinkage); |
| 127 | break; |
Davide Italiano | d4c2a03 | 2016-03-22 22:31:34 +0000 | [diff] [blame] | 128 | } |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 129 | |
| 130 | // We collect the set of symbols we want to internalize here |
| 131 | // and change the linkage after the IRMover executed, i.e. after |
| 132 | // we imported the symbols and satisfied undefined references |
| 133 | // to it. We can't just change linkage here because otherwise |
| 134 | // the IRMover will just rename the symbol. |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame^] | 135 | if (shouldInternalize(Used, *B, GV)) |
| 136 | InternalizedSyms.insert(GV->getName()); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 137 | |
Davide Italiano | 1460e9f | 2016-03-26 18:33:09 +0000 | [diff] [blame] | 138 | Keep.push_back(GV); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | Mover.move(Obj->takeModule(), Keep, |
| 142 | [](GlobalValue &, IRMover::ValueAdder) {}); |
| 143 | } |
| 144 | |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 145 | static void internalize(GlobalValue &GV) { |
| 146 | assert(!GV.hasLocalLinkage() && |
Davide Italiano | 47c33f0 | 2016-03-29 21:48:25 +0000 | [diff] [blame] | 147 | "Trying to internalize a symbol with local linkage!"); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 148 | GV.setLinkage(GlobalValue::InternalLinkage); |
| 149 | } |
| 150 | |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 151 | std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::runSplitCodegen( |
| 152 | const std::function<std::unique_ptr<TargetMachine>()> &TMFactory) { |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 153 | unsigned NumThreads = Config->LtoJobs; |
| 154 | OwningData.resize(NumThreads); |
| 155 | |
| 156 | std::list<raw_svector_ostream> OSs; |
| 157 | std::vector<raw_pwrite_stream *> OSPtrs; |
| 158 | for (SmallString<0> &Obj : OwningData) { |
| 159 | OSs.emplace_back(Obj); |
| 160 | OSPtrs.push_back(&OSs.back()); |
| 161 | } |
| 162 | |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 163 | splitCodeGen(std::move(Combined), OSPtrs, {}, TMFactory); |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 164 | |
| 165 | std::vector<std::unique_ptr<InputFile>> ObjFiles; |
| 166 | for (SmallString<0> &Obj : OwningData) |
| 167 | ObjFiles.push_back(createObjectFile( |
| 168 | MemoryBufferRef(Obj, "LLD-INTERNAL-combined-lto-object"))); |
| 169 | |
| 170 | if (Config->SaveTemps) |
| 171 | for (unsigned I = 0; I < NumThreads; ++I) |
| 172 | saveLtoObjectFile(OwningData[I], I, NumThreads > 1); |
| 173 | |
| 174 | return ObjFiles; |
| 175 | } |
| 176 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 177 | // Merge all the bitcode files we have seen, codegen the result |
| 178 | // and return the resulting ObjectFile. |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 179 | std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::compile() { |
| 180 | TheTriple = Combined->getTargetTriple(); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 181 | for (const auto &Name : InternalizedSyms) { |
Davide Italiano | 15c41b2 | 2016-04-11 22:39:51 +0000 | [diff] [blame] | 182 | GlobalValue *GV = Combined->getNamedValue(Name.first()); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 183 | assert(GV); |
| 184 | internalize(*GV); |
| 185 | } |
| 186 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 187 | if (Config->SaveTemps) |
Davide Italiano | 15c41b2 | 2016-04-11 22:39:51 +0000 | [diff] [blame] | 188 | saveBCFile(*Combined, ".lto.bc"); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 189 | |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 190 | std::string Msg; |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 191 | const Target *T = TargetRegistry::lookupTarget(TheTriple, Msg); |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 192 | if (!T) |
| 193 | fatal("target not found: " + Msg); |
Davide Italiano | 8eca282 | 2016-04-01 00:35:29 +0000 | [diff] [blame] | 194 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 195 | Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static; |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 196 | |
| 197 | auto CreateTargetMachine = [&]() { |
| 198 | return std::unique_ptr<TargetMachine>( |
| 199 | T->createTargetMachine(TheTriple, "", "", Options, R)); |
| 200 | }; |
| 201 | |
| 202 | std::unique_ptr<TargetMachine> TM = CreateTargetMachine(); |
| 203 | runLTOPasses(*Combined, *TM); |
| 204 | |
| 205 | return runSplitCodegen(CreateTargetMachine); |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 206 | } |