1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
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 #include "clang/CodeGen/BackendUtil.h"
11 #include "clang/Basic/Diagnostic.h"
12 #include "clang/Basic/LangOptions.h"
13 #include "clang/Basic/TargetOptions.h"
14 #include "clang/Frontend/CodeGenOptions.h"
15 #include "clang/Frontend/FrontendDiagnostic.h"
16 #include "clang/Frontend/Utils.h"
17 #include "clang/Lex/HeaderSearchOptions.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include "llvm/Analysis/TargetTransformInfo.h"
24 #include "llvm/Bitcode/BitcodeReader.h"
25 #include "llvm/Bitcode/BitcodeWriter.h"
26 #include "llvm/Bitcode/BitcodeWriterPass.h"
27 #include "llvm/CodeGen/RegAllocRegistry.h"
28 #include "llvm/CodeGen/SchedulerRegistry.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/IRPrintingPasses.h"
31 #include "llvm/IR/LegacyPassManager.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/ModuleSummaryIndex.h"
34 #include "llvm/IR/Verifier.h"
35 #include "llvm/LTO/LTOBackend.h"
36 #include "llvm/MC/MCAsmInfo.h"
37 #include "llvm/MC/SubtargetFeature.h"
38 #include "llvm/Passes/PassBuilder.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/MemoryBuffer.h"
41 #include "llvm/Support/PrettyStackTrace.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/Timer.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Target/TargetMachine.h"
46 #include "llvm/Target/TargetOptions.h"
47 #include "llvm/Target/TargetSubtargetInfo.h"
48 #include "llvm/Transforms/Coroutines.h"
49 #include "llvm/Transforms/IPO.h"
50 #include "llvm/Transforms/IPO/AlwaysInliner.h"
51 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
52 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
53 #include "llvm/Transforms/Instrumentation.h"
54 #include "llvm/Transforms/ObjCARC.h"
55 #include "llvm/Transforms/Scalar.h"
56 #include "llvm/Transforms/Scalar/GVN.h"
57 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
58 #include "llvm/Transforms/Utils/SymbolRewriter.h"
59 #include <memory>
60 using namespace clang;
61 using namespace llvm;
62 
63 namespace {
64 
65 // Default filename used for profile generation.
66 static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw";
67 
68 class EmitAssemblyHelper {
69   DiagnosticsEngine &Diags;
70   const HeaderSearchOptions &HSOpts;
71   const CodeGenOptions &CodeGenOpts;
72   const clang::TargetOptions &TargetOpts;
73   const LangOptions &LangOpts;
74   Module *TheModule;
75 
76   Timer CodeGenerationTime;
77 
78   std::unique_ptr<raw_pwrite_stream> OS;
79 
80   TargetIRAnalysis getTargetIRAnalysis() const {
81     if (TM)
82       return TM->getTargetIRAnalysis();
83 
84     return TargetIRAnalysis();
85   }
86 
87   void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
88 
89   /// Generates the TargetMachine.
90   /// Leaves TM unchanged if it is unable to create the target machine.
91   /// Some of our clang tests specify triples which are not built
92   /// into clang. This is okay because these tests check the generated
93   /// IR, and they require DataLayout which depends on the triple.
94   /// In this case, we allow this method to fail and not report an error.
95   /// When MustCreateTM is used, we print an error if we are unable to load
96   /// the requested target.
97   void CreateTargetMachine(bool MustCreateTM);
98 
99   /// Add passes necessary to emit assembly or LLVM IR.
100   ///
101   /// \return True on success.
102   bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
103                      raw_pwrite_stream &OS);
104 
105 public:
106   EmitAssemblyHelper(DiagnosticsEngine &_Diags,
107                      const HeaderSearchOptions &HeaderSearchOpts,
108                      const CodeGenOptions &CGOpts,
109                      const clang::TargetOptions &TOpts,
110                      const LangOptions &LOpts, Module *M)
111       : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
112         TargetOpts(TOpts), LangOpts(LOpts), TheModule(M),
113         CodeGenerationTime("codegen", "Code Generation Time") {}
114 
115   ~EmitAssemblyHelper() {
116     if (CodeGenOpts.DisableFree)
117       BuryPointer(std::move(TM));
118   }
119 
120   std::unique_ptr<TargetMachine> TM;
121 
122   void EmitAssembly(BackendAction Action,
123                     std::unique_ptr<raw_pwrite_stream> OS);
124 
125   void EmitAssemblyWithNewPassManager(BackendAction Action,
126                                       std::unique_ptr<raw_pwrite_stream> OS);
127 };
128 
129 // We need this wrapper to access LangOpts and CGOpts from extension functions
130 // that we add to the PassManagerBuilder.
131 class PassManagerBuilderWrapper : public PassManagerBuilder {
132 public:
133   PassManagerBuilderWrapper(const Triple &TargetTriple,
134                             const CodeGenOptions &CGOpts,
135                             const LangOptions &LangOpts)
136       : PassManagerBuilder(), TargetTriple(TargetTriple), CGOpts(CGOpts),
137         LangOpts(LangOpts) {}
138   const Triple &getTargetTriple() const { return TargetTriple; }
139   const CodeGenOptions &getCGOpts() const { return CGOpts; }
140   const LangOptions &getLangOpts() const { return LangOpts; }
141 
142 private:
143   const Triple &TargetTriple;
144   const CodeGenOptions &CGOpts;
145   const LangOptions &LangOpts;
146 };
147 }
148 
149 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
150   if (Builder.OptLevel > 0)
151     PM.add(createObjCARCAPElimPass());
152 }
153 
154 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
155   if (Builder.OptLevel > 0)
156     PM.add(createObjCARCExpandPass());
157 }
158 
159 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
160   if (Builder.OptLevel > 0)
161     PM.add(createObjCARCOptPass());
162 }
163 
164 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
165                                      legacy::PassManagerBase &PM) {
166   PM.add(createAddDiscriminatorsPass());
167 }
168 
169 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
170                                   legacy::PassManagerBase &PM) {
171   PM.add(createBoundsCheckingPass());
172 }
173 
174 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
175                                      legacy::PassManagerBase &PM) {
176   const PassManagerBuilderWrapper &BuilderWrapper =
177       static_cast<const PassManagerBuilderWrapper&>(Builder);
178   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
179   SanitizerCoverageOptions Opts;
180   Opts.CoverageType =
181       static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
182   Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
183   Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
184   Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
185   Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
186   Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
187   Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
188   Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
189   Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
190   Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
191   Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
192   Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
193   Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
194   PM.add(createSanitizerCoverageModulePass(Opts));
195 }
196 
197 // Check if ASan should use GC-friendly instrumentation for globals.
198 // First of all, there is no point if -fdata-sections is off (expect for MachO,
199 // where this is not a factor). Also, on ELF this feature requires an assembler
200 // extension that only works with -integrated-as at the moment.
201 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
202   if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
203     return false;
204   switch (T.getObjectFormat()) {
205   case Triple::MachO:
206   case Triple::COFF:
207     return true;
208   case Triple::ELF:
209     return CGOpts.DataSections && !CGOpts.DisableIntegratedAS;
210   default:
211     return false;
212   }
213 }
214 
215 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
216                                       legacy::PassManagerBase &PM) {
217   const PassManagerBuilderWrapper &BuilderWrapper =
218       static_cast<const PassManagerBuilderWrapper&>(Builder);
219   const Triple &T = BuilderWrapper.getTargetTriple();
220   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
221   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
222   bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
223   bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts);
224   PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
225                                             UseAfterScope));
226   PM.add(createAddressSanitizerModulePass(/*CompileKernel*/ false, Recover,
227                                           UseGlobalsGC));
228 }
229 
230 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
231                                             legacy::PassManagerBase &PM) {
232   PM.add(createAddressSanitizerFunctionPass(
233       /*CompileKernel*/ true,
234       /*Recover*/ true, /*UseAfterScope*/ false));
235   PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true,
236                                           /*Recover*/true));
237 }
238 
239 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
240                                    legacy::PassManagerBase &PM) {
241   const PassManagerBuilderWrapper &BuilderWrapper =
242       static_cast<const PassManagerBuilderWrapper&>(Builder);
243   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
244   int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
245   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
246   PM.add(createMemorySanitizerPass(TrackOrigins, Recover));
247 
248   // MemorySanitizer inserts complex instrumentation that mostly follows
249   // the logic of the original code, but operates on "shadow" values.
250   // It can benefit from re-running some general purpose optimization passes.
251   if (Builder.OptLevel > 0) {
252     PM.add(createEarlyCSEPass());
253     PM.add(createReassociatePass());
254     PM.add(createLICMPass());
255     PM.add(createGVNPass());
256     PM.add(createInstructionCombiningPass());
257     PM.add(createDeadStoreEliminationPass());
258   }
259 }
260 
261 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
262                                    legacy::PassManagerBase &PM) {
263   PM.add(createThreadSanitizerPass());
264 }
265 
266 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
267                                      legacy::PassManagerBase &PM) {
268   const PassManagerBuilderWrapper &BuilderWrapper =
269       static_cast<const PassManagerBuilderWrapper&>(Builder);
270   const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
271   PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
272 }
273 
274 static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder,
275                                        legacy::PassManagerBase &PM) {
276   const PassManagerBuilderWrapper &BuilderWrapper =
277       static_cast<const PassManagerBuilderWrapper&>(Builder);
278   const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
279   EfficiencySanitizerOptions Opts;
280   if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag))
281     Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag;
282   else if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyWorkingSet))
283     Opts.ToolType = EfficiencySanitizerOptions::ESAN_WorkingSet;
284   PM.add(createEfficiencySanitizerPass(Opts));
285 }
286 
287 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
288                                          const CodeGenOptions &CodeGenOpts) {
289   TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
290   if (!CodeGenOpts.SimplifyLibCalls)
291     TLII->disableAllFunctions();
292   else {
293     // Disable individual libc/libm calls in TargetLibraryInfo.
294     LibFunc F;
295     for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
296       if (TLII->getLibFunc(FuncName, F))
297         TLII->setUnavailable(F);
298   }
299 
300   switch (CodeGenOpts.getVecLib()) {
301   case CodeGenOptions::Accelerate:
302     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
303     break;
304   case CodeGenOptions::SVML:
305     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
306     break;
307   default:
308     break;
309   }
310   return TLII;
311 }
312 
313 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
314                                   legacy::PassManager *MPM) {
315   llvm::SymbolRewriter::RewriteDescriptorList DL;
316 
317   llvm::SymbolRewriter::RewriteMapParser MapParser;
318   for (const auto &MapFile : Opts.RewriteMapFiles)
319     MapParser.parse(MapFile, &DL);
320 
321   MPM->add(createRewriteSymbolsPass(DL));
322 }
323 
324 static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
325   switch (CodeGenOpts.OptimizationLevel) {
326   default:
327     llvm_unreachable("Invalid optimization level!");
328   case 0:
329     return CodeGenOpt::None;
330   case 1:
331     return CodeGenOpt::Less;
332   case 2:
333     return CodeGenOpt::Default; // O2/Os/Oz
334   case 3:
335     return CodeGenOpt::Aggressive;
336   }
337 }
338 
339 static Optional<llvm::CodeModel::Model>
340 getCodeModel(const CodeGenOptions &CodeGenOpts) {
341   unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
342                            .Case("small", llvm::CodeModel::Small)
343                            .Case("kernel", llvm::CodeModel::Kernel)
344                            .Case("medium", llvm::CodeModel::Medium)
345                            .Case("large", llvm::CodeModel::Large)
346                            .Case("default", ~1u)
347                            .Default(~0u);
348   assert(CodeModel != ~0u && "invalid code model!");
349   if (CodeModel == ~1u)
350     return None;
351   return static_cast<llvm::CodeModel::Model>(CodeModel);
352 }
353 
354 static llvm::Reloc::Model getRelocModel(const CodeGenOptions &CodeGenOpts) {
355   // Keep this synced with the equivalent code in
356   // lib/Frontend/CompilerInvocation.cpp
357   llvm::Optional<llvm::Reloc::Model> RM;
358   RM = llvm::StringSwitch<llvm::Reloc::Model>(CodeGenOpts.RelocationModel)
359       .Case("static", llvm::Reloc::Static)
360       .Case("pic", llvm::Reloc::PIC_)
361       .Case("ropi", llvm::Reloc::ROPI)
362       .Case("rwpi", llvm::Reloc::RWPI)
363       .Case("ropi-rwpi", llvm::Reloc::ROPI_RWPI)
364       .Case("dynamic-no-pic", llvm::Reloc::DynamicNoPIC);
365   assert(RM.hasValue() && "invalid PIC model!");
366   return *RM;
367 }
368 
369 static TargetMachine::CodeGenFileType getCodeGenFileType(BackendAction Action) {
370   if (Action == Backend_EmitObj)
371     return TargetMachine::CGFT_ObjectFile;
372   else if (Action == Backend_EmitMCNull)
373     return TargetMachine::CGFT_Null;
374   else {
375     assert(Action == Backend_EmitAssembly && "Invalid action!");
376     return TargetMachine::CGFT_AssemblyFile;
377   }
378 }
379 
380 static void initTargetOptions(llvm::TargetOptions &Options,
381                               const CodeGenOptions &CodeGenOpts,
382                               const clang::TargetOptions &TargetOpts,
383                               const LangOptions &LangOpts,
384                               const HeaderSearchOptions &HSOpts) {
385   Options.ThreadModel =
386       llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
387           .Case("posix", llvm::ThreadModel::POSIX)
388           .Case("single", llvm::ThreadModel::Single);
389 
390   // Set float ABI type.
391   assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
392           CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
393          "Invalid Floating Point ABI!");
394   Options.FloatABIType =
395       llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
396           .Case("soft", llvm::FloatABI::Soft)
397           .Case("softfp", llvm::FloatABI::Soft)
398           .Case("hard", llvm::FloatABI::Hard)
399           .Default(llvm::FloatABI::Default);
400 
401   // Set FP fusion mode.
402   switch (LangOpts.getDefaultFPContractMode()) {
403   case LangOptions::FPC_Off:
404     // Preserve any contraction performed by the front-end.  (Strict performs
405     // splitting of the muladd instrinsic in the backend.)
406     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
407     break;
408   case LangOptions::FPC_On:
409     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
410     break;
411   case LangOptions::FPC_Fast:
412     Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
413     break;
414   }
415 
416   Options.UseInitArray = CodeGenOpts.UseInitArray;
417   Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
418   Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
419   Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
420 
421   // Set EABI version.
422   Options.EABIVersion = TargetOpts.EABIVersion;
423 
424   if (LangOpts.SjLjExceptions)
425     Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
426 
427   Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
428   Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
429   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
430   Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
431   Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
432   Options.FunctionSections = CodeGenOpts.FunctionSections;
433   Options.DataSections = CodeGenOpts.DataSections;
434   Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
435   Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
436   Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
437 
438   if (CodeGenOpts.EnableSplitDwarf)
439     Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
440   Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
441   Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
442   Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
443   Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
444   Options.MCOptions.MCIncrementalLinkerCompatible =
445       CodeGenOpts.IncrementalLinkerCompatible;
446   Options.MCOptions.MCPIECopyRelocations = CodeGenOpts.PIECopyRelocations;
447   Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
448   Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
449   Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
450   Options.MCOptions.ABIName = TargetOpts.ABI;
451   for (const auto &Entry : HSOpts.UserEntries)
452     if (!Entry.IsFramework &&
453         (Entry.Group == frontend::IncludeDirGroup::Quoted ||
454          Entry.Group == frontend::IncludeDirGroup::Angled ||
455          Entry.Group == frontend::IncludeDirGroup::System))
456       Options.MCOptions.IASSearchPaths.push_back(
457           Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
458 }
459 
460 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
461                                       legacy::FunctionPassManager &FPM) {
462   // Handle disabling of all LLVM passes, where we want to preserve the
463   // internal module before any optimization.
464   if (CodeGenOpts.DisableLLVMPasses)
465     return;
466 
467   // Figure out TargetLibraryInfo.  This needs to be added to MPM and FPM
468   // manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
469   // are inserted before PMBuilder ones - they'd get the default-constructed
470   // TLI with an unknown target otherwise.
471   Triple TargetTriple(TheModule->getTargetTriple());
472   std::unique_ptr<TargetLibraryInfoImpl> TLII(
473       createTLII(TargetTriple, CodeGenOpts));
474 
475   PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts);
476 
477   // At O0 and O1 we only run the always inliner which is more efficient. At
478   // higher optimization levels we run the normal inliner.
479   if (CodeGenOpts.OptimizationLevel <= 1) {
480     bool InsertLifetimeIntrinsics = (CodeGenOpts.OptimizationLevel != 0 &&
481                                      !CodeGenOpts.DisableLifetimeMarkers);
482     PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
483   } else {
484     // We do not want to inline hot callsites for SamplePGO module-summary build
485     // because profile annotation will happen again in ThinLTO backend, and we
486     // want the IR of the hot path to match the profile.
487     PMBuilder.Inliner = createFunctionInliningPass(
488         CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
489         (!CodeGenOpts.SampleProfileFile.empty() &&
490          CodeGenOpts.EmitSummaryIndex));
491   }
492 
493   PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
494   PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
495   PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
496   PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
497 
498   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
499   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
500   PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
501   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
502   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
503 
504   MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
505 
506   if (TM)
507     TM->adjustPassManager(PMBuilder);
508 
509   if (CodeGenOpts.DebugInfoForProfiling ||
510       !CodeGenOpts.SampleProfileFile.empty())
511     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
512                            addAddDiscriminatorsPass);
513 
514   // In ObjC ARC mode, add the main ARC optimization passes.
515   if (LangOpts.ObjCAutoRefCount) {
516     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
517                            addObjCARCExpandPass);
518     PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
519                            addObjCARCAPElimPass);
520     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
521                            addObjCARCOptPass);
522   }
523 
524   if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
525     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
526                            addBoundsCheckingPass);
527     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
528                            addBoundsCheckingPass);
529   }
530 
531   if (CodeGenOpts.SanitizeCoverageType ||
532       CodeGenOpts.SanitizeCoverageIndirectCalls ||
533       CodeGenOpts.SanitizeCoverageTraceCmp) {
534     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
535                            addSanitizerCoveragePass);
536     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
537                            addSanitizerCoveragePass);
538   }
539 
540   if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
541     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
542                            addAddressSanitizerPasses);
543     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
544                            addAddressSanitizerPasses);
545   }
546 
547   if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
548     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
549                            addKernelAddressSanitizerPasses);
550     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
551                            addKernelAddressSanitizerPasses);
552   }
553 
554   if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
555     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
556                            addMemorySanitizerPass);
557     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
558                            addMemorySanitizerPass);
559   }
560 
561   if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
562     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
563                            addThreadSanitizerPass);
564     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
565                            addThreadSanitizerPass);
566   }
567 
568   if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
569     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
570                            addDataFlowSanitizerPass);
571     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
572                            addDataFlowSanitizerPass);
573   }
574 
575   if (LangOpts.CoroutinesTS)
576     addCoroutinePassesToExtensionPoints(PMBuilder);
577 
578   if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) {
579     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
580                            addEfficiencySanitizerPass);
581     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
582                            addEfficiencySanitizerPass);
583   }
584 
585   // Set up the per-function pass manager.
586   FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
587   if (CodeGenOpts.VerifyModule)
588     FPM.add(createVerifierPass());
589 
590   // Set up the per-module pass manager.
591   if (!CodeGenOpts.RewriteMapFiles.empty())
592     addSymbolRewriterPass(CodeGenOpts, &MPM);
593 
594   if (!CodeGenOpts.DisableGCov &&
595       (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
596     // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
597     // LLVM's -default-gcov-version flag is set to something invalid.
598     GCOVOptions Options;
599     Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
600     Options.EmitData = CodeGenOpts.EmitGcovArcs;
601     memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
602     Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
603     Options.NoRedZone = CodeGenOpts.DisableRedZone;
604     Options.FunctionNamesInData =
605         !CodeGenOpts.CoverageNoFunctionNamesInData;
606     Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
607     MPM.add(createGCOVProfilerPass(Options));
608     if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
609       MPM.add(createStripSymbolsPass(true));
610   }
611 
612   if (CodeGenOpts.hasProfileClangInstr()) {
613     InstrProfOptions Options;
614     Options.NoRedZone = CodeGenOpts.DisableRedZone;
615     Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
616     MPM.add(createInstrProfilingLegacyPass(Options));
617   }
618   if (CodeGenOpts.hasProfileIRInstr()) {
619     PMBuilder.EnablePGOInstrGen = true;
620     if (!CodeGenOpts.InstrProfileOutput.empty())
621       PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
622     else
623       PMBuilder.PGOInstrGen = DefaultProfileGenName;
624   }
625   if (CodeGenOpts.hasProfileIRUse())
626     PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
627 
628   if (!CodeGenOpts.SampleProfileFile.empty())
629     PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
630 
631   PMBuilder.populateFunctionPassManager(FPM);
632   PMBuilder.populateModulePassManager(MPM);
633 }
634 
635 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
636   SmallVector<const char *, 16> BackendArgs;
637   BackendArgs.push_back("clang"); // Fake program name.
638   if (!CodeGenOpts.DebugPass.empty()) {
639     BackendArgs.push_back("-debug-pass");
640     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
641   }
642   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
643     BackendArgs.push_back("-limit-float-precision");
644     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
645   }
646   for (const std::string &BackendOption : CodeGenOpts.BackendOptions)
647     BackendArgs.push_back(BackendOption.c_str());
648   BackendArgs.push_back(nullptr);
649   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
650                                     BackendArgs.data());
651 }
652 
653 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
654   // Create the TargetMachine for generating code.
655   std::string Error;
656   std::string Triple = TheModule->getTargetTriple();
657   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
658   if (!TheTarget) {
659     if (MustCreateTM)
660       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
661     return;
662   }
663 
664   Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
665   std::string FeaturesStr =
666       llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
667   llvm::Reloc::Model RM = getRelocModel(CodeGenOpts);
668   CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
669 
670   llvm::TargetOptions Options;
671   initTargetOptions(Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts);
672   TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
673                                           Options, RM, CM, OptLevel));
674 }
675 
676 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
677                                        BackendAction Action,
678                                        raw_pwrite_stream &OS) {
679   // Add LibraryInfo.
680   llvm::Triple TargetTriple(TheModule->getTargetTriple());
681   std::unique_ptr<TargetLibraryInfoImpl> TLII(
682       createTLII(TargetTriple, CodeGenOpts));
683   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
684 
685   // Normal mode, emit a .s or .o file by running the code generator. Note,
686   // this also adds codegenerator level optimization passes.
687   TargetMachine::CodeGenFileType CGFT = getCodeGenFileType(Action);
688 
689   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
690   // "codegen" passes so that it isn't run multiple times when there is
691   // inlining happening.
692   if (CodeGenOpts.OptimizationLevel > 0)
693     CodeGenPasses.add(createObjCARCContractPass());
694 
695   if (TM->addPassesToEmitFile(CodeGenPasses, OS, CGFT,
696                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
697     Diags.Report(diag::err_fe_unable_to_interface_with_target);
698     return false;
699   }
700 
701   return true;
702 }
703 
704 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
705                                       std::unique_ptr<raw_pwrite_stream> OS) {
706   TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
707 
708   setCommandLineOpts(CodeGenOpts);
709 
710   bool UsesCodeGen = (Action != Backend_EmitNothing &&
711                       Action != Backend_EmitBC &&
712                       Action != Backend_EmitLL);
713   CreateTargetMachine(UsesCodeGen);
714 
715   if (UsesCodeGen && !TM)
716     return;
717   if (TM)
718     TheModule->setDataLayout(TM->createDataLayout());
719 
720   legacy::PassManager PerModulePasses;
721   PerModulePasses.add(
722       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
723 
724   legacy::FunctionPassManager PerFunctionPasses(TheModule);
725   PerFunctionPasses.add(
726       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
727 
728   CreatePasses(PerModulePasses, PerFunctionPasses);
729 
730   legacy::PassManager CodeGenPasses;
731   CodeGenPasses.add(
732       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
733 
734   std::unique_ptr<raw_fd_ostream> ThinLinkOS;
735 
736   switch (Action) {
737   case Backend_EmitNothing:
738     break;
739 
740   case Backend_EmitBC:
741     if (CodeGenOpts.EmitSummaryIndex) {
742       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
743         std::error_code EC;
744         ThinLinkOS.reset(new llvm::raw_fd_ostream(
745             CodeGenOpts.ThinLinkBitcodeFile, EC,
746             llvm::sys::fs::F_None));
747         if (EC) {
748           Diags.Report(diag::err_fe_unable_to_open_output) << CodeGenOpts.ThinLinkBitcodeFile
749                                                            << EC.message();
750           return;
751         }
752       }
753       PerModulePasses.add(
754           createWriteThinLTOBitcodePass(*OS, ThinLinkOS.get()));
755     }
756     else
757       PerModulePasses.add(
758           createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
759     break;
760 
761   case Backend_EmitLL:
762     PerModulePasses.add(
763         createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
764     break;
765 
766   default:
767     if (!AddEmitPasses(CodeGenPasses, Action, *OS))
768       return;
769   }
770 
771   // Before executing passes, print the final values of the LLVM options.
772   cl::PrintOptionValues();
773 
774   // Run passes. For now we do all passes at once, but eventually we
775   // would like to have the option of streaming code generation.
776 
777   {
778     PrettyStackTraceString CrashInfo("Per-function optimization");
779 
780     PerFunctionPasses.doInitialization();
781     for (Function &F : *TheModule)
782       if (!F.isDeclaration())
783         PerFunctionPasses.run(F);
784     PerFunctionPasses.doFinalization();
785   }
786 
787   {
788     PrettyStackTraceString CrashInfo("Per-module optimization passes");
789     PerModulePasses.run(*TheModule);
790   }
791 
792   {
793     PrettyStackTraceString CrashInfo("Code generation");
794     CodeGenPasses.run(*TheModule);
795   }
796 }
797 
798 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
799   switch (Opts.OptimizationLevel) {
800   default:
801     llvm_unreachable("Invalid optimization level!");
802 
803   case 1:
804     return PassBuilder::O1;
805 
806   case 2:
807     switch (Opts.OptimizeSize) {
808     default:
809       llvm_unreachable("Invalide optimization level for size!");
810 
811     case 0:
812       return PassBuilder::O2;
813 
814     case 1:
815       return PassBuilder::Os;
816 
817     case 2:
818       return PassBuilder::Oz;
819     }
820 
821   case 3:
822     return PassBuilder::O3;
823   }
824 }
825 
826 /// A clean version of `EmitAssembly` that uses the new pass manager.
827 ///
828 /// Not all features are currently supported in this system, but where
829 /// necessary it falls back to the legacy pass manager to at least provide
830 /// basic functionality.
831 ///
832 /// This API is planned to have its functionality finished and then to replace
833 /// `EmitAssembly` at some point in the future when the default switches.
834 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
835     BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
836   TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
837   setCommandLineOpts(CodeGenOpts);
838 
839   // The new pass manager always makes a target machine available to passes
840   // during construction.
841   CreateTargetMachine(/*MustCreateTM*/ true);
842   if (!TM)
843     // This will already be diagnosed, just bail.
844     return;
845   TheModule->setDataLayout(TM->createDataLayout());
846 
847   Optional<PGOOptions> PGOOpt;
848 
849   if (CodeGenOpts.hasProfileIRInstr())
850     // -fprofile-generate.
851     PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty()
852                             ? DefaultProfileGenName
853                             : CodeGenOpts.InstrProfileOutput,
854                         "", "", true, CodeGenOpts.DebugInfoForProfiling);
855   else if (CodeGenOpts.hasProfileIRUse())
856     // -fprofile-use.
857     PGOOpt = PGOOptions("", CodeGenOpts.ProfileInstrumentUsePath, "", false,
858                         CodeGenOpts.DebugInfoForProfiling);
859   else if (!CodeGenOpts.SampleProfileFile.empty())
860     // -fprofile-sample-use
861     PGOOpt = PGOOptions("", "", CodeGenOpts.SampleProfileFile, false,
862                         CodeGenOpts.DebugInfoForProfiling);
863   else if (CodeGenOpts.DebugInfoForProfiling)
864     // -fdebug-info-for-profiling
865     PGOOpt = PGOOptions("", "", "", false, true);
866 
867   PassBuilder PB(TM.get(), PGOOpt);
868 
869   LoopAnalysisManager LAM;
870   FunctionAnalysisManager FAM;
871   CGSCCAnalysisManager CGAM;
872   ModuleAnalysisManager MAM;
873 
874   // Register the AA manager first so that our version is the one used.
875   FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
876 
877   // Register the target library analysis directly and give it a customized
878   // preset TLI.
879   Triple TargetTriple(TheModule->getTargetTriple());
880   std::unique_ptr<TargetLibraryInfoImpl> TLII(
881       createTLII(TargetTriple, CodeGenOpts));
882   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
883   MAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
884 
885   // Register all the basic analyses with the managers.
886   PB.registerModuleAnalyses(MAM);
887   PB.registerCGSCCAnalyses(CGAM);
888   PB.registerFunctionAnalyses(FAM);
889   PB.registerLoopAnalyses(LAM);
890   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
891 
892   ModulePassManager MPM(CodeGenOpts.DebugPassManager);
893 
894   if (!CodeGenOpts.DisableLLVMPasses) {
895     bool IsThinLTO = CodeGenOpts.EmitSummaryIndex;
896     bool IsLTO = CodeGenOpts.PrepareForLTO;
897 
898     if (CodeGenOpts.OptimizationLevel == 0) {
899       // Build a minimal pipeline based on the semantics required by Clang,
900       // which is just that always inlining occurs.
901       MPM.addPass(AlwaysInlinerPass());
902       if (IsThinLTO)
903         MPM.addPass(NameAnonGlobalPass());
904     } else {
905       // Map our optimization levels into one of the distinct levels used to
906       // configure the pipeline.
907       PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
908 
909       if (IsThinLTO) {
910         MPM = PB.buildThinLTOPreLinkDefaultPipeline(
911             Level, CodeGenOpts.DebugPassManager);
912         MPM.addPass(NameAnonGlobalPass());
913       } else if (IsLTO) {
914         MPM = PB.buildLTOPreLinkDefaultPipeline(Level,
915                                                 CodeGenOpts.DebugPassManager);
916       } else {
917         MPM = PB.buildPerModuleDefaultPipeline(Level,
918                                                CodeGenOpts.DebugPassManager);
919       }
920     }
921   }
922 
923   // FIXME: We still use the legacy pass manager to do code generation. We
924   // create that pass manager here and use it as needed below.
925   legacy::PassManager CodeGenPasses;
926   bool NeedCodeGen = false;
927   Optional<raw_fd_ostream> ThinLinkOS;
928 
929   // Append any output we need to the pass manager.
930   switch (Action) {
931   case Backend_EmitNothing:
932     break;
933 
934   case Backend_EmitBC:
935     if (CodeGenOpts.EmitSummaryIndex) {
936       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
937         std::error_code EC;
938         ThinLinkOS.emplace(CodeGenOpts.ThinLinkBitcodeFile, EC,
939                            llvm::sys::fs::F_None);
940         if (EC) {
941           Diags.Report(diag::err_fe_unable_to_open_output)
942               << CodeGenOpts.ThinLinkBitcodeFile << EC.message();
943           return;
944         }
945       }
946       MPM.addPass(
947           ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &*ThinLinkOS : nullptr));
948     } else {
949       MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
950                                     CodeGenOpts.EmitSummaryIndex,
951                                     CodeGenOpts.EmitSummaryIndex));
952     }
953     break;
954 
955   case Backend_EmitLL:
956     MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
957     break;
958 
959   case Backend_EmitAssembly:
960   case Backend_EmitMCNull:
961   case Backend_EmitObj:
962     NeedCodeGen = true;
963     CodeGenPasses.add(
964         createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
965     if (!AddEmitPasses(CodeGenPasses, Action, *OS))
966       // FIXME: Should we handle this error differently?
967       return;
968     break;
969   }
970 
971   // Before executing passes, print the final values of the LLVM options.
972   cl::PrintOptionValues();
973 
974   // Now that we have all of the passes ready, run them.
975   {
976     PrettyStackTraceString CrashInfo("Optimizer");
977     MPM.run(*TheModule, MAM);
978   }
979 
980   // Now if needed, run the legacy PM for codegen.
981   if (NeedCodeGen) {
982     PrettyStackTraceString CrashInfo("Code generation");
983     CodeGenPasses.run(*TheModule);
984   }
985 }
986 
987 Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) {
988   Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
989   if (!BMsOrErr)
990     return BMsOrErr.takeError();
991 
992   // The bitcode file may contain multiple modules, we want the one that is
993   // marked as being the ThinLTO module.
994   for (BitcodeModule &BM : *BMsOrErr) {
995     Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
996     if (LTOInfo && LTOInfo->IsThinLTO)
997       return BM;
998   }
999 
1000   return make_error<StringError>("Could not find module summary",
1001                                  inconvertibleErrorCode());
1002 }
1003 
1004 static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
1005                               const HeaderSearchOptions &HeaderOpts,
1006                               const CodeGenOptions &CGOpts,
1007                               const clang::TargetOptions &TOpts,
1008                               const LangOptions &LOpts,
1009                               std::unique_ptr<raw_pwrite_stream> OS,
1010                               std::string SampleProfile,
1011                               BackendAction Action) {
1012   StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1013       ModuleToDefinedGVSummaries;
1014   CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1015 
1016   setCommandLineOpts(CGOpts);
1017 
1018   // We can simply import the values mentioned in the combined index, since
1019   // we should only invoke this using the individual indexes written out
1020   // via a WriteIndexesThinBackend.
1021   FunctionImporter::ImportMapTy ImportList;
1022   for (auto &GlobalList : *CombinedIndex) {
1023     // Ignore entries for undefined references.
1024     if (GlobalList.second.SummaryList.empty())
1025       continue;
1026 
1027     auto GUID = GlobalList.first;
1028     assert(GlobalList.second.SummaryList.size() == 1 &&
1029            "Expected individual combined index to have one summary per GUID");
1030     auto &Summary = GlobalList.second.SummaryList[0];
1031     // Skip the summaries for the importing module. These are included to
1032     // e.g. record required linkage changes.
1033     if (Summary->modulePath() == M->getModuleIdentifier())
1034       continue;
1035     // Doesn't matter what value we plug in to the map, just needs an entry
1036     // to provoke importing by thinBackend.
1037     ImportList[Summary->modulePath()][GUID] = 1;
1038   }
1039 
1040   std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
1041   MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
1042 
1043   for (auto &I : ImportList) {
1044     ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
1045         llvm::MemoryBuffer::getFile(I.first());
1046     if (!MBOrErr) {
1047       errs() << "Error loading imported file '" << I.first()
1048              << "': " << MBOrErr.getError().message() << "\n";
1049       return;
1050     }
1051 
1052     Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr);
1053     if (!BMOrErr) {
1054       handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) {
1055         errs() << "Error loading imported file '" << I.first()
1056                << "': " << EIB.message() << '\n';
1057       });
1058       return;
1059     }
1060     ModuleMap.insert({I.first(), *BMOrErr});
1061 
1062     OwnedImports.push_back(std::move(*MBOrErr));
1063   }
1064   auto AddStream = [&](size_t Task) {
1065     return llvm::make_unique<lto::NativeObjectStream>(std::move(OS));
1066   };
1067   lto::Config Conf;
1068   Conf.CPU = TOpts.CPU;
1069   Conf.CodeModel = getCodeModel(CGOpts);
1070   Conf.MAttrs = TOpts.Features;
1071   Conf.RelocModel = getRelocModel(CGOpts);
1072   Conf.CGOptLevel = getCGOptLevel(CGOpts);
1073   initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1074   Conf.SampleProfile = std::move(SampleProfile);
1075   Conf.UseNewPM = CGOpts.ExperimentalNewPassManager;
1076   switch (Action) {
1077   case Backend_EmitNothing:
1078     Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1079       return false;
1080     };
1081     break;
1082   case Backend_EmitLL:
1083     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1084       M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1085       return false;
1086     };
1087     break;
1088   case Backend_EmitBC:
1089     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1090       WriteBitcodeToFile(M, *OS, CGOpts.EmitLLVMUseLists);
1091       return false;
1092     };
1093     break;
1094   default:
1095     Conf.CGFileType = getCodeGenFileType(Action);
1096     break;
1097   }
1098   if (Error E = thinBackend(
1099           Conf, 0, AddStream, *M, *CombinedIndex, ImportList,
1100           ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
1101     handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1102       errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1103     });
1104   }
1105 }
1106 
1107 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1108                               const HeaderSearchOptions &HeaderOpts,
1109                               const CodeGenOptions &CGOpts,
1110                               const clang::TargetOptions &TOpts,
1111                               const LangOptions &LOpts,
1112                               const llvm::DataLayout &TDesc, Module *M,
1113                               BackendAction Action,
1114                               std::unique_ptr<raw_pwrite_stream> OS) {
1115   if (!CGOpts.ThinLTOIndexFile.empty()) {
1116     // If we are performing a ThinLTO importing compile, load the function index
1117     // into memory and pass it into runThinLTOBackend, which will run the
1118     // function importer and invoke LTO passes.
1119     Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
1120         llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile,
1121                                            /*IgnoreEmptyThinLTOIndexFile*/true);
1122     if (!IndexOrErr) {
1123       logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1124                             "Error loading index file '" +
1125                             CGOpts.ThinLTOIndexFile + "': ");
1126       return;
1127     }
1128     std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1129     // A null CombinedIndex means we should skip ThinLTO compilation
1130     // (LLVM will optionally ignore empty index files, returning null instead
1131     // of an error).
1132     bool DoThinLTOBackend = CombinedIndex != nullptr;
1133     if (DoThinLTOBackend) {
1134       runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts,
1135                         LOpts, std::move(OS), CGOpts.SampleProfileFile, Action);
1136       return;
1137     }
1138   }
1139 
1140   EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1141 
1142   if (CGOpts.ExperimentalNewPassManager)
1143     AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
1144   else
1145     AsmHelper.EmitAssembly(Action, std::move(OS));
1146 
1147   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1148   // DataLayout.
1149   if (AsmHelper.TM) {
1150     std::string DLDesc = M->getDataLayout().getStringRepresentation();
1151     if (DLDesc != TDesc.getStringRepresentation()) {
1152       unsigned DiagID = Diags.getCustomDiagID(
1153           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1154                                     "expected target description '%1'");
1155       Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
1156     }
1157   }
1158 }
1159 
1160 static const char* getSectionNameForBitcode(const Triple &T) {
1161   switch (T.getObjectFormat()) {
1162   case Triple::MachO:
1163     return "__LLVM,__bitcode";
1164   case Triple::COFF:
1165   case Triple::ELF:
1166   case Triple::Wasm:
1167   case Triple::UnknownObjectFormat:
1168     return ".llvmbc";
1169   }
1170   llvm_unreachable("Unimplemented ObjectFormatType");
1171 }
1172 
1173 static const char* getSectionNameForCommandline(const Triple &T) {
1174   switch (T.getObjectFormat()) {
1175   case Triple::MachO:
1176     return "__LLVM,__cmdline";
1177   case Triple::COFF:
1178   case Triple::ELF:
1179   case Triple::Wasm:
1180   case Triple::UnknownObjectFormat:
1181     return ".llvmcmd";
1182   }
1183   llvm_unreachable("Unimplemented ObjectFormatType");
1184 }
1185 
1186 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1187 // __LLVM,__bitcode section.
1188 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1189                          llvm::MemoryBufferRef Buf) {
1190   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1191     return;
1192 
1193   // Save llvm.compiler.used and remote it.
1194   SmallVector<Constant*, 2> UsedArray;
1195   SmallSet<GlobalValue*, 4> UsedGlobals;
1196   Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0);
1197   GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true);
1198   for (auto *GV : UsedGlobals) {
1199     if (GV->getName() != "llvm.embedded.module" &&
1200         GV->getName() != "llvm.cmdline")
1201       UsedArray.push_back(
1202           ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1203   }
1204   if (Used)
1205     Used->eraseFromParent();
1206 
1207   // Embed the bitcode for the llvm module.
1208   std::string Data;
1209   ArrayRef<uint8_t> ModuleData;
1210   Triple T(M->getTargetTriple());
1211   // Create a constant that contains the bitcode.
1212   // In case of embedding a marker, ignore the input Buf and use the empty
1213   // ArrayRef. It is also legal to create a bitcode marker even Buf is empty.
1214   if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
1215     if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
1216                    (const unsigned char *)Buf.getBufferEnd())) {
1217       // If the input is LLVM Assembly, bitcode is produced by serializing
1218       // the module. Use-lists order need to be perserved in this case.
1219       llvm::raw_string_ostream OS(Data);
1220       llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
1221       ModuleData =
1222           ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
1223     } else
1224       // If the input is LLVM bitcode, write the input byte stream directly.
1225       ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
1226                                      Buf.getBufferSize());
1227   }
1228   llvm::Constant *ModuleConstant =
1229       llvm::ConstantDataArray::get(M->getContext(), ModuleData);
1230   llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1231       *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
1232       ModuleConstant);
1233   GV->setSection(getSectionNameForBitcode(T));
1234   UsedArray.push_back(
1235       ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1236   if (llvm::GlobalVariable *Old =
1237           M->getGlobalVariable("llvm.embedded.module", true)) {
1238     assert(Old->hasOneUse() &&
1239            "llvm.embedded.module can only be used once in llvm.compiler.used");
1240     GV->takeName(Old);
1241     Old->eraseFromParent();
1242   } else {
1243     GV->setName("llvm.embedded.module");
1244   }
1245 
1246   // Skip if only bitcode needs to be embedded.
1247   if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) {
1248     // Embed command-line options.
1249     ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()),
1250                               CGOpts.CmdArgs.size());
1251     llvm::Constant *CmdConstant =
1252       llvm::ConstantDataArray::get(M->getContext(), CmdData);
1253     GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
1254                                   llvm::GlobalValue::PrivateLinkage,
1255                                   CmdConstant);
1256     GV->setSection(getSectionNameForCommandline(T));
1257     UsedArray.push_back(
1258         ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1259     if (llvm::GlobalVariable *Old =
1260             M->getGlobalVariable("llvm.cmdline", true)) {
1261       assert(Old->hasOneUse() &&
1262              "llvm.cmdline can only be used once in llvm.compiler.used");
1263       GV->takeName(Old);
1264       Old->eraseFromParent();
1265     } else {
1266       GV->setName("llvm.cmdline");
1267     }
1268   }
1269 
1270   if (UsedArray.empty())
1271     return;
1272 
1273   // Recreate llvm.compiler.used.
1274   ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
1275   auto *NewUsed = new GlobalVariable(
1276       *M, ATy, false, llvm::GlobalValue::AppendingLinkage,
1277       llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
1278   NewUsed->setSection("llvm.metadata");
1279 }
1280