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