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