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