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