Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 1 | //===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements functions and classes used to support LTO. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/LTO/LTO.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 16 | #include "llvm/Analysis/TargetTransformInfo.h" |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/BitcodeReader.h" |
| 18 | #include "llvm/Bitcode/BitcodeWriter.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Analysis.h" |
Bob Haarman | dd4ebc1 | 2017-02-02 23:00:49 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 21 | #include "llvm/IR/AutoUpgrade.h" |
| 22 | #include "llvm/IR/DiagnosticPrinter.h" |
| 23 | #include "llvm/IR/LegacyPassManager.h" |
Bob Haarman | dd4ebc1 | 2017-02-02 23:00:49 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Mangler.h" |
| 25 | #include "llvm/IR/Metadata.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 26 | #include "llvm/LTO/LTOBackend.h" |
| 27 | #include "llvm/Linker/IRMover.h" |
| 28 | #include "llvm/Object/ModuleSummaryIndexObjectFile.h" |
Bob Haarman | dd4ebc1 | 2017-02-02 23:00:49 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Error.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ManagedStatic.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MemoryBuffer.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Path.h" |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 33 | #include "llvm/Support/SHA1.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/SourceMgr.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 35 | #include "llvm/Support/TargetRegistry.h" |
| 36 | #include "llvm/Support/ThreadPool.h" |
Teresa Johnson | ec544c5 | 2016-10-19 17:35:01 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Threading.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetMachine.h" |
| 40 | #include "llvm/Target/TargetOptions.h" |
| 41 | #include "llvm/Transforms/IPO.h" |
| 42 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 43 | #include "llvm/Transforms/Utils/SplitModule.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 44 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 45 | #include <set> |
| 46 | |
| 47 | using namespace llvm; |
| 48 | using namespace lto; |
| 49 | using namespace object; |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 50 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 51 | #define DEBUG_TYPE "lto" |
| 52 | |
| 53 | // Returns a unique hash for the Module considering the current list of |
| 54 | // export/import and other global analysis results. |
| 55 | // The hash is produced in \p Key. |
| 56 | static void computeCacheKey( |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 57 | SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index, |
| 58 | StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 59 | const FunctionImporter::ExportSetTy &ExportList, |
| 60 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
| 61 | const GVSummaryMapTy &DefinedGlobals) { |
| 62 | // Compute the unique hash for this entry. |
| 63 | // This is based on the current compiler version, the module itself, the |
| 64 | // export list, the hash for every single module in the import list, the |
| 65 | // list of ResolvedODR for the module, and the list of preserved symbols. |
| 66 | SHA1 Hasher; |
| 67 | |
| 68 | // Start with the compiler revision |
| 69 | Hasher.update(LLVM_VERSION_STRING); |
| 70 | #ifdef HAVE_LLVM_REVISION |
| 71 | Hasher.update(LLVM_REVISION); |
| 72 | #endif |
| 73 | |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 74 | // Include the parts of the LTO configuration that affect code generation. |
| 75 | auto AddString = [&](StringRef Str) { |
| 76 | Hasher.update(Str); |
| 77 | Hasher.update(ArrayRef<uint8_t>{0}); |
| 78 | }; |
| 79 | auto AddUnsigned = [&](unsigned I) { |
| 80 | uint8_t Data[4]; |
| 81 | Data[0] = I; |
| 82 | Data[1] = I >> 8; |
| 83 | Data[2] = I >> 16; |
| 84 | Data[3] = I >> 24; |
| 85 | Hasher.update(ArrayRef<uint8_t>{Data, 4}); |
| 86 | }; |
| 87 | AddString(Conf.CPU); |
| 88 | // FIXME: Hash more of Options. For now all clients initialize Options from |
| 89 | // command-line flags (which is unsupported in production), but may set |
| 90 | // RelaxELFRelocations. The clang driver can also pass FunctionSections, |
| 91 | // DataSections and DebuggerTuning via command line flags. |
| 92 | AddUnsigned(Conf.Options.RelaxELFRelocations); |
| 93 | AddUnsigned(Conf.Options.FunctionSections); |
| 94 | AddUnsigned(Conf.Options.DataSections); |
| 95 | AddUnsigned((unsigned)Conf.Options.DebuggerTuning); |
| 96 | for (auto &A : Conf.MAttrs) |
| 97 | AddString(A); |
| 98 | AddUnsigned(Conf.RelocModel); |
| 99 | AddUnsigned(Conf.CodeModel); |
| 100 | AddUnsigned(Conf.CGOptLevel); |
Tobias Edler von Koch | f454b9e | 2017-02-15 20:36:36 +0000 | [diff] [blame] | 101 | AddUnsigned(Conf.CGFileType); |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 102 | AddUnsigned(Conf.OptLevel); |
| 103 | AddString(Conf.OptPipeline); |
| 104 | AddString(Conf.AAPipeline); |
| 105 | AddString(Conf.OverrideTriple); |
| 106 | AddString(Conf.DefaultTriple); |
| 107 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 108 | // Include the hash for the current module |
| 109 | auto ModHash = Index.getModuleHash(ModuleID); |
| 110 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash))); |
| 111 | for (auto F : ExportList) |
| 112 | // The export list can impact the internalization, be conservative here |
| 113 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F))); |
| 114 | |
| 115 | // Include the hash for every module we import functions from |
| 116 | for (auto &Entry : ImportList) { |
| 117 | auto ModHash = Index.getModuleHash(Entry.first()); |
| 118 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash))); |
| 119 | } |
| 120 | |
| 121 | // Include the hash for the resolved ODR. |
| 122 | for (auto &Entry : ResolvedODR) { |
| 123 | Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first, |
| 124 | sizeof(GlobalValue::GUID))); |
| 125 | Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second, |
| 126 | sizeof(GlobalValue::LinkageTypes))); |
| 127 | } |
| 128 | |
| 129 | // Include the hash for the linkage type to reflect internalization and weak |
| 130 | // resolution. |
| 131 | for (auto &GS : DefinedGlobals) { |
| 132 | GlobalValue::LinkageTypes Linkage = GS.second->linkage(); |
| 133 | Hasher.update( |
| 134 | ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage))); |
| 135 | } |
| 136 | |
Dehao Chen | 2797800 | 2016-12-16 16:48:46 +0000 | [diff] [blame] | 137 | if (!Conf.SampleProfile.empty()) { |
| 138 | auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile); |
| 139 | if (FileOrErr) |
| 140 | Hasher.update(FileOrErr.get()->getBuffer()); |
| 141 | } |
| 142 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 143 | Key = toHex(Hasher.result()); |
| 144 | } |
| 145 | |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 146 | static void thinLTOResolveWeakForLinkerGUID( |
| 147 | GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID, |
| 148 | DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 149 | function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)> |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 150 | isPrevailing, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 151 | function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 152 | recordNewLinkage) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 153 | for (auto &S : GVSummaryList) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 154 | GlobalValue::LinkageTypes OriginalLinkage = S->linkage(); |
| 155 | if (!GlobalValue::isWeakForLinker(OriginalLinkage)) |
| 156 | continue; |
Peter Collingbourne | 73589f3 | 2016-07-07 18:31:51 +0000 | [diff] [blame] | 157 | // We need to emit only one of these. The prevailing module will keep it, |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 158 | // but turned into a weak, while the others will drop it when possible. |
Teresa Johnson | 3bc8abd | 2016-10-30 05:15:23 +0000 | [diff] [blame] | 159 | // This is both a compile-time optimization and a correctness |
| 160 | // transformation. This is necessary for correctness when we have exported |
| 161 | // a reference - we need to convert the linkonce to weak to |
| 162 | // ensure a copy is kept to satisfy the exported reference. |
| 163 | // FIXME: We may want to split the compile time and correctness |
| 164 | // aspects into separate routines. |
Peter Collingbourne | 73589f3 | 2016-07-07 18:31:51 +0000 | [diff] [blame] | 165 | if (isPrevailing(GUID, S.get())) { |
Teresa Johnson | 28c03b5 | 2016-05-26 14:16:52 +0000 | [diff] [blame] | 166 | if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) |
| 167 | S->setLinkage(GlobalValue::getWeakLinkage( |
| 168 | GlobalValue::isLinkOnceODRLinkage(OriginalLinkage))); |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 169 | } |
Teresa Johnson | 3bc8abd | 2016-10-30 05:15:23 +0000 | [diff] [blame] | 170 | // Alias and aliasee can't be turned into available_externally. |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 171 | else if (!isa<AliasSummary>(S.get()) && |
Teresa Johnson | 4566c6d | 2017-01-20 21:54:58 +0000 | [diff] [blame] | 172 | !GlobalInvolvedWithAlias.count(S.get())) |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 173 | S->setLinkage(GlobalValue::AvailableExternallyLinkage); |
| 174 | if (S->linkage() != OriginalLinkage) |
| 175 | recordNewLinkage(S->modulePath(), GUID, S->linkage()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Resolve Weak and LinkOnce values in the \p Index. |
| 180 | // |
| 181 | // We'd like to drop these functions if they are no longer referenced in the |
| 182 | // current module. However there is a chance that another module is still |
| 183 | // referencing them because of the import. We make sure we always emit at least |
| 184 | // one copy. |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 185 | void llvm::thinLTOResolveWeakForLinkerInIndex( |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 186 | ModuleSummaryIndex &Index, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 187 | function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)> |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 188 | isPrevailing, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 189 | function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 190 | recordNewLinkage) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 191 | // We won't optimize the globals that are referenced by an alias for now |
| 192 | // Ideally we should turn the alias into a global and duplicate the definition |
| 193 | // when needed. |
| 194 | DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias; |
| 195 | for (auto &I : Index) |
| 196 | for (auto &S : I.second) |
| 197 | if (auto AS = dyn_cast<AliasSummary>(S.get())) |
| 198 | GlobalInvolvedWithAlias.insert(&AS->getAliasee()); |
| 199 | |
| 200 | for (auto &I : Index) |
| 201 | thinLTOResolveWeakForLinkerGUID(I.second, I.first, GlobalInvolvedWithAlias, |
Peter Collingbourne | 73589f3 | 2016-07-07 18:31:51 +0000 | [diff] [blame] | 202 | isPrevailing, recordNewLinkage); |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void thinLTOInternalizeAndPromoteGUID( |
| 206 | GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID, |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 207 | function_ref<bool(StringRef, GlobalValue::GUID)> isExported) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 208 | for (auto &S : GVSummaryList) { |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 209 | if (isExported(S->modulePath(), GUID)) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 210 | if (GlobalValue::isLocalLinkage(S->linkage())) |
| 211 | S->setLinkage(GlobalValue::ExternalLinkage); |
| 212 | } else if (!GlobalValue::isLocalLinkage(S->linkage())) |
| 213 | S->setLinkage(GlobalValue::InternalLinkage); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Update the linkages in the given \p Index to mark exported values |
| 218 | // as external and non-exported values as internal. |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 219 | void llvm::thinLTOInternalizeAndPromoteInIndex( |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 220 | ModuleSummaryIndex &Index, |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 221 | function_ref<bool(StringRef, GlobalValue::GUID)> isExported) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 222 | for (auto &I : Index) |
| 223 | thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported); |
| 224 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 225 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 226 | struct InputFile::InputModule { |
| 227 | BitcodeModule BM; |
| 228 | std::unique_ptr<Module> Mod; |
| 229 | |
| 230 | // The range of ModuleSymbolTable entries for this input module. |
| 231 | size_t SymBegin, SymEnd; |
| 232 | }; |
| 233 | |
| 234 | // Requires a destructor for std::vector<InputModule>. |
| 235 | InputFile::~InputFile() = default; |
| 236 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 237 | Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) { |
| 238 | std::unique_ptr<InputFile> File(new InputFile); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 239 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 240 | ErrorOr<MemoryBufferRef> BCOrErr = |
| 241 | IRObjectFile::findBitcodeInMemBuffer(Object); |
| 242 | if (!BCOrErr) |
| 243 | return errorCodeToError(BCOrErr.getError()); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 244 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 245 | Expected<std::vector<BitcodeModule>> BMsOrErr = |
| 246 | getBitcodeModuleList(*BCOrErr); |
| 247 | if (!BMsOrErr) |
| 248 | return BMsOrErr.takeError(); |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 249 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 250 | if (BMsOrErr->empty()) |
| 251 | return make_error<StringError>("Bitcode file does not contain any modules", |
| 252 | inconvertibleErrorCode()); |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 253 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 254 | // Create an InputModule for each module in the InputFile, and add it to the |
| 255 | // ModuleSymbolTable. |
| 256 | for (auto BM : *BMsOrErr) { |
| 257 | Expected<std::unique_ptr<Module>> MOrErr = |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 258 | BM.getLazyModule(File->Ctx, /*ShouldLazyLoadMetadata*/ true, |
| 259 | /*IsImporting*/ false); |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 260 | if (!MOrErr) |
| 261 | return MOrErr.takeError(); |
| 262 | |
| 263 | size_t SymBegin = File->SymTab.symbols().size(); |
| 264 | File->SymTab.addModule(MOrErr->get()); |
| 265 | size_t SymEnd = File->SymTab.symbols().size(); |
| 266 | |
| 267 | for (const auto &C : (*MOrErr)->getComdatSymbolTable()) { |
| 268 | auto P = File->ComdatMap.insert( |
| 269 | std::make_pair(&C.second, File->Comdats.size())); |
| 270 | assert(P.second); |
| 271 | (void)P; |
| 272 | File->Comdats.push_back(C.first()); |
| 273 | } |
| 274 | |
| 275 | File->Mods.push_back({BM, std::move(*MOrErr), SymBegin, SymEnd}); |
Rafael Espindola | 7912110 | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 278 | return std::move(File); |
| 279 | } |
| 280 | |
Rafael Espindola | 7912110 | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 281 | Expected<int> InputFile::Symbol::getComdatIndex() const { |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 282 | if (!isGV()) |
Rafael Espindola | 7912110 | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 283 | return -1; |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 284 | const GlobalObject *GO = getGV()->getBaseObject(); |
| 285 | if (!GO) |
| 286 | return make_error<StringError>("Unable to determine comdat of alias!", |
| 287 | inconvertibleErrorCode()); |
Rafael Espindola | 7912110 | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 288 | if (const Comdat *C = GO->getComdat()) { |
| 289 | auto I = File->ComdatMap.find(C); |
| 290 | assert(I != File->ComdatMap.end()); |
| 291 | return I->second; |
| 292 | } |
| 293 | return -1; |
| 294 | } |
| 295 | |
Bob Haarman | dd4ebc1 | 2017-02-02 23:00:49 +0000 | [diff] [blame] | 296 | Expected<std::string> InputFile::getLinkerOpts() { |
| 297 | std::string LinkerOpts; |
| 298 | raw_string_ostream LOS(LinkerOpts); |
| 299 | // Extract linker options from module metadata. |
| 300 | for (InputModule &Mod : Mods) { |
| 301 | std::unique_ptr<Module> &M = Mod.Mod; |
| 302 | if (auto E = M->materializeMetadata()) |
| 303 | return std::move(E); |
| 304 | if (Metadata *Val = M->getModuleFlag("Linker Options")) { |
| 305 | MDNode *LinkerOptions = cast<MDNode>(Val); |
| 306 | for (const MDOperand &MDOptions : LinkerOptions->operands()) |
| 307 | for (const MDOperand &MDOption : cast<MDNode>(MDOptions)->operands()) |
| 308 | LOS << " " << cast<MDString>(MDOption)->getString(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Synthesize export flags for symbols with dllexport storage. |
| 313 | const Triple TT(Mods[0].Mod->getTargetTriple()); |
| 314 | Mangler M; |
| 315 | for (const ModuleSymbolTable::Symbol &Sym : SymTab.symbols()) |
| 316 | if (auto *GV = Sym.dyn_cast<GlobalValue*>()) |
| 317 | emitLinkerFlagsForGlobalCOFF(LOS, GV, TT, M); |
| 318 | LOS.flush(); |
| 319 | return LinkerOpts; |
| 320 | } |
| 321 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 322 | StringRef InputFile::getName() const { |
| 323 | return Mods[0].BM.getModuleIdentifier(); |
| 324 | } |
| 325 | |
| 326 | StringRef InputFile::getSourceFileName() const { |
| 327 | return Mods[0].Mod->getSourceFileName(); |
| 328 | } |
| 329 | |
| 330 | iterator_range<InputFile::symbol_iterator> |
| 331 | InputFile::module_symbols(InputModule &IM) { |
| 332 | return llvm::make_range( |
| 333 | symbol_iterator(SymTab.symbols().data() + IM.SymBegin, SymTab, this), |
| 334 | symbol_iterator(SymTab.symbols().data() + IM.SymEnd, SymTab, this)); |
| 335 | } |
| 336 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 337 | LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel, |
| 338 | Config &Conf) |
| 339 | : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel), |
Mehdi Amini | e749453 | 2016-08-23 18:39:12 +0000 | [diff] [blame] | 340 | Ctx(Conf) {} |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 341 | |
| 342 | LTO::ThinLTOState::ThinLTOState(ThinBackend Backend) : Backend(Backend) { |
| 343 | if (!Backend) |
Teresa Johnson | ec544c5 | 2016-10-19 17:35:01 +0000 | [diff] [blame] | 344 | this->Backend = |
| 345 | createInProcessThinBackend(llvm::heavyweight_hardware_concurrency()); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | LTO::LTO(Config Conf, ThinBackend Backend, |
| 349 | unsigned ParallelCodeGenParallelismLevel) |
| 350 | : Conf(std::move(Conf)), |
| 351 | RegularLTO(ParallelCodeGenParallelismLevel, this->Conf), |
Mehdi Amini | 026ddbb | 2016-08-19 05:56:37 +0000 | [diff] [blame] | 352 | ThinLTO(std::move(Backend)) {} |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 353 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 354 | // Requires a destructor for MapVector<BitcodeModule>. |
| 355 | LTO::~LTO() = default; |
| 356 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 357 | // Add the given symbol to the GlobalResolutions map, and resolve its partition. |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 358 | void LTO::addSymbolToGlobalRes(SmallPtrSet<GlobalValue *, 8> &Used, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 359 | const InputFile::Symbol &Sym, |
Teresa Johnson | faa7506 | 2016-08-11 20:38:39 +0000 | [diff] [blame] | 360 | SymbolResolution Res, unsigned Partition) { |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 361 | GlobalValue *GV = Sym.isGV() ? Sym.getGV() : nullptr; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 362 | |
| 363 | auto &GlobalRes = GlobalResolutions[Sym.getName()]; |
| 364 | if (GV) { |
| 365 | GlobalRes.UnnamedAddr &= GV->hasGlobalUnnamedAddr(); |
| 366 | if (Res.Prevailing) |
| 367 | GlobalRes.IRName = GV->getName(); |
| 368 | } |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 369 | // Set the partition to external if we know it is used elsewhere, e.g. |
| 370 | // it is visible to a regular object, is referenced from llvm.compiler_used, |
| 371 | // or was already recorded as being referenced from a different partition. |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 372 | if (Res.VisibleToRegularObj || (GV && Used.count(GV)) || |
| 373 | (GlobalRes.Partition != GlobalResolution::Unknown && |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 374 | GlobalRes.Partition != Partition)) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 375 | GlobalRes.Partition = GlobalResolution::External; |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 376 | } else |
| 377 | // First recorded reference, save the current partition. |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 378 | GlobalRes.Partition = Partition; |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 379 | |
| 380 | // Flag as visible outside of ThinLTO if visible from a regular object or |
| 381 | // if this is a reference in the regular LTO partition. |
| 382 | GlobalRes.VisibleOutsideThinLTO |= |
| 383 | (Res.VisibleToRegularObj || (Partition == GlobalResolution::RegularLTO)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 386 | static void writeToResolutionFile(raw_ostream &OS, InputFile *Input, |
| 387 | ArrayRef<SymbolResolution> Res) { |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 388 | StringRef Path = Input->getName(); |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 389 | OS << Path << '\n'; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 390 | auto ResI = Res.begin(); |
| 391 | for (const InputFile::Symbol &Sym : Input->symbols()) { |
| 392 | assert(ResI != Res.end()); |
| 393 | SymbolResolution Res = *ResI++; |
| 394 | |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 395 | OS << "-r=" << Path << ',' << Sym.getName() << ','; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 396 | if (Res.Prevailing) |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 397 | OS << 'p'; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 398 | if (Res.FinalDefinitionInLinkageUnit) |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 399 | OS << 'l'; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 400 | if (Res.VisibleToRegularObj) |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 401 | OS << 'x'; |
| 402 | OS << '\n'; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 403 | } |
Peter Collingbourne | 58ffcfb | 2017-01-19 23:10:14 +0000 | [diff] [blame] | 404 | OS.flush(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 405 | assert(ResI == Res.end()); |
| 406 | } |
| 407 | |
| 408 | Error LTO::add(std::unique_ptr<InputFile> Input, |
| 409 | ArrayRef<SymbolResolution> Res) { |
| 410 | assert(!CalledGetMaxTasks); |
| 411 | |
| 412 | if (Conf.ResolutionFile) |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 413 | writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 414 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 415 | const SymbolResolution *ResI = Res.begin(); |
| 416 | for (InputFile::InputModule &IM : Input->Mods) |
| 417 | if (Error Err = addModule(*Input, IM, ResI, Res.end())) |
| 418 | return Err; |
| 419 | |
| 420 | assert(ResI == Res.end()); |
| 421 | return Error::success(); |
| 422 | } |
| 423 | |
| 424 | Error LTO::addModule(InputFile &Input, InputFile::InputModule &IM, |
| 425 | const SymbolResolution *&ResI, |
| 426 | const SymbolResolution *ResE) { |
Mehdi Amini | 9989f80 | 2016-08-19 15:35:44 +0000 | [diff] [blame] | 427 | // FIXME: move to backend |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 428 | Module &M = *IM.Mod; |
Davide Italiano | 2ceb628 | 2016-12-14 21:57:04 +0000 | [diff] [blame] | 429 | |
| 430 | if (M.getDataLayoutStr().empty()) |
| 431 | return make_error<StringError>("input module has no datalayout", |
| 432 | inconvertibleErrorCode()); |
| 433 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 434 | if (!Conf.OverrideTriple.empty()) |
| 435 | M.setTargetTriple(Conf.OverrideTriple); |
| 436 | else if (M.getTargetTriple().empty()) |
| 437 | M.setTargetTriple(Conf.DefaultTriple); |
| 438 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 439 | Expected<bool> HasThinLTOSummary = IM.BM.hasSummary(); |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 440 | if (!HasThinLTOSummary) |
| 441 | return HasThinLTOSummary.takeError(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 442 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 443 | if (*HasThinLTOSummary) |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 444 | return addThinLTO(IM.BM, M, Input.module_symbols(IM), ResI, ResE); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 445 | else |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 446 | return addRegularLTO(IM.BM, ResI, ResE); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // Add a regular LTO object to the link. |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 450 | Error LTO::addRegularLTO(BitcodeModule BM, const SymbolResolution *&ResI, |
| 451 | const SymbolResolution *ResE) { |
Mehdi Amini | e749453 | 2016-08-23 18:39:12 +0000 | [diff] [blame] | 452 | if (!RegularLTO.CombinedModule) { |
| 453 | RegularLTO.CombinedModule = |
| 454 | llvm::make_unique<Module>("ld-temp.o", RegularLTO.Ctx); |
| 455 | RegularLTO.Mover = llvm::make_unique<IRMover>(*RegularLTO.CombinedModule); |
| 456 | } |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 457 | Expected<std::unique_ptr<Module>> MOrErr = |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 458 | BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true, |
| 459 | /*IsImporting*/ false); |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 460 | if (!MOrErr) |
| 461 | return MOrErr.takeError(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 462 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 463 | Module &M = **MOrErr; |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 464 | if (Error Err = M.materializeMetadata()) |
| 465 | return Err; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 466 | UpgradeDebugInfo(M); |
| 467 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 468 | ModuleSymbolTable SymTab; |
| 469 | SymTab.addModule(&M); |
| 470 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 471 | SmallPtrSet<GlobalValue *, 8> Used; |
| 472 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); |
| 473 | |
| 474 | std::vector<GlobalValue *> Keep; |
| 475 | |
| 476 | for (GlobalVariable &GV : M.globals()) |
| 477 | if (GV.hasAppendingLinkage()) |
| 478 | Keep.push_back(&GV); |
| 479 | |
Peter Collingbourne | 4613626 | 2017-02-02 05:22:42 +0000 | [diff] [blame] | 480 | DenseSet<GlobalObject *> AliasedGlobals; |
| 481 | for (auto &GA : M.aliases()) |
| 482 | if (GlobalObject *GO = GA.getBaseObject()) |
| 483 | AliasedGlobals.insert(GO); |
| 484 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 485 | for (const InputFile::Symbol &Sym : |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 486 | make_range(InputFile::symbol_iterator(SymTab.symbols().begin(), SymTab, |
| 487 | nullptr), |
| 488 | InputFile::symbol_iterator(SymTab.symbols().end(), SymTab, |
| 489 | nullptr))) { |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 490 | assert(ResI != ResE); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 491 | SymbolResolution Res = *ResI++; |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 492 | addSymbolToGlobalRes(Used, Sym, Res, 0); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 493 | |
Peter Collingbourne | c387e70 | 2017-02-02 05:12:15 +0000 | [diff] [blame] | 494 | if (Sym.isGV()) { |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 495 | GlobalValue *GV = Sym.getGV(); |
Peter Collingbourne | c387e70 | 2017-02-02 05:12:15 +0000 | [diff] [blame] | 496 | if (Res.Prevailing) { |
| 497 | if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) |
| 498 | continue; |
| 499 | Keep.push_back(GV); |
| 500 | switch (GV->getLinkage()) { |
| 501 | default: |
| 502 | break; |
| 503 | case GlobalValue::LinkOnceAnyLinkage: |
| 504 | GV->setLinkage(GlobalValue::WeakAnyLinkage); |
| 505 | break; |
| 506 | case GlobalValue::LinkOnceODRLinkage: |
| 507 | GV->setLinkage(GlobalValue::WeakODRLinkage); |
| 508 | break; |
| 509 | } |
Peter Collingbourne | 4613626 | 2017-02-02 05:22:42 +0000 | [diff] [blame] | 510 | } else if (isa<GlobalObject>(GV) && |
| 511 | (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() || |
| 512 | GV->hasAvailableExternallyLinkage()) && |
| 513 | !AliasedGlobals.count(cast<GlobalObject>(GV))) { |
| 514 | // Either of the above three types of linkage indicates that the |
| 515 | // chosen prevailing symbol will have the same semantics as this copy of |
| 516 | // the symbol, so we can link it with available_externally linkage. We |
| 517 | // only need to do this if the symbol is undefined. |
Peter Collingbourne | c387e70 | 2017-02-02 05:12:15 +0000 | [diff] [blame] | 518 | GlobalValue *CombinedGV = |
| 519 | RegularLTO.CombinedModule->getNamedValue(GV->getName()); |
Peter Collingbourne | 4613626 | 2017-02-02 05:22:42 +0000 | [diff] [blame] | 520 | if (!CombinedGV || CombinedGV->isDeclaration()) { |
Peter Collingbourne | c387e70 | 2017-02-02 05:12:15 +0000 | [diff] [blame] | 521 | Keep.push_back(GV); |
Peter Collingbourne | 4613626 | 2017-02-02 05:22:42 +0000 | [diff] [blame] | 522 | GV->setLinkage(GlobalValue::AvailableExternallyLinkage); |
| 523 | cast<GlobalObject>(GV)->setComdat(nullptr); |
| 524 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
Mehdi Amini | b2f46d1d | 2016-09-14 21:05:04 +0000 | [diff] [blame] | 527 | // Common resolution: collect the maximum size/alignment over all commons. |
| 528 | // We also record if we see an instance of a common as prevailing, so that |
| 529 | // if none is prevailing we can ignore it later. |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 530 | if (Sym.getFlags() & object::BasicSymbolRef::SF_Common) { |
Peter Collingbourne | fb8c2a4 | 2016-12-01 02:51:12 +0000 | [diff] [blame] | 531 | // FIXME: We should figure out what to do about commons defined by asm. |
| 532 | // For now they aren't reported correctly by ModuleSymbolTable. |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 533 | auto &CommonRes = RegularLTO.Commons[Sym.getGV()->getName()]; |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 534 | CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize()); |
| 535 | CommonRes.Align = std::max(CommonRes.Align, Sym.getCommonAlignment()); |
Mehdi Amini | b2f46d1d | 2016-09-14 21:05:04 +0000 | [diff] [blame] | 536 | CommonRes.Prevailing |= Res.Prevailing; |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 537 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 538 | |
| 539 | // FIXME: use proposed local attribute for FinalDefinitionInLinkageUnit. |
| 540 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 541 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 542 | return RegularLTO.Mover->move(std::move(*MOrErr), Keep, |
Teresa Johnson | 4b9b379 | 2016-10-12 18:39:29 +0000 | [diff] [blame] | 543 | [](GlobalValue &, IRMover::ValueAdder) {}, |
Teresa Johnson | 040cc16 | 2016-12-12 16:09:30 +0000 | [diff] [blame] | 544 | /* IsPerformingImport */ false); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // Add a ThinLTO object to the link. |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 548 | // FIXME: This function should not need to take as many parameters once we have |
| 549 | // a bitcode symbol table. |
| 550 | Error LTO::addThinLTO(BitcodeModule BM, Module &M, |
| 551 | iterator_range<InputFile::symbol_iterator> Syms, |
| 552 | const SymbolResolution *&ResI, |
| 553 | const SymbolResolution *ResE) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 554 | SmallPtrSet<GlobalValue *, 8> Used; |
| 555 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); |
| 556 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 557 | Expected<std::unique_ptr<ModuleSummaryIndex>> SummaryOrErr = BM.getSummary(); |
| 558 | if (!SummaryOrErr) |
| 559 | return SummaryOrErr.takeError(); |
| 560 | ThinLTO.CombinedIndex.mergeFrom(std::move(*SummaryOrErr), |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 561 | ThinLTO.ModuleMap.size()); |
| 562 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 563 | for (const InputFile::Symbol &Sym : Syms) { |
| 564 | assert(ResI != ResE); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 565 | SymbolResolution Res = *ResI++; |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 566 | addSymbolToGlobalRes(Used, Sym, Res, ThinLTO.ModuleMap.size() + 1); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 567 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 568 | if (Res.Prevailing && Sym.isGV()) |
| 569 | ThinLTO.PrevailingModuleForGUID[Sym.getGV()->getGUID()] = |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 570 | BM.getModuleIdentifier(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 571 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 572 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 573 | if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second) |
| 574 | return make_error<StringError>( |
| 575 | "Expected at most one ThinLTO module per bitcode file", |
| 576 | inconvertibleErrorCode()); |
| 577 | |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 578 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Teresa Johnson | faa7506 | 2016-08-11 20:38:39 +0000 | [diff] [blame] | 581 | unsigned LTO::getMaxTasks() const { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 582 | CalledGetMaxTasks = true; |
| 583 | return RegularLTO.ParallelCodeGenParallelismLevel + ThinLTO.ModuleMap.size(); |
| 584 | } |
| 585 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 586 | Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) { |
Teresa Johnson | 8dd61ae | 2016-09-16 13:54:19 +0000 | [diff] [blame] | 587 | // Save the status of having a regularLTO combined module, as |
| 588 | // this is needed for generating the ThinLTO Task ID, and |
| 589 | // the CombinedModule will be moved at the end of runRegularLTO. |
| 590 | bool HasRegularLTO = RegularLTO.CombinedModule != nullptr; |
Mehdi Amini | e749453 | 2016-08-23 18:39:12 +0000 | [diff] [blame] | 591 | // Invoke regular LTO if there was a regular LTO module to start with. |
Teresa Johnson | 8dd61ae | 2016-09-16 13:54:19 +0000 | [diff] [blame] | 592 | if (HasRegularLTO) |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 593 | if (auto E = runRegularLTO(AddStream)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 594 | return E; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 595 | return runThinLTO(AddStream, Cache, HasRegularLTO); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 598 | Error LTO::runRegularLTO(AddStreamFn AddStream) { |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 599 | // Make sure commons have the right size/alignment: we kept the largest from |
| 600 | // all the prevailing when adding the inputs, and we apply it here. |
Teresa Johnson | e2e621a | 2016-08-27 04:41:22 +0000 | [diff] [blame] | 601 | const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout(); |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 602 | for (auto &I : RegularLTO.Commons) { |
Mehdi Amini | b2f46d1d | 2016-09-14 21:05:04 +0000 | [diff] [blame] | 603 | if (!I.second.Prevailing) |
| 604 | // Don't do anything if no instance of this common was prevailing. |
| 605 | continue; |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 606 | GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first); |
Teresa Johnson | e2e621a | 2016-08-27 04:41:22 +0000 | [diff] [blame] | 607 | if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) { |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 608 | // Don't create a new global if the type is already correct, just make |
| 609 | // sure the alignment is correct. |
| 610 | OldGV->setAlignment(I.second.Align); |
| 611 | continue; |
| 612 | } |
Teresa Johnson | e2e621a | 2016-08-27 04:41:22 +0000 | [diff] [blame] | 613 | ArrayType *Ty = |
| 614 | ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size); |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 615 | auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false, |
| 616 | GlobalValue::CommonLinkage, |
| 617 | ConstantAggregateZero::get(Ty), ""); |
| 618 | GV->setAlignment(I.second.Align); |
| 619 | if (OldGV) { |
| 620 | OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType())); |
| 621 | GV->takeName(OldGV); |
| 622 | OldGV->eraseFromParent(); |
| 623 | } else { |
| 624 | GV->setName(I.first); |
| 625 | } |
| 626 | } |
| 627 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 628 | if (Conf.PreOptModuleHook && |
| 629 | !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 630 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 631 | |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 632 | if (!Conf.CodeGenOnly) { |
| 633 | for (const auto &R : GlobalResolutions) { |
| 634 | if (R.second.IRName.empty()) |
| 635 | continue; |
| 636 | if (R.second.Partition != 0 && |
| 637 | R.second.Partition != GlobalResolution::External) |
| 638 | continue; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 639 | |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 640 | GlobalValue *GV = |
| 641 | RegularLTO.CombinedModule->getNamedValue(R.second.IRName); |
| 642 | // Ignore symbols defined in other partitions. |
| 643 | if (!GV || GV->hasLocalLinkage()) |
| 644 | continue; |
| 645 | GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global |
| 646 | : GlobalValue::UnnamedAddr::None); |
| 647 | if (R.second.Partition == 0) |
| 648 | GV->setLinkage(GlobalValue::InternalLinkage); |
| 649 | } |
| 650 | |
| 651 | if (Conf.PostInternalizeModuleHook && |
| 652 | !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 653 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 654 | } |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 655 | return backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel, |
Peter Collingbourne | e02b74e | 2017-01-20 22:18:52 +0000 | [diff] [blame] | 656 | std::move(RegularLTO.CombinedModule), ThinLTO.CombinedIndex); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /// This class defines the interface to the ThinLTO backend. |
| 660 | class lto::ThinBackendProc { |
| 661 | protected: |
| 662 | Config &Conf; |
| 663 | ModuleSummaryIndex &CombinedIndex; |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 664 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 665 | |
| 666 | public: |
| 667 | ThinBackendProc(Config &Conf, ModuleSummaryIndex &CombinedIndex, |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 668 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) |
Mehdi Amini | 18b9111 | 2016-08-19 06:10:03 +0000 | [diff] [blame] | 669 | : Conf(Conf), CombinedIndex(CombinedIndex), |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 670 | ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {} |
| 671 | |
| 672 | virtual ~ThinBackendProc() {} |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 673 | virtual Error start( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 674 | unsigned Task, BitcodeModule BM, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 675 | const FunctionImporter::ImportMapTy &ImportList, |
| 676 | const FunctionImporter::ExportSetTy &ExportList, |
| 677 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 678 | MapVector<StringRef, BitcodeModule> &ModuleMap) = 0; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 679 | virtual Error wait() = 0; |
| 680 | }; |
| 681 | |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 682 | namespace { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 683 | class InProcessThinBackend : public ThinBackendProc { |
| 684 | ThreadPool BackendThreadPool; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 685 | AddStreamFn AddStream; |
| 686 | NativeObjectCache Cache; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 687 | |
| 688 | Optional<Error> Err; |
| 689 | std::mutex ErrMu; |
| 690 | |
| 691 | public: |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 692 | InProcessThinBackend( |
| 693 | Config &Conf, ModuleSummaryIndex &CombinedIndex, |
| 694 | unsigned ThinLTOParallelismLevel, |
| 695 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 696 | AddStreamFn AddStream, NativeObjectCache Cache) |
Mehdi Amini | 18b9111 | 2016-08-19 06:10:03 +0000 | [diff] [blame] | 697 | : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries), |
| 698 | BackendThreadPool(ThinLTOParallelismLevel), |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 699 | AddStream(std::move(AddStream)), Cache(std::move(Cache)) {} |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 700 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 701 | Error runThinLTOBackendThread( |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 702 | AddStreamFn AddStream, NativeObjectCache Cache, unsigned Task, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 703 | BitcodeModule BM, ModuleSummaryIndex &CombinedIndex, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 704 | const FunctionImporter::ImportMapTy &ImportList, |
| 705 | const FunctionImporter::ExportSetTy &ExportList, |
| 706 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
| 707 | const GVSummaryMapTy &DefinedGlobals, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 708 | MapVector<StringRef, BitcodeModule> &ModuleMap) { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 709 | auto RunThinBackend = [&](AddStreamFn AddStream) { |
| 710 | LTOLLVMContext BackendContext(Conf); |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 711 | Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext); |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 712 | if (!MOrErr) |
| 713 | return MOrErr.takeError(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 714 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 715 | return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex, |
| 716 | ImportList, DefinedGlobals, ModuleMap); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 717 | }; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 718 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 719 | auto ModuleID = BM.getModuleIdentifier(); |
Mehdi Amini | f82bda0 | 2016-10-08 04:44:23 +0000 | [diff] [blame] | 720 | |
| 721 | if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) || |
| 722 | all_of(CombinedIndex.getModuleHash(ModuleID), |
| 723 | [](uint32_t V) { return V == 0; })) |
| 724 | // Cache disabled or no entry for this module in the combined index or |
| 725 | // no module hash. |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 726 | return RunThinBackend(AddStream); |
| 727 | |
| 728 | SmallString<40> Key; |
| 729 | // The module may be cached, this helps handling it. |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 730 | computeCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList, ExportList, |
Mehdi Amini | 00fa140 | 2016-10-08 04:44:18 +0000 | [diff] [blame] | 731 | ResolvedODR, DefinedGlobals); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 732 | if (AddStreamFn CacheAddStream = Cache(Task, Key)) |
| 733 | return RunThinBackend(CacheAddStream); |
| 734 | |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 735 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 738 | Error start( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 739 | unsigned Task, BitcodeModule BM, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 740 | const FunctionImporter::ImportMapTy &ImportList, |
| 741 | const FunctionImporter::ExportSetTy &ExportList, |
| 742 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 743 | MapVector<StringRef, BitcodeModule> &ModuleMap) override { |
| 744 | StringRef ModulePath = BM.getModuleIdentifier(); |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 745 | assert(ModuleToDefinedGVSummaries.count(ModulePath)); |
| 746 | const GVSummaryMapTy &DefinedGlobals = |
| 747 | ModuleToDefinedGVSummaries.find(ModulePath)->second; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 748 | BackendThreadPool.async( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 749 | [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 750 | const FunctionImporter::ImportMapTy &ImportList, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 751 | const FunctionImporter::ExportSetTy &ExportList, |
| 752 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> |
| 753 | &ResolvedODR, |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 754 | const GVSummaryMapTy &DefinedGlobals, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 755 | MapVector<StringRef, BitcodeModule> &ModuleMap) { |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 756 | Error E = runThinLTOBackendThread( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 757 | AddStream, Cache, Task, BM, CombinedIndex, ImportList, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 758 | ExportList, ResolvedODR, DefinedGlobals, ModuleMap); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 759 | if (E) { |
| 760 | std::unique_lock<std::mutex> L(ErrMu); |
| 761 | if (Err) |
| 762 | Err = joinErrors(std::move(*Err), std::move(E)); |
| 763 | else |
| 764 | Err = std::move(E); |
| 765 | } |
| 766 | }, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 767 | BM, std::ref(CombinedIndex), std::ref(ImportList), |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 768 | std::ref(ExportList), std::ref(ResolvedODR), std::ref(DefinedGlobals), |
| 769 | std::ref(ModuleMap)); |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 770 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | Error wait() override { |
| 774 | BackendThreadPool.wait(); |
| 775 | if (Err) |
| 776 | return std::move(*Err); |
| 777 | else |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 778 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 779 | } |
| 780 | }; |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 781 | } // end anonymous namespace |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 782 | |
| 783 | ThinBackend lto::createInProcessThinBackend(unsigned ParallelismLevel) { |
| 784 | return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex, |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 785 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 786 | AddStreamFn AddStream, NativeObjectCache Cache) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 787 | return llvm::make_unique<InProcessThinBackend>( |
| 788 | Conf, CombinedIndex, ParallelismLevel, ModuleToDefinedGVSummaries, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 789 | AddStream, Cache); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 790 | }; |
| 791 | } |
| 792 | |
Teresa Johnson | 3f212b8 | 2016-09-21 19:12:05 +0000 | [diff] [blame] | 793 | // Given the original \p Path to an output file, replace any path |
| 794 | // prefix matching \p OldPrefix with \p NewPrefix. Also, create the |
| 795 | // resulting directory if it does not yet exist. |
| 796 | std::string lto::getThinLTOOutputFile(const std::string &Path, |
| 797 | const std::string &OldPrefix, |
| 798 | const std::string &NewPrefix) { |
| 799 | if (OldPrefix.empty() && NewPrefix.empty()) |
| 800 | return Path; |
| 801 | SmallString<128> NewPath(Path); |
| 802 | llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix); |
| 803 | StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str()); |
| 804 | if (!ParentPath.empty()) { |
| 805 | // Make sure the new directory exists, creating it if necessary. |
| 806 | if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath)) |
| 807 | llvm::errs() << "warning: could not create directory '" << ParentPath |
| 808 | << "': " << EC.message() << '\n'; |
| 809 | } |
| 810 | return NewPath.str(); |
| 811 | } |
| 812 | |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 813 | namespace { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 814 | class WriteIndexesThinBackend : public ThinBackendProc { |
| 815 | std::string OldPrefix, NewPrefix; |
| 816 | bool ShouldEmitImportsFiles; |
| 817 | |
| 818 | std::string LinkedObjectsFileName; |
| 819 | std::unique_ptr<llvm::raw_fd_ostream> LinkedObjectsFile; |
| 820 | |
| 821 | public: |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 822 | WriteIndexesThinBackend( |
| 823 | Config &Conf, ModuleSummaryIndex &CombinedIndex, |
| 824 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
| 825 | std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles, |
| 826 | std::string LinkedObjectsFileName) |
Mehdi Amini | 18b9111 | 2016-08-19 06:10:03 +0000 | [diff] [blame] | 827 | : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries), |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 828 | OldPrefix(OldPrefix), NewPrefix(NewPrefix), |
| 829 | ShouldEmitImportsFiles(ShouldEmitImportsFiles), |
| 830 | LinkedObjectsFileName(LinkedObjectsFileName) {} |
| 831 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 832 | Error start( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 833 | unsigned Task, BitcodeModule BM, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 834 | const FunctionImporter::ImportMapTy &ImportList, |
| 835 | const FunctionImporter::ExportSetTy &ExportList, |
| 836 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 837 | MapVector<StringRef, BitcodeModule> &ModuleMap) override { |
| 838 | StringRef ModulePath = BM.getModuleIdentifier(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 839 | std::string NewModulePath = |
| 840 | getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix); |
| 841 | |
| 842 | std::error_code EC; |
| 843 | if (!LinkedObjectsFileName.empty()) { |
| 844 | if (!LinkedObjectsFile) { |
| 845 | LinkedObjectsFile = llvm::make_unique<raw_fd_ostream>( |
| 846 | LinkedObjectsFileName, EC, sys::fs::OpenFlags::F_None); |
| 847 | if (EC) |
| 848 | return errorCodeToError(EC); |
| 849 | } |
| 850 | *LinkedObjectsFile << NewModulePath << '\n'; |
| 851 | } |
| 852 | |
| 853 | std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex; |
| 854 | gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries, |
Mehdi Amini | cdbcbf7 | 2016-08-16 05:46:05 +0000 | [diff] [blame] | 855 | ImportList, ModuleToSummariesForIndex); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 856 | |
| 857 | raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC, |
| 858 | sys::fs::OpenFlags::F_None); |
| 859 | if (EC) |
| 860 | return errorCodeToError(EC); |
| 861 | WriteIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex); |
| 862 | |
| 863 | if (ShouldEmitImportsFiles) |
Mehdi Amini | cdbcbf7 | 2016-08-16 05:46:05 +0000 | [diff] [blame] | 864 | return errorCodeToError( |
| 865 | EmitImportsFiles(ModulePath, NewModulePath + ".imports", ImportList)); |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 866 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 869 | Error wait() override { return Error::success(); } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 870 | }; |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 871 | } // end anonymous namespace |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 872 | |
| 873 | ThinBackend lto::createWriteIndexesThinBackend(std::string OldPrefix, |
| 874 | std::string NewPrefix, |
| 875 | bool ShouldEmitImportsFiles, |
| 876 | std::string LinkedObjectsFile) { |
| 877 | return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex, |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 878 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 879 | AddStreamFn AddStream, NativeObjectCache Cache) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 880 | return llvm::make_unique<WriteIndexesThinBackend>( |
Mehdi Amini | 18b9111 | 2016-08-19 06:10:03 +0000 | [diff] [blame] | 881 | Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix, |
| 882 | ShouldEmitImportsFiles, LinkedObjectsFile); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 883 | }; |
| 884 | } |
| 885 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 886 | Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache, |
| 887 | bool HasRegularLTO) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 888 | if (ThinLTO.ModuleMap.empty()) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 889 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 890 | |
| 891 | if (Conf.CombinedIndexHook && !Conf.CombinedIndexHook(ThinLTO.CombinedIndex)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 892 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 893 | |
| 894 | // Collect for each module the list of function it defines (GUID -> |
| 895 | // Summary). |
| 896 | StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>> |
| 897 | ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size()); |
| 898 | ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule( |
| 899 | ModuleToDefinedGVSummaries); |
Teresa Johnson | 620c140 | 2016-09-20 23:07:17 +0000 | [diff] [blame] | 900 | // Create entries for any modules that didn't have any GV summaries |
| 901 | // (either they didn't have any GVs to start with, or we suppressed |
| 902 | // generation of the summaries because they e.g. had inline assembly |
| 903 | // uses that couldn't be promoted/renamed on export). This is so |
| 904 | // InProcessThinBackend::start can still launch a backend thread, which |
| 905 | // is passed the map of summaries for the module, without any special |
| 906 | // handling for this case. |
| 907 | for (auto &Mod : ThinLTO.ModuleMap) |
| 908 | if (!ModuleToDefinedGVSummaries.count(Mod.first)) |
| 909 | ModuleToDefinedGVSummaries.try_emplace(Mod.first); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 910 | |
| 911 | StringMap<FunctionImporter::ImportMapTy> ImportLists( |
| 912 | ThinLTO.ModuleMap.size()); |
| 913 | StringMap<FunctionImporter::ExportSetTy> ExportLists( |
| 914 | ThinLTO.ModuleMap.size()); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 915 | StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 916 | |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 917 | if (Conf.OptLevel > 0) { |
Mehdi Amini | f39ce99 | 2017-01-20 23:34:12 +0000 | [diff] [blame] | 918 | // Compute "dead" symbols, we don't want to import/export these! |
| 919 | DenseSet<GlobalValue::GUID> GUIDPreservedSymbols; |
| 920 | for (auto &Res : GlobalResolutions) { |
| 921 | if (Res.second.VisibleOutsideThinLTO && |
| 922 | // IRName will be defined if we have seen the prevailing copy of |
| 923 | // this value. If not, no need to preserve any ThinLTO copies. |
| 924 | !Res.second.IRName.empty()) |
| 925 | GUIDPreservedSymbols.insert(GlobalValue::getGUID(Res.second.IRName)); |
| 926 | } |
| 927 | |
| 928 | auto DeadSymbols = |
| 929 | computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols); |
| 930 | |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 931 | ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 932 | ImportLists, ExportLists, &DeadSymbols); |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 933 | |
| 934 | std::set<GlobalValue::GUID> ExportedGUIDs; |
| 935 | for (auto &Res : GlobalResolutions) { |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 936 | // First check if the symbol was flagged as having external references. |
| 937 | if (Res.second.Partition != GlobalResolution::External) |
| 938 | continue; |
| 939 | // IRName will be defined if we have seen the prevailing copy of |
| 940 | // this value. If not, no need to mark as exported from a ThinLTO |
| 941 | // partition (and we can't get the GUID). |
| 942 | if (Res.second.IRName.empty()) |
| 943 | continue; |
| 944 | auto GUID = GlobalValue::getGUID(Res.second.IRName); |
| 945 | // Mark exported unless index-based analysis determined it to be dead. |
| 946 | if (!DeadSymbols.count(GUID)) |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 947 | ExportedGUIDs.insert(GlobalValue::getGUID(Res.second.IRName)); |
| 948 | } |
| 949 | |
| 950 | auto isPrevailing = [&](GlobalValue::GUID GUID, |
| 951 | const GlobalValueSummary *S) { |
| 952 | return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath(); |
| 953 | }; |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 954 | auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) { |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 955 | const auto &ExportList = ExportLists.find(ModuleIdentifier); |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 956 | return (ExportList != ExportLists.end() && |
| 957 | ExportList->second.count(GUID)) || |
| 958 | ExportedGUIDs.count(GUID); |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 959 | }; |
| 960 | thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported); |
| 961 | |
| 962 | auto recordNewLinkage = [&](StringRef ModuleIdentifier, |
| 963 | GlobalValue::GUID GUID, |
| 964 | GlobalValue::LinkageTypes NewLinkage) { |
| 965 | ResolvedODR[ModuleIdentifier][GUID] = NewLinkage; |
| 966 | }; |
| 967 | |
| 968 | thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing, |
| 969 | recordNewLinkage); |
| 970 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 971 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 972 | std::unique_ptr<ThinBackendProc> BackendProc = |
| 973 | ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, |
| 974 | AddStream, Cache); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 975 | |
Davide Italiano | 6309895 | 2017-01-04 20:37:57 +0000 | [diff] [blame] | 976 | // Task numbers start at ParallelCodeGenParallelismLevel if an LTO |
| 977 | // module is present, as tasks 0 through ParallelCodeGenParallelismLevel-1 |
| 978 | // are reserved for parallel code generation partitions. |
Teresa Johnson | 8dd61ae | 2016-09-16 13:54:19 +0000 | [diff] [blame] | 979 | unsigned Task = |
| 980 | HasRegularLTO ? RegularLTO.ParallelCodeGenParallelismLevel : 0; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 981 | for (auto &Mod : ThinLTO.ModuleMap) { |
Mehdi Amini | cdbcbf7 | 2016-08-16 05:46:05 +0000 | [diff] [blame] | 982 | if (Error E = BackendProc->start(Task, Mod.second, ImportLists[Mod.first], |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 983 | ExportLists[Mod.first], |
| 984 | ResolvedODR[Mod.first], ThinLTO.ModuleMap)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 985 | return E; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 986 | ++Task; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | return BackendProc->wait(); |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 990 | } |
Davide Italiano | 690ed9d | 2017-02-10 23:49:38 +0000 | [diff] [blame] | 991 | |
| 992 | Expected<std::unique_ptr<tool_output_file>> |
| 993 | lto::setupOptimizationRemarks(LLVMContext &Context, |
| 994 | StringRef LTORemarksFilename, |
| 995 | bool LTOPassRemarksWithHotness, int Count) { |
| 996 | if (LTORemarksFilename.empty()) |
| 997 | return nullptr; |
| 998 | |
| 999 | std::string Filename = LTORemarksFilename; |
| 1000 | if (Count != -1) |
| 1001 | Filename += ".thin." + llvm::utostr(Count) + ".yaml"; |
| 1002 | |
| 1003 | std::error_code EC; |
| 1004 | auto DiagnosticFile = |
| 1005 | llvm::make_unique<tool_output_file>(Filename, EC, sys::fs::F_None); |
| 1006 | if (EC) |
| 1007 | return errorCodeToError(EC); |
| 1008 | Context.setDiagnosticsOutputFile( |
| 1009 | llvm::make_unique<yaml::Output>(DiagnosticFile->os())); |
| 1010 | if (LTOPassRemarksWithHotness) |
| 1011 | Context.setDiagnosticHotnessRequested(true); |
| 1012 | DiagnosticFile->keep(); |
| 1013 | return std::move(DiagnosticFile); |
| 1014 | } |