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