blob: d7fac6a993f61ff6e1699c425f07b027a0025294 [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
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000017#include "llvm/ADT/Statistic.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000018#include "llvm/ADT/StringExtras.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000019#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Piotr Padlewskid9830eb2016-09-26 20:37:32 +000020#include "llvm/Analysis/ProfileSummaryInfo.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000021#include "llvm/Analysis/TargetLibraryInfo.h"
22#include "llvm/Analysis/TargetTransformInfo.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000023#include "llvm/Bitcode/BitcodeReader.h"
24#include "llvm/Bitcode/BitcodeWriter.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000025#include "llvm/Bitcode/BitcodeWriterPass.h"
Nico Weber432a3882018-04-30 14:59:11 +000026#include "llvm/Config/llvm-config.h"
Adrian Prantl981a7992017-05-20 00:00:08 +000027#include "llvm/IR/DebugInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000028#include "llvm/IR/DiagnosticPrinter.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000029#include "llvm/IR/LLVMContext.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000030#include "llvm/IR/LegacyPassManager.h"
31#include "llvm/IR/Mangler.h"
Adrian Prantl981a7992017-05-20 00:00:08 +000032#include "llvm/IR/Verifier.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000033#include "llvm/IRReader/IRReader.h"
Teresa Johnsondf6edc52016-05-23 22:54:06 +000034#include "llvm/LTO/LTO.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000035#include "llvm/MC/SubtargetFeature.h"
Mehdi Amini059464f2016-04-24 03:18:01 +000036#include "llvm/Object/IRObjectFile.h"
Mehdi Aminif95f77a2016-04-21 05:54:23 +000037#include "llvm/Support/CachePruning.h"
38#include "llvm/Support/Debug.h"
Mehdi Amini19f176b2016-11-19 18:20:05 +000039#include "llvm/Support/Error.h"
Mehdi Aminif95f77a2016-04-21 05:54:23 +000040#include "llvm/Support/Path.h"
41#include "llvm/Support/SHA1.h"
Weiming Zhao79f2d092018-04-16 03:44:03 +000042#include "llvm/Support/SmallVectorMemoryBuffer.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000043#include "llvm/Support/TargetRegistry.h"
44#include "llvm/Support/ThreadPool.h"
Teresa Johnsonec544c52016-10-19 17:35:01 +000045#include "llvm/Support/Threading.h"
Mehdi Amini19f176b2016-11-19 18:20:05 +000046#include "llvm/Support/ToolOutputFile.h"
Peter Collingbourne942fa562017-04-13 01:26:12 +000047#include "llvm/Support/VCSRevision.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000048#include "llvm/Target/TargetMachine.h"
49#include "llvm/Transforms/IPO.h"
50#include "llvm/Transforms/IPO/FunctionImport.h"
Mehdi Amini059464f2016-04-24 03:18:01 +000051#include "llvm/Transforms/IPO/Internalize.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000052#include "llvm/Transforms/IPO/PassManagerBuilder.h"
53#include "llvm/Transforms/ObjCARC.h"
54#include "llvm/Transforms/Utils/FunctionImportUtils.h"
55
Mehdi Amini819e9cd2016-05-16 19:33:07 +000056#include <numeric>
57
Andrew Ng089303d2018-07-04 14:17:10 +000058#if !defined(_MSC_VER) && !defined(__MINGW32__)
59#include <unistd.h>
60#else
61#include <io.h>
62#endif
63
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000064using namespace llvm;
65
Mehdi Amini1aafabf2016-04-16 07:02:16 +000066#define DEBUG_TYPE "thinlto"
67
Mehdi Amini09b4a8d2016-03-10 01:28:54 +000068namespace llvm {
69// Flags -discard-value-names, defined in LTOCodeGenerator.cpp
70extern cl::opt<bool> LTODiscardValueNames;
Mehdi Amini19f176b2016-11-19 18:20:05 +000071extern cl::opt<std::string> LTORemarksFilename;
Adam Nemet4c207a62016-12-02 17:53:56 +000072extern cl::opt<bool> LTOPassRemarksWithHotness;
Mehdi Amini09b4a8d2016-03-10 01:28:54 +000073}
74
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000075namespace {
76
Teresa Johnsonec544c52016-10-19 17:35:01 +000077static cl::opt<int>
78 ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000079
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000080// Simple helper to save temporary files for debug.
81static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
82 unsigned count, StringRef Suffix) {
83 if (TempDir.empty())
84 return;
85 // User asked to save temps, let dump the bitcode file after import.
Benjamin Kramer3a13ed62017-12-28 16:58:54 +000086 std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000087 std::error_code EC;
Teresa Johnsonc44a1222016-08-15 23:24:57 +000088 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000089 if (EC)
90 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
91 " to save optimized bitcode\n");
Rafael Espindola6a86e252018-02-14 19:11:32 +000092 WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000093}
94
Teresa Johnson4d2613f2016-05-24 17:24:25 +000095static const GlobalValueSummary *
96getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
97 // If there is any strong definition anywhere, get it.
98 auto StrongDefForLinker = llvm::find_if(
99 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
100 auto Linkage = Summary->linkage();
101 return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
102 !GlobalValue::isWeakForLinker(Linkage);
103 });
104 if (StrongDefForLinker != GVSummaryList.end())
105 return StrongDefForLinker->get();
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000106 // Get the first *linker visible* definition for this global in the summary
107 // list.
108 auto FirstDefForLinker = llvm::find_if(
Teresa Johnson28e457b2016-04-24 14:57:11 +0000109 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
110 auto Linkage = Summary->linkage();
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000111 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
112 });
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000113 // Extern templates can be emitted as available_externally.
114 if (FirstDefForLinker == GVSummaryList.end())
115 return nullptr;
116 return FirstDefForLinker->get();
Hans Wennborgfa6e4142016-04-02 01:03:41 +0000117}
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000118
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000119// Populate map of GUID to the prevailing copy for any multiply defined
120// symbols. Currently assume first copy is prevailing, or any strong
121// definition. Can be refined with Linker information in the future.
122static void computePrevailingCopies(
123 const ModuleSummaryIndex &Index,
124 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
Teresa Johnson28e457b2016-04-24 14:57:11 +0000125 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
126 return GVSummaryList.size() > 1;
127 };
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000128
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000129 for (auto &I : Index) {
Peter Collingbourne9667b912017-05-04 18:03:25 +0000130 if (HasMultipleCopies(I.second.SummaryList))
131 PrevailingCopy[I.first] =
132 getFirstDefinitionForLinker(I.second.SummaryList);
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000133 }
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000134}
135
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000136static StringMap<MemoryBufferRef>
Mehdi Aminia0ddb1e2017-02-14 02:20:51 +0000137generateModuleMap(const std::vector<ThinLTOBuffer> &Modules) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000138 StringMap<MemoryBufferRef> ModuleMap;
139 for (auto &ModuleBuffer : Modules) {
140 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
141 ModuleMap.end() &&
142 "Expect unique Buffer Identifier");
Mehdi Aminia0ddb1e2017-02-14 02:20:51 +0000143 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer.getMemBuffer();
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000144 }
145 return ModuleMap;
146}
147
Teresa Johnson26ab5772016-03-15 00:04:37 +0000148static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000149 if (renameModuleForThinLTO(TheModule, Index))
150 report_fatal_error("renameModuleForThinLTO failed");
151}
152
Adrian Prantl981a7992017-05-20 00:00:08 +0000153namespace {
154class ThinLTODiagnosticInfo : public DiagnosticInfo {
155 const Twine &Msg;
156public:
157 ThinLTODiagnosticInfo(const Twine &DiagMsg,
158 DiagnosticSeverity Severity = DS_Error)
159 : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
160 void print(DiagnosticPrinter &DP) const override { DP << Msg; }
161};
162}
163
164/// Verify the module and strip broken debug info.
165static void verifyLoadedModule(Module &TheModule) {
166 bool BrokenDebugInfo = false;
Adrian Prantla8b2ddb2017-10-02 18:31:29 +0000167 if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
Adrian Prantl981a7992017-05-20 00:00:08 +0000168 report_fatal_error("Broken module found, compilation aborted!");
169 if (BrokenDebugInfo) {
170 TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
171 "Invalid debug info found, debug info will be stripped", DS_Warning));
172 StripDebugInfo(TheModule);
173 }
174}
175
Peter Collingbournedac43b42016-12-01 05:52:32 +0000176static std::unique_ptr<Module>
177loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
Teresa Johnsona61f5e32016-12-16 21:25:01 +0000178 bool Lazy, bool IsImporting) {
Peter Collingbournedac43b42016-12-01 05:52:32 +0000179 SMDiagnostic Err;
180 Expected<std::unique_ptr<Module>> ModuleOrErr =
Teresa Johnsona61f5e32016-12-16 21:25:01 +0000181 Lazy
182 ? getLazyBitcodeModule(Buffer, Context,
183 /* ShouldLazyLoadMetadata */ true, IsImporting)
184 : parseBitcodeFile(Buffer, Context);
Peter Collingbournedac43b42016-12-01 05:52:32 +0000185 if (!ModuleOrErr) {
186 handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
187 SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
188 SourceMgr::DK_Error, EIB.message());
189 Err.print("ThinLTO", errs());
190 });
191 report_fatal_error("Can't load module, abort.");
192 }
Adrian Prantl981a7992017-05-20 00:00:08 +0000193 if (!Lazy)
194 verifyLoadedModule(*ModuleOrErr.get());
Peter Collingbournedac43b42016-12-01 05:52:32 +0000195 return std::move(ModuleOrErr.get());
196}
197
Mehdi Amini01e32132016-03-26 05:40:34 +0000198static void
199crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
200 StringMap<MemoryBufferRef> &ModuleMap,
201 const FunctionImporter::ImportMapTy &ImportList) {
Peter Collingbournedac43b42016-12-01 05:52:32 +0000202 auto Loader = [&](StringRef Identifier) {
203 return loadModuleFromBuffer(ModuleMap[Identifier], TheModule.getContext(),
Teresa Johnsona61f5e32016-12-16 21:25:01 +0000204 /*Lazy=*/true, /*IsImporting*/ true);
Peter Collingbournedac43b42016-12-01 05:52:32 +0000205 };
206
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000207 FunctionImporter Importer(Index, Loader);
Adrian Prantl66043792017-05-19 23:32:21 +0000208 Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
Mehdi Amini83a807e2017-01-08 00:30:27 +0000209 if (!Result) {
210 handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
211 SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(),
212 SourceMgr::DK_Error, EIB.message());
213 Err.print("ThinLTO", errs());
214 });
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000215 report_fatal_error("importFunctions failed");
Mehdi Amini83a807e2017-01-08 00:30:27 +0000216 }
Adrian Prantl981a7992017-05-20 00:00:08 +0000217 // Verify again after cross-importing.
218 verifyLoadedModule(TheModule);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000219}
220
Mehdi Aminicc7fbf72016-12-28 19:37:16 +0000221static void optimizeModule(Module &TheModule, TargetMachine &TM,
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000222 unsigned OptLevel, bool Freestanding) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000223 // Populate the PassManager
224 PassManagerBuilder PMB;
225 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000226 if (Freestanding)
227 PMB.LibraryInfo->disableAllFunctions();
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000228 PMB.Inliner = createFunctionInliningPass();
229 // FIXME: should get it from the bitcode?
Mehdi Aminicc7fbf72016-12-28 19:37:16 +0000230 PMB.OptLevel = OptLevel;
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000231 PMB.LoopVectorize = true;
232 PMB.SLPVectorize = true;
Adrian Prantl981a7992017-05-20 00:00:08 +0000233 // Already did this in verifyLoadedModule().
234 PMB.VerifyInput = false;
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000235 PMB.VerifyOutput = false;
236
237 legacy::PassManager PM;
238
239 // Add the TTI (required to inform the vectorizer about register size for
240 // instance)
241 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
242
243 // Add optimizations
244 PMB.populateThinLTOPassManager(PM);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000245
246 PM.run(TheModule);
247}
248
Mehdi Amini059464f2016-04-24 03:18:01 +0000249// Convert the PreservedSymbols map from "Name" based to "GUID" based.
250static DenseSet<GlobalValue::GUID>
Mehdi Amini1380edf2017-02-03 07:41:43 +0000251computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
252 const Triple &TheTriple) {
253 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
254 for (auto &Entry : PreservedSymbols) {
Mehdi Amini059464f2016-04-24 03:18:01 +0000255 StringRef Name = Entry.first();
256 if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
257 Name = Name.drop_front();
Mehdi Amini1380edf2017-02-03 07:41:43 +0000258 GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
Mehdi Amini059464f2016-04-24 03:18:01 +0000259 }
Mehdi Amini1380edf2017-02-03 07:41:43 +0000260 return GUIDPreservedSymbols;
Mehdi Amini059464f2016-04-24 03:18:01 +0000261}
262
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000263std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
264 TargetMachine &TM) {
265 SmallVector<char, 128> OutputBuffer;
266
267 // CodeGen
268 {
269 raw_svector_ostream OS(OutputBuffer);
270 legacy::PassManager PM;
Mehdi Amini215d59e2016-04-01 08:22:59 +0000271
272 // If the bitcode files contain ARC code and were compiled with optimization,
273 // the ObjCARCContractPass must be run, so do it unconditionally here.
274 PM.add(createObjCARCContractPass());
275
276 // Setup the codegen now.
Peter Collingbourne9a451142018-05-21 20:16:41 +0000277 if (TM.addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile,
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000278 /* DisableVerify */ true))
279 report_fatal_error("Failed to setup codegen");
280
281 // Run codegen now. resulting binary is in OutputBuffer.
282 PM.run(TheModule);
283 }
Weiming Zhao79f2d092018-04-16 03:44:03 +0000284 return make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000285}
286
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000287/// Manage caching for a single Module.
288class ModuleCacheEntry {
289 SmallString<128> EntryPath;
290
291public:
292 // Create a cache entry. This compute a unique hash for the Module considering
293 // the current list of export/import, and offer an interface to query to
294 // access the content in the cache.
295 ModuleCacheEntry(
296 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
297 const FunctionImporter::ImportMapTy &ImportList,
298 const FunctionImporter::ExportSetTy &ExportList,
299 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
Teresa Johnsonc851d212016-04-25 21:09:51 +0000300 const GVSummaryMapTy &DefinedFunctions,
Mehdi Aminic92b6122017-01-10 00:55:47 +0000301 const DenseSet<GlobalValue::GUID> &PreservedSymbols, unsigned OptLevel,
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000302 bool Freestanding, const TargetMachineBuilder &TMBuilder) {
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000303 if (CachePath.empty())
304 return;
305
Mehdi Amini00fa1402016-10-08 04:44:18 +0000306 if (!Index.modulePaths().count(ModuleID))
307 // The module does not have an entry, it can't have a hash at all
308 return;
309
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000310 // Compute the unique hash for this entry
311 // This is based on the current compiler version, the module itself, the
312 // export list, the hash for every single module in the import list, the
313 // list of ResolvedODR for the module, and the list of preserved symbols.
314
Mehdi Aminif82bda02016-10-08 04:44:23 +0000315 // Include the hash for the current module
316 auto ModHash = Index.getModuleHash(ModuleID);
317
318 if (all_of(ModHash, [](uint32_t V) { return V == 0; }))
319 // No hash entry, no caching!
320 return;
321
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000322 SHA1 Hasher;
323
Mehdi Aminic92b6122017-01-10 00:55:47 +0000324 // Include the parts of the LTO configuration that affect code generation.
325 auto AddString = [&](StringRef Str) {
326 Hasher.update(Str);
327 Hasher.update(ArrayRef<uint8_t>{0});
328 };
329 auto AddUnsigned = [&](unsigned I) {
330 uint8_t Data[4];
331 Data[0] = I;
332 Data[1] = I >> 8;
333 Data[2] = I >> 16;
334 Data[3] = I >> 24;
335 Hasher.update(ArrayRef<uint8_t>{Data, 4});
336 };
337
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000338 // Start with the compiler revision
339 Hasher.update(LLVM_VERSION_STRING);
Peter Collingbourne942fa562017-04-13 01:26:12 +0000340#ifdef LLVM_REVISION
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000341 Hasher.update(LLVM_REVISION);
342#endif
343
Mehdi Aminic92b6122017-01-10 00:55:47 +0000344 // Hash the optimization level and the target machine settings.
345 AddString(TMBuilder.MCpu);
346 // FIXME: Hash more of Options. For now all clients initialize Options from
347 // command-line flags (which is unsupported in production), but may set
348 // RelaxELFRelocations. The clang driver can also pass FunctionSections,
349 // DataSections and DebuggerTuning via command line flags.
350 AddUnsigned(TMBuilder.Options.RelaxELFRelocations);
351 AddUnsigned(TMBuilder.Options.FunctionSections);
352 AddUnsigned(TMBuilder.Options.DataSections);
353 AddUnsigned((unsigned)TMBuilder.Options.DebuggerTuning);
354 AddString(TMBuilder.MAttr);
355 if (TMBuilder.RelocModel)
356 AddUnsigned(*TMBuilder.RelocModel);
357 AddUnsigned(TMBuilder.CGOptLevel);
358 AddUnsigned(OptLevel);
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000359 AddUnsigned(Freestanding);
Mehdi Aminic92b6122017-01-10 00:55:47 +0000360
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000361 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
362 for (auto F : ExportList)
363 // The export list can impact the internalization, be conservative here
364 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
365
366 // Include the hash for every module we import functions from
367 for (auto &Entry : ImportList) {
368 auto ModHash = Index.getModuleHash(Entry.first());
369 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
370 }
371
372 // Include the hash for the resolved ODR.
373 for (auto &Entry : ResolvedODR) {
Sjoerd Meijer41beee62016-04-27 18:35:02 +0000374 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000375 sizeof(GlobalValue::GUID)));
Sjoerd Meijer41beee62016-04-27 18:35:02 +0000376 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000377 sizeof(GlobalValue::LinkageTypes)));
378 }
379
380 // Include the hash for the preserved symbols.
381 for (auto &Entry : PreservedSymbols) {
382 if (DefinedFunctions.count(Entry))
383 Hasher.update(
Sjoerd Meijer41beee62016-04-27 18:35:02 +0000384 ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000385 }
386
Peter Collingbourne25a17ba2017-03-20 16:41:57 +0000387 // This choice of file name allows the cache to be pruned (see pruneCache()
388 // in include/llvm/Support/CachePruning.h).
389 sys::path::append(EntryPath, CachePath,
390 "llvmcache-" + toHex(Hasher.result()));
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000391 }
392
Mehdi Amini059464f2016-04-24 03:18:01 +0000393 // Access the path to this entry in the cache.
394 StringRef getEntryPath() { return EntryPath; }
395
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000396 // Try loading the buffer for this cache entry.
397 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
398 if (EntryPath.empty())
399 return std::error_code();
Andrew Ng089303d2018-07-04 14:17:10 +0000400 int FD;
401 SmallString<64> ResultPath;
402 std::error_code EC = sys::fs::openFileForRead(
403 Twine(EntryPath), FD, sys::fs::OF_UpdateAtime, &ResultPath);
404 if (EC)
405 return EC;
406 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
407 MemoryBuffer::getOpenFile(FD, EntryPath,
408 /*FileSize*/ -1,
409 /*RequiresNullTerminator*/ false);
410 close(FD);
411 return MBOrErr;
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000412 }
413
414 // Cache the Produced object file
Mehdi Amini8e13bc42016-12-14 04:56:42 +0000415 void write(const MemoryBuffer &OutputBuffer) {
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000416 if (EntryPath.empty())
Mehdi Amini8e13bc42016-12-14 04:56:42 +0000417 return;
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000418
419 // Write to a temporary to avoid race condition
420 SmallString<128> TempFilename;
Ekaterina Romanova0b01dfb2018-03-30 21:35:42 +0000421 SmallString<128> CachePath(EntryPath);
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000422 int TempFD;
Ekaterina Romanova0b01dfb2018-03-30 21:35:42 +0000423 llvm::sys::path::remove_filename(CachePath);
424 sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
425 std::error_code EC =
426 sys::fs::createUniqueFile(TempFilename, TempFD, TempFilename);
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000427 if (EC) {
428 errs() << "Error: " << EC.message() << "\n";
429 report_fatal_error("ThinLTO: Can't get a temporary file");
430 }
431 {
432 raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
Mehdi Amini8e13bc42016-12-14 04:56:42 +0000433 OS << OutputBuffer.getBuffer();
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000434 }
Ekaterina Romanova0b01dfb2018-03-30 21:35:42 +0000435 // Rename temp file to final destination; rename is atomic
Mehdi Amini2a16a5f2016-05-14 04:58:38 +0000436 EC = sys::fs::rename(TempFilename, EntryPath);
Ekaterina Romanova0b01dfb2018-03-30 21:35:42 +0000437 if (EC)
Mehdi Aminib02139d2016-05-14 05:16:35 +0000438 sys::fs::remove(TempFilename);
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000439 }
440};
441
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000442static std::unique_ptr<MemoryBuffer>
443ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
444 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
445 const FunctionImporter::ImportMapTy &ImportList,
446 const FunctionImporter::ExportSetTy &ExportList,
447 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
448 const GVSummaryMapTy &DefinedGlobals,
Benjamin Kramerc321e532016-06-08 19:09:22 +0000449 const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000450 bool DisableCodeGen, StringRef SaveTempsDir,
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000451 bool Freestanding, unsigned OptLevel, unsigned count) {
Mehdi Amini059464f2016-04-24 03:18:01 +0000452
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000453 // "Benchmark"-like optimization: single-source case
454 bool SingleModule = (ModuleMap.size() == 1);
455
456 if (!SingleModule) {
457 promoteModule(TheModule, Index);
458
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000459 // Apply summary-based LinkOnce/Weak resolution decisions.
460 thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000461
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000462 // Save temps: after promotion.
Mehdi Amini4b300e0a2016-05-05 05:14:16 +0000463 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
Mehdi Amini059464f2016-04-24 03:18:01 +0000464 }
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000465
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000466 // Be friendly and don't nuke totally the module when the client didn't
467 // supply anything to preserve.
468 if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
469 // Apply summary-based internalization decisions.
470 thinLTOInternalizeModule(TheModule, DefinedGlobals);
471 }
Mehdi Amini059464f2016-04-24 03:18:01 +0000472
473 // Save internalized bitcode
Mehdi Amini4b300e0a2016-05-05 05:14:16 +0000474 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
Mehdi Amini059464f2016-04-24 03:18:01 +0000475
476 if (!SingleModule) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000477 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000478
479 // Save temps: after cross-module import.
Mehdi Amini4b300e0a2016-05-05 05:14:16 +0000480 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000481 }
482
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000483 optimizeModule(TheModule, TM, OptLevel, Freestanding);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000484
Mehdi Amini4b300e0a2016-05-05 05:14:16 +0000485 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000486
Mehdi Amini43b657b2016-04-01 06:47:02 +0000487 if (DisableCodeGen) {
488 // Configured to stop before CodeGen, serialize the bitcode and return.
489 SmallVector<char, 128> OutputBuffer;
490 {
491 raw_svector_ostream OS(OutputBuffer);
Dehao Chen5461d8b2016-09-28 21:00:58 +0000492 ProfileSummaryInfo PSI(TheModule);
Teresa Johnson94624ac2017-05-10 18:52:16 +0000493 auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
Rafael Espindola6a86e252018-02-14 19:11:32 +0000494 WriteBitcodeToFile(TheModule, OS, true, &Index);
Mehdi Amini43b657b2016-04-01 06:47:02 +0000495 }
Weiming Zhao79f2d092018-04-16 03:44:03 +0000496 return make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
Mehdi Amini43b657b2016-04-01 06:47:02 +0000497 }
498
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000499 return codegenModule(TheModule, TM);
500}
501
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000502/// Resolve LinkOnce/Weak symbols. Record resolutions in the \p ResolvedODR map
503/// for caching, and in the \p Index for application during the ThinLTO
504/// backends. This is needed for correctness for exported symbols (ensure
505/// at least one copy kept) and a compile-time optimization (to drop duplicate
506/// copies when possible).
507static void resolveWeakForLinkerInIndex(
508 ModuleSummaryIndex &Index,
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000509 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
510 &ResolvedODR) {
511
512 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
513 computePrevailingCopies(Index, PrevailingCopy);
514
515 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
516 const auto &Prevailing = PrevailingCopy.find(GUID);
517 // Not in map means that there was only one copy, which must be prevailing.
518 if (Prevailing == PrevailingCopy.end())
519 return true;
520 return Prevailing->second == S;
521 };
522
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000523 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
524 GlobalValue::GUID GUID,
525 GlobalValue::LinkageTypes NewLinkage) {
526 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
527 };
528
Peter Collingbourne73589f32016-07-07 18:31:51 +0000529 thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, recordNewLinkage);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000530}
531
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000532// Initialize the TargetMachine builder for a given Triple
533static void initTMBuilder(TargetMachineBuilder &TMBuilder,
534 const Triple &TheTriple) {
535 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
536 // FIXME this looks pretty terrible...
537 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
538 if (TheTriple.getArch() == llvm::Triple::x86_64)
539 TMBuilder.MCpu = "core2";
540 else if (TheTriple.getArch() == llvm::Triple::x86)
541 TMBuilder.MCpu = "yonah";
542 else if (TheTriple.getArch() == llvm::Triple::aarch64)
543 TMBuilder.MCpu = "cyclone";
544 }
545 TMBuilder.TheTriple = std::move(TheTriple);
546}
547
548} // end anonymous namespace
549
550void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
Johan Engelendcebe4f2017-09-17 18:11:26 +0000551 ThinLTOBuffer Buffer(Data, Identifier);
Akira Hatanakab10bff12017-05-18 03:52:29 +0000552 LLVMContext Context;
553 StringRef TripleStr;
554 ErrorOr<std::string> TripleOrErr = expectedToErrorOrAndEmitErrors(
555 Context, getBitcodeTargetTriple(Buffer.getMemBuffer()));
556
557 if (TripleOrErr)
558 TripleStr = *TripleOrErr;
559
560 Triple TheTriple(TripleStr);
561
562 if (Modules.empty())
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000563 initTMBuilder(TMBuilder, Triple(TheTriple));
Akira Hatanakab10bff12017-05-18 03:52:29 +0000564 else if (TMBuilder.TheTriple != TheTriple) {
565 if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
566 report_fatal_error("ThinLTO modules with incompatible triples not "
567 "supported");
568 initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000569 }
Akira Hatanakab10bff12017-05-18 03:52:29 +0000570
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000571 Modules.push_back(Buffer);
572}
573
574void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
575 PreservedSymbols.insert(Name);
576}
577
578void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
Mehdi Amini1380edf2017-02-03 07:41:43 +0000579 // FIXME: At the moment, we don't take advantage of this extra information,
580 // we're conservatively considering cross-references as preserved.
581 // CrossReferencedSymbols.insert(Name);
582 PreservedSymbols.insert(Name);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000583}
584
585// TargetMachine factory
586std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
587 std::string ErrMsg;
588 const Target *TheTarget =
589 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
590 if (!TheTarget) {
591 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
592 }
593
594 // Use MAttr as the default set of features.
595 SubtargetFeatures Features(MAttr);
596 Features.getDefaultSubtargetFeatures(TheTriple);
597 std::string FeatureStr = Features.getString();
Mehdi Aminicc7fbf72016-12-28 19:37:16 +0000598
Rafael Espindola79e238a2017-08-03 02:16:21 +0000599 return std::unique_ptr<TargetMachine>(
600 TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
601 RelocModel, None, CGOptLevel));
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000602}
603
604/**
Teresa Johnson26ab5772016-03-15 00:04:37 +0000605 * Produce the combined summary index from all the bitcode files:
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000606 * "thin-link".
607 */
Teresa Johnson26ab5772016-03-15 00:04:37 +0000608std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
Peter Collingbourne74d22dd2017-05-01 22:04:36 +0000609 std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
Teresa Johnson4ffc3e72018-06-06 22:22:01 +0000610 llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000611 uint64_t NextModuleId = 0;
612 for (auto &ModuleBuffer : Modules) {
Peter Collingbourne74d22dd2017-05-01 22:04:36 +0000613 if (Error Err = readModuleSummaryIndex(ModuleBuffer.getMemBuffer(),
614 *CombinedIndex, NextModuleId++)) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000615 // FIXME diagnose
Peter Collingbourne6de481a2016-11-11 19:50:39 +0000616 logAllUnhandledErrors(
Peter Collingbourne74d22dd2017-05-01 22:04:36 +0000617 std::move(Err), errs(),
Peter Collingbournec15d60b2017-05-01 20:42:32 +0000618 "error: can't create module summary index for buffer: ");
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000619 return nullptr;
620 }
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000621 }
622 return CombinedIndex;
623}
624
George Rimar2421b6f2018-01-17 10:33:05 +0000625static void internalizeAndPromoteInIndex(
626 const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
627 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
628 ModuleSummaryIndex &Index) {
629 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
630 const auto &ExportList = ExportLists.find(ModuleIdentifier);
631 return (ExportList != ExportLists.end() &&
632 ExportList->second.count(GUID)) ||
633 GUIDPreservedSymbols.count(GUID);
634 };
635
636 thinLTOInternalizeAndPromoteInIndex(Index, isExported);
637}
638
George Rimareaf51722018-01-29 08:03:30 +0000639static void computeDeadSymbolsInIndex(
640 ModuleSummaryIndex &Index,
641 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
642 // We have no symbols resolution available. And can't do any better now in the
643 // case where the prevailing symbol is in a native object. It can be refined
644 // with linker information in the future.
645 auto isPrevailing = [&](GlobalValue::GUID G) {
646 return PrevailingType::Unknown;
647 };
648 computeDeadSymbols(Index, GUIDPreservedSymbols, isPrevailing);
649}
650
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000651/**
652 * Perform promotion and renaming of exported internal functions.
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000653 * Index is updated to reflect linkage changes from weak resolution.
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000654 */
655void ThinLTOCodeGenerator::promote(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000656 ModuleSummaryIndex &Index) {
Mehdi Aminia71a5a62016-04-21 05:47:17 +0000657 auto ModuleCount = Index.modulePaths().size();
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000658 auto ModuleIdentifier = TheModule.getModuleIdentifier();
Teresa Johnson4fef68c2016-11-14 19:21:41 +0000659
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000660 // Collect for each module the list of function it defines (GUID -> Summary).
Teresa Johnsonc851d212016-04-25 21:09:51 +0000661 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000662 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000663
Teresa Johnson6c475a72017-01-05 21:34:18 +0000664 // Convert the preserved symbols set from string to GUID
Mehdi Amini1380edf2017-02-03 07:41:43 +0000665 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
Teresa Johnson6c475a72017-01-05 21:34:18 +0000666 PreservedSymbols, Triple(TheModule.getTargetTriple()));
667
668 // Compute "dead" symbols, we don't want to import/export these!
George Rimareaf51722018-01-29 08:03:30 +0000669 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
Teresa Johnson6c475a72017-01-05 21:34:18 +0000670
Mehdi Aminia71a5a62016-04-21 05:47:17 +0000671 // Generate import/export list
672 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
673 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
674 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
Evgeniy Stepanov56584bb2017-06-01 20:30:06 +0000675 ExportLists);
Mehdi Aminia71a5a62016-04-21 05:47:17 +0000676
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000677 // Resolve LinkOnce/Weak symbols.
678 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
Peter Collingbourne73589f32016-07-07 18:31:51 +0000679 resolveWeakForLinkerInIndex(Index, ResolvedODR);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000680
681 thinLTOResolveWeakForLinkerModule(
682 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000683
Teresa Johnson4fef68c2016-11-14 19:21:41 +0000684 // Promote the exported values in the index, so that they are promoted
685 // in the module.
George Rimar2421b6f2018-01-17 10:33:05 +0000686 internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
Teresa Johnson4fef68c2016-11-14 19:21:41 +0000687
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000688 promoteModule(TheModule, Index);
689}
690
691/**
692 * Perform cross-module importing for the module identified by ModuleIdentifier.
693 */
694void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000695 ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000696 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000697 auto ModuleCount = Index.modulePaths().size();
698
699 // Collect for each module the list of function it defines (GUID -> Summary).
Teresa Johnsonc851d212016-04-25 21:09:51 +0000700 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000701 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
Mehdi Amini01e32132016-03-26 05:40:34 +0000702
Teresa Johnson6c475a72017-01-05 21:34:18 +0000703 // Convert the preserved symbols set from string to GUID
Mehdi Amini1380edf2017-02-03 07:41:43 +0000704 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
Teresa Johnson6c475a72017-01-05 21:34:18 +0000705 PreservedSymbols, Triple(TheModule.getTargetTriple()));
706
707 // Compute "dead" symbols, we don't want to import/export these!
George Rimareaf51722018-01-29 08:03:30 +0000708 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
Teresa Johnson6c475a72017-01-05 21:34:18 +0000709
Mehdi Amini01e32132016-03-26 05:40:34 +0000710 // Generate import/export list
Mehdi Amini01e32132016-03-26 05:40:34 +0000711 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
712 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000713 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
Evgeniy Stepanov56584bb2017-06-01 20:30:06 +0000714 ExportLists);
Mehdi Amini01e32132016-03-26 05:40:34 +0000715 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
716
717 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000718}
719
720/**
Teresa Johnson84174c32016-05-10 13:48:23 +0000721 * Compute the list of summaries needed for importing into module.
722 */
723void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
724 StringRef ModulePath, ModuleSummaryIndex &Index,
725 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
726 auto ModuleCount = Index.modulePaths().size();
727
728 // Collect for each module the list of function it defines (GUID -> Summary).
729 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
730 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
731
732 // Generate import/export list
733 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
734 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
735 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
736 ExportLists);
737
738 llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
Mehdi Aminicdbcbf72016-08-16 05:46:05 +0000739 ImportLists[ModulePath],
Teresa Johnson84174c32016-05-10 13:48:23 +0000740 ModuleToSummariesForIndex);
741}
742
743/**
Teresa Johnson8570fe42016-05-10 15:54:09 +0000744 * Emit the list of files needed for importing into module.
745 */
746void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
747 StringRef OutputName,
748 ModuleSummaryIndex &Index) {
749 auto ModuleCount = Index.modulePaths().size();
750
751 // Collect for each module the list of function it defines (GUID -> Summary).
752 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
753 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
754
755 // Generate import/export list
756 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
757 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
758 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
759 ExportLists);
760
761 std::error_code EC;
Mehdi Aminicdbcbf72016-08-16 05:46:05 +0000762 if ((EC = EmitImportsFiles(ModulePath, OutputName, ImportLists[ModulePath])))
Teresa Johnson8570fe42016-05-10 15:54:09 +0000763 report_fatal_error(Twine("Failed to open ") + OutputName +
764 " to save imports lists\n");
765}
766
767/**
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000768 * Perform internalization. Index is updated to reflect linkage changes.
Mehdi Amini059464f2016-04-24 03:18:01 +0000769 */
770void ThinLTOCodeGenerator::internalize(Module &TheModule,
771 ModuleSummaryIndex &Index) {
772 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
773 auto ModuleCount = Index.modulePaths().size();
774 auto ModuleIdentifier = TheModule.getModuleIdentifier();
775
776 // Convert the preserved symbols set from string to GUID
777 auto GUIDPreservedSymbols =
Mehdi Amini1380edf2017-02-03 07:41:43 +0000778 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
Mehdi Amini059464f2016-04-24 03:18:01 +0000779
780 // Collect for each module the list of function it defines (GUID -> Summary).
Teresa Johnsonc851d212016-04-25 21:09:51 +0000781 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
Mehdi Amini059464f2016-04-24 03:18:01 +0000782 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
783
Teresa Johnson6c475a72017-01-05 21:34:18 +0000784 // Compute "dead" symbols, we don't want to import/export these!
George Rimareaf51722018-01-29 08:03:30 +0000785 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
Teresa Johnson6c475a72017-01-05 21:34:18 +0000786
Mehdi Amini059464f2016-04-24 03:18:01 +0000787 // Generate import/export list
788 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
789 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
790 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
Evgeniy Stepanov56584bb2017-06-01 20:30:06 +0000791 ExportLists);
Mehdi Amini059464f2016-04-24 03:18:01 +0000792 auto &ExportList = ExportLists[ModuleIdentifier];
793
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000794 // Be friendly and don't nuke totally the module when the client didn't
795 // supply anything to preserve.
796 if (ExportList.empty() && GUIDPreservedSymbols.empty())
797 return;
798
Mehdi Amini059464f2016-04-24 03:18:01 +0000799 // Internalization
George Rimar2421b6f2018-01-17 10:33:05 +0000800 internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000801 thinLTOInternalizeModule(TheModule,
802 ModuleToDefinedGVSummaries[ModuleIdentifier]);
Mehdi Amini059464f2016-04-24 03:18:01 +0000803}
804
805/**
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000806 * Perform post-importing ThinLTO optimizations.
807 */
808void ThinLTOCodeGenerator::optimize(Module &TheModule) {
809 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
Mehdi Amini059464f2016-04-24 03:18:01 +0000810
811 // Optimize now
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000812 optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000813}
814
815/**
816 * Perform ThinLTO CodeGen.
817 */
818std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
819 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
820 return codegenModule(TheModule, *TMBuilder.create());
821}
822
Mehdi Amini8e13bc42016-12-14 04:56:42 +0000823/// Write out the generated object file, either from CacheEntryPath or from
824/// OutputBuffer, preferring hard-link when possible.
825/// Returns the path to the generated file in SavedObjectsDirectoryPath.
826static std::string writeGeneratedObject(int count, StringRef CacheEntryPath,
827 StringRef SavedObjectsDirectoryPath,
828 const MemoryBuffer &OutputBuffer) {
829 SmallString<128> OutputPath(SavedObjectsDirectoryPath);
830 llvm::sys::path::append(OutputPath, Twine(count) + ".thinlto.o");
831 OutputPath.c_str(); // Ensure the string is null terminated.
832 if (sys::fs::exists(OutputPath))
833 sys::fs::remove(OutputPath);
834
835 // We don't return a memory buffer to the linker, just a list of files.
836 if (!CacheEntryPath.empty()) {
837 // Cache is enabled, hard-link the entry (or copy if hard-link fails).
838 auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
839 if (!Err)
840 return OutputPath.str();
841 // Hard linking failed, try to copy.
842 Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
843 if (!Err)
844 return OutputPath.str();
845 // Copy failed (could be because the CacheEntry was removed from the cache
846 // in the meantime by another process), fall back and try to write down the
847 // buffer to the output.
848 errs() << "error: can't link or copy from cached entry '" << CacheEntryPath
849 << "' to '" << OutputPath << "'\n";
850 }
851 // No cache entry, just write out the buffer.
852 std::error_code Err;
853 raw_fd_ostream OS(OutputPath, Err, sys::fs::F_None);
854 if (Err)
855 report_fatal_error("Can't open output '" + OutputPath + "'\n");
856 OS << OutputBuffer.getBuffer();
857 return OutputPath.str();
858}
859
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000860// Main entry point for the ThinLTO processing
861void ThinLTOCodeGenerator::run() {
Mehdi Aminib2990462017-01-20 22:45:34 +0000862 // Prepare the resulting object vector
863 assert(ProducedBinaries.empty() && "The generator should not be reused");
864 if (SavedObjectsDirectoryPath.empty())
865 ProducedBinaries.resize(Modules.size());
866 else {
867 sys::fs::create_directories(SavedObjectsDirectoryPath);
868 bool IsDir;
869 sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
870 if (!IsDir)
871 report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'");
872 ProducedBinaryFiles.resize(Modules.size());
873 }
874
Mehdi Amini43b657b2016-04-01 06:47:02 +0000875 if (CodeGenOnly) {
876 // Perform only parallel codegen and return.
877 ThreadPool Pool;
Mehdi Amini43b657b2016-04-01 06:47:02 +0000878 int count = 0;
879 for (auto &ModuleBuffer : Modules) {
880 Pool.async([&](int count) {
881 LLVMContext Context;
882 Context.setDiscardValueNames(LTODiscardValueNames);
883
884 // Parse module now
Mehdi Aminia0ddb1e2017-02-14 02:20:51 +0000885 auto TheModule =
886 loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
887 /*IsImporting*/ false);
Mehdi Amini43b657b2016-04-01 06:47:02 +0000888
889 // CodeGen
Mehdi Aminib2990462017-01-20 22:45:34 +0000890 auto OutputBuffer = codegen(*TheModule);
891 if (SavedObjectsDirectoryPath.empty())
892 ProducedBinaries[count] = std::move(OutputBuffer);
893 else
894 ProducedBinaryFiles[count] = writeGeneratedObject(
895 count, "", SavedObjectsDirectoryPath, *OutputBuffer);
Mehdi Amini43b657b2016-04-01 06:47:02 +0000896 }, count++);
897 }
898
899 return;
900 }
901
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000902 // Sequential linking phase
903 auto Index = linkCombinedIndex();
904
905 // Save temps: index.
906 if (!SaveTempsDir.empty()) {
907 auto SaveTempPath = SaveTempsDir + "index.bc";
908 std::error_code EC;
909 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
910 if (EC)
911 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
912 " to save optimized bitcode\n");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000913 WriteIndexToFile(*Index, OS);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000914 }
915
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000916
917 // Prepare the module map.
918 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini01e32132016-03-26 05:40:34 +0000919 auto ModuleCount = Modules.size();
920
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000921 // Collect for each module the list of function it defines (GUID -> Summary).
Teresa Johnsonc851d212016-04-25 21:09:51 +0000922 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000923 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
924
Teresa Johnson6c475a72017-01-05 21:34:18 +0000925 // Convert the preserved symbols set from string to GUID, this is needed for
926 // computing the caching hash and the internalization.
927 auto GUIDPreservedSymbols =
Mehdi Amini1380edf2017-02-03 07:41:43 +0000928 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
Teresa Johnson6c475a72017-01-05 21:34:18 +0000929
930 // Compute "dead" symbols, we don't want to import/export these!
George Rimareaf51722018-01-29 08:03:30 +0000931 computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
Teresa Johnson6c475a72017-01-05 21:34:18 +0000932
Mehdi Amini01e32132016-03-26 05:40:34 +0000933 // Collect the import/export lists for all modules from the call-graph in the
934 // combined index.
935 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
936 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000937 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
Evgeniy Stepanov56584bb2017-06-01 20:30:06 +0000938 ExportLists);
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000939
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000940 // We use a std::map here to be able to have a defined ordering when
941 // producing a hash for the cache entry.
942 // FIXME: we should be able to compute the caching hash for the entry based
943 // on the index, and nuke this map.
944 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
945
946 // Resolve LinkOnce/Weak symbols, this has to be computed early because it
947 // impacts the caching.
Peter Collingbourne73589f32016-07-07 18:31:51 +0000948 resolveWeakForLinkerInIndex(*Index, ResolvedODR);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000949
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000950 // Use global summary-based analysis to identify symbols that can be
951 // internalized (because they aren't exported or preserved as per callback).
952 // Changes are made in the index, consumed in the ThinLTO backends.
George Rimar2421b6f2018-01-17 10:33:05 +0000953 internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, *Index);
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000954
Teresa Johnson141149f2016-05-24 18:44:01 +0000955 // Make sure that every module has an entry in the ExportLists and
956 // ResolvedODR maps to enable threaded access to these maps below.
957 for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
Mehdi Aminiaf52f282016-05-15 05:49:47 +0000958 ExportLists[DefinedGVSummaries.first()];
Teresa Johnson141149f2016-05-24 18:44:01 +0000959 ResolvedODR[DefinedGVSummaries.first()];
960 }
Mehdi Aminiaf52f282016-05-15 05:49:47 +0000961
Mehdi Amini819e9cd2016-05-16 19:33:07 +0000962 // Compute the ordering we will process the inputs: the rough heuristic here
963 // is to sort them per size so that the largest module get schedule as soon as
964 // possible. This is purely a compile-time optimization.
965 std::vector<int> ModulesOrdering;
966 ModulesOrdering.resize(Modules.size());
967 std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
Mandeep Singh Grang5416af72018-04-13 19:12:20 +0000968 llvm::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
969 [&](int LeftIndex, int RightIndex) {
970 auto LSize = Modules[LeftIndex].getBuffer().size();
971 auto RSize = Modules[RightIndex].getBuffer().size();
972 return LSize > RSize;
973 });
Mehdi Amini819e9cd2016-05-16 19:33:07 +0000974
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000975 // Parallel optimizer + codegen
976 {
977 ThreadPool Pool(ThreadCount);
Mehdi Amini819e9cd2016-05-16 19:33:07 +0000978 for (auto IndexCount : ModulesOrdering) {
979 auto &ModuleBuffer = Modules[IndexCount];
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000980 Pool.async([&](int count) {
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000981 auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
Mehdi Aminia71a5a62016-04-21 05:47:17 +0000982 auto &ExportList = ExportLists[ModuleIdentifier];
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000983
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000984 auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
985
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000986 // The module may be cached, this helps handling it.
Mehdi Amini059464f2016-04-24 03:18:01 +0000987 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
988 ImportLists[ModuleIdentifier], ExportList,
Teresa Johnson4d2613f2016-05-24 17:24:25 +0000989 ResolvedODR[ModuleIdentifier],
Mehdi Aminic92b6122017-01-10 00:55:47 +0000990 DefinedFunctions, GUIDPreservedSymbols,
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000991 OptLevel, Freestanding, TMBuilder);
Mehdi Amini8e13bc42016-12-14 04:56:42 +0000992 auto CacheEntryPath = CacheEntry.getEntryPath();
Mehdi Aminif95f77a2016-04-21 05:54:23 +0000993
994 {
995 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000996 LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
997 << " '" << CacheEntryPath << "' for buffer "
998 << count << " " << ModuleIdentifier << "\n");
Mehdi Amini059464f2016-04-24 03:18:01 +0000999
Mehdi Aminif95f77a2016-04-21 05:54:23 +00001000 if (ErrOrBuffer) {
1001 // Cache Hit!
Mehdi Amini8e13bc42016-12-14 04:56:42 +00001002 if (SavedObjectsDirectoryPath.empty())
1003 ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1004 else
1005 ProducedBinaryFiles[count] = writeGeneratedObject(
1006 count, CacheEntryPath, SavedObjectsDirectoryPath,
1007 *ErrOrBuffer.get());
Mehdi Aminif95f77a2016-04-21 05:54:23 +00001008 return;
1009 }
1010 }
1011
1012 LLVMContext Context;
1013 Context.setDiscardValueNames(LTODiscardValueNames);
1014 Context.enableDebugTypeODRUniquing();
Davide Italiano690ed9d2017-02-10 23:49:38 +00001015 auto DiagFileOrErr = lto::setupOptimizationRemarks(
Bob Haarmanfb2d3422018-03-08 01:13:10 +00001016 Context, LTORemarksFilename, LTOPassRemarksWithHotness, count);
Mehdi Amini19f176b2016-11-19 18:20:05 +00001017 if (!DiagFileOrErr) {
1018 errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1019 report_fatal_error("ThinLTO: Can't get an output file for the "
1020 "remarks");
1021 }
Mehdi Aminif95f77a2016-04-21 05:54:23 +00001022
Mehdi Amini7c4a1a82016-03-09 01:37:22 +00001023 // Parse module now
Mehdi Aminia0ddb1e2017-02-14 02:20:51 +00001024 auto TheModule =
1025 loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
1026 /*IsImporting*/ false);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +00001027
1028 // Save temps: original file.
Mehdi Amini059464f2016-04-24 03:18:01 +00001029 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
Mehdi Amini7c4a1a82016-03-09 01:37:22 +00001030
Mehdi Amini1aafabf2016-04-16 07:02:16 +00001031 auto &ImportList = ImportLists[ModuleIdentifier];
Mehdi Amini059464f2016-04-24 03:18:01 +00001032 // Run the main process now, and generates a binary
Mehdi Aminif95f77a2016-04-21 05:54:23 +00001033 auto OutputBuffer = ProcessThinLTOModule(
Mehdi Amini01e32132016-03-26 05:40:34 +00001034 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
Teresa Johnson4d2613f2016-05-24 17:24:25 +00001035 ExportList, GUIDPreservedSymbols,
1036 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
Mehdi Aminib5a46c12017-03-28 18:55:44 +00001037 DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
Mehdi Aminif95f77a2016-04-21 05:54:23 +00001038
Mehdi Amini8e13bc42016-12-14 04:56:42 +00001039 // Commit to the cache (if enabled)
1040 CacheEntry.write(*OutputBuffer);
1041
1042 if (SavedObjectsDirectoryPath.empty()) {
1043 // We need to generated a memory buffer for the linker.
1044 if (!CacheEntryPath.empty()) {
Ekaterina Romanova0b01dfb2018-03-30 21:35:42 +00001045 // When cache is enabled, reload from the cache if possible.
1046 // Releasing the buffer from the heap and reloading it from the
1047 // cache file with mmap helps us to lower memory pressure.
1048 // The freed memory can be used for the next input file.
1049 // The final binary link will read from the VFS cache (hopefully!)
1050 // or from disk (if the memory pressure was too high).
Mehdi Amini8e13bc42016-12-14 04:56:42 +00001051 auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1052 if (auto EC = ReloadedBufferOrErr.getError()) {
Ekaterina Romanova0b01dfb2018-03-30 21:35:42 +00001053 // On error, keep the preexisting buffer and print a diagnostic.
Mehdi Amini8e13bc42016-12-14 04:56:42 +00001054 errs() << "error: can't reload cached file '" << CacheEntryPath
1055 << "': " << EC.message() << "\n";
1056 } else {
1057 OutputBuffer = std::move(*ReloadedBufferOrErr);
1058 }
1059 }
1060 ProducedBinaries[count] = std::move(OutputBuffer);
1061 return;
1062 }
1063 ProducedBinaryFiles[count] = writeGeneratedObject(
1064 count, CacheEntryPath, SavedObjectsDirectoryPath, *OutputBuffer);
Mehdi Amini819e9cd2016-05-16 19:33:07 +00001065 }, IndexCount);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +00001066 }
1067 }
1068
Peter Collingbournecead56f2017-03-15 22:54:18 +00001069 pruneCache(CacheOptions.Path, CacheOptions.Policy);
Mehdi Aminif95f77a2016-04-21 05:54:23 +00001070
Mehdi Amini7c4a1a82016-03-09 01:37:22 +00001071 // If statistics were requested, print them out now.
1072 if (llvm::AreStatisticsEnabled())
1073 llvm::PrintStatistics();
James Henderson852f6fd2017-05-16 09:43:21 +00001074 reportAndResetTimings();
Mehdi Amini7c4a1a82016-03-09 01:37:22 +00001075}