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 | |
Peter Collingbourne | 5c73220 | 2016-07-14 21:21:16 +0000 | [diff] [blame] | 15 | #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 16 | |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 22 | #include "llvm/Analysis/TargetTransformInfo.h" |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 23 | #include "llvm/Bitcode/BitcodeReader.h" |
| 24 | #include "llvm/Bitcode/BitcodeWriter.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 25 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 26 | #include "llvm/Config/llvm-config.h" |
Adrian Prantl | 981a799 | 2017-05-20 00:00:08 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 29 | #include "llvm/IR/LLVMContext.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 30 | #include "llvm/IR/LegacyPassManager.h" |
| 31 | #include "llvm/IR/Mangler.h" |
Adrian Prantl | 981a799 | 2017-05-20 00:00:08 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Verifier.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 33 | #include "llvm/IRReader/IRReader.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 34 | #include "llvm/LTO/LTO.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 35 | #include "llvm/MC/SubtargetFeature.h" |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 36 | #include "llvm/Object/IRObjectFile.h" |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CachePruning.h" |
| 38 | #include "llvm/Support/Debug.h" |
Mehdi Amini | 19f176b | 2016-11-19 18:20:05 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Error.h" |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Path.h" |
| 41 | #include "llvm/Support/SHA1.h" |
Weiming Zhao | 79f2d09 | 2018-04-16 03:44:03 +0000 | [diff] [blame] | 42 | #include "llvm/Support/SmallVectorMemoryBuffer.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 43 | #include "llvm/Support/TargetRegistry.h" |
| 44 | #include "llvm/Support/ThreadPool.h" |
Teresa Johnson | ec544c5 | 2016-10-19 17:35:01 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Threading.h" |
Mehdi Amini | 19f176b | 2016-11-19 18:20:05 +0000 | [diff] [blame] | 46 | #include "llvm/Support/ToolOutputFile.h" |
Peter Collingbourne | 942fa56 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 47 | #include "llvm/Support/VCSRevision.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 48 | #include "llvm/Target/TargetMachine.h" |
| 49 | #include "llvm/Transforms/IPO.h" |
| 50 | #include "llvm/Transforms/IPO/FunctionImport.h" |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 51 | #include "llvm/Transforms/IPO/Internalize.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 52 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 53 | #include "llvm/Transforms/ObjCARC.h" |
| 54 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 55 | |
Mehdi Amini | 819e9cd | 2016-05-16 19:33:07 +0000 | [diff] [blame] | 56 | #include <numeric> |
| 57 | |
Andrew Ng | 089303d | 2018-07-04 14:17:10 +0000 | [diff] [blame] | 58 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 59 | #include <unistd.h> |
| 60 | #else |
| 61 | #include <io.h> |
| 62 | #endif |
| 63 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 64 | using namespace llvm; |
| 65 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 66 | #define DEBUG_TYPE "thinlto" |
| 67 | |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 68 | namespace llvm { |
| 69 | // Flags -discard-value-names, defined in LTOCodeGenerator.cpp |
| 70 | extern cl::opt<bool> LTODiscardValueNames; |
Mehdi Amini | 19f176b | 2016-11-19 18:20:05 +0000 | [diff] [blame] | 71 | extern cl::opt<std::string> LTORemarksFilename; |
Adam Nemet | 4c207a6 | 2016-12-02 17:53:56 +0000 | [diff] [blame] | 72 | extern cl::opt<bool> LTOPassRemarksWithHotness; |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 75 | namespace { |
| 76 | |
Teresa Johnson | ec544c5 | 2016-10-19 17:35:01 +0000 | [diff] [blame] | 77 | static cl::opt<int> |
| 78 | ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency())); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 79 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 80 | // Simple helper to save temporary files for debug. |
| 81 | static void saveTempBitcode(const Module &TheModule, StringRef TempDir, |
| 82 | unsigned count, StringRef Suffix) { |
| 83 | if (TempDir.empty()) |
| 84 | return; |
| 85 | // User asked to save temps, let dump the bitcode file after import. |
Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 86 | std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str(); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 87 | std::error_code EC; |
Teresa Johnson | c44a122 | 2016-08-15 23:24:57 +0000 | [diff] [blame] | 88 | raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 89 | if (EC) |
| 90 | report_fatal_error(Twine("Failed to open ") + SaveTempPath + |
| 91 | " to save optimized bitcode\n"); |
Rafael Espindola | 6a86e25 | 2018-02-14 19:11:32 +0000 | [diff] [blame] | 92 | WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 95 | static const GlobalValueSummary * |
| 96 | getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) { |
| 97 | // If there is any strong definition anywhere, get it. |
| 98 | auto StrongDefForLinker = llvm::find_if( |
| 99 | GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) { |
| 100 | auto Linkage = Summary->linkage(); |
| 101 | return !GlobalValue::isAvailableExternallyLinkage(Linkage) && |
| 102 | !GlobalValue::isWeakForLinker(Linkage); |
| 103 | }); |
| 104 | if (StrongDefForLinker != GVSummaryList.end()) |
| 105 | return StrongDefForLinker->get(); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 106 | // Get the first *linker visible* definition for this global in the summary |
| 107 | // list. |
| 108 | auto FirstDefForLinker = llvm::find_if( |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 109 | GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) { |
| 110 | auto Linkage = Summary->linkage(); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 111 | return !GlobalValue::isAvailableExternallyLinkage(Linkage); |
| 112 | }); |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 113 | // Extern templates can be emitted as available_externally. |
| 114 | if (FirstDefForLinker == GVSummaryList.end()) |
| 115 | return nullptr; |
| 116 | return FirstDefForLinker->get(); |
Hans Wennborg | fa6e414 | 2016-04-02 01:03:41 +0000 | [diff] [blame] | 117 | } |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 118 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 119 | // Populate map of GUID to the prevailing copy for any multiply defined |
| 120 | // symbols. Currently assume first copy is prevailing, or any strong |
| 121 | // definition. Can be refined with Linker information in the future. |
| 122 | static void computePrevailingCopies( |
| 123 | const ModuleSummaryIndex &Index, |
| 124 | DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 125 | auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) { |
| 126 | return GVSummaryList.size() > 1; |
| 127 | }; |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 128 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 129 | for (auto &I : Index) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 130 | if (HasMultipleCopies(I.second.SummaryList)) |
| 131 | PrevailingCopy[I.first] = |
| 132 | getFirstDefinitionForLinker(I.second.SummaryList); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 133 | } |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 136 | static StringMap<MemoryBufferRef> |
Mehdi Amini | a0ddb1e | 2017-02-14 02:20:51 +0000 | [diff] [blame] | 137 | generateModuleMap(const std::vector<ThinLTOBuffer> &Modules) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 138 | StringMap<MemoryBufferRef> ModuleMap; |
| 139 | for (auto &ModuleBuffer : Modules) { |
| 140 | assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) == |
| 141 | ModuleMap.end() && |
| 142 | "Expect unique Buffer Identifier"); |
Mehdi Amini | a0ddb1e | 2017-02-14 02:20:51 +0000 | [diff] [blame] | 143 | ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer.getMemBuffer(); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 144 | } |
| 145 | return ModuleMap; |
| 146 | } |
| 147 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 148 | static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 149 | if (renameModuleForThinLTO(TheModule, Index)) |
| 150 | report_fatal_error("renameModuleForThinLTO failed"); |
| 151 | } |
| 152 | |
Adrian Prantl | 981a799 | 2017-05-20 00:00:08 +0000 | [diff] [blame] | 153 | namespace { |
| 154 | class ThinLTODiagnosticInfo : public DiagnosticInfo { |
| 155 | const Twine &Msg; |
| 156 | public: |
| 157 | ThinLTODiagnosticInfo(const Twine &DiagMsg, |
| 158 | DiagnosticSeverity Severity = DS_Error) |
| 159 | : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {} |
| 160 | void print(DiagnosticPrinter &DP) const override { DP << Msg; } |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | /// Verify the module and strip broken debug info. |
| 165 | static void verifyLoadedModule(Module &TheModule) { |
| 166 | bool BrokenDebugInfo = false; |
Adrian Prantl | a8b2ddb | 2017-10-02 18:31:29 +0000 | [diff] [blame] | 167 | if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo)) |
Adrian Prantl | 981a799 | 2017-05-20 00:00:08 +0000 | [diff] [blame] | 168 | report_fatal_error("Broken module found, compilation aborted!"); |
| 169 | if (BrokenDebugInfo) { |
| 170 | TheModule.getContext().diagnose(ThinLTODiagnosticInfo( |
| 171 | "Invalid debug info found, debug info will be stripped", DS_Warning)); |
| 172 | StripDebugInfo(TheModule); |
| 173 | } |
| 174 | } |
| 175 | |
Peter Collingbourne | dac43b4 | 2016-12-01 05:52:32 +0000 | [diff] [blame] | 176 | static std::unique_ptr<Module> |
| 177 | loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context, |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 178 | bool Lazy, bool IsImporting) { |
Peter Collingbourne | dac43b4 | 2016-12-01 05:52:32 +0000 | [diff] [blame] | 179 | SMDiagnostic Err; |
| 180 | Expected<std::unique_ptr<Module>> ModuleOrErr = |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 181 | Lazy |
| 182 | ? getLazyBitcodeModule(Buffer, Context, |
| 183 | /* ShouldLazyLoadMetadata */ true, IsImporting) |
| 184 | : parseBitcodeFile(Buffer, Context); |
Peter Collingbourne | dac43b4 | 2016-12-01 05:52:32 +0000 | [diff] [blame] | 185 | if (!ModuleOrErr) { |
| 186 | handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) { |
| 187 | SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(), |
| 188 | SourceMgr::DK_Error, EIB.message()); |
| 189 | Err.print("ThinLTO", errs()); |
| 190 | }); |
| 191 | report_fatal_error("Can't load module, abort."); |
| 192 | } |
Adrian Prantl | 981a799 | 2017-05-20 00:00:08 +0000 | [diff] [blame] | 193 | if (!Lazy) |
| 194 | verifyLoadedModule(*ModuleOrErr.get()); |
Peter Collingbourne | dac43b4 | 2016-12-01 05:52:32 +0000 | [diff] [blame] | 195 | return std::move(ModuleOrErr.get()); |
| 196 | } |
| 197 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 198 | static void |
| 199 | crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index, |
| 200 | StringMap<MemoryBufferRef> &ModuleMap, |
| 201 | const FunctionImporter::ImportMapTy &ImportList) { |
Peter Collingbourne | dac43b4 | 2016-12-01 05:52:32 +0000 | [diff] [blame] | 202 | auto Loader = [&](StringRef Identifier) { |
| 203 | return loadModuleFromBuffer(ModuleMap[Identifier], TheModule.getContext(), |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 204 | /*Lazy=*/true, /*IsImporting*/ true); |
Peter Collingbourne | dac43b4 | 2016-12-01 05:52:32 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 207 | FunctionImporter Importer(Index, Loader); |
Adrian Prantl | 6604379 | 2017-05-19 23:32:21 +0000 | [diff] [blame] | 208 | Expected<bool> Result = Importer.importFunctions(TheModule, ImportList); |
Mehdi Amini | 83a807e | 2017-01-08 00:30:27 +0000 | [diff] [blame] | 209 | if (!Result) { |
| 210 | handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) { |
| 211 | SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(), |
| 212 | SourceMgr::DK_Error, EIB.message()); |
| 213 | Err.print("ThinLTO", errs()); |
| 214 | }); |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 215 | report_fatal_error("importFunctions failed"); |
Mehdi Amini | 83a807e | 2017-01-08 00:30:27 +0000 | [diff] [blame] | 216 | } |
Adrian Prantl | 981a799 | 2017-05-20 00:00:08 +0000 | [diff] [blame] | 217 | // Verify again after cross-importing. |
| 218 | verifyLoadedModule(TheModule); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Mehdi Amini | cc7fbf7 | 2016-12-28 19:37:16 +0000 | [diff] [blame] | 221 | static void optimizeModule(Module &TheModule, TargetMachine &TM, |
Mehdi Amini | b5a46c1 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 222 | unsigned OptLevel, bool Freestanding) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 223 | // Populate the PassManager |
| 224 | PassManagerBuilder PMB; |
| 225 | PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple()); |
Mehdi Amini | b5a46c1 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 226 | if (Freestanding) |
| 227 | PMB.LibraryInfo->disableAllFunctions(); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 228 | PMB.Inliner = createFunctionInliningPass(); |
| 229 | // FIXME: should get it from the bitcode? |
Mehdi Amini | cc7fbf7 | 2016-12-28 19:37:16 +0000 | [diff] [blame] | 230 | PMB.OptLevel = OptLevel; |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 231 | PMB.LoopVectorize = true; |
| 232 | PMB.SLPVectorize = true; |
Adrian Prantl | 981a799 | 2017-05-20 00:00:08 +0000 | [diff] [blame] | 233 | // Already did this in verifyLoadedModule(). |
| 234 | PMB.VerifyInput = false; |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 235 | PMB.VerifyOutput = false; |
| 236 | |
| 237 | legacy::PassManager PM; |
| 238 | |
| 239 | // Add the TTI (required to inform the vectorizer about register size for |
| 240 | // instance) |
| 241 | PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis())); |
| 242 | |
| 243 | // Add optimizations |
| 244 | PMB.populateThinLTOPassManager(PM); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 245 | |
| 246 | PM.run(TheModule); |
| 247 | } |
| 248 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 249 | // Convert the PreservedSymbols map from "Name" based to "GUID" based. |
| 250 | static DenseSet<GlobalValue::GUID> |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 251 | computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols, |
| 252 | const Triple &TheTriple) { |
| 253 | DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size()); |
| 254 | for (auto &Entry : PreservedSymbols) { |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 255 | StringRef Name = Entry.first(); |
| 256 | if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_') |
| 257 | Name = Name.drop_front(); |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 258 | GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name)); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 259 | } |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 260 | return GUIDPreservedSymbols; |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 263 | std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule, |
| 264 | TargetMachine &TM) { |
| 265 | SmallVector<char, 128> OutputBuffer; |
| 266 | |
| 267 | // CodeGen |
| 268 | { |
| 269 | raw_svector_ostream OS(OutputBuffer); |
| 270 | legacy::PassManager PM; |
Mehdi Amini | 215d59e | 2016-04-01 08:22:59 +0000 | [diff] [blame] | 271 | |
| 272 | // If the bitcode files contain ARC code and were compiled with optimization, |
| 273 | // the ObjCARCContractPass must be run, so do it unconditionally here. |
| 274 | PM.add(createObjCARCContractPass()); |
| 275 | |
| 276 | // Setup the codegen now. |
Peter Collingbourne | 9a45114 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 277 | if (TM.addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile, |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 278 | /* DisableVerify */ true)) |
| 279 | report_fatal_error("Failed to setup codegen"); |
| 280 | |
| 281 | // Run codegen now. resulting binary is in OutputBuffer. |
| 282 | PM.run(TheModule); |
| 283 | } |
Weiming Zhao | 79f2d09 | 2018-04-16 03:44:03 +0000 | [diff] [blame] | 284 | return make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer)); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 287 | /// Manage caching for a single Module. |
| 288 | class ModuleCacheEntry { |
| 289 | SmallString<128> EntryPath; |
| 290 | |
| 291 | public: |
| 292 | // Create a cache entry. This compute a unique hash for the Module considering |
| 293 | // the current list of export/import, and offer an interface to query to |
| 294 | // access the content in the cache. |
| 295 | ModuleCacheEntry( |
| 296 | StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID, |
| 297 | const FunctionImporter::ImportMapTy &ImportList, |
| 298 | const FunctionImporter::ExportSetTy &ExportList, |
| 299 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 300 | const GVSummaryMapTy &DefinedFunctions, |
Mehdi Amini | c92b612 | 2017-01-10 00:55:47 +0000 | [diff] [blame] | 301 | const DenseSet<GlobalValue::GUID> &PreservedSymbols, unsigned OptLevel, |
Mehdi Amini | b5a46c1 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 302 | bool Freestanding, const TargetMachineBuilder &TMBuilder) { |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 303 | if (CachePath.empty()) |
| 304 | return; |
| 305 | |
Mehdi Amini | 00fa140 | 2016-10-08 04:44:18 +0000 | [diff] [blame] | 306 | if (!Index.modulePaths().count(ModuleID)) |
| 307 | // The module does not have an entry, it can't have a hash at all |
| 308 | return; |
| 309 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 310 | // Compute the unique hash for this entry |
| 311 | // This is based on the current compiler version, the module itself, the |
| 312 | // export list, the hash for every single module in the import list, the |
| 313 | // list of ResolvedODR for the module, and the list of preserved symbols. |
| 314 | |
Mehdi Amini | f82bda0 | 2016-10-08 04:44:23 +0000 | [diff] [blame] | 315 | // Include the hash for the current module |
| 316 | auto ModHash = Index.getModuleHash(ModuleID); |
| 317 | |
| 318 | if (all_of(ModHash, [](uint32_t V) { return V == 0; })) |
| 319 | // No hash entry, no caching! |
| 320 | return; |
| 321 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 322 | SHA1 Hasher; |
| 323 | |
Mehdi Amini | c92b612 | 2017-01-10 00:55:47 +0000 | [diff] [blame] | 324 | // Include the parts of the LTO configuration that affect code generation. |
| 325 | auto AddString = [&](StringRef Str) { |
| 326 | Hasher.update(Str); |
| 327 | Hasher.update(ArrayRef<uint8_t>{0}); |
| 328 | }; |
| 329 | auto AddUnsigned = [&](unsigned I) { |
| 330 | uint8_t Data[4]; |
| 331 | Data[0] = I; |
| 332 | Data[1] = I >> 8; |
| 333 | Data[2] = I >> 16; |
| 334 | Data[3] = I >> 24; |
| 335 | Hasher.update(ArrayRef<uint8_t>{Data, 4}); |
| 336 | }; |
| 337 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 338 | // Start with the compiler revision |
| 339 | Hasher.update(LLVM_VERSION_STRING); |
Peter Collingbourne | 942fa56 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 340 | #ifdef LLVM_REVISION |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 341 | Hasher.update(LLVM_REVISION); |
| 342 | #endif |
| 343 | |
Mehdi Amini | c92b612 | 2017-01-10 00:55:47 +0000 | [diff] [blame] | 344 | // Hash the optimization level and the target machine settings. |
| 345 | AddString(TMBuilder.MCpu); |
| 346 | // FIXME: Hash more of Options. For now all clients initialize Options from |
| 347 | // command-line flags (which is unsupported in production), but may set |
| 348 | // RelaxELFRelocations. The clang driver can also pass FunctionSections, |
| 349 | // DataSections and DebuggerTuning via command line flags. |
| 350 | AddUnsigned(TMBuilder.Options.RelaxELFRelocations); |
| 351 | AddUnsigned(TMBuilder.Options.FunctionSections); |
| 352 | AddUnsigned(TMBuilder.Options.DataSections); |
| 353 | AddUnsigned((unsigned)TMBuilder.Options.DebuggerTuning); |
| 354 | AddString(TMBuilder.MAttr); |
| 355 | if (TMBuilder.RelocModel) |
| 356 | AddUnsigned(*TMBuilder.RelocModel); |
| 357 | AddUnsigned(TMBuilder.CGOptLevel); |
| 358 | AddUnsigned(OptLevel); |
Mehdi Amini | b5a46c1 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 359 | AddUnsigned(Freestanding); |
Mehdi Amini | c92b612 | 2017-01-10 00:55:47 +0000 | [diff] [blame] | 360 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 361 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash))); |
| 362 | for (auto F : ExportList) |
| 363 | // The export list can impact the internalization, be conservative here |
| 364 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F))); |
| 365 | |
| 366 | // Include the hash for every module we import functions from |
| 367 | for (auto &Entry : ImportList) { |
| 368 | auto ModHash = Index.getModuleHash(Entry.first()); |
| 369 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash))); |
| 370 | } |
| 371 | |
| 372 | // Include the hash for the resolved ODR. |
| 373 | for (auto &Entry : ResolvedODR) { |
Sjoerd Meijer | 41beee6 | 2016-04-27 18:35:02 +0000 | [diff] [blame] | 374 | Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first, |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 375 | sizeof(GlobalValue::GUID))); |
Sjoerd Meijer | 41beee6 | 2016-04-27 18:35:02 +0000 | [diff] [blame] | 376 | Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second, |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 377 | sizeof(GlobalValue::LinkageTypes))); |
| 378 | } |
| 379 | |
| 380 | // Include the hash for the preserved symbols. |
| 381 | for (auto &Entry : PreservedSymbols) { |
| 382 | if (DefinedFunctions.count(Entry)) |
| 383 | Hasher.update( |
Sjoerd Meijer | 41beee6 | 2016-04-27 18:35:02 +0000 | [diff] [blame] | 384 | ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID))); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Peter Collingbourne | 25a17ba | 2017-03-20 16:41:57 +0000 | [diff] [blame] | 387 | // This choice of file name allows the cache to be pruned (see pruneCache() |
| 388 | // in include/llvm/Support/CachePruning.h). |
| 389 | sys::path::append(EntryPath, CachePath, |
| 390 | "llvmcache-" + toHex(Hasher.result())); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 393 | // Access the path to this entry in the cache. |
| 394 | StringRef getEntryPath() { return EntryPath; } |
| 395 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 396 | // Try loading the buffer for this cache entry. |
| 397 | ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() { |
| 398 | if (EntryPath.empty()) |
| 399 | return std::error_code(); |
Andrew Ng | 089303d | 2018-07-04 14:17:10 +0000 | [diff] [blame] | 400 | int FD; |
| 401 | SmallString<64> ResultPath; |
| 402 | std::error_code EC = sys::fs::openFileForRead( |
| 403 | Twine(EntryPath), FD, sys::fs::OF_UpdateAtime, &ResultPath); |
| 404 | if (EC) |
| 405 | return EC; |
| 406 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
| 407 | MemoryBuffer::getOpenFile(FD, EntryPath, |
| 408 | /*FileSize*/ -1, |
| 409 | /*RequiresNullTerminator*/ false); |
| 410 | close(FD); |
| 411 | return MBOrErr; |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // Cache the Produced object file |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 415 | void write(const MemoryBuffer &OutputBuffer) { |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 416 | if (EntryPath.empty()) |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 417 | return; |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 418 | |
| 419 | // Write to a temporary to avoid race condition |
| 420 | SmallString<128> TempFilename; |
Ekaterina Romanova | 0b01dfb | 2018-03-30 21:35:42 +0000 | [diff] [blame] | 421 | SmallString<128> CachePath(EntryPath); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 422 | int TempFD; |
Ekaterina Romanova | 0b01dfb | 2018-03-30 21:35:42 +0000 | [diff] [blame] | 423 | llvm::sys::path::remove_filename(CachePath); |
| 424 | sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 425 | std::error_code EC = |
Ekaterina Romanova | 0b01dfb | 2018-03-30 21:35:42 +0000 | [diff] [blame] | 426 | sys::fs::createUniqueFile(TempFilename, TempFD, TempFilename); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 427 | if (EC) { |
| 428 | errs() << "Error: " << EC.message() << "\n"; |
| 429 | report_fatal_error("ThinLTO: Can't get a temporary file"); |
| 430 | } |
| 431 | { |
| 432 | raw_fd_ostream OS(TempFD, /* ShouldClose */ true); |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 433 | OS << OutputBuffer.getBuffer(); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 434 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 435 | // Rename temp file to final destination; rename is atomic |
Mehdi Amini | 2a16a5f | 2016-05-14 04:58:38 +0000 | [diff] [blame] | 436 | EC = sys::fs::rename(TempFilename, EntryPath); |
Ekaterina Romanova | 0b01dfb | 2018-03-30 21:35:42 +0000 | [diff] [blame] | 437 | if (EC) |
Mehdi Amini | b02139d | 2016-05-14 05:16:35 +0000 | [diff] [blame] | 438 | sys::fs::remove(TempFilename); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 439 | } |
| 440 | }; |
| 441 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 442 | static std::unique_ptr<MemoryBuffer> |
| 443 | ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index, |
| 444 | StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM, |
| 445 | const FunctionImporter::ImportMapTy &ImportList, |
| 446 | const FunctionImporter::ExportSetTy &ExportList, |
| 447 | const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, |
| 448 | const GVSummaryMapTy &DefinedGlobals, |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 449 | const ThinLTOCodeGenerator::CachingOptions &CacheOptions, |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 450 | bool DisableCodeGen, StringRef SaveTempsDir, |
Mehdi Amini | b5a46c1 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 451 | bool Freestanding, unsigned OptLevel, unsigned count) { |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 452 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 453 | // "Benchmark"-like optimization: single-source case |
| 454 | bool SingleModule = (ModuleMap.size() == 1); |
| 455 | |
| 456 | if (!SingleModule) { |
| 457 | promoteModule(TheModule, Index); |
| 458 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 459 | // Apply summary-based LinkOnce/Weak resolution decisions. |
| 460 | thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 461 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 462 | // Save temps: after promotion. |
Mehdi Amini | 4b300e0a | 2016-05-05 05:14:16 +0000 | [diff] [blame] | 463 | saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc"); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 464 | } |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 465 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 466 | // Be friendly and don't nuke totally the module when the client didn't |
| 467 | // supply anything to preserve. |
| 468 | if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) { |
| 469 | // Apply summary-based internalization decisions. |
| 470 | thinLTOInternalizeModule(TheModule, DefinedGlobals); |
| 471 | } |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 472 | |
| 473 | // Save internalized bitcode |
Mehdi Amini | 4b300e0a | 2016-05-05 05:14:16 +0000 | [diff] [blame] | 474 | saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc"); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 475 | |
| 476 | if (!SingleModule) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 477 | crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 478 | |
| 479 | // Save temps: after cross-module import. |
Mehdi Amini | 4b300e0a | 2016-05-05 05:14:16 +0000 | [diff] [blame] | 480 | saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc"); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Mehdi Amini | b5a46c1 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 483 | optimizeModule(TheModule, TM, OptLevel, Freestanding); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 484 | |
Mehdi Amini | 4b300e0a | 2016-05-05 05:14:16 +0000 | [diff] [blame] | 485 | saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc"); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 486 | |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 487 | if (DisableCodeGen) { |
| 488 | // Configured to stop before CodeGen, serialize the bitcode and return. |
| 489 | SmallVector<char, 128> OutputBuffer; |
| 490 | { |
| 491 | raw_svector_ostream OS(OutputBuffer); |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 492 | ProfileSummaryInfo PSI(TheModule); |
Teresa Johnson | 94624ac | 2017-05-10 18:52:16 +0000 | [diff] [blame] | 493 | auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI); |
Rafael Espindola | 6a86e25 | 2018-02-14 19:11:32 +0000 | [diff] [blame] | 494 | WriteBitcodeToFile(TheModule, OS, true, &Index); |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 495 | } |
Weiming Zhao | 79f2d09 | 2018-04-16 03:44:03 +0000 | [diff] [blame] | 496 | return make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer)); |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 499 | return codegenModule(TheModule, TM); |
| 500 | } |
| 501 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 502 | /// Resolve LinkOnce/Weak symbols. Record resolutions in the \p ResolvedODR map |
| 503 | /// for caching, and in the \p Index for application during the ThinLTO |
| 504 | /// backends. This is needed for correctness for exported symbols (ensure |
| 505 | /// at least one copy kept) and a compile-time optimization (to drop duplicate |
| 506 | /// copies when possible). |
| 507 | static void resolveWeakForLinkerInIndex( |
| 508 | ModuleSummaryIndex &Index, |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 509 | StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> |
| 510 | &ResolvedODR) { |
| 511 | |
| 512 | DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy; |
| 513 | computePrevailingCopies(Index, PrevailingCopy); |
| 514 | |
| 515 | auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) { |
| 516 | const auto &Prevailing = PrevailingCopy.find(GUID); |
| 517 | // Not in map means that there was only one copy, which must be prevailing. |
| 518 | if (Prevailing == PrevailingCopy.end()) |
| 519 | return true; |
| 520 | return Prevailing->second == S; |
| 521 | }; |
| 522 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 523 | auto recordNewLinkage = [&](StringRef ModuleIdentifier, |
| 524 | GlobalValue::GUID GUID, |
| 525 | GlobalValue::LinkageTypes NewLinkage) { |
| 526 | ResolvedODR[ModuleIdentifier][GUID] = NewLinkage; |
| 527 | }; |
| 528 | |
Peter Collingbourne | 73589f3 | 2016-07-07 18:31:51 +0000 | [diff] [blame] | 529 | thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, recordNewLinkage); |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 532 | // Initialize the TargetMachine builder for a given Triple |
| 533 | static void initTMBuilder(TargetMachineBuilder &TMBuilder, |
| 534 | const Triple &TheTriple) { |
| 535 | // Set a default CPU for Darwin triples (copied from LTOCodeGenerator). |
| 536 | // FIXME this looks pretty terrible... |
| 537 | if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) { |
| 538 | if (TheTriple.getArch() == llvm::Triple::x86_64) |
| 539 | TMBuilder.MCpu = "core2"; |
| 540 | else if (TheTriple.getArch() == llvm::Triple::x86) |
| 541 | TMBuilder.MCpu = "yonah"; |
| 542 | else if (TheTriple.getArch() == llvm::Triple::aarch64) |
| 543 | TMBuilder.MCpu = "cyclone"; |
| 544 | } |
| 545 | TMBuilder.TheTriple = std::move(TheTriple); |
| 546 | } |
| 547 | |
| 548 | } // end anonymous namespace |
| 549 | |
| 550 | void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) { |
Johan Engelen | dcebe4f | 2017-09-17 18:11:26 +0000 | [diff] [blame] | 551 | ThinLTOBuffer Buffer(Data, Identifier); |
Akira Hatanaka | b10bff1 | 2017-05-18 03:52:29 +0000 | [diff] [blame] | 552 | LLVMContext Context; |
| 553 | StringRef TripleStr; |
| 554 | ErrorOr<std::string> TripleOrErr = expectedToErrorOrAndEmitErrors( |
| 555 | Context, getBitcodeTargetTriple(Buffer.getMemBuffer())); |
| 556 | |
| 557 | if (TripleOrErr) |
| 558 | TripleStr = *TripleOrErr; |
| 559 | |
| 560 | Triple TheTriple(TripleStr); |
| 561 | |
| 562 | if (Modules.empty()) |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 563 | initTMBuilder(TMBuilder, Triple(TheTriple)); |
Akira Hatanaka | b10bff1 | 2017-05-18 03:52:29 +0000 | [diff] [blame] | 564 | else if (TMBuilder.TheTriple != TheTriple) { |
| 565 | if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple)) |
| 566 | report_fatal_error("ThinLTO modules with incompatible triples not " |
| 567 | "supported"); |
| 568 | initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple))); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 569 | } |
Akira Hatanaka | b10bff1 | 2017-05-18 03:52:29 +0000 | [diff] [blame] | 570 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 571 | Modules.push_back(Buffer); |
| 572 | } |
| 573 | |
| 574 | void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) { |
| 575 | PreservedSymbols.insert(Name); |
| 576 | } |
| 577 | |
| 578 | void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) { |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 579 | // FIXME: At the moment, we don't take advantage of this extra information, |
| 580 | // we're conservatively considering cross-references as preserved. |
| 581 | // CrossReferencedSymbols.insert(Name); |
| 582 | PreservedSymbols.insert(Name); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | // TargetMachine factory |
| 586 | std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const { |
| 587 | std::string ErrMsg; |
| 588 | const Target *TheTarget = |
| 589 | TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg); |
| 590 | if (!TheTarget) { |
| 591 | report_fatal_error("Can't load target for this Triple: " + ErrMsg); |
| 592 | } |
| 593 | |
| 594 | // Use MAttr as the default set of features. |
| 595 | SubtargetFeatures Features(MAttr); |
| 596 | Features.getDefaultSubtargetFeatures(TheTriple); |
| 597 | std::string FeatureStr = Features.getString(); |
Mehdi Amini | cc7fbf7 | 2016-12-28 19:37:16 +0000 | [diff] [blame] | 598 | |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 599 | return std::unique_ptr<TargetMachine>( |
| 600 | TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options, |
| 601 | RelocModel, None, CGOptLevel)); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /** |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 605 | * Produce the combined summary index from all the bitcode files: |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 606 | * "thin-link". |
| 607 | */ |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 608 | std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() { |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 609 | std::unique_ptr<ModuleSummaryIndex> CombinedIndex = |
Teresa Johnson | 4ffc3e7 | 2018-06-06 22:22:01 +0000 | [diff] [blame] | 610 | llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 611 | uint64_t NextModuleId = 0; |
| 612 | for (auto &ModuleBuffer : Modules) { |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 613 | if (Error Err = readModuleSummaryIndex(ModuleBuffer.getMemBuffer(), |
| 614 | *CombinedIndex, NextModuleId++)) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 615 | // FIXME diagnose |
Peter Collingbourne | 6de481a | 2016-11-11 19:50:39 +0000 | [diff] [blame] | 616 | logAllUnhandledErrors( |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 617 | std::move(Err), errs(), |
Peter Collingbourne | c15d60b | 2017-05-01 20:42:32 +0000 | [diff] [blame] | 618 | "error: can't create module summary index for buffer: "); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 619 | return nullptr; |
| 620 | } |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 621 | } |
| 622 | return CombinedIndex; |
| 623 | } |
| 624 | |
George Rimar | 2421b6f | 2018-01-17 10:33:05 +0000 | [diff] [blame] | 625 | static void internalizeAndPromoteInIndex( |
| 626 | const StringMap<FunctionImporter::ExportSetTy> &ExportLists, |
| 627 | const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, |
| 628 | ModuleSummaryIndex &Index) { |
| 629 | auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) { |
| 630 | const auto &ExportList = ExportLists.find(ModuleIdentifier); |
| 631 | return (ExportList != ExportLists.end() && |
| 632 | ExportList->second.count(GUID)) || |
| 633 | GUIDPreservedSymbols.count(GUID); |
| 634 | }; |
| 635 | |
| 636 | thinLTOInternalizeAndPromoteInIndex(Index, isExported); |
| 637 | } |
| 638 | |
George Rimar | eaf5172 | 2018-01-29 08:03:30 +0000 | [diff] [blame] | 639 | static void computeDeadSymbolsInIndex( |
| 640 | ModuleSummaryIndex &Index, |
| 641 | const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) { |
| 642 | // We have no symbols resolution available. And can't do any better now in the |
| 643 | // case where the prevailing symbol is in a native object. It can be refined |
| 644 | // with linker information in the future. |
| 645 | auto isPrevailing = [&](GlobalValue::GUID G) { |
| 646 | return PrevailingType::Unknown; |
| 647 | }; |
| 648 | computeDeadSymbols(Index, GUIDPreservedSymbols, isPrevailing); |
| 649 | } |
| 650 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 651 | /** |
| 652 | * Perform promotion and renaming of exported internal functions. |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 653 | * Index is updated to reflect linkage changes from weak resolution. |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 654 | */ |
| 655 | void ThinLTOCodeGenerator::promote(Module &TheModule, |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 656 | ModuleSummaryIndex &Index) { |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 657 | auto ModuleCount = Index.modulePaths().size(); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 658 | auto ModuleIdentifier = TheModule.getModuleIdentifier(); |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 659 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 660 | // Collect for each module the list of function it defines (GUID -> Summary). |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 661 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries; |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 662 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 663 | |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 664 | // Convert the preserved symbols set from string to GUID |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 665 | auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 666 | PreservedSymbols, Triple(TheModule.getTargetTriple())); |
| 667 | |
| 668 | // Compute "dead" symbols, we don't want to import/export these! |
George Rimar | eaf5172 | 2018-01-29 08:03:30 +0000 | [diff] [blame] | 669 | computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 670 | |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 671 | // Generate import/export list |
| 672 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 673 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 674 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
Evgeniy Stepanov | 56584bb | 2017-06-01 20:30:06 +0000 | [diff] [blame] | 675 | ExportLists); |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 676 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 677 | // Resolve LinkOnce/Weak symbols. |
| 678 | StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; |
Peter Collingbourne | 73589f3 | 2016-07-07 18:31:51 +0000 | [diff] [blame] | 679 | resolveWeakForLinkerInIndex(Index, ResolvedODR); |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 680 | |
| 681 | thinLTOResolveWeakForLinkerModule( |
| 682 | TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]); |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 683 | |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 684 | // Promote the exported values in the index, so that they are promoted |
| 685 | // in the module. |
George Rimar | 2421b6f | 2018-01-17 10:33:05 +0000 | [diff] [blame] | 686 | internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index); |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 687 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 688 | promoteModule(TheModule, Index); |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * Perform cross-module importing for the module identified by ModuleIdentifier. |
| 693 | */ |
| 694 | void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule, |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 695 | ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 696 | auto ModuleMap = generateModuleMap(Modules); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 697 | auto ModuleCount = Index.modulePaths().size(); |
| 698 | |
| 699 | // Collect for each module the list of function it defines (GUID -> Summary). |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 700 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 701 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 702 | |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 703 | // Convert the preserved symbols set from string to GUID |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 704 | auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 705 | PreservedSymbols, Triple(TheModule.getTargetTriple())); |
| 706 | |
| 707 | // Compute "dead" symbols, we don't want to import/export these! |
George Rimar | eaf5172 | 2018-01-29 08:03:30 +0000 | [diff] [blame] | 708 | computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 709 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 710 | // Generate import/export list |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 711 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 712 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 713 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
Evgeniy Stepanov | 56584bb | 2017-06-01 20:30:06 +0000 | [diff] [blame] | 714 | ExportLists); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 715 | auto &ImportList = ImportLists[TheModule.getModuleIdentifier()]; |
| 716 | |
| 717 | crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /** |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 721 | * Compute the list of summaries needed for importing into module. |
| 722 | */ |
| 723 | void ThinLTOCodeGenerator::gatherImportedSummariesForModule( |
| 724 | StringRef ModulePath, ModuleSummaryIndex &Index, |
| 725 | std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) { |
| 726 | auto ModuleCount = Index.modulePaths().size(); |
| 727 | |
| 728 | // Collect for each module the list of function it defines (GUID -> Summary). |
| 729 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
| 730 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
| 731 | |
| 732 | // Generate import/export list |
| 733 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 734 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 735 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
| 736 | ExportLists); |
| 737 | |
| 738 | llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries, |
Mehdi Amini | cdbcbf7 | 2016-08-16 05:46:05 +0000 | [diff] [blame] | 739 | ImportLists[ModulePath], |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 740 | ModuleToSummariesForIndex); |
| 741 | } |
| 742 | |
| 743 | /** |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 744 | * Emit the list of files needed for importing into module. |
| 745 | */ |
| 746 | void ThinLTOCodeGenerator::emitImports(StringRef ModulePath, |
| 747 | StringRef OutputName, |
| 748 | ModuleSummaryIndex &Index) { |
| 749 | auto ModuleCount = Index.modulePaths().size(); |
| 750 | |
| 751 | // Collect for each module the list of function it defines (GUID -> Summary). |
| 752 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
| 753 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
| 754 | |
| 755 | // Generate import/export list |
| 756 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 757 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 758 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
| 759 | ExportLists); |
| 760 | |
Teresa Johnson | c0320ef | 2018-07-10 20:06:04 +0000 | [diff] [blame] | 761 | std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex; |
| 762 | llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries, |
| 763 | ImportLists[ModulePath], |
| 764 | ModuleToSummariesForIndex); |
| 765 | |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 766 | std::error_code EC; |
Teresa Johnson | c0320ef | 2018-07-10 20:06:04 +0000 | [diff] [blame] | 767 | if ((EC = |
| 768 | EmitImportsFiles(ModulePath, OutputName, ModuleToSummariesForIndex))) |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 769 | report_fatal_error(Twine("Failed to open ") + OutputName + |
| 770 | " to save imports lists\n"); |
| 771 | } |
| 772 | |
| 773 | /** |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 774 | * Perform internalization. Index is updated to reflect linkage changes. |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 775 | */ |
| 776 | void ThinLTOCodeGenerator::internalize(Module &TheModule, |
| 777 | ModuleSummaryIndex &Index) { |
| 778 | initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); |
| 779 | auto ModuleCount = Index.modulePaths().size(); |
| 780 | auto ModuleIdentifier = TheModule.getModuleIdentifier(); |
| 781 | |
| 782 | // Convert the preserved symbols set from string to GUID |
| 783 | auto GUIDPreservedSymbols = |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 784 | computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 785 | |
| 786 | // Collect for each module the list of function it defines (GUID -> Summary). |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 787 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 788 | Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
| 789 | |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 790 | // Compute "dead" symbols, we don't want to import/export these! |
George Rimar | eaf5172 | 2018-01-29 08:03:30 +0000 | [diff] [blame] | 791 | computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 792 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 793 | // Generate import/export list |
| 794 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 795 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 796 | ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, |
Evgeniy Stepanov | 56584bb | 2017-06-01 20:30:06 +0000 | [diff] [blame] | 797 | ExportLists); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 798 | auto &ExportList = ExportLists[ModuleIdentifier]; |
| 799 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 800 | // Be friendly and don't nuke totally the module when the client didn't |
| 801 | // supply anything to preserve. |
| 802 | if (ExportList.empty() && GUIDPreservedSymbols.empty()) |
| 803 | return; |
| 804 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 805 | // Internalization |
George Rimar | 2421b6f | 2018-01-17 10:33:05 +0000 | [diff] [blame] | 806 | internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index); |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 807 | thinLTOInternalizeModule(TheModule, |
| 808 | ModuleToDefinedGVSummaries[ModuleIdentifier]); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /** |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 812 | * Perform post-importing ThinLTO optimizations. |
| 813 | */ |
| 814 | void ThinLTOCodeGenerator::optimize(Module &TheModule) { |
| 815 | initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 816 | |
| 817 | // Optimize now |
Mehdi Amini | b5a46c1 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 818 | optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 821 | /// Write out the generated object file, either from CacheEntryPath or from |
| 822 | /// OutputBuffer, preferring hard-link when possible. |
| 823 | /// Returns the path to the generated file in SavedObjectsDirectoryPath. |
| 824 | static std::string writeGeneratedObject(int count, StringRef CacheEntryPath, |
| 825 | StringRef SavedObjectsDirectoryPath, |
| 826 | const MemoryBuffer &OutputBuffer) { |
| 827 | SmallString<128> OutputPath(SavedObjectsDirectoryPath); |
| 828 | llvm::sys::path::append(OutputPath, Twine(count) + ".thinlto.o"); |
| 829 | OutputPath.c_str(); // Ensure the string is null terminated. |
| 830 | if (sys::fs::exists(OutputPath)) |
| 831 | sys::fs::remove(OutputPath); |
| 832 | |
| 833 | // We don't return a memory buffer to the linker, just a list of files. |
| 834 | if (!CacheEntryPath.empty()) { |
| 835 | // Cache is enabled, hard-link the entry (or copy if hard-link fails). |
| 836 | auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath); |
| 837 | if (!Err) |
| 838 | return OutputPath.str(); |
| 839 | // Hard linking failed, try to copy. |
| 840 | Err = sys::fs::copy_file(CacheEntryPath, OutputPath); |
| 841 | if (!Err) |
| 842 | return OutputPath.str(); |
| 843 | // Copy failed (could be because the CacheEntry was removed from the cache |
| 844 | // in the meantime by another process), fall back and try to write down the |
| 845 | // buffer to the output. |
| 846 | errs() << "error: can't link or copy from cached entry '" << CacheEntryPath |
| 847 | << "' to '" << OutputPath << "'\n"; |
| 848 | } |
| 849 | // No cache entry, just write out the buffer. |
| 850 | std::error_code Err; |
| 851 | raw_fd_ostream OS(OutputPath, Err, sys::fs::F_None); |
| 852 | if (Err) |
| 853 | report_fatal_error("Can't open output '" + OutputPath + "'\n"); |
| 854 | OS << OutputBuffer.getBuffer(); |
| 855 | return OutputPath.str(); |
| 856 | } |
| 857 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 858 | // Main entry point for the ThinLTO processing |
| 859 | void ThinLTOCodeGenerator::run() { |
Mehdi Amini | b299046 | 2017-01-20 22:45:34 +0000 | [diff] [blame] | 860 | // Prepare the resulting object vector |
| 861 | assert(ProducedBinaries.empty() && "The generator should not be reused"); |
| 862 | if (SavedObjectsDirectoryPath.empty()) |
| 863 | ProducedBinaries.resize(Modules.size()); |
| 864 | else { |
| 865 | sys::fs::create_directories(SavedObjectsDirectoryPath); |
| 866 | bool IsDir; |
| 867 | sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir); |
| 868 | if (!IsDir) |
| 869 | report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'"); |
| 870 | ProducedBinaryFiles.resize(Modules.size()); |
| 871 | } |
| 872 | |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 873 | if (CodeGenOnly) { |
| 874 | // Perform only parallel codegen and return. |
| 875 | ThreadPool Pool; |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 876 | int count = 0; |
| 877 | for (auto &ModuleBuffer : Modules) { |
| 878 | Pool.async([&](int count) { |
| 879 | LLVMContext Context; |
| 880 | Context.setDiscardValueNames(LTODiscardValueNames); |
| 881 | |
| 882 | // Parse module now |
Mehdi Amini | a0ddb1e | 2017-02-14 02:20:51 +0000 | [diff] [blame] | 883 | auto TheModule = |
| 884 | loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false, |
| 885 | /*IsImporting*/ false); |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 886 | |
| 887 | // CodeGen |
Steven Wu | cf90203 | 2018-09-04 22:54:17 +0000 | [diff] [blame] | 888 | auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create()); |
Mehdi Amini | b299046 | 2017-01-20 22:45:34 +0000 | [diff] [blame] | 889 | if (SavedObjectsDirectoryPath.empty()) |
| 890 | ProducedBinaries[count] = std::move(OutputBuffer); |
| 891 | else |
| 892 | ProducedBinaryFiles[count] = writeGeneratedObject( |
| 893 | count, "", SavedObjectsDirectoryPath, *OutputBuffer); |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 894 | }, count++); |
| 895 | } |
| 896 | |
| 897 | return; |
| 898 | } |
| 899 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 900 | // Sequential linking phase |
| 901 | auto Index = linkCombinedIndex(); |
| 902 | |
| 903 | // Save temps: index. |
| 904 | if (!SaveTempsDir.empty()) { |
| 905 | auto SaveTempPath = SaveTempsDir + "index.bc"; |
| 906 | std::error_code EC; |
| 907 | raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None); |
| 908 | if (EC) |
| 909 | report_fatal_error(Twine("Failed to open ") + SaveTempPath + |
| 910 | " to save optimized bitcode\n"); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 911 | WriteIndexToFile(*Index, OS); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 914 | |
| 915 | // Prepare the module map. |
| 916 | auto ModuleMap = generateModuleMap(Modules); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 917 | auto ModuleCount = Modules.size(); |
| 918 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 919 | // Collect for each module the list of function it defines (GUID -> Summary). |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 920 | StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 921 | Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); |
| 922 | |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 923 | // Convert the preserved symbols set from string to GUID, this is needed for |
| 924 | // computing the caching hash and the internalization. |
| 925 | auto GUIDPreservedSymbols = |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 926 | computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 927 | |
| 928 | // Compute "dead" symbols, we don't want to import/export these! |
George Rimar | eaf5172 | 2018-01-29 08:03:30 +0000 | [diff] [blame] | 929 | computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols); |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 930 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 931 | // Collect the import/export lists for all modules from the call-graph in the |
| 932 | // combined index. |
| 933 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 934 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 935 | ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists, |
Evgeniy Stepanov | 56584bb | 2017-06-01 20:30:06 +0000 | [diff] [blame] | 936 | ExportLists); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 937 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 938 | // We use a std::map here to be able to have a defined ordering when |
| 939 | // producing a hash for the cache entry. |
| 940 | // FIXME: we should be able to compute the caching hash for the entry based |
| 941 | // on the index, and nuke this map. |
| 942 | StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; |
| 943 | |
| 944 | // Resolve LinkOnce/Weak symbols, this has to be computed early because it |
| 945 | // impacts the caching. |
Peter Collingbourne | 73589f3 | 2016-07-07 18:31:51 +0000 | [diff] [blame] | 946 | resolveWeakForLinkerInIndex(*Index, ResolvedODR); |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 947 | |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 948 | // Use global summary-based analysis to identify symbols that can be |
| 949 | // internalized (because they aren't exported or preserved as per callback). |
| 950 | // Changes are made in the index, consumed in the ThinLTO backends. |
George Rimar | 2421b6f | 2018-01-17 10:33:05 +0000 | [diff] [blame] | 951 | internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, *Index); |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 952 | |
Steven Wu | ec53c89 | 2018-09-14 19:38:21 +0000 | [diff] [blame^] | 953 | // Make sure that every module has an entry in the ExportLists, ImportList, |
| 954 | // GVSummary and ResolvedODR maps to enable threaded access to these maps |
| 955 | // below. |
| 956 | for (auto &Module : Modules) { |
| 957 | auto ModuleIdentifier = Module.getBufferIdentifier(); |
| 958 | ExportLists[ModuleIdentifier]; |
| 959 | ImportLists[ModuleIdentifier]; |
| 960 | ResolvedODR[ModuleIdentifier]; |
| 961 | ModuleToDefinedGVSummaries[ModuleIdentifier]; |
Teresa Johnson | 141149f | 2016-05-24 18:44:01 +0000 | [diff] [blame] | 962 | } |
Mehdi Amini | af52f28 | 2016-05-15 05:49:47 +0000 | [diff] [blame] | 963 | |
Mehdi Amini | 819e9cd | 2016-05-16 19:33:07 +0000 | [diff] [blame] | 964 | // Compute the ordering we will process the inputs: the rough heuristic here |
| 965 | // is to sort them per size so that the largest module get schedule as soon as |
| 966 | // possible. This is purely a compile-time optimization. |
| 967 | std::vector<int> ModulesOrdering; |
| 968 | ModulesOrdering.resize(Modules.size()); |
| 969 | std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0); |
Mandeep Singh Grang | 5416af7 | 2018-04-13 19:12:20 +0000 | [diff] [blame] | 970 | llvm::sort(ModulesOrdering.begin(), ModulesOrdering.end(), |
| 971 | [&](int LeftIndex, int RightIndex) { |
| 972 | auto LSize = Modules[LeftIndex].getBuffer().size(); |
| 973 | auto RSize = Modules[RightIndex].getBuffer().size(); |
| 974 | return LSize > RSize; |
| 975 | }); |
Mehdi Amini | 819e9cd | 2016-05-16 19:33:07 +0000 | [diff] [blame] | 976 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 977 | // Parallel optimizer + codegen |
| 978 | { |
| 979 | ThreadPool Pool(ThreadCount); |
Mehdi Amini | 819e9cd | 2016-05-16 19:33:07 +0000 | [diff] [blame] | 980 | for (auto IndexCount : ModulesOrdering) { |
| 981 | auto &ModuleBuffer = Modules[IndexCount]; |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 982 | Pool.async([&](int count) { |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 983 | auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier(); |
Mehdi Amini | a71a5a6 | 2016-04-21 05:47:17 +0000 | [diff] [blame] | 984 | auto &ExportList = ExportLists[ModuleIdentifier]; |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 985 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 986 | auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier]; |
| 987 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 988 | // The module may be cached, this helps handling it. |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 989 | ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier, |
| 990 | ImportLists[ModuleIdentifier], ExportList, |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 991 | ResolvedODR[ModuleIdentifier], |
Mehdi Amini | c92b612 | 2017-01-10 00:55:47 +0000 | [diff] [blame] | 992 | DefinedFunctions, GUIDPreservedSymbols, |
Mehdi Amini | b5a46c1 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 993 | OptLevel, Freestanding, TMBuilder); |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 994 | auto CacheEntryPath = CacheEntry.getEntryPath(); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 995 | |
| 996 | { |
| 997 | auto ErrOrBuffer = CacheEntry.tryLoadingBuffer(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 998 | LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") |
| 999 | << " '" << CacheEntryPath << "' for buffer " |
| 1000 | << count << " " << ModuleIdentifier << "\n"); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 1001 | |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 1002 | if (ErrOrBuffer) { |
| 1003 | // Cache Hit! |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 1004 | if (SavedObjectsDirectoryPath.empty()) |
| 1005 | ProducedBinaries[count] = std::move(ErrOrBuffer.get()); |
| 1006 | else |
| 1007 | ProducedBinaryFiles[count] = writeGeneratedObject( |
| 1008 | count, CacheEntryPath, SavedObjectsDirectoryPath, |
| 1009 | *ErrOrBuffer.get()); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 1010 | return; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | LLVMContext Context; |
| 1015 | Context.setDiscardValueNames(LTODiscardValueNames); |
| 1016 | Context.enableDebugTypeODRUniquing(); |
Davide Italiano | 690ed9d | 2017-02-10 23:49:38 +0000 | [diff] [blame] | 1017 | auto DiagFileOrErr = lto::setupOptimizationRemarks( |
Bob Haarman | fb2d342 | 2018-03-08 01:13:10 +0000 | [diff] [blame] | 1018 | Context, LTORemarksFilename, LTOPassRemarksWithHotness, count); |
Mehdi Amini | 19f176b | 2016-11-19 18:20:05 +0000 | [diff] [blame] | 1019 | if (!DiagFileOrErr) { |
| 1020 | errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n"; |
| 1021 | report_fatal_error("ThinLTO: Can't get an output file for the " |
| 1022 | "remarks"); |
| 1023 | } |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 1024 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 1025 | // Parse module now |
Mehdi Amini | a0ddb1e | 2017-02-14 02:20:51 +0000 | [diff] [blame] | 1026 | auto TheModule = |
| 1027 | loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false, |
| 1028 | /*IsImporting*/ false); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 1029 | |
| 1030 | // Save temps: original file. |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 1031 | saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc"); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 1032 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 1033 | auto &ImportList = ImportLists[ModuleIdentifier]; |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 1034 | // Run the main process now, and generates a binary |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 1035 | auto OutputBuffer = ProcessThinLTOModule( |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 1036 | *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList, |
Teresa Johnson | 4d2613f | 2016-05-24 17:24:25 +0000 | [diff] [blame] | 1037 | ExportList, GUIDPreservedSymbols, |
| 1038 | ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions, |
Mehdi Amini | b5a46c1 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 1039 | DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 1040 | |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 1041 | // Commit to the cache (if enabled) |
| 1042 | CacheEntry.write(*OutputBuffer); |
| 1043 | |
| 1044 | if (SavedObjectsDirectoryPath.empty()) { |
| 1045 | // We need to generated a memory buffer for the linker. |
| 1046 | if (!CacheEntryPath.empty()) { |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1047 | // When cache is enabled, reload from the cache if possible. |
Ekaterina Romanova | 0b01dfb | 2018-03-30 21:35:42 +0000 | [diff] [blame] | 1048 | // Releasing the buffer from the heap and reloading it from the |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1049 | // cache file with mmap helps us to lower memory pressure. |
| 1050 | // The freed memory can be used for the next input file. |
Ekaterina Romanova | 0b01dfb | 2018-03-30 21:35:42 +0000 | [diff] [blame] | 1051 | // The final binary link will read from the VFS cache (hopefully!) |
| 1052 | // or from disk (if the memory pressure was too high). |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 1053 | auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer(); |
| 1054 | if (auto EC = ReloadedBufferOrErr.getError()) { |
Ekaterina Romanova | 0b01dfb | 2018-03-30 21:35:42 +0000 | [diff] [blame] | 1055 | // On error, keep the preexisting buffer and print a diagnostic. |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 1056 | errs() << "error: can't reload cached file '" << CacheEntryPath |
| 1057 | << "': " << EC.message() << "\n"; |
| 1058 | } else { |
| 1059 | OutputBuffer = std::move(*ReloadedBufferOrErr); |
| 1060 | } |
| 1061 | } |
| 1062 | ProducedBinaries[count] = std::move(OutputBuffer); |
| 1063 | return; |
| 1064 | } |
| 1065 | ProducedBinaryFiles[count] = writeGeneratedObject( |
| 1066 | count, CacheEntryPath, SavedObjectsDirectoryPath, *OutputBuffer); |
Mehdi Amini | 819e9cd | 2016-05-16 19:33:07 +0000 | [diff] [blame] | 1067 | }, IndexCount); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
Peter Collingbourne | cead56f | 2017-03-15 22:54:18 +0000 | [diff] [blame] | 1071 | pruneCache(CacheOptions.Path, CacheOptions.Policy); |
Mehdi Amini | f95f77a | 2016-04-21 05:54:23 +0000 | [diff] [blame] | 1072 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 1073 | // If statistics were requested, print them out now. |
| 1074 | if (llvm::AreStatisticsEnabled()) |
| 1075 | llvm::PrintStatistics(); |
James Henderson | 852f6fd | 2017-05-16 09:43:21 +0000 | [diff] [blame] | 1076 | reportAndResetTimings(); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 1077 | } |