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