blob: 578b3e88b012f3039af9f71bb6d036d46d55d479 [file] [log] [blame]
Mehdi Amini7c4a1a82016-03-09 01:37:22 +00001//===-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 Collingbourne5c732202016-07-14 21:21:16 +000015#include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000016
Mehdi Aminif95f77a2016-04-21 05:54:23 +000017#ifdef HAVE_LLVM_REVISION
18#include "LLVMLTORevision.h"
19#endif
Mehdi Amini059464f2016-04-24 03:18:01 +000020
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000021#include "llvm/ADT/Statistic.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000022#include "llvm/ADT/StringExtras.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000023#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Piotr Padlewskid9830eb2016-09-26 20:37:32 +000024#include "llvm/Analysis/ProfileSummaryInfo.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000025#include "llvm/Analysis/TargetLibraryInfo.h"
26#include "llvm/Analysis/TargetTransformInfo.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000027#include "llvm/Bitcode/BitcodeWriterPass.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000028#include "llvm/Bitcode/ReaderWriter.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000029#include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000030#include "llvm/IR/DiagnosticPrinter.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000031#include "llvm/IR/LLVMContext.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000032#include "llvm/IR/LegacyPassManager.h"
33#include "llvm/IR/Mangler.h"
34#include "llvm/IRReader/IRReader.h"
Teresa Johnsondf6edc52016-05-23 22:54:06 +000035#include "llvm/LTO/LTO.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000036#include "llvm/Linker/Linker.h"
37#include "llvm/MC/SubtargetFeature.h"
Mehdi Amini059464f2016-04-24 03:18:01 +000038#include "llvm/Object/IRObjectFile.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000039#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
Mehdi Aminif95f77a2016-04-21 05:54:23 +000040#include "llvm/Support/CachePruning.h"
41#include "llvm/Support/Debug.h"
42#include "llvm/Support/Path.h"
43#include "llvm/Support/SHA1.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000044#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/ThreadPool.h"
Teresa Johnsonec544c52016-10-19 17:35:01 +000046#include "llvm/Support/Threading.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000047#include "llvm/Target/TargetMachine.h"
48#include "llvm/Transforms/IPO.h"
49#include "llvm/Transforms/IPO/FunctionImport.h"
Mehdi Amini059464f2016-04-24 03:18:01 +000050#include "llvm/Transforms/IPO/Internalize.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000051#include "llvm/Transforms/IPO/PassManagerBuilder.h"
52#include "llvm/Transforms/ObjCARC.h"
53#include "llvm/Transforms/Utils/FunctionImportUtils.h"
54
Mehdi Amini819e9cd2016-05-16 19:33:07 +000055#include <numeric>
56
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000057using namespace llvm;
58
Mehdi Amini1aafabf2016-04-16 07:02:16 +000059#define DEBUG_TYPE "thinlto"
60
Mehdi Amini09b4a8d2016-03-10 01:28:54 +000061namespace llvm {
62// Flags -discard-value-names, defined in LTOCodeGenerator.cpp
63extern cl::opt<bool> LTODiscardValueNames;
64}
65
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000066namespace {
67
Teresa Johnsonec544c52016-10-19 17:35:01 +000068static cl::opt<int>
69 ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000070
71static void diagnosticHandler(const DiagnosticInfo &DI) {
72 DiagnosticPrinterRawOStream DP(errs());
73 DI.print(DP);
74 errs() << '\n';
75}
76
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000077// Simple helper to save temporary files for debug.
78static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
79 unsigned count, StringRef Suffix) {
80 if (TempDir.empty())
81 return;
82 // User asked to save temps, let dump the bitcode file after import.
Teresa Johnsonc44a1222016-08-15 23:24:57 +000083 std::string SaveTempPath = (TempDir + llvm::utostr(count) + Suffix).str();
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000084 std::error_code EC;
Teresa Johnsonc44a1222016-08-15 23:24:57 +000085 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000086 if (EC)
87 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
88 " to save optimized bitcode\n");
Teresa Johnson3c35e092016-04-04 21:19:31 +000089 WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000090}
91
Teresa Johnson4d2613f2016-05-24 17:24:25 +000092static const GlobalValueSummary *
93getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
94 // If there is any strong definition anywhere, get it.
95 auto StrongDefForLinker = llvm::find_if(
96 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
97 auto Linkage = Summary->linkage();
98 return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
99 !GlobalValue::isWeakForLinker(Linkage);
100 });
101 if (StrongDefForLinker != GVSummaryList.end())
102 return StrongDefForLinker->get();
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000103 // Get the first *linker visible* definition for this global in the summary
104 // list.
105 auto FirstDefForLinker = llvm::find_if(
Teresa Johnson28e457b2016-04-24 14:57:11 +0000106 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
107 auto Linkage = Summary->linkage();
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000108 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
109 });
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000110 // Extern templates can be emitted as available_externally.
111 if (FirstDefForLinker == GVSummaryList.end())
112 return nullptr;
113 return FirstDefForLinker->get();
Hans Wennborgfa6e4142016-04-02 01:03:41 +0000114}
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000115
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000116// Populate map of GUID to the prevailing copy for any multiply defined
117// symbols. Currently assume first copy is prevailing, or any strong
118// definition. Can be refined with Linker information in the future.
119static void computePrevailingCopies(
120 const ModuleSummaryIndex &Index,
121 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
Teresa Johnson28e457b2016-04-24 14:57:11 +0000122 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
123 return GVSummaryList.size() > 1;
124 };
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000125
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000126 for (auto &I : Index) {
127 if (HasMultipleCopies(I.second))
128 PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000129 }
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000130}
131
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000132static StringMap<MemoryBufferRef>
133generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
134 StringMap<MemoryBufferRef> ModuleMap;
135 for (auto &ModuleBuffer : Modules) {
136 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
137 ModuleMap.end() &&
138 "Expect unique Buffer Identifier");
139 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
140 }
141 return ModuleMap;
142}
143
Teresa Johnson26ab5772016-03-15 00:04:37 +0000144static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000145 if (renameModuleForThinLTO(TheModule, Index))
146 report_fatal_error("renameModuleForThinLTO failed");
147}
148
Mehdi Amini01e32132016-03-26 05:40:34 +0000149static void
150crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
151 StringMap<MemoryBufferRef> &ModuleMap,
152 const FunctionImporter::ImportMapTy &ImportList) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000153 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
154 FunctionImporter Importer(Index, Loader);
Mehdi Amini01e32132016-03-26 05:40:34 +0000155 Importer.importFunctions(TheModule, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000156}
157
158static void optimizeModule(Module &TheModule, TargetMachine &TM) {
159 // Populate the PassManager
160 PassManagerBuilder PMB;
161 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
162 PMB.Inliner = createFunctionInliningPass();
163 // FIXME: should get it from the bitcode?
164 PMB.OptLevel = 3;
165 PMB.LoopVectorize = true;
166 PMB.SLPVectorize = true;
167 PMB.VerifyInput = true;
168 PMB.VerifyOutput = false;
169
170 legacy::PassManager PM;
171
172 // Add the TTI (required to inform the vectorizer about register size for
173 // instance)
174 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
175
176 // Add optimizations
177 PMB.populateThinLTOPassManager(PM);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000178
179 PM.run(TheModule);
180}
181
Mehdi Amini059464f2016-04-24 03:18:01 +0000182// Convert the PreservedSymbols map from "Name" based to "GUID" based.
183static DenseSet<GlobalValue::GUID>
184computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
185 const Triple &TheTriple) {
186 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
187 for (auto &Entry : PreservedSymbols) {
188 StringRef Name = Entry.first();
189 if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
190 Name = Name.drop_front();
191 GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
192 }
193 return GUIDPreservedSymbols;
194}
195
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000196std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
197 TargetMachine &TM) {
198 SmallVector<char, 128> OutputBuffer;
199
200 // CodeGen
201 {
202 raw_svector_ostream OS(OutputBuffer);
203 legacy::PassManager PM;
Mehdi Amini215d59e2016-04-01 08:22:59 +0000204
205 // If the bitcode files contain ARC code and were compiled with optimization,
206 // the ObjCARCContractPass must be run, so do it unconditionally here.
207 PM.add(createObjCARCContractPass());
208
209 // Setup the codegen now.
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000210 if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
211 /* DisableVerify */ true))
212 report_fatal_error("Failed to setup codegen");
213
214 // Run codegen now. resulting binary is in OutputBuffer.
215 PM.run(TheModule);
216 }
217 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
218}
219
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000220/// Manage caching for a single Module.
221class ModuleCacheEntry {
222 SmallString<128> EntryPath;
223
224public:
225 // Create a cache entry. This compute a unique hash for the Module considering
226 // the current list of export/import, and offer an interface to query to
227 // access the content in the cache.
228 ModuleCacheEntry(
229 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
230 const FunctionImporter::ImportMapTy &ImportList,
231 const FunctionImporter::ExportSetTy &ExportList,
232 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
Teresa Johnsonc851d212016-04-25 21:09:51 +0000233 const GVSummaryMapTy &DefinedFunctions,
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000234 const DenseSet<GlobalValue::GUID> &PreservedSymbols) {
235 if (CachePath.empty())
236 return;
237
Mehdi Amini00fa1402016-10-08 04:44:18 +0000238 if (!Index.modulePaths().count(ModuleID))
239 // The module does not have an entry, it can't have a hash at all
240 return;
241
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000242 // Compute the unique hash for this entry
243 // This is based on the current compiler version, the module itself, the
244 // export list, the hash for every single module in the import list, the
245 // list of ResolvedODR for the module, and the list of preserved symbols.
246
Mehdi Aminif82bda02016-10-08 04:44:23 +0000247 // Include the hash for the current module
248 auto ModHash = Index.getModuleHash(ModuleID);
249
250 if (all_of(ModHash, [](uint32_t V) { return V == 0; }))
251 // No hash entry, no caching!
252 return;
253
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000254 SHA1 Hasher;
255
256 // Start with the compiler revision
257 Hasher.update(LLVM_VERSION_STRING);
258#ifdef HAVE_LLVM_REVISION
259 Hasher.update(LLVM_REVISION);
260#endif
261
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000262 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
263 for (auto F : ExportList)
264 // The export list can impact the internalization, be conservative here
265 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
266
267 // Include the hash for every module we import functions from
268 for (auto &Entry : ImportList) {
269 auto ModHash = Index.getModuleHash(Entry.first());
270 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
271 }
272
273 // Include the hash for the resolved ODR.
274 for (auto &Entry : ResolvedODR) {
Sjoerd Meijer41beee62016-04-27 18:35:02 +0000275 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000276 sizeof(GlobalValue::GUID)));
Sjoerd Meijer41beee62016-04-27 18:35:02 +0000277 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000278 sizeof(GlobalValue::LinkageTypes)));
279 }
280
281 // Include the hash for the preserved symbols.
282 for (auto &Entry : PreservedSymbols) {
283 if (DefinedFunctions.count(Entry))
284 Hasher.update(
Sjoerd Meijer41beee62016-04-27 18:35:02 +0000285 ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000286 }
287
288 sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
289 }
290
Mehdi Amini059464f2016-04-24 03:18:01 +0000291 // Access the path to this entry in the cache.
292 StringRef getEntryPath() { return EntryPath; }
293
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000294 // Try loading the buffer for this cache entry.
295 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
296 if (EntryPath.empty())
297 return std::error_code();
298 return MemoryBuffer::getFile(EntryPath);
299 }
300
301 // Cache the Produced object file
Mehdi Amini001bb412016-05-16 19:11:59 +0000302 std::unique_ptr<MemoryBuffer>
303 write(std::unique_ptr<MemoryBuffer> OutputBuffer) {
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000304 if (EntryPath.empty())
Mehdi Amini001bb412016-05-16 19:11:59 +0000305 return OutputBuffer;
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000306
307 // Write to a temporary to avoid race condition
308 SmallString<128> TempFilename;
309 int TempFD;
310 std::error_code EC =
311 sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
312 if (EC) {
313 errs() << "Error: " << EC.message() << "\n";
314 report_fatal_error("ThinLTO: Can't get a temporary file");
315 }
316 {
317 raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
Mehdi Amini001bb412016-05-16 19:11:59 +0000318 OS << OutputBuffer->getBuffer();
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000319 }
320 // Rename to final destination (hopefully race condition won't matter here)
Mehdi Amini2a16a5f2016-05-14 04:58:38 +0000321 EC = sys::fs::rename(TempFilename, EntryPath);
322 if (EC) {
Mehdi Aminib02139d2016-05-14 05:16:35 +0000323 sys::fs::remove(TempFilename);
324 raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None);
325 if (EC)
326 report_fatal_error(Twine("Failed to open ") + EntryPath +
327 " to save cached entry\n");
Mehdi Amini001bb412016-05-16 19:11:59 +0000328 OS << OutputBuffer->getBuffer();
Mehdi Amini2a16a5f2016-05-14 04:58:38 +0000329 }
Mehdi Amini001bb412016-05-16 19:11:59 +0000330 auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
331 if (auto EC = ReloadedBufferOrErr.getError()) {
332 // FIXME diagnose
333 errs() << "error: can't reload cached file '" << EntryPath
334 << "': " << EC.message() << "\n";
335 return OutputBuffer;
336 }
337 return std::move(*ReloadedBufferOrErr);
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000338 }
339};
340
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000341static std::unique_ptr<MemoryBuffer>
342ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
343 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
344 const FunctionImporter::ImportMapTy &ImportList,
345 const FunctionImporter::ExportSetTy &ExportList,
346 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
347 const GVSummaryMapTy &DefinedGlobals,
Benjamin Kramerc321e532016-06-08 19:09:22 +0000348 const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000349 bool DisableCodeGen, StringRef SaveTempsDir,
350 unsigned count) {
Mehdi Amini059464f2016-04-24 03:18:01 +0000351
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000352 // "Benchmark"-like optimization: single-source case
353 bool SingleModule = (ModuleMap.size() == 1);
354
355 if (!SingleModule) {
356 promoteModule(TheModule, Index);
357
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000358 // Apply summary-based LinkOnce/Weak resolution decisions.
359 thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000360
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000361 // Save temps: after promotion.
Mehdi Amini4b300e0a2016-05-05 05:14:16 +0000362 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
Mehdi Amini059464f2016-04-24 03:18:01 +0000363 }
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000364
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000365 // Be friendly and don't nuke totally the module when the client didn't
366 // supply anything to preserve.
367 if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
368 // Apply summary-based internalization decisions.
369 thinLTOInternalizeModule(TheModule, DefinedGlobals);
370 }
Mehdi Amini059464f2016-04-24 03:18:01 +0000371
372 // Save internalized bitcode
Mehdi Amini4b300e0a2016-05-05 05:14:16 +0000373 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
Mehdi Amini059464f2016-04-24 03:18:01 +0000374
375 if (!SingleModule) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000376 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000377
378 // Save temps: after cross-module import.
Mehdi Amini4b300e0a2016-05-05 05:14:16 +0000379 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000380 }
381
382 optimizeModule(TheModule, TM);
383
Mehdi Amini4b300e0a2016-05-05 05:14:16 +0000384 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000385
Mehdi Amini43b657b2016-04-01 06:47:02 +0000386 if (DisableCodeGen) {
387 // Configured to stop before CodeGen, serialize the bitcode and return.
388 SmallVector<char, 128> OutputBuffer;
389 {
390 raw_svector_ostream OS(OutputBuffer);
Dehao Chen5461d8b2016-09-28 21:00:58 +0000391 ProfileSummaryInfo PSI(TheModule);
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000392 auto Index = buildModuleSummaryIndex(TheModule, nullptr, nullptr);
Chandler Carruthb7be5b62016-08-19 07:49:19 +0000393 WriteBitcodeToFile(&TheModule, OS, true, &Index);
Mehdi Amini43b657b2016-04-01 06:47:02 +0000394 }
395 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
396 }
397
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000398 return codegenModule(TheModule, TM);
399}
400
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000401/// Resolve LinkOnce/Weak symbols. Record resolutions in the \p ResolvedODR map
402/// for caching, and in the \p Index for application during the ThinLTO
403/// backends. This is needed for correctness for exported symbols (ensure
404/// at least one copy kept) and a compile-time optimization (to drop duplicate
405/// copies when possible).
406static void resolveWeakForLinkerInIndex(
407 ModuleSummaryIndex &Index,
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000408 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
409 &ResolvedODR) {
410
411 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
412 computePrevailingCopies(Index, PrevailingCopy);
413
414 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
415 const auto &Prevailing = PrevailingCopy.find(GUID);
416 // Not in map means that there was only one copy, which must be prevailing.
417 if (Prevailing == PrevailingCopy.end())
418 return true;
419 return Prevailing->second == S;
420 };
421
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000422 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
423 GlobalValue::GUID GUID,
424 GlobalValue::LinkageTypes NewLinkage) {
425 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
426 };
427
Peter Collingbourne73589f32016-07-07 18:31:51 +0000428 thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, recordNewLinkage);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000429}
430
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000431// Initialize the TargetMachine builder for a given Triple
432static void initTMBuilder(TargetMachineBuilder &TMBuilder,
433 const Triple &TheTriple) {
434 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
435 // FIXME this looks pretty terrible...
436 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
437 if (TheTriple.getArch() == llvm::Triple::x86_64)
438 TMBuilder.MCpu = "core2";
439 else if (TheTriple.getArch() == llvm::Triple::x86)
440 TMBuilder.MCpu = "yonah";
441 else if (TheTriple.getArch() == llvm::Triple::aarch64)
442 TMBuilder.MCpu = "cyclone";
443 }
444 TMBuilder.TheTriple = std::move(TheTriple);
445}
446
447} // end anonymous namespace
448
449void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
450 MemoryBufferRef Buffer(Data, Identifier);
451 if (Modules.empty()) {
452 // First module added, so initialize the triple and some options
453 LLVMContext Context;
454 Triple TheTriple(getBitcodeTargetTriple(Buffer, Context));
455 initTMBuilder(TMBuilder, Triple(TheTriple));
456 }
457#ifndef NDEBUG
458 else {
459 LLVMContext Context;
460 assert(TMBuilder.TheTriple.str() ==
461 getBitcodeTargetTriple(Buffer, Context) &&
462 "ThinLTO modules with different triple not supported");
463 }
464#endif
465 Modules.push_back(Buffer);
466}
467
468void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
469 PreservedSymbols.insert(Name);
470}
471
472void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
Mehdi Amini059464f2016-04-24 03:18:01 +0000473 // FIXME: At the moment, we don't take advantage of this extra information,
474 // we're conservatively considering cross-references as preserved.
475 // CrossReferencedSymbols.insert(Name);
476 PreservedSymbols.insert(Name);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000477}
478
479// TargetMachine factory
480std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
481 std::string ErrMsg;
482 const Target *TheTarget =
483 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
484 if (!TheTarget) {
485 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
486 }
487
488 // Use MAttr as the default set of features.
489 SubtargetFeatures Features(MAttr);
490 Features.getDefaultSubtargetFeatures(TheTriple);
491 std::string FeatureStr = Features.getString();
492 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
493 TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
494 CodeModel::Default, CGOptLevel));
495}
496
497/**
Teresa Johnson26ab5772016-03-15 00:04:37 +0000498 * Produce the combined summary index from all the bitcode files:
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000499 * "thin-link".
500 */
Teresa Johnson26ab5772016-03-15 00:04:37 +0000501std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
502 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000503 uint64_t NextModuleId = 0;
504 for (auto &ModuleBuffer : Modules) {
Teresa Johnson26ab5772016-03-15 00:04:37 +0000505 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
506 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
Teresa Johnson6fb3f192016-04-22 01:52:00 +0000507 diagnosticHandler);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000508 if (std::error_code EC = ObjOrErr.getError()) {
509 // FIXME diagnose
Teresa Johnson26ab5772016-03-15 00:04:37 +0000510 errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000511 << EC.message() << "\n";
512 return nullptr;
513 }
514 auto Index = (*ObjOrErr)->takeIndex();
515 if (CombinedIndex) {
516 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
517 } else {
518 CombinedIndex = std::move(Index);
519 }
520 }
521 return CombinedIndex;
522}
523
524/**
525 * Perform promotion and renaming of exported internal functions.
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000526 * Index is updated to reflect linkage changes from weak resolution.
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000527 */
528void ThinLTOCodeGenerator::promote(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000529 ModuleSummaryIndex &Index) {
Mehdi Aminia71a5a62016-04-21 05:47:17 +0000530 auto ModuleCount = Index.modulePaths().size();
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000531 auto ModuleIdentifier = TheModule.getModuleIdentifier();
532 // Collect for each module the list of function it defines (GUID -> Summary).
Teresa Johnsonc851d212016-04-25 21:09:51 +0000533 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000534 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000535
Mehdi Aminia71a5a62016-04-21 05:47:17 +0000536 // Generate import/export list
537 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
538 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
539 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
540 ExportLists);
Mehdi Aminia71a5a62016-04-21 05:47:17 +0000541
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000542 // Resolve LinkOnce/Weak symbols.
543 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
Peter Collingbourne73589f32016-07-07 18:31:51 +0000544 resolveWeakForLinkerInIndex(Index, ResolvedODR);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000545
546 thinLTOResolveWeakForLinkerModule(
547 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000548
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000549 promoteModule(TheModule, Index);
550}
551
552/**
553 * Perform cross-module importing for the module identified by ModuleIdentifier.
554 */
555void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000556 ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000557 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000558 auto ModuleCount = Index.modulePaths().size();
559
560 // Collect for each module the list of function it defines (GUID -> Summary).
Teresa Johnsonc851d212016-04-25 21:09:51 +0000561 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000562 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
Mehdi Amini01e32132016-03-26 05:40:34 +0000563
564 // Generate import/export list
Mehdi Amini01e32132016-03-26 05:40:34 +0000565 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
566 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000567 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
568 ExportLists);
Mehdi Amini01e32132016-03-26 05:40:34 +0000569 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
570
571 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000572}
573
574/**
Teresa Johnson84174c32016-05-10 13:48:23 +0000575 * Compute the list of summaries needed for importing into module.
576 */
577void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
578 StringRef ModulePath, ModuleSummaryIndex &Index,
579 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
580 auto ModuleCount = Index.modulePaths().size();
581
582 // Collect for each module the list of function it defines (GUID -> Summary).
583 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
584 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
585
586 // Generate import/export list
587 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
588 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
589 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
590 ExportLists);
591
592 llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
Mehdi Aminicdbcbf72016-08-16 05:46:05 +0000593 ImportLists[ModulePath],
Teresa Johnson84174c32016-05-10 13:48:23 +0000594 ModuleToSummariesForIndex);
595}
596
597/**
Teresa Johnson8570fe42016-05-10 15:54:09 +0000598 * Emit the list of files needed for importing into module.
599 */
600void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
601 StringRef OutputName,
602 ModuleSummaryIndex &Index) {
603 auto ModuleCount = Index.modulePaths().size();
604
605 // Collect for each module the list of function it defines (GUID -> Summary).
606 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
607 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
608
609 // Generate import/export list
610 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
611 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
612 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
613 ExportLists);
614
615 std::error_code EC;
Mehdi Aminicdbcbf72016-08-16 05:46:05 +0000616 if ((EC = EmitImportsFiles(ModulePath, OutputName, ImportLists[ModulePath])))
Teresa Johnson8570fe42016-05-10 15:54:09 +0000617 report_fatal_error(Twine("Failed to open ") + OutputName +
618 " to save imports lists\n");
619}
620
621/**
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000622 * Perform internalization. Index is updated to reflect linkage changes.
Mehdi Amini059464f2016-04-24 03:18:01 +0000623 */
624void ThinLTOCodeGenerator::internalize(Module &TheModule,
625 ModuleSummaryIndex &Index) {
626 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
627 auto ModuleCount = Index.modulePaths().size();
628 auto ModuleIdentifier = TheModule.getModuleIdentifier();
629
630 // Convert the preserved symbols set from string to GUID
631 auto GUIDPreservedSymbols =
632 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
633
634 // Collect for each module the list of function it defines (GUID -> Summary).
Teresa Johnsonc851d212016-04-25 21:09:51 +0000635 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
Mehdi Amini059464f2016-04-24 03:18:01 +0000636 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
637
638 // Generate import/export list
639 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
640 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
641 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
642 ExportLists);
643 auto &ExportList = ExportLists[ModuleIdentifier];
644
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000645 // Be friendly and don't nuke totally the module when the client didn't
646 // supply anything to preserve.
647 if (ExportList.empty() && GUIDPreservedSymbols.empty())
648 return;
649
Mehdi Amini059464f2016-04-24 03:18:01 +0000650 // Internalization
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000651 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
652 const auto &ExportList = ExportLists.find(ModuleIdentifier);
Teresa Johnson4ae5ce72016-05-24 19:12:48 +0000653 return (ExportList != ExportLists.end() &&
654 ExportList->second.count(GUID)) ||
655 GUIDPreservedSymbols.count(GUID);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000656 };
657 thinLTOInternalizeAndPromoteInIndex(Index, isExported);
658 thinLTOInternalizeModule(TheModule,
659 ModuleToDefinedGVSummaries[ModuleIdentifier]);
Mehdi Amini059464f2016-04-24 03:18:01 +0000660}
661
662/**
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000663 * Perform post-importing ThinLTO optimizations.
664 */
665void ThinLTOCodeGenerator::optimize(Module &TheModule) {
666 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
Mehdi Amini059464f2016-04-24 03:18:01 +0000667
668 // Optimize now
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000669 optimizeModule(TheModule, *TMBuilder.create());
670}
671
672/**
673 * Perform ThinLTO CodeGen.
674 */
675std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
676 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
677 return codegenModule(TheModule, *TMBuilder.create());
678}
679
680// Main entry point for the ThinLTO processing
681void ThinLTOCodeGenerator::run() {
Mehdi Amini43b657b2016-04-01 06:47:02 +0000682 if (CodeGenOnly) {
683 // Perform only parallel codegen and return.
684 ThreadPool Pool;
685 assert(ProducedBinaries.empty() && "The generator should not be reused");
686 ProducedBinaries.resize(Modules.size());
687 int count = 0;
688 for (auto &ModuleBuffer : Modules) {
689 Pool.async([&](int count) {
690 LLVMContext Context;
691 Context.setDiscardValueNames(LTODiscardValueNames);
692
693 // Parse module now
694 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
695
696 // CodeGen
697 ProducedBinaries[count] = codegen(*TheModule);
698 }, count++);
699 }
700
701 return;
702 }
703
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000704 // Sequential linking phase
705 auto Index = linkCombinedIndex();
706
707 // Save temps: index.
708 if (!SaveTempsDir.empty()) {
709 auto SaveTempPath = SaveTempsDir + "index.bc";
710 std::error_code EC;
711 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
712 if (EC)
713 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
714 " to save optimized bitcode\n");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000715 WriteIndexToFile(*Index, OS);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000716 }
717
718 // Prepare the resulting object vector
719 assert(ProducedBinaries.empty() && "The generator should not be reused");
720 ProducedBinaries.resize(Modules.size());
721
722 // Prepare the module map.
723 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini01e32132016-03-26 05:40:34 +0000724 auto ModuleCount = Modules.size();
725
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000726 // Collect for each module the list of function it defines (GUID -> Summary).
Teresa Johnsonc851d212016-04-25 21:09:51 +0000727 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000728 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
729
Mehdi Amini01e32132016-03-26 05:40:34 +0000730 // Collect the import/export lists for all modules from the call-graph in the
731 // combined index.
732 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
733 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000734 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
735 ExportLists);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000736
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000737 // Convert the preserved symbols set from string to GUID, this is needed for
Mehdi Amini059464f2016-04-24 03:18:01 +0000738 // computing the caching hash and the internalization.
739 auto GUIDPreservedSymbols =
740 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000741
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000742 // We use a std::map here to be able to have a defined ordering when
743 // producing a hash for the cache entry.
744 // FIXME: we should be able to compute the caching hash for the entry based
745 // on the index, and nuke this map.
746 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
747
748 // Resolve LinkOnce/Weak symbols, this has to be computed early because it
749 // impacts the caching.
Peter Collingbourne73589f32016-07-07 18:31:51 +0000750 resolveWeakForLinkerInIndex(*Index, ResolvedODR);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000751
752 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
753 const auto &ExportList = ExportLists.find(ModuleIdentifier);
Teresa Johnson4ae5ce72016-05-24 19:12:48 +0000754 return (ExportList != ExportLists.end() &&
755 ExportList->second.count(GUID)) ||
756 GUIDPreservedSymbols.count(GUID);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000757 };
758
759 // Use global summary-based analysis to identify symbols that can be
760 // internalized (because they aren't exported or preserved as per callback).
761 // Changes are made in the index, consumed in the ThinLTO backends.
762 thinLTOInternalizeAndPromoteInIndex(*Index, isExported);
763
Teresa Johnson141149f2016-05-24 18:44:01 +0000764 // Make sure that every module has an entry in the ExportLists and
765 // ResolvedODR maps to enable threaded access to these maps below.
766 for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
Mehdi Aminiaf52f282016-05-15 05:49:47 +0000767 ExportLists[DefinedGVSummaries.first()];
Teresa Johnson141149f2016-05-24 18:44:01 +0000768 ResolvedODR[DefinedGVSummaries.first()];
769 }
Mehdi Aminiaf52f282016-05-15 05:49:47 +0000770
Mehdi Amini819e9cd2016-05-16 19:33:07 +0000771 // Compute the ordering we will process the inputs: the rough heuristic here
772 // is to sort them per size so that the largest module get schedule as soon as
773 // possible. This is purely a compile-time optimization.
774 std::vector<int> ModulesOrdering;
775 ModulesOrdering.resize(Modules.size());
776 std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
777 std::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
778 [&](int LeftIndex, int RightIndex) {
779 auto LSize = Modules[LeftIndex].getBufferSize();
780 auto RSize = Modules[RightIndex].getBufferSize();
781 return LSize > RSize;
782 });
783
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000784 // Parallel optimizer + codegen
785 {
786 ThreadPool Pool(ThreadCount);
Mehdi Amini819e9cd2016-05-16 19:33:07 +0000787 for (auto IndexCount : ModulesOrdering) {
788 auto &ModuleBuffer = Modules[IndexCount];
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000789 Pool.async([&](int count) {
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000790 auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
Mehdi Aminia71a5a62016-04-21 05:47:17 +0000791 auto &ExportList = ExportLists[ModuleIdentifier];
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000792
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000793 auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
794
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000795 // The module may be cached, this helps handling it.
Mehdi Amini059464f2016-04-24 03:18:01 +0000796 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
797 ImportLists[ModuleIdentifier], ExportList,
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000798 ResolvedODR[ModuleIdentifier],
799 DefinedFunctions, GUIDPreservedSymbols);
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000800
801 {
802 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
Mehdi Amini059464f2016-04-24 03:18:01 +0000803 DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
804 << CacheEntry.getEntryPath() << "' for buffer " << count
805 << " " << ModuleIdentifier << "\n");
806
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000807 if (ErrOrBuffer) {
808 // Cache Hit!
809 ProducedBinaries[count] = std::move(ErrOrBuffer.get());
810 return;
811 }
812 }
813
814 LLVMContext Context;
815 Context.setDiscardValueNames(LTODiscardValueNames);
816 Context.enableDebugTypeODRUniquing();
817
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000818 // Parse module now
819 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
820
821 // Save temps: original file.
Mehdi Amini059464f2016-04-24 03:18:01 +0000822 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000823
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000824 auto &ImportList = ImportLists[ModuleIdentifier];
Mehdi Amini059464f2016-04-24 03:18:01 +0000825 // Run the main process now, and generates a binary
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000826 auto OutputBuffer = ProcessThinLTOModule(
Mehdi Amini01e32132016-03-26 05:40:34 +0000827 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000828 ExportList, GUIDPreservedSymbols,
829 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
Mehdi Amini059464f2016-04-24 03:18:01 +0000830 DisableCodeGen, SaveTempsDir, count);
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000831
Mehdi Amini001bb412016-05-16 19:11:59 +0000832 OutputBuffer = CacheEntry.write(std::move(OutputBuffer));
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000833 ProducedBinaries[count] = std::move(OutputBuffer);
Mehdi Amini819e9cd2016-05-16 19:33:07 +0000834 }, IndexCount);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000835 }
836 }
837
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000838 CachePruning(CacheOptions.Path)
839 .setPruningInterval(CacheOptions.PruningInterval)
840 .setEntryExpiration(CacheOptions.Expiration)
841 .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
842 .prune();
843
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000844 // If statistics were requested, print them out now.
845 if (llvm::AreStatisticsEnabled())
846 llvm::PrintStatistics();
847}