blob: b8679767e208517bef9a1257ddba055ced8c8d3e [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
15#include "llvm/LTO/ThinLTOCodeGenerator.h"
16
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"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000019#include "llvm/Analysis/TargetLibraryInfo.h"
20#include "llvm/Analysis/TargetTransformInfo.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000021#include "llvm/Bitcode/BitcodeWriterPass.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000022#include "llvm/Bitcode/ReaderWriter.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000023#include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000024#include "llvm/IR/DiagnosticPrinter.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000025#include "llvm/IR/LLVMContext.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000026#include "llvm/IR/LegacyPassManager.h"
27#include "llvm/IR/Mangler.h"
28#include "llvm/IRReader/IRReader.h"
29#include "llvm/Linker/Linker.h"
30#include "llvm/MC/SubtargetFeature.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000031#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000032#include "llvm/Support/SourceMgr.h"
33#include "llvm/Support/TargetRegistry.h"
34#include "llvm/Support/ThreadPool.h"
35#include "llvm/Target/TargetMachine.h"
36#include "llvm/Transforms/IPO.h"
37#include "llvm/Transforms/IPO/FunctionImport.h"
38#include "llvm/Transforms/IPO/PassManagerBuilder.h"
39#include "llvm/Transforms/ObjCARC.h"
40#include "llvm/Transforms/Utils/FunctionImportUtils.h"
41
42using namespace llvm;
43
Mehdi Amini09b4a8d2016-03-10 01:28:54 +000044namespace llvm {
45// Flags -discard-value-names, defined in LTOCodeGenerator.cpp
46extern cl::opt<bool> LTODiscardValueNames;
47}
48
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000049namespace {
50
51static cl::opt<int> ThreadCount("threads",
52 cl::init(std::thread::hardware_concurrency()));
53
54static void diagnosticHandler(const DiagnosticInfo &DI) {
55 DiagnosticPrinterRawOStream DP(errs());
56 DI.print(DP);
57 errs() << '\n';
58}
59
60// Simple helper to load a module from bitcode
61static std::unique_ptr<Module>
62loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
63 bool Lazy) {
64 SMDiagnostic Err;
65 ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr);
66 if (Lazy) {
67 ModuleOrErr =
68 getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
69 /* ShouldLazyLoadMetadata */ Lazy);
70 } else {
71 ModuleOrErr = parseBitcodeFile(Buffer, Context);
72 }
73 if (std::error_code EC = ModuleOrErr.getError()) {
74 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
75 EC.message());
76 Err.print("ThinLTO", errs());
77 report_fatal_error("Can't load module, abort.");
78 }
79 return std::move(ModuleOrErr.get());
80}
81
82// Simple helper to save temporary files for debug.
83static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
84 unsigned count, StringRef Suffix) {
85 if (TempDir.empty())
86 return;
87 // User asked to save temps, let dump the bitcode file after import.
88 auto SaveTempPath = TempDir + llvm::utostr(count) + Suffix;
89 std::error_code EC;
90 raw_fd_ostream OS(SaveTempPath.str(), EC, sys::fs::F_None);
91 if (EC)
92 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
93 " to save optimized bitcode\n");
94 WriteBitcodeToFile(&TheModule, OS, true, false);
95}
96
97static StringMap<MemoryBufferRef>
98generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
99 StringMap<MemoryBufferRef> ModuleMap;
100 for (auto &ModuleBuffer : Modules) {
101 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
102 ModuleMap.end() &&
103 "Expect unique Buffer Identifier");
104 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
105 }
106 return ModuleMap;
107}
108
109/// Provide a "loader" for the FunctionImporter to access function from other
110/// modules.
111class ModuleLoader {
112 /// The context that will be used for importing.
113 LLVMContext &Context;
114
115 /// Map from Module identifier to MemoryBuffer. Used by clients like the
116 /// FunctionImported to request loading a Module.
117 StringMap<MemoryBufferRef> &ModuleMap;
118
119public:
120 ModuleLoader(LLVMContext &Context, StringMap<MemoryBufferRef> &ModuleMap)
121 : Context(Context), ModuleMap(ModuleMap) {}
122
123 /// Load a module on demand.
124 std::unique_ptr<Module> operator()(StringRef Identifier) {
125 return loadModuleFromBuffer(ModuleMap[Identifier], Context, /*Lazy*/ true);
126 }
127};
128
Teresa Johnson26ab5772016-03-15 00:04:37 +0000129static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000130 if (renameModuleForThinLTO(TheModule, Index))
131 report_fatal_error("renameModuleForThinLTO failed");
132}
133
Mehdi Amini01e32132016-03-26 05:40:34 +0000134static void
135crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
136 StringMap<MemoryBufferRef> &ModuleMap,
137 const FunctionImporter::ImportMapTy &ImportList) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000138 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
139 FunctionImporter Importer(Index, Loader);
Mehdi Amini01e32132016-03-26 05:40:34 +0000140 Importer.importFunctions(TheModule, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000141}
142
143static void optimizeModule(Module &TheModule, TargetMachine &TM) {
144 // Populate the PassManager
145 PassManagerBuilder PMB;
146 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
147 PMB.Inliner = createFunctionInliningPass();
148 // FIXME: should get it from the bitcode?
149 PMB.OptLevel = 3;
150 PMB.LoopVectorize = true;
151 PMB.SLPVectorize = true;
152 PMB.VerifyInput = true;
153 PMB.VerifyOutput = false;
154
155 legacy::PassManager PM;
156
157 // Add the TTI (required to inform the vectorizer about register size for
158 // instance)
159 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
160
161 // Add optimizations
162 PMB.populateThinLTOPassManager(PM);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000163
164 PM.run(TheModule);
165}
166
167std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
168 TargetMachine &TM) {
169 SmallVector<char, 128> OutputBuffer;
170
171 // CodeGen
172 {
173 raw_svector_ostream OS(OutputBuffer);
174 legacy::PassManager PM;
Mehdi Amini215d59e2016-04-01 08:22:59 +0000175
176 // If the bitcode files contain ARC code and were compiled with optimization,
177 // the ObjCARCContractPass must be run, so do it unconditionally here.
178 PM.add(createObjCARCContractPass());
179
180 // Setup the codegen now.
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000181 if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
182 /* DisableVerify */ true))
183 report_fatal_error("Failed to setup codegen");
184
185 // Run codegen now. resulting binary is in OutputBuffer.
186 PM.run(TheModule);
187 }
188 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
189}
190
191static std::unique_ptr<MemoryBuffer>
Teresa Johnson26ab5772016-03-15 00:04:37 +0000192ProcessThinLTOModule(Module &TheModule, const ModuleSummaryIndex &Index,
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000193 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
Mehdi Amini01e32132016-03-26 05:40:34 +0000194 const FunctionImporter::ImportMapTy &ImportList,
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000195 ThinLTOCodeGenerator::CachingOptions CacheOptions,
Mehdi Amini43b657b2016-04-01 06:47:02 +0000196 bool DisableCodeGen, StringRef SaveTempsDir,
197 unsigned count) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000198
199 // Save temps: after IPO.
200 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.IPO.bc");
201
202 // "Benchmark"-like optimization: single-source case
203 bool SingleModule = (ModuleMap.size() == 1);
204
205 if (!SingleModule) {
206 promoteModule(TheModule, Index);
207
208 // Save temps: after promotion.
209 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.promoted.bc");
210
Mehdi Amini01e32132016-03-26 05:40:34 +0000211 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000212
213 // Save temps: after cross-module import.
214 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
215 }
216
217 optimizeModule(TheModule, TM);
218
219 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.opt.bc");
220
Mehdi Amini43b657b2016-04-01 06:47:02 +0000221 if (DisableCodeGen) {
222 // Configured to stop before CodeGen, serialize the bitcode and return.
223 SmallVector<char, 128> OutputBuffer;
224 {
225 raw_svector_ostream OS(OutputBuffer);
226 WriteBitcodeToFile(&TheModule, OS, true, true);
227 }
228 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
229 }
230
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000231 return codegenModule(TheModule, TM);
232}
233
234// Initialize the TargetMachine builder for a given Triple
235static void initTMBuilder(TargetMachineBuilder &TMBuilder,
236 const Triple &TheTriple) {
237 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
238 // FIXME this looks pretty terrible...
239 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
240 if (TheTriple.getArch() == llvm::Triple::x86_64)
241 TMBuilder.MCpu = "core2";
242 else if (TheTriple.getArch() == llvm::Triple::x86)
243 TMBuilder.MCpu = "yonah";
244 else if (TheTriple.getArch() == llvm::Triple::aarch64)
245 TMBuilder.MCpu = "cyclone";
246 }
247 TMBuilder.TheTriple = std::move(TheTriple);
248}
249
250} // end anonymous namespace
251
252void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
253 MemoryBufferRef Buffer(Data, Identifier);
254 if (Modules.empty()) {
255 // First module added, so initialize the triple and some options
256 LLVMContext Context;
257 Triple TheTriple(getBitcodeTargetTriple(Buffer, Context));
258 initTMBuilder(TMBuilder, Triple(TheTriple));
259 }
260#ifndef NDEBUG
261 else {
262 LLVMContext Context;
263 assert(TMBuilder.TheTriple.str() ==
264 getBitcodeTargetTriple(Buffer, Context) &&
265 "ThinLTO modules with different triple not supported");
266 }
267#endif
268 Modules.push_back(Buffer);
269}
270
271void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
272 PreservedSymbols.insert(Name);
273}
274
275void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
276 CrossReferencedSymbols.insert(Name);
277}
278
279// TargetMachine factory
280std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
281 std::string ErrMsg;
282 const Target *TheTarget =
283 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
284 if (!TheTarget) {
285 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
286 }
287
288 // Use MAttr as the default set of features.
289 SubtargetFeatures Features(MAttr);
290 Features.getDefaultSubtargetFeatures(TheTriple);
291 std::string FeatureStr = Features.getString();
292 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
293 TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
294 CodeModel::Default, CGOptLevel));
295}
296
297/**
Teresa Johnson26ab5772016-03-15 00:04:37 +0000298 * Produce the combined summary index from all the bitcode files:
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000299 * "thin-link".
300 */
Teresa Johnson26ab5772016-03-15 00:04:37 +0000301std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
302 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000303 uint64_t NextModuleId = 0;
304 for (auto &ModuleBuffer : Modules) {
Teresa Johnson26ab5772016-03-15 00:04:37 +0000305 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
306 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
307 diagnosticHandler, false);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000308 if (std::error_code EC = ObjOrErr.getError()) {
309 // FIXME diagnose
Teresa Johnson26ab5772016-03-15 00:04:37 +0000310 errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000311 << EC.message() << "\n";
312 return nullptr;
313 }
314 auto Index = (*ObjOrErr)->takeIndex();
315 if (CombinedIndex) {
316 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
317 } else {
318 CombinedIndex = std::move(Index);
319 }
320 }
321 return CombinedIndex;
322}
323
324/**
325 * Perform promotion and renaming of exported internal functions.
326 */
327void ThinLTOCodeGenerator::promote(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000328 ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000329 promoteModule(TheModule, Index);
330}
331
332/**
333 * Perform cross-module importing for the module identified by ModuleIdentifier.
334 */
335void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000336 ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000337 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini01e32132016-03-26 05:40:34 +0000338
339 // Generate import/export list
340 auto ModuleCount = Index.modulePaths().size();
341 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
342 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
343 ComputeCrossModuleImport(Index, ImportLists, ExportLists);
344 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
345
346 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000347}
348
349/**
350 * Perform post-importing ThinLTO optimizations.
351 */
352void ThinLTOCodeGenerator::optimize(Module &TheModule) {
353 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
354 optimizeModule(TheModule, *TMBuilder.create());
355}
356
357/**
358 * Perform ThinLTO CodeGen.
359 */
360std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
361 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
362 return codegenModule(TheModule, *TMBuilder.create());
363}
364
365// Main entry point for the ThinLTO processing
366void ThinLTOCodeGenerator::run() {
Mehdi Amini43b657b2016-04-01 06:47:02 +0000367 if (CodeGenOnly) {
368 // Perform only parallel codegen and return.
369 ThreadPool Pool;
370 assert(ProducedBinaries.empty() && "The generator should not be reused");
371 ProducedBinaries.resize(Modules.size());
372 int count = 0;
373 for (auto &ModuleBuffer : Modules) {
374 Pool.async([&](int count) {
375 LLVMContext Context;
376 Context.setDiscardValueNames(LTODiscardValueNames);
377
378 // Parse module now
379 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
380
381 // CodeGen
382 ProducedBinaries[count] = codegen(*TheModule);
383 }, count++);
384 }
385
386 return;
387 }
388
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000389 // Sequential linking phase
390 auto Index = linkCombinedIndex();
391
392 // Save temps: index.
393 if (!SaveTempsDir.empty()) {
394 auto SaveTempPath = SaveTempsDir + "index.bc";
395 std::error_code EC;
396 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
397 if (EC)
398 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
399 " to save optimized bitcode\n");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000400 WriteIndexToFile(*Index, OS);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000401 }
402
403 // Prepare the resulting object vector
404 assert(ProducedBinaries.empty() && "The generator should not be reused");
405 ProducedBinaries.resize(Modules.size());
406
407 // Prepare the module map.
408 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini01e32132016-03-26 05:40:34 +0000409 auto ModuleCount = Modules.size();
410
411 // Collect the import/export lists for all modules from the call-graph in the
412 // combined index.
413 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
414 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
415 ComputeCrossModuleImport(*Index, ImportLists, ExportLists);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000416
417 // Parallel optimizer + codegen
418 {
419 ThreadPool Pool(ThreadCount);
420 int count = 0;
421 for (auto &ModuleBuffer : Modules) {
422 Pool.async([&](int count) {
423 LLVMContext Context;
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000424 Context.setDiscardValueNames(LTODiscardValueNames);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000425
426 // Parse module now
427 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
428
429 // Save temps: original file.
430 if (!SaveTempsDir.empty()) {
431 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
432 }
433
Mehdi Amini01e32132016-03-26 05:40:34 +0000434 auto &ImportList = ImportLists[TheModule->getModuleIdentifier()];
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000435 ProducedBinaries[count] = ProcessThinLTOModule(
Mehdi Amini01e32132016-03-26 05:40:34 +0000436 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
Mehdi Amini43b657b2016-04-01 06:47:02 +0000437 CacheOptions, DisableCodeGen, SaveTempsDir, count);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000438 }, count);
439 count++;
440 }
441 }
442
443 // If statistics were requested, print them out now.
444 if (llvm::AreStatisticsEnabled())
445 llvm::PrintStatistics();
446}