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