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