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