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