Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 1 | //===-ThinLTOCodeGenerator.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 the Thin Link Time Optimization library. This library is |
| 11 | // intended to be used by linker to optimize code at link time. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/LTO/ThinLTOCodeGenerator.h" |
| 16 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 17 | #ifdef HAVE_LLVM_REVISION |
| 18 | #include "LLVMLTORevision.h" |
| 19 | #endif |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 20 | |
| 21 | #include "UpdateCompilerUsed.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 26 | #include "llvm/Analysis/TargetTransformInfo.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 27 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 28 | #include "llvm/Bitcode/ReaderWriter.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 29 | #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 31 | #include "llvm/IR/LLVMContext.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 32 | #include "llvm/IR/LegacyPassManager.h" |
| 33 | #include "llvm/IR/Mangler.h" |
| 34 | #include "llvm/IRReader/IRReader.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 35 | #include "llvm/LTO/LTO.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 36 | #include "llvm/Linker/Linker.h" |
| 37 | #include "llvm/MC/SubtargetFeature.h" |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 38 | #include "llvm/Object/IRObjectFile.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 39 | #include "llvm/Object/ModuleSummaryIndexObjectFile.h" |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 41 | #include "llvm/Support/CachePruning.h" |
| 42 | #include "llvm/Support/Debug.h" |
| 43 | #include "llvm/Support/Path.h" |
| 44 | #include "llvm/Support/SHA1.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 45 | #include "llvm/Support/TargetRegistry.h" |
| 46 | #include "llvm/Support/ThreadPool.h" |
| 47 | #include "llvm/Target/TargetMachine.h" |
| 48 | #include "llvm/Transforms/IPO.h" |
| 49 | #include "llvm/Transforms/IPO/FunctionImport.h" |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/IPO/Internalize.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 51 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 52 | #include "llvm/Transforms/ObjCARC.h" |
| 53 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 54 | |
Mehdi Amini | 819e9cd | 2016-05-16 19:33:07 +0000 | [diff] [blame] | 55 | #include <numeric> |
| 56 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 57 | using namespace llvm; |
| 58 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 59 | #define DEBUG_TYPE "thinlto" |
| 60 | |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 61 | namespace llvm { |
| 62 | // Flags -discard-value-names, defined in LTOCodeGenerator.cpp |
| 63 | extern cl::opt<bool> LTODiscardValueNames; |
| 64 | } |
| 65 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 66 | namespace { |
| 67 | |
| 68 | static cl::opt<int> ThreadCount("threads", |
| 69 | cl::init(std::thread::hardware_concurrency())); |
| 70 | |
| 71 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 72 | DiagnosticPrinterRawOStream DP(errs()); |
| 73 | DI.print(DP); |
| 74 | errs() << '\n'; |
| 75 | } |
| 76 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 77 | // Simple helper to save temporary files for debug. |
| 78 | static void saveTempBitcode(const Module &TheModule, StringRef TempDir, |
| 79 | unsigned count, StringRef Suffix) { |
| 80 | if (TempDir.empty()) |
| 81 | return; |
| 82 | // User asked to save temps, let dump the bitcode file after import. |
| 83 | auto SaveTempPath = TempDir + llvm::utostr(count) + Suffix; |
| 84 | std::error_code EC; |
| 85 | raw_fd_ostream OS(SaveTempPath.str(), EC, sys::fs::F_None); |
| 86 | if (EC) |
| 87 | report_fatal_error(Twine("Failed to open ") + SaveTempPath + |
| 88 | " to save optimized bitcode\n"); |
Teresa Johnson | 3c35e09 | 2016-04-04 21:19:31 +0000 | [diff] [blame] | 89 | WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 92 | bool IsFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList, |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 93 | const ModuleSummaryIndex &Index, |
| 94 | StringRef ModulePath) { |
| 95 | // Get the first *linker visible* definition for this global in the summary |
| 96 | // list. |
| 97 | auto FirstDefForLinker = llvm::find_if( |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 98 | GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) { |
| 99 | auto Linkage = Summary->linkage(); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 100 | return !GlobalValue::isAvailableExternallyLinkage(Linkage); |
| 101 | }); |
| 102 | // If \p GV is not the first definition, give up... |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 103 | if ((*FirstDefForLinker)->modulePath() != ModulePath) |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 104 | return false; |
| 105 | // If there is any strong definition anywhere, do not bother emitting this. |
| 106 | if (llvm::any_of( |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 107 | GVSummaryList, |
| 108 | [](const std::unique_ptr<GlobalValueSummary> &Summary) { |
| 109 | auto Linkage = Summary->linkage(); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 110 | return !GlobalValue::isAvailableExternallyLinkage(Linkage) && |
| 111 | !GlobalValue::isWeakForLinker(Linkage); |
| 112 | })) |
| 113 | return false; |
| 114 | return true; |
Hans Wennborg | fa6e414 | 2016-04-02 01:03:41 +0000 | [diff] [blame] | 115 | } |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 116 | |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 117 | static GlobalValue::LinkageTypes |
| 118 | ResolveODR(const ModuleSummaryIndex &Index, |
| 119 | const FunctionImporter::ExportSetTy &ExportList, |
Mehdi Amini | aa309b1 | 2016-04-26 10:35:01 +0000 | [diff] [blame] | 120 | const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 121 | StringRef ModuleIdentifier, GlobalValue::GUID GUID, |
| 122 | const GlobalValueSummary &GV) { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 123 | auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) { |
| 124 | return GVSummaryList.size() > 1; |
| 125 | }; |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 126 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 127 | auto OriginalLinkage = GV.linkage(); |
| 128 | switch (OriginalLinkage) { |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 129 | case GlobalValue::ExternalLinkage: |
| 130 | case GlobalValue::AvailableExternallyLinkage: |
| 131 | case GlobalValue::AppendingLinkage: |
| 132 | case GlobalValue::InternalLinkage: |
| 133 | case GlobalValue::PrivateLinkage: |
| 134 | case GlobalValue::ExternalWeakLinkage: |
| 135 | case GlobalValue::CommonLinkage: |
| 136 | case GlobalValue::LinkOnceAnyLinkage: |
| 137 | case GlobalValue::WeakAnyLinkage: |
| 138 | break; |
| 139 | case GlobalValue::LinkOnceODRLinkage: |
| 140 | case GlobalValue::WeakODRLinkage: { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 141 | auto &GVSummaryList = Index.findGlobalValueSummaryList(GUID)->second; |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 142 | // We need to emit only one of these, the first module will keep |
| 143 | // it, but turned into a weak while the others will drop it. |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 144 | if (!HasMultipleCopies(GVSummaryList)) { |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 145 | // Exported LinkonceODR needs to be promoted to not be discarded |
| 146 | if (GlobalValue::isDiscardableIfUnused(OriginalLinkage) && |
Mehdi Amini | aa309b1 | 2016-04-26 10:35:01 +0000 | [diff] [blame] | 147 | (ExportList.count(GUID) || GUIDPreservedSymbols.count(GUID))) |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 148 | return GlobalValue::WeakODRLinkage; |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 149 | break; |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 150 | } |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 151 | if (IsFirstDefinitionForLinker(GVSummaryList, Index, ModuleIdentifier)) |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 152 | return GlobalValue::WeakODRLinkage; |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 153 | else if (isa<AliasSummary>(&GV)) |
| 154 | // Alias can't be turned into available_externally. |
| 155 | return OriginalLinkage; |
| 156 | return GlobalValue::AvailableExternallyLinkage; |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 159 | return OriginalLinkage; |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /// Resolve LinkOnceODR and WeakODR. |
| 163 | /// |
| 164 | /// We'd like to drop these function if they are no longer referenced in the |
| 165 | /// current module. However there is a chance that another module is still |
| 166 | /// referencing them because of the import. We make sure we always emit at least |
| 167 | /// one copy. |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 168 | static void ResolveODR( |
| 169 | const ModuleSummaryIndex &Index, |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 170 | const FunctionImporter::ExportSetTy &ExportList, |
Mehdi Amini | aa309b1 | 2016-04-26 10:35:01 +0000 | [diff] [blame] | 171 | const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 172 | const GVSummaryMapTy &DefinedGlobals, StringRef ModuleIdentifier, |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 173 | std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR) { |
Mehdi Amini | 8dcc808 | 2016-04-14 08:46:22 +0000 | [diff] [blame] | 174 | if (Index.modulePaths().size() == 1) |
| 175 | // Nothing to do if we don't have multiple modules |
| 176 | return; |
| 177 | |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 178 | // We won't optimize the globals that are referenced by an alias for now |
| 179 | // Ideally we should turn the alias into a global and duplicate the definition |
| 180 | // when needed. |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 181 | DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias; |
| 182 | for (auto &GA : DefinedGlobals) { |
| 183 | if (auto AS = dyn_cast<AliasSummary>(GA.second)) |
| 184 | GlobalInvolvedWithAlias.insert(&AS->getAliasee()); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 185 | } |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 186 | |
| 187 | for (auto &GV : DefinedGlobals) { |
| 188 | if (GlobalInvolvedWithAlias.count(GV.second)) |
| 189 | continue; |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 190 | auto NewLinkage = |
Mehdi Amini | aa309b1 | 2016-04-26 10:35:01 +0000 | [diff] [blame] | 191 | ResolveODR(Index, ExportList, GUIDPreservedSymbols, ModuleIdentifier, GV.first, *GV.second); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 192 | if (NewLinkage != GV.second->linkage()) { |
| 193 | ResolvedODR[GV.first] = NewLinkage; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /// Fixup linkage, see ResolveODR() above. |
| 199 | void fixupODR( |
| 200 | Module &TheModule, |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 201 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR) { |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 202 | // Process functions and global now |
| 203 | for (auto &GV : TheModule) { |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 204 | auto NewLinkage = ResolvedODR.find(GV.getGUID()); |
| 205 | if (NewLinkage == ResolvedODR.end()) |
| 206 | continue; |
| 207 | DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from " |
| 208 | << GV.getLinkage() << " to " << NewLinkage->second << "\n"); |
| 209 | GV.setLinkage(NewLinkage->second); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 210 | } |
| 211 | for (auto &GV : TheModule.globals()) { |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 212 | auto NewLinkage = ResolvedODR.find(GV.getGUID()); |
| 213 | if (NewLinkage == ResolvedODR.end()) |
| 214 | continue; |
| 215 | DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from " |
| 216 | << GV.getLinkage() << " to " << NewLinkage->second << "\n"); |
| 217 | GV.setLinkage(NewLinkage->second); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 218 | } |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 219 | for (auto &GV : TheModule.aliases()) { |
| 220 | auto NewLinkage = ResolvedODR.find(GV.getGUID()); |
| 221 | if (NewLinkage == ResolvedODR.end()) |
| 222 | continue; |
| 223 | DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from " |
| 224 | << GV.getLinkage() << " to " << NewLinkage->second << "\n"); |
| 225 | GV.setLinkage(NewLinkage->second); |
| 226 | } |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 229 | static StringMap<MemoryBufferRef> |
| 230 | generateModuleMap(const std::vector<MemoryBufferRef> &Modules) { |
| 231 | StringMap<MemoryBufferRef> ModuleMap; |
| 232 | for (auto &ModuleBuffer : Modules) { |
| 233 | assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) == |
| 234 | ModuleMap.end() && |
| 235 | "Expect unique Buffer Identifier"); |
| 236 | ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer; |
| 237 | } |
| 238 | return ModuleMap; |
| 239 | } |
| 240 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 241 | static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 242 | if (renameModuleForThinLTO(TheModule, Index)) |
| 243 | report_fatal_error("renameModuleForThinLTO failed"); |
| 244 | } |
| 245 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 246 | static void |
| 247 | crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index, |
| 248 | StringMap<MemoryBufferRef> &ModuleMap, |
| 249 | const FunctionImporter::ImportMapTy &ImportList) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 250 | ModuleLoader Loader(TheModule.getContext(), ModuleMap); |
| 251 | FunctionImporter Importer(Index, Loader); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 252 | Importer.importFunctions(TheModule, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void optimizeModule(Module &TheModule, TargetMachine &TM) { |
| 256 | // Populate the PassManager |
| 257 | PassManagerBuilder PMB; |
| 258 | PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple()); |
| 259 | PMB.Inliner = createFunctionInliningPass(); |
| 260 | // FIXME: should get it from the bitcode? |
| 261 | PMB.OptLevel = 3; |
| 262 | PMB.LoopVectorize = true; |
| 263 | PMB.SLPVectorize = true; |
| 264 | PMB.VerifyInput = true; |
| 265 | PMB.VerifyOutput = false; |
| 266 | |
| 267 | legacy::PassManager PM; |
| 268 | |
| 269 | // Add the TTI (required to inform the vectorizer about register size for |
| 270 | // instance) |
| 271 | PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis())); |
| 272 | |
| 273 | // Add optimizations |
| 274 | PMB.populateThinLTOPassManager(PM); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 275 | |
| 276 | PM.run(TheModule); |
| 277 | } |
| 278 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 279 | // Create a DenseSet of GlobalValue to be used with the Internalizer. |
| 280 | static DenseSet<const GlobalValue *> computePreservedSymbolsForModule( |
| 281 | Module &TheModule, const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, |
| 282 | const FunctionImporter::ExportSetTy &ExportList) { |
| 283 | DenseSet<const GlobalValue *> PreservedGV; |
| 284 | if (GUIDPreservedSymbols.empty()) |
| 285 | // Early exit: internalize is disabled when there is nothing to preserve. |
| 286 | return PreservedGV; |
| 287 | |
| 288 | auto AddPreserveGV = [&](const GlobalValue &GV) { |
| 289 | auto GUID = GV.getGUID(); |
| 290 | if (GUIDPreservedSymbols.count(GUID) || ExportList.count(GUID)) |
| 291 | PreservedGV.insert(&GV); |
| 292 | }; |
| 293 | |
| 294 | for (auto &GV : TheModule) |
| 295 | AddPreserveGV(GV); |
| 296 | for (auto &GV : TheModule.globals()) |
| 297 | AddPreserveGV(GV); |
| 298 | for (auto &GV : TheModule.aliases()) |
| 299 | AddPreserveGV(GV); |
| 300 | |
| 301 | return PreservedGV; |
| 302 | } |
| 303 | |
| 304 | // Run internalization on \p TheModule |
| 305 | static void |
| 306 | doInternalizeModule(Module &TheModule, const TargetMachine &TM, |
| 307 | const DenseSet<const GlobalValue *> &PreservedGV) { |
| 308 | if (PreservedGV.empty()) { |
| 309 | // Be friendly and don't nuke totally the module when the client didn't |
| 310 | // supply anything to preserve. |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | // Parse inline ASM and collect the list of symbols that are not defined in |
| 315 | // the current module. |
| 316 | StringSet<> AsmUndefinedRefs; |
| 317 | object::IRObjectFile::CollectAsmUndefinedRefs( |
| 318 | Triple(TheModule.getTargetTriple()), TheModule.getModuleInlineAsm(), |
| 319 | [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) { |
| 320 | if (Flags & object::BasicSymbolRef::SF_Undefined) |
| 321 | AsmUndefinedRefs.insert(Name); |
| 322 | }); |
| 323 | |
| 324 | // Update the llvm.compiler_used globals to force preserving libcalls and |
| 325 | // symbols referenced from asm |
| 326 | UpdateCompilerUsed(TheModule, TM, AsmUndefinedRefs); |
| 327 | |
| 328 | // Declare a callback for the internalize pass that will ask for every |
| 329 | // candidate GlobalValue if it can be internalized or not. |
| 330 | auto MustPreserveGV = |
| 331 | [&](const GlobalValue &GV) -> bool { return PreservedGV.count(&GV); }; |
| 332 | |
| 333 | llvm::internalizeModule(TheModule, MustPreserveGV); |
| 334 | } |
| 335 | |
| 336 | // Convert the PreservedSymbols map from "Name" based to "GUID" based. |
| 337 | static DenseSet<GlobalValue::GUID> |
| 338 | computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols, |
| 339 | const Triple &TheTriple) { |
| 340 | DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size()); |
| 341 | for (auto &Entry : PreservedSymbols) { |
| 342 | StringRef Name = Entry.first(); |
| 343 | if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_') |
| 344 | Name = Name.drop_front(); |
| 345 | GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name)); |
| 346 | } |
| 347 | return GUIDPreservedSymbols; |
| 348 | } |
| 349 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 350 | std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule, |
| 351 | TargetMachine &TM) { |
| 352 | SmallVector<char, 128> OutputBuffer; |
| 353 | |
| 354 | // CodeGen |
| 355 | { |
| 356 | raw_svector_ostream OS(OutputBuffer); |
| 357 | legacy::PassManager PM; |
Mehdi Amini | 215d59e | 2016-04-01 08:22:59 +0000 | [diff] [blame] | 358 | |
| 359 | // If the bitcode files contain ARC code and were compiled with optimization, |
| 360 | // the ObjCARCContractPass must be run, so do it unconditionally here. |
| 361 | PM.add(createObjCARCContractPass()); |
| 362 | |
| 363 | // Setup the codegen now. |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 364 | if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile, |
| 365 | /* DisableVerify */ true)) |
| 366 | report_fatal_error("Failed to setup codegen"); |
| 367 | |
| 368 | // Run codegen now. resulting binary is in OutputBuffer. |
| 369 | PM.run(TheModule); |
| 370 | } |
| 371 | return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer)); |
| 372 | } |
| 373 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 374 | /// Manage caching for a single Module. |
| 375 | class ModuleCacheEntry { |
| 376 | SmallString<128> EntryPath; |
| 377 | |
| 378 | public: |
| 379 | // Create a cache entry. This compute a unique hash for the Module considering |
| 380 | // the current list of export/import, and offer an interface to query to |
| 381 | // access the content in the cache. |
| 382 | ModuleCacheEntry( |
| 383 | StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID, |
| 384 | const FunctionImporter::ImportMapTy &ImportList, |
| 385 | const FunctionImporter::ExportSetTy &ExportList, |
| 386 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 387 | const GVSummaryMapTy &DefinedFunctions, |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 388 | const DenseSet<GlobalValue::GUID> &PreservedSymbols) { |
| 389 | if (CachePath.empty()) |
| 390 | return; |
| 391 | |
| 392 | // Compute the unique hash for this entry |
| 393 | // This is based on the current compiler version, the module itself, the |
| 394 | // export list, the hash for every single module in the import list, the |
| 395 | // list of ResolvedODR for the module, and the list of preserved symbols. |
| 396 | |
| 397 | SHA1 Hasher; |
| 398 | |
| 399 | // Start with the compiler revision |
| 400 | Hasher.update(LLVM_VERSION_STRING); |
| 401 | #ifdef HAVE_LLVM_REVISION |
| 402 | Hasher.update(LLVM_REVISION); |
| 403 | #endif |
| 404 | |
| 405 | // Include the hash for the current module |
| 406 | auto ModHash = Index.getModuleHash(ModuleID); |
| 407 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash))); |
| 408 | for (auto F : ExportList) |
| 409 | // The export list can impact the internalization, be conservative here |
| 410 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F))); |
| 411 | |
| 412 | // Include the hash for every module we import functions from |
| 413 | for (auto &Entry : ImportList) { |
| 414 | auto ModHash = Index.getModuleHash(Entry.first()); |
| 415 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash))); |
| 416 | } |
| 417 | |
| 418 | // Include the hash for the resolved ODR. |
| 419 | for (auto &Entry : ResolvedODR) { |
Sjoerd Meijer | 41beee6 | 2016-04-27 18:35:02 +0000 | [diff] [blame] | 420 | Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first, |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 421 | sizeof(GlobalValue::GUID))); |
Sjoerd Meijer | 41beee6 | 2016-04-27 18:35:02 +0000 | [diff] [blame] | 422 | Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second, |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 423 | sizeof(GlobalValue::LinkageTypes))); |
| 424 | } |
| 425 | |
| 426 | // Include the hash for the preserved symbols. |
| 427 | for (auto &Entry : PreservedSymbols) { |
| 428 | if (DefinedFunctions.count(Entry)) |
| 429 | Hasher.update( |
Sjoerd Meijer | 41beee6 | 2016-04-27 18:35:02 +0000 | [diff] [blame] | 430 | ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID))); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | sys::path::append(EntryPath, CachePath, toHex(Hasher.result())); |
| 434 | } |
| 435 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 436 | // Access the path to this entry in the cache. |
| 437 | StringRef getEntryPath() { return EntryPath; } |
| 438 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 439 | // Try loading the buffer for this cache entry. |
| 440 | ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() { |
| 441 | if (EntryPath.empty()) |
| 442 | return std::error_code(); |
| 443 | return MemoryBuffer::getFile(EntryPath); |
| 444 | } |
| 445 | |
| 446 | // Cache the Produced object file |
Mehdi Amini | 001bb41 | 2016-05-16 19:11:59 +0000 | [diff] [blame] | 447 | std::unique_ptr<MemoryBuffer> |
| 448 | write(std::unique_ptr<MemoryBuffer> OutputBuffer) { |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 449 | if (EntryPath.empty()) |
Mehdi Amini | 001bb41 | 2016-05-16 19:11:59 +0000 | [diff] [blame] | 450 | return OutputBuffer; |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 451 | |
| 452 | // Write to a temporary to avoid race condition |
| 453 | SmallString<128> TempFilename; |
| 454 | int TempFD; |
| 455 | std::error_code EC = |
| 456 | sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename); |
| 457 | if (EC) { |
| 458 | errs() << "Error: " << EC.message() << "\n"; |
| 459 | report_fatal_error("ThinLTO: Can't get a temporary file"); |
| 460 | } |
| 461 | { |
| 462 | raw_fd_ostream OS(TempFD, /* ShouldClose */ true); |
Mehdi Amini | 001bb41 | 2016-05-16 19:11:59 +0000 | [diff] [blame] | 463 | OS << OutputBuffer->getBuffer(); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 464 | } |
| 465 | // Rename to final destination (hopefully race condition won't matter here) |
Mehdi Amini | 2a16a5f | 2016-05-14 04:58:38 +0000 | [diff] [blame] | 466 | EC = sys::fs::rename(TempFilename, EntryPath); |
| 467 | if (EC) { |
Mehdi Amini | b02139d | 2016-05-14 05:16:35 +0000 | [diff] [blame] | 468 | sys::fs::remove(TempFilename); |
| 469 | raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None); |
| 470 | if (EC) |
| 471 | report_fatal_error(Twine("Failed to open ") + EntryPath + |
| 472 | " to save cached entry\n"); |
Mehdi Amini | 001bb41 | 2016-05-16 19:11:59 +0000 | [diff] [blame] | 473 | OS << OutputBuffer->getBuffer(); |
Mehdi Amini | 2a16a5f | 2016-05-14 04:58:38 +0000 | [diff] [blame] | 474 | } |
Mehdi Amini | 001bb41 | 2016-05-16 19:11:59 +0000 | [diff] [blame] | 475 | auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath); |
| 476 | if (auto EC = ReloadedBufferOrErr.getError()) { |
| 477 | // FIXME diagnose |
| 478 | errs() << "error: can't reload cached file '" << EntryPath |
| 479 | << "': " << EC.message() << "\n"; |
| 480 | return OutputBuffer; |
| 481 | } |
| 482 | return std::move(*ReloadedBufferOrErr); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 483 | } |
| 484 | }; |
| 485 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 486 | static std::unique_ptr<MemoryBuffer> ProcessThinLTOModule( |
| 487 | Module &TheModule, const ModuleSummaryIndex &Index, |
| 488 | StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM, |
| 489 | const FunctionImporter::ImportMapTy &ImportList, |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 490 | const FunctionImporter::ExportSetTy &ExportList, |
| 491 | const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 492 | std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 493 | ThinLTOCodeGenerator::CachingOptions CacheOptions, bool DisableCodeGen, |
| 494 | StringRef SaveTempsDir, unsigned count) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 495 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 496 | // Prepare for internalization by computing the set of symbols to preserve. |
| 497 | // We need to compute the list of symbols to preserve during internalization |
| 498 | // before doing any promotion because after renaming we won't (easily) match |
| 499 | // to the original name. |
| 500 | auto PreservedGV = computePreservedSymbolsForModule( |
| 501 | TheModule, GUIDPreservedSymbols, ExportList); |
| 502 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 503 | // "Benchmark"-like optimization: single-source case |
| 504 | bool SingleModule = (ModuleMap.size() == 1); |
| 505 | |
| 506 | if (!SingleModule) { |
| 507 | promoteModule(TheModule, Index); |
| 508 | |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 509 | // Resolve the LinkOnce/Weak ODR, trying to turn them into |
| 510 | // "available_externally" when possible. |
| 511 | // This is a compile-time optimization. |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 512 | fixupODR(TheModule, ResolvedODR); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 513 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 514 | // Save temps: after promotion. |
Mehdi Amini | 4b300e0a | 2016-05-05 05:14:16 +0000 | [diff] [blame] | 515 | saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc"); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 516 | } |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 517 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 518 | // Internalization |
| 519 | doInternalizeModule(TheModule, TM, PreservedGV); |
| 520 | |
| 521 | // Save internalized bitcode |
Mehdi Amini | 4b300e0a | 2016-05-05 05:14:16 +0000 | [diff] [blame] | 522 | saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc"); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 523 | |
| 524 | if (!SingleModule) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 525 | crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 526 | |
| 527 | // Save temps: after cross-module import. |
Mehdi Amini | 4b300e0a | 2016-05-05 05:14:16 +0000 | [diff] [blame] | 528 | saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc"); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | optimizeModule(TheModule, TM); |
| 532 | |
Mehdi Amini | 4b300e0a | 2016-05-05 05:14:16 +0000 | [diff] [blame] | 533 | saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc"); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 534 | |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 535 | if (DisableCodeGen) { |
| 536 | // Configured to stop before CodeGen, serialize the bitcode and return. |
| 537 | SmallVector<char, 128> OutputBuffer; |
| 538 | { |
| 539 | raw_svector_ostream OS(OutputBuffer); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 540 | ModuleSummaryIndexBuilder IndexBuilder(&TheModule); |
| 541 | WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex()); |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 542 | } |
| 543 | return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer)); |
| 544 | } |
| 545 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 546 | return codegenModule(TheModule, TM); |
| 547 | } |
| 548 | |
| 549 | // Initialize the TargetMachine builder for a given Triple |
| 550 | static void initTMBuilder(TargetMachineBuilder &TMBuilder, |
| 551 | const Triple &TheTriple) { |
| 552 | // Set a default CPU for Darwin triples (copied from LTOCodeGenerator). |
| 553 | // FIXME this looks pretty terrible... |
| 554 | if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) { |
| 555 | if (TheTriple.getArch() == llvm::Triple::x86_64) |
| 556 | TMBuilder.MCpu = "core2"; |
| 557 | else if (TheTriple.getArch() == llvm::Triple::x86) |
| 558 | TMBuilder.MCpu = "yonah"; |
| 559 | else if (TheTriple.getArch() == llvm::Triple::aarch64) |
| 560 | TMBuilder.MCpu = "cyclone"; |
| 561 | } |
| 562 | TMBuilder.TheTriple = std::move(TheTriple); |
| 563 | } |
| 564 | |
| 565 | } // end anonymous namespace |
| 566 | |
| 567 | void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) { |
| 568 | MemoryBufferRef Buffer(Data, Identifier); |
| 569 | if (Modules.empty()) { |
| 570 | // First module added, so initialize the triple and some options |
| 571 | LLVMContext Context; |
| 572 | Triple TheTriple(getBitcodeTargetTriple(Buffer, Context)); |
| 573 | initTMBuilder(TMBuilder, Triple(TheTriple)); |
| 574 | } |
| 575 | #ifndef NDEBUG |
| 576 | else { |
| 577 | LLVMContext Context; |
| 578 | assert(TMBuilder.TheTriple.str() == |
| 579 | getBitcodeTargetTriple(Buffer, Context) && |
| 580 | "ThinLTO modules with different triple not supported"); |
| 581 | } |
| 582 | #endif |
| 583 | Modules.push_back(Buffer); |
| 584 | } |
| 585 | |
| 586 | void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) { |
| 587 | PreservedSymbols.insert(Name); |
| 588 | } |
| 589 | |
| 590 | void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) { |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 591 | // FIXME: At the moment, we don't take advantage of this extra information, |
| 592 | // we're conservatively considering cross-references as preserved. |
| 593 | // CrossReferencedSymbols.insert(Name); |
| 594 | PreservedSymbols.insert(Name); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // TargetMachine factory |
| 598 | std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const { |
| 599 | std::string ErrMsg; |
| 600 | const Target *TheTarget = |
| 601 | TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg); |
| 602 | if (!TheTarget) { |
| 603 | report_fatal_error("Can't load target for this Triple: " + ErrMsg); |
| 604 | } |
| 605 | |
| 606 | // Use MAttr as the default set of features. |
| 607 | SubtargetFeatures Features(MAttr); |
| 608 | Features.getDefaultSubtargetFeatures(TheTriple); |
| 609 | std::string FeatureStr = Features.getString(); |
| 610 | return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
| 611 | TheTriple.str(), MCpu, FeatureStr, Options, RelocModel, |
| 612 | CodeModel::Default, CGOptLevel)); |
| 613 | } |
| 614 | |
| 615 | /** |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 616 | * Produce the combined summary index from all the bitcode files: |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 617 | * "thin-link". |
| 618 | */ |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 619 | std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() { |
| 620 | std::unique_ptr<ModuleSummaryIndex> CombinedIndex; |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 621 | uint64_t NextModuleId = 0; |
| 622 | for (auto &ModuleBuffer : Modules) { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 623 | ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr = |
| 624 | object::ModuleSummaryIndexObjectFile::create(ModuleBuffer, |
Teresa Johnson | 6fb3f19 | 2016-04-22 01:52:00 +0000 | [diff] [blame] | 625 | diagnosticHandler); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 626 | if (std::error_code EC = ObjOrErr.getError()) { |
| 627 | // FIXME diagnose |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 628 | errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: " |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 629 | << EC.message() << "\n"; |
| 630 | return nullptr; |
| 631 | } |
| 632 | auto Index = (*ObjOrErr)->takeIndex(); |
| 633 | if (CombinedIndex) { |
| 634 | CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId); |
| 635 | } else { |
| 636 | CombinedIndex = std::move(Index); |
| 637 | } |
| 638 | } |
| 639 | return CombinedIndex; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Perform promotion and renaming of exported internal functions. |
| 644 | */ |
| 645 | void ThinLTOCodeGenerator::promote(Module &TheModule, |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 646 | ModuleSummaryIndex &Index) { |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 647 | auto ModuleCount = Index.modulePaths().size(); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 648 | auto ModuleIdentifier = TheModule.getModuleIdentifier(); |
| 649 | // Collect for each module the list of function it defines (GUID -> Summary). |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 650 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries; |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 651 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 652 | |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 653 | // Generate import/export list |
| 654 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 655 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 656 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
| 657 | ExportLists); |
| 658 | auto &ExportList = ExportLists[ModuleIdentifier]; |
| 659 | |
Mehdi Amini | aa309b1 | 2016-04-26 10:35:01 +0000 | [diff] [blame] | 660 | // Convert the preserved symbols set from string to GUID |
| 661 | auto GUIDPreservedSymbols = |
| 662 | computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); |
| 663 | |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 664 | // Resolve the LinkOnceODR, trying to turn them into "available_externally" |
| 665 | // where possible. |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 666 | // This is a compile-time optimization. |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 667 | // We use a std::map here to be able to have a defined ordering when |
| 668 | // producing a hash for the cache entry. |
| 669 | std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> ResolvedODR; |
Mehdi Amini | aa309b1 | 2016-04-26 10:35:01 +0000 | [diff] [blame] | 670 | ResolveODR(Index, ExportList, GUIDPreservedSymbols, ModuleToDefinedGVSummaries[ModuleIdentifier], |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 671 | ModuleIdentifier, ResolvedODR); |
| 672 | fixupODR(TheModule, ResolvedODR); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 673 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 674 | promoteModule(TheModule, Index); |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Perform cross-module importing for the module identified by ModuleIdentifier. |
| 679 | */ |
| 680 | void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule, |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 681 | ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 682 | auto ModuleMap = generateModuleMap(Modules); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 683 | auto ModuleCount = Index.modulePaths().size(); |
| 684 | |
| 685 | // Collect for each module the list of function it defines (GUID -> Summary). |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 686 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 687 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 688 | |
| 689 | // Generate import/export list |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 690 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 691 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 692 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
| 693 | ExportLists); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 694 | auto &ImportList = ImportLists[TheModule.getModuleIdentifier()]; |
| 695 | |
| 696 | crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /** |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 700 | * Compute the list of summaries needed for importing into module. |
| 701 | */ |
| 702 | void ThinLTOCodeGenerator::gatherImportedSummariesForModule( |
| 703 | StringRef ModulePath, ModuleSummaryIndex &Index, |
| 704 | std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) { |
| 705 | auto ModuleCount = Index.modulePaths().size(); |
| 706 | |
| 707 | // Collect for each module the list of function it defines (GUID -> Summary). |
| 708 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
| 709 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
| 710 | |
| 711 | // Generate import/export list |
| 712 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 713 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 714 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
| 715 | ExportLists); |
| 716 | |
| 717 | llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries, |
| 718 | ImportLists, |
| 719 | ModuleToSummariesForIndex); |
| 720 | } |
| 721 | |
| 722 | /** |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 723 | * Emit the list of files needed for importing into module. |
| 724 | */ |
| 725 | void ThinLTOCodeGenerator::emitImports(StringRef ModulePath, |
| 726 | StringRef OutputName, |
| 727 | ModuleSummaryIndex &Index) { |
| 728 | auto ModuleCount = Index.modulePaths().size(); |
| 729 | |
| 730 | // Collect for each module the list of function it defines (GUID -> Summary). |
| 731 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
| 732 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
| 733 | |
| 734 | // Generate import/export list |
| 735 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 736 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 737 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
| 738 | ExportLists); |
| 739 | |
| 740 | std::error_code EC; |
| 741 | if ((EC = EmitImportsFiles(ModulePath, OutputName, ImportLists))) |
| 742 | report_fatal_error(Twine("Failed to open ") + OutputName + |
| 743 | " to save imports lists\n"); |
| 744 | } |
| 745 | |
| 746 | /** |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 747 | * Perform internalization. |
| 748 | */ |
| 749 | void ThinLTOCodeGenerator::internalize(Module &TheModule, |
| 750 | ModuleSummaryIndex &Index) { |
| 751 | initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); |
| 752 | auto ModuleCount = Index.modulePaths().size(); |
| 753 | auto ModuleIdentifier = TheModule.getModuleIdentifier(); |
| 754 | |
| 755 | // Convert the preserved symbols set from string to GUID |
| 756 | auto GUIDPreservedSymbols = |
| 757 | computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); |
| 758 | |
| 759 | // Collect for each module the list of function it defines (GUID -> Summary). |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 760 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 761 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
| 762 | |
| 763 | // Generate import/export list |
| 764 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 765 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 766 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
| 767 | ExportLists); |
| 768 | auto &ExportList = ExportLists[ModuleIdentifier]; |
| 769 | |
| 770 | // Internalization |
| 771 | auto PreservedGV = computePreservedSymbolsForModule( |
| 772 | TheModule, GUIDPreservedSymbols, ExportList); |
| 773 | doInternalizeModule(TheModule, *TMBuilder.create(), PreservedGV); |
| 774 | } |
| 775 | |
| 776 | /** |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 777 | * Perform post-importing ThinLTO optimizations. |
| 778 | */ |
| 779 | void ThinLTOCodeGenerator::optimize(Module &TheModule) { |
| 780 | initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 781 | |
| 782 | // Optimize now |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 783 | optimizeModule(TheModule, *TMBuilder.create()); |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * Perform ThinLTO CodeGen. |
| 788 | */ |
| 789 | std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) { |
| 790 | initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); |
| 791 | return codegenModule(TheModule, *TMBuilder.create()); |
| 792 | } |
| 793 | |
| 794 | // Main entry point for the ThinLTO processing |
| 795 | void ThinLTOCodeGenerator::run() { |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 796 | if (CodeGenOnly) { |
| 797 | // Perform only parallel codegen and return. |
| 798 | ThreadPool Pool; |
| 799 | assert(ProducedBinaries.empty() && "The generator should not be reused"); |
| 800 | ProducedBinaries.resize(Modules.size()); |
| 801 | int count = 0; |
| 802 | for (auto &ModuleBuffer : Modules) { |
| 803 | Pool.async([&](int count) { |
| 804 | LLVMContext Context; |
| 805 | Context.setDiscardValueNames(LTODiscardValueNames); |
| 806 | |
| 807 | // Parse module now |
| 808 | auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false); |
| 809 | |
| 810 | // CodeGen |
| 811 | ProducedBinaries[count] = codegen(*TheModule); |
| 812 | }, count++); |
| 813 | } |
| 814 | |
| 815 | return; |
| 816 | } |
| 817 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 818 | // Sequential linking phase |
| 819 | auto Index = linkCombinedIndex(); |
| 820 | |
| 821 | // Save temps: index. |
| 822 | if (!SaveTempsDir.empty()) { |
| 823 | auto SaveTempPath = SaveTempsDir + "index.bc"; |
| 824 | std::error_code EC; |
| 825 | raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None); |
| 826 | if (EC) |
| 827 | report_fatal_error(Twine("Failed to open ") + SaveTempPath + |
| 828 | " to save optimized bitcode\n"); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 829 | WriteIndexToFile(*Index, OS); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | // Prepare the resulting object vector |
| 833 | assert(ProducedBinaries.empty() && "The generator should not be reused"); |
| 834 | ProducedBinaries.resize(Modules.size()); |
| 835 | |
| 836 | // Prepare the module map. |
| 837 | auto ModuleMap = generateModuleMap(Modules); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 838 | auto ModuleCount = Modules.size(); |
| 839 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 840 | // Collect for each module the list of function it defines (GUID -> Summary). |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 841 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 842 | Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
| 843 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 844 | // Collect the import/export lists for all modules from the call-graph in the |
| 845 | // combined index. |
| 846 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 847 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 848 | ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists, |
| 849 | ExportLists); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 850 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 851 | // Convert the preserved symbols set from string to GUID, this is needed for |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 852 | // computing the caching hash and the internalization. |
| 853 | auto GUIDPreservedSymbols = |
| 854 | computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 855 | |
Mehdi Amini | af52f28 | 2016-05-15 05:49:47 +0000 | [diff] [blame] | 856 | // Make sure that every module has an entry in the ExportLists to enable |
| 857 | // threaded access to this map below |
| 858 | for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) |
| 859 | ExportLists[DefinedGVSummaries.first()]; |
| 860 | |
Mehdi Amini | 819e9cd | 2016-05-16 19:33:07 +0000 | [diff] [blame] | 861 | // Compute the ordering we will process the inputs: the rough heuristic here |
| 862 | // is to sort them per size so that the largest module get schedule as soon as |
| 863 | // possible. This is purely a compile-time optimization. |
| 864 | std::vector<int> ModulesOrdering; |
| 865 | ModulesOrdering.resize(Modules.size()); |
| 866 | std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0); |
| 867 | std::sort(ModulesOrdering.begin(), ModulesOrdering.end(), |
| 868 | [&](int LeftIndex, int RightIndex) { |
| 869 | auto LSize = Modules[LeftIndex].getBufferSize(); |
| 870 | auto RSize = Modules[RightIndex].getBufferSize(); |
| 871 | return LSize > RSize; |
| 872 | }); |
| 873 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 874 | // Parallel optimizer + codegen |
| 875 | { |
| 876 | ThreadPool Pool(ThreadCount); |
Mehdi Amini | 819e9cd | 2016-05-16 19:33:07 +0000 | [diff] [blame] | 877 | for (auto IndexCount : ModulesOrdering) { |
| 878 | auto &ModuleBuffer = Modules[IndexCount]; |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 879 | Pool.async([&](int count) { |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 880 | auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier(); |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 881 | auto &ExportList = ExportLists[ModuleIdentifier]; |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 882 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 883 | auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier]; |
| 884 | |
| 885 | // Resolve ODR, this has to be done early because it impacts the caching |
| 886 | // We use a std::map here to be able to have a defined ordering when |
| 887 | // producing a hash for the cache entry. |
| 888 | std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> ResolvedODR; |
Mehdi Amini | aa309b1 | 2016-04-26 10:35:01 +0000 | [diff] [blame] | 889 | ResolveODR(*Index, ExportList, GUIDPreservedSymbols, DefinedFunctions, ModuleIdentifier, |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 890 | ResolvedODR); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 891 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 892 | // The module may be cached, this helps handling it. |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 893 | ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier, |
| 894 | ImportLists[ModuleIdentifier], ExportList, |
| 895 | ResolvedODR, DefinedFunctions, |
| 896 | GUIDPreservedSymbols); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 897 | |
| 898 | { |
| 899 | auto ErrOrBuffer = CacheEntry.tryLoadingBuffer(); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 900 | DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '" |
| 901 | << CacheEntry.getEntryPath() << "' for buffer " << count |
| 902 | << " " << ModuleIdentifier << "\n"); |
| 903 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 904 | if (ErrOrBuffer) { |
| 905 | // Cache Hit! |
| 906 | ProducedBinaries[count] = std::move(ErrOrBuffer.get()); |
| 907 | return; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | LLVMContext Context; |
| 912 | Context.setDiscardValueNames(LTODiscardValueNames); |
| 913 | Context.enableDebugTypeODRUniquing(); |
| 914 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 915 | // Parse module now |
| 916 | auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false); |
| 917 | |
| 918 | // Save temps: original file. |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 919 | saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc"); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 920 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 921 | auto &ImportList = ImportLists[ModuleIdentifier]; |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 922 | // Run the main process now, and generates a binary |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 923 | auto OutputBuffer = ProcessThinLTOModule( |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 924 | *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList, |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 925 | ExportList, GUIDPreservedSymbols, ResolvedODR, CacheOptions, |
| 926 | DisableCodeGen, SaveTempsDir, count); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 927 | |
Mehdi Amini | 001bb41 | 2016-05-16 19:11:59 +0000 | [diff] [blame] | 928 | OutputBuffer = CacheEntry.write(std::move(OutputBuffer)); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 929 | ProducedBinaries[count] = std::move(OutputBuffer); |
Mehdi Amini | 819e9cd | 2016-05-16 19:33:07 +0000 | [diff] [blame] | 930 | }, IndexCount); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 934 | CachePruning(CacheOptions.Path) |
| 935 | .setPruningInterval(CacheOptions.PruningInterval) |
| 936 | .setEntryExpiration(CacheOptions.Expiration) |
| 937 | .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace) |
| 938 | .prune(); |
| 939 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 940 | // If statistics were requested, print them out now. |
| 941 | if (llvm::AreStatisticsEnabled()) |
| 942 | llvm::PrintStatistics(); |
| 943 | } |