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