xref: /freebsd/contrib/llvm-project/llvm/lib/LTO/LTOCodeGenerator.cpp (revision 6966ac055c3b7a39266fb982493330df7a097997)
1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Link Time Optimization library. This library is
10 // intended to be used by linker to optimize code at link time.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/LTO/legacy/LTOCodeGenerator.h"
15 
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Analysis/Passes.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Bitcode/BitcodeWriter.h"
22 #include "llvm/CodeGen/ParallelCG.h"
23 #include "llvm/CodeGen/TargetSubtargetInfo.h"
24 #include "llvm/Config/config.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/DiagnosticInfo.h"
30 #include "llvm/IR/DiagnosticPrinter.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Mangler.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/PassTimingInfo.h"
36 #include "llvm/IR/RemarkStreamer.h"
37 #include "llvm/IR/Verifier.h"
38 #include "llvm/InitializePasses.h"
39 #include "llvm/LTO/LTO.h"
40 #include "llvm/LTO/legacy/LTOModule.h"
41 #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
42 #include "llvm/Linker/Linker.h"
43 #include "llvm/MC/MCAsmInfo.h"
44 #include "llvm/MC/MCContext.h"
45 #include "llvm/MC/SubtargetFeature.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Support/FileSystem.h"
48 #include "llvm/Support/Host.h"
49 #include "llvm/Support/MemoryBuffer.h"
50 #include "llvm/Support/Signals.h"
51 #include "llvm/Support/TargetRegistry.h"
52 #include "llvm/Support/TargetSelect.h"
53 #include "llvm/Support/ToolOutputFile.h"
54 #include "llvm/Support/YAMLTraits.h"
55 #include "llvm/Support/raw_ostream.h"
56 #include "llvm/Target/TargetOptions.h"
57 #include "llvm/Transforms/IPO.h"
58 #include "llvm/Transforms/IPO/Internalize.h"
59 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
60 #include "llvm/Transforms/ObjCARC.h"
61 #include "llvm/Transforms/Utils/ModuleUtils.h"
62 #include <system_error>
63 using namespace llvm;
64 
65 const char* LTOCodeGenerator::getVersionString() {
66 #ifdef LLVM_VERSION_INFO
67   return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
68 #else
69   return PACKAGE_NAME " version " PACKAGE_VERSION;
70 #endif
71 }
72 
73 namespace llvm {
74 cl::opt<bool> LTODiscardValueNames(
75     "lto-discard-value-names",
76     cl::desc("Strip names from Value during LTO (other than GlobalValue)."),
77 #ifdef NDEBUG
78     cl::init(true),
79 #else
80     cl::init(false),
81 #endif
82     cl::Hidden);
83 
84 cl::opt<bool> RemarksWithHotness(
85     "lto-pass-remarks-with-hotness",
86     cl::desc("With PGO, include profile count in optimization remarks"),
87     cl::Hidden);
88 
89 cl::opt<std::string>
90     RemarksFilename("lto-pass-remarks-output",
91                     cl::desc("Output filename for pass remarks"),
92                     cl::value_desc("filename"));
93 
94 cl::opt<std::string>
95     RemarksPasses("lto-pass-remarks-filter",
96                   cl::desc("Only record optimization remarks from passes whose "
97                            "names match the given regular expression"),
98                   cl::value_desc("regex"));
99 
100 cl::opt<std::string> RemarksFormat(
101     "lto-pass-remarks-format",
102     cl::desc("The format used for serializing remarks (default: YAML)"),
103     cl::value_desc("format"), cl::init("yaml"));
104 
105 cl::opt<std::string> LTOStatsFile(
106     "lto-stats-file",
107     cl::desc("Save statistics to the specified file"),
108     cl::Hidden);
109 }
110 
111 LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
112     : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
113       TheLinker(new Linker(*MergedModule)) {
114   Context.setDiscardValueNames(LTODiscardValueNames);
115   Context.enableDebugTypeODRUniquing();
116   initializeLTOPasses();
117 }
118 
119 LTOCodeGenerator::~LTOCodeGenerator() {}
120 
121 // Initialize LTO passes. Please keep this function in sync with
122 // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
123 // passes are initialized.
124 void LTOCodeGenerator::initializeLTOPasses() {
125   PassRegistry &R = *PassRegistry::getPassRegistry();
126 
127   initializeInternalizeLegacyPassPass(R);
128   initializeIPSCCPLegacyPassPass(R);
129   initializeGlobalOptLegacyPassPass(R);
130   initializeConstantMergeLegacyPassPass(R);
131   initializeDAHPass(R);
132   initializeInstructionCombiningPassPass(R);
133   initializeSimpleInlinerPass(R);
134   initializePruneEHPass(R);
135   initializeGlobalDCELegacyPassPass(R);
136   initializeArgPromotionPass(R);
137   initializeJumpThreadingPass(R);
138   initializeSROALegacyPassPass(R);
139   initializeAttributorLegacyPassPass(R);
140   initializePostOrderFunctionAttrsLegacyPassPass(R);
141   initializeReversePostOrderFunctionAttrsLegacyPassPass(R);
142   initializeGlobalsAAWrapperPassPass(R);
143   initializeLegacyLICMPassPass(R);
144   initializeMergedLoadStoreMotionLegacyPassPass(R);
145   initializeGVNLegacyPassPass(R);
146   initializeMemCpyOptLegacyPassPass(R);
147   initializeDCELegacyPassPass(R);
148   initializeCFGSimplifyPassPass(R);
149 }
150 
151 void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) {
152   const std::vector<StringRef> &undefs = Mod->getAsmUndefinedRefs();
153   for (int i = 0, e = undefs.size(); i != e; ++i)
154     AsmUndefinedRefs[undefs[i]] = 1;
155 }
156 
157 bool LTOCodeGenerator::addModule(LTOModule *Mod) {
158   assert(&Mod->getModule().getContext() == &Context &&
159          "Expected module in same context");
160 
161   bool ret = TheLinker->linkInModule(Mod->takeModule());
162   setAsmUndefinedRefs(Mod);
163 
164   // We've just changed the input, so let's make sure we verify it.
165   HasVerifiedInput = false;
166 
167   return !ret;
168 }
169 
170 void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
171   assert(&Mod->getModule().getContext() == &Context &&
172          "Expected module in same context");
173 
174   AsmUndefinedRefs.clear();
175 
176   MergedModule = Mod->takeModule();
177   TheLinker = make_unique<Linker>(*MergedModule);
178   setAsmUndefinedRefs(&*Mod);
179 
180   // We've just changed the input, so let's make sure we verify it.
181   HasVerifiedInput = false;
182 }
183 
184 void LTOCodeGenerator::setTargetOptions(const TargetOptions &Options) {
185   this->Options = Options;
186 }
187 
188 void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) {
189   switch (Debug) {
190   case LTO_DEBUG_MODEL_NONE:
191     EmitDwarfDebugInfo = false;
192     return;
193 
194   case LTO_DEBUG_MODEL_DWARF:
195     EmitDwarfDebugInfo = true;
196     return;
197   }
198   llvm_unreachable("Unknown debug format!");
199 }
200 
201 void LTOCodeGenerator::setOptLevel(unsigned Level) {
202   OptLevel = Level;
203   switch (OptLevel) {
204   case 0:
205     CGOptLevel = CodeGenOpt::None;
206     return;
207   case 1:
208     CGOptLevel = CodeGenOpt::Less;
209     return;
210   case 2:
211     CGOptLevel = CodeGenOpt::Default;
212     return;
213   case 3:
214     CGOptLevel = CodeGenOpt::Aggressive;
215     return;
216   }
217   llvm_unreachable("Unknown optimization level!");
218 }
219 
220 bool LTOCodeGenerator::writeMergedModules(StringRef Path) {
221   if (!determineTarget())
222     return false;
223 
224   // We always run the verifier once on the merged module.
225   verifyMergedModuleOnce();
226 
227   // mark which symbols can not be internalized
228   applyScopeRestrictions();
229 
230   // create output file
231   std::error_code EC;
232   ToolOutputFile Out(Path, EC, sys::fs::F_None);
233   if (EC) {
234     std::string ErrMsg = "could not open bitcode file for writing: ";
235     ErrMsg += Path.str() + ": " + EC.message();
236     emitError(ErrMsg);
237     return false;
238   }
239 
240   // write bitcode to it
241   WriteBitcodeToFile(*MergedModule, Out.os(), ShouldEmbedUselists);
242   Out.os().close();
243 
244   if (Out.os().has_error()) {
245     std::string ErrMsg = "could not write bitcode file: ";
246     ErrMsg += Path.str() + ": " + Out.os().error().message();
247     emitError(ErrMsg);
248     Out.os().clear_error();
249     return false;
250   }
251 
252   Out.keep();
253   return true;
254 }
255 
256 bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
257   // make unique temp output file to put generated code
258   SmallString<128> Filename;
259   int FD;
260 
261   StringRef Extension
262       (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o");
263 
264   std::error_code EC =
265       sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
266   if (EC) {
267     emitError(EC.message());
268     return false;
269   }
270 
271   // generate object file
272   ToolOutputFile objFile(Filename, FD);
273 
274   bool genResult = compileOptimized(&objFile.os());
275   objFile.os().close();
276   if (objFile.os().has_error()) {
277     emitError((Twine("could not write object file: ") + Filename + ": " +
278                objFile.os().error().message())
279                   .str());
280     objFile.os().clear_error();
281     sys::fs::remove(Twine(Filename));
282     return false;
283   }
284 
285   objFile.keep();
286   if (!genResult) {
287     sys::fs::remove(Twine(Filename));
288     return false;
289   }
290 
291   NativeObjectPath = Filename.c_str();
292   *Name = NativeObjectPath.c_str();
293   return true;
294 }
295 
296 std::unique_ptr<MemoryBuffer>
297 LTOCodeGenerator::compileOptimized() {
298   const char *name;
299   if (!compileOptimizedToFile(&name))
300     return nullptr;
301 
302   // read .o file into memory buffer
303   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
304       MemoryBuffer::getFile(name, -1, false);
305   if (std::error_code EC = BufferOrErr.getError()) {
306     emitError(EC.message());
307     sys::fs::remove(NativeObjectPath);
308     return nullptr;
309   }
310 
311   // remove temp files
312   sys::fs::remove(NativeObjectPath);
313 
314   return std::move(*BufferOrErr);
315 }
316 
317 bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
318                                        bool DisableInline,
319                                        bool DisableGVNLoadPRE,
320                                        bool DisableVectorization) {
321   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
322                 DisableVectorization))
323     return false;
324 
325   return compileOptimizedToFile(Name);
326 }
327 
328 std::unique_ptr<MemoryBuffer>
329 LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
330                           bool DisableGVNLoadPRE, bool DisableVectorization) {
331   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
332                 DisableVectorization))
333     return nullptr;
334 
335   return compileOptimized();
336 }
337 
338 bool LTOCodeGenerator::determineTarget() {
339   if (TargetMach)
340     return true;
341 
342   TripleStr = MergedModule->getTargetTriple();
343   if (TripleStr.empty()) {
344     TripleStr = sys::getDefaultTargetTriple();
345     MergedModule->setTargetTriple(TripleStr);
346   }
347   llvm::Triple Triple(TripleStr);
348 
349   // create target machine from info for merged modules
350   std::string ErrMsg;
351   MArch = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
352   if (!MArch) {
353     emitError(ErrMsg);
354     return false;
355   }
356 
357   // Construct LTOModule, hand over ownership of module and target. Use MAttr as
358   // the default set of features.
359   SubtargetFeatures Features(MAttr);
360   Features.getDefaultSubtargetFeatures(Triple);
361   FeatureStr = Features.getString();
362   // Set a default CPU for Darwin triples.
363   if (MCpu.empty() && Triple.isOSDarwin()) {
364     if (Triple.getArch() == llvm::Triple::x86_64)
365       MCpu = "core2";
366     else if (Triple.getArch() == llvm::Triple::x86)
367       MCpu = "yonah";
368     else if (Triple.getArch() == llvm::Triple::aarch64)
369       MCpu = "cyclone";
370   }
371 
372   TargetMach = createTargetMachine();
373   return true;
374 }
375 
376 std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
377   return std::unique_ptr<TargetMachine>(MArch->createTargetMachine(
378       TripleStr, MCpu, FeatureStr, Options, RelocModel, None, CGOptLevel));
379 }
380 
381 // If a linkonce global is present in the MustPreserveSymbols, we need to make
382 // sure we honor this. To force the compiler to not drop it, we add it to the
383 // "llvm.compiler.used" global.
384 void LTOCodeGenerator::preserveDiscardableGVs(
385     Module &TheModule,
386     llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV) {
387   std::vector<GlobalValue *> Used;
388   auto mayPreserveGlobal = [&](GlobalValue &GV) {
389     if (!GV.isDiscardableIfUnused() || GV.isDeclaration() ||
390         !mustPreserveGV(GV))
391       return;
392     if (GV.hasAvailableExternallyLinkage())
393       return emitWarning(
394           (Twine("Linker asked to preserve available_externally global: '") +
395            GV.getName() + "'").str());
396     if (GV.hasInternalLinkage())
397       return emitWarning((Twine("Linker asked to preserve internal global: '") +
398                    GV.getName() + "'").str());
399     Used.push_back(&GV);
400   };
401   for (auto &GV : TheModule)
402     mayPreserveGlobal(GV);
403   for (auto &GV : TheModule.globals())
404     mayPreserveGlobal(GV);
405   for (auto &GV : TheModule.aliases())
406     mayPreserveGlobal(GV);
407 
408   if (Used.empty())
409     return;
410 
411   appendToCompilerUsed(TheModule, Used);
412 }
413 
414 void LTOCodeGenerator::applyScopeRestrictions() {
415   if (ScopeRestrictionsDone)
416     return;
417 
418   // Declare a callback for the internalize pass that will ask for every
419   // candidate GlobalValue if it can be internalized or not.
420   Mangler Mang;
421   SmallString<64> MangledName;
422   auto mustPreserveGV = [&](const GlobalValue &GV) -> bool {
423     // Unnamed globals can't be mangled, but they can't be preserved either.
424     if (!GV.hasName())
425       return false;
426 
427     // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled
428     // with the linker supplied name, which on Darwin includes a leading
429     // underscore.
430     MangledName.clear();
431     MangledName.reserve(GV.getName().size() + 1);
432     Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
433     return MustPreserveSymbols.count(MangledName);
434   };
435 
436   // Preserve linkonce value on linker request
437   preserveDiscardableGVs(*MergedModule, mustPreserveGV);
438 
439   if (!ShouldInternalize)
440     return;
441 
442   if (ShouldRestoreGlobalsLinkage) {
443     // Record the linkage type of non-local symbols so they can be restored
444     // prior
445     // to module splitting.
446     auto RecordLinkage = [&](const GlobalValue &GV) {
447       if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() &&
448           GV.hasName())
449         ExternalSymbols.insert(std::make_pair(GV.getName(), GV.getLinkage()));
450     };
451     for (auto &GV : *MergedModule)
452       RecordLinkage(GV);
453     for (auto &GV : MergedModule->globals())
454       RecordLinkage(GV);
455     for (auto &GV : MergedModule->aliases())
456       RecordLinkage(GV);
457   }
458 
459   // Update the llvm.compiler_used globals to force preserving libcalls and
460   // symbols referenced from asm
461   updateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs);
462 
463   internalizeModule(*MergedModule, mustPreserveGV);
464 
465   ScopeRestrictionsDone = true;
466 }
467 
468 /// Restore original linkage for symbols that may have been internalized
469 void LTOCodeGenerator::restoreLinkageForExternals() {
470   if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage)
471     return;
472 
473   assert(ScopeRestrictionsDone &&
474          "Cannot externalize without internalization!");
475 
476   if (ExternalSymbols.empty())
477     return;
478 
479   auto externalize = [this](GlobalValue &GV) {
480     if (!GV.hasLocalLinkage() || !GV.hasName())
481       return;
482 
483     auto I = ExternalSymbols.find(GV.getName());
484     if (I == ExternalSymbols.end())
485       return;
486 
487     GV.setLinkage(I->second);
488   };
489 
490   llvm::for_each(MergedModule->functions(), externalize);
491   llvm::for_each(MergedModule->globals(), externalize);
492   llvm::for_each(MergedModule->aliases(), externalize);
493 }
494 
495 void LTOCodeGenerator::verifyMergedModuleOnce() {
496   // Only run on the first call.
497   if (HasVerifiedInput)
498     return;
499   HasVerifiedInput = true;
500 
501   bool BrokenDebugInfo = false;
502   if (verifyModule(*MergedModule, &dbgs(), &BrokenDebugInfo))
503     report_fatal_error("Broken module found, compilation aborted!");
504   if (BrokenDebugInfo) {
505     emitWarning("Invalid debug info found, debug info will be stripped");
506     StripDebugInfo(*MergedModule);
507   }
508 }
509 
510 void LTOCodeGenerator::finishOptimizationRemarks() {
511   if (DiagnosticOutputFile) {
512     DiagnosticOutputFile->keep();
513     // FIXME: LTOCodeGenerator dtor is not invoked on Darwin
514     DiagnosticOutputFile->os().flush();
515   }
516 }
517 
518 /// Optimize merged modules using various IPO passes
519 bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
520                                 bool DisableGVNLoadPRE,
521                                 bool DisableVectorization) {
522   if (!this->determineTarget())
523     return false;
524 
525   auto DiagFileOrErr =
526       lto::setupOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
527                                     RemarksFormat, RemarksWithHotness);
528   if (!DiagFileOrErr) {
529     errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
530     report_fatal_error("Can't get an output file for the remarks");
531   }
532   DiagnosticOutputFile = std::move(*DiagFileOrErr);
533 
534   // Setup output file to emit statistics.
535   auto StatsFileOrErr = lto::setupStatsFile(LTOStatsFile);
536   if (!StatsFileOrErr) {
537     errs() << "Error: " << toString(StatsFileOrErr.takeError()) << "\n";
538     report_fatal_error("Can't get an output file for the statistics");
539   }
540   StatsFile = std::move(StatsFileOrErr.get());
541 
542   // We always run the verifier once on the merged module, the `DisableVerify`
543   // parameter only applies to subsequent verify.
544   verifyMergedModuleOnce();
545 
546   // Mark which symbols can not be internalized
547   this->applyScopeRestrictions();
548 
549   // Instantiate the pass manager to organize the passes.
550   legacy::PassManager passes;
551 
552   // Add an appropriate DataLayout instance for this module...
553   MergedModule->setDataLayout(TargetMach->createDataLayout());
554 
555   passes.add(
556       createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
557 
558   Triple TargetTriple(TargetMach->getTargetTriple());
559   PassManagerBuilder PMB;
560   PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
561   PMB.LoopVectorize = !DisableVectorization;
562   PMB.SLPVectorize = !DisableVectorization;
563   if (!DisableInline)
564     PMB.Inliner = createFunctionInliningPass();
565   PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
566   if (Freestanding)
567     PMB.LibraryInfo->disableAllFunctions();
568   PMB.OptLevel = OptLevel;
569   PMB.VerifyInput = !DisableVerify;
570   PMB.VerifyOutput = !DisableVerify;
571 
572   PMB.populateLTOPassManager(passes);
573 
574   // Run our queue of passes all at once now, efficiently.
575   passes.run(*MergedModule);
576 
577   return true;
578 }
579 
580 bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
581   if (!this->determineTarget())
582     return false;
583 
584   // We always run the verifier once on the merged module.  If it has already
585   // been called in optimize(), this call will return early.
586   verifyMergedModuleOnce();
587 
588   legacy::PassManager preCodeGenPasses;
589 
590   // If the bitcode files contain ARC code and were compiled with optimization,
591   // the ObjCARCContractPass must be run, so do it unconditionally here.
592   preCodeGenPasses.add(createObjCARCContractPass());
593   preCodeGenPasses.run(*MergedModule);
594 
595   // Re-externalize globals that may have been internalized to increase scope
596   // for splitting
597   restoreLinkageForExternals();
598 
599   // Do code generation. We need to preserve the module in case the client calls
600   // writeMergedModules() after compilation, but we only need to allow this at
601   // parallelism level 1. This is achieved by having splitCodeGen return the
602   // original module at parallelism level 1 which we then assign back to
603   // MergedModule.
604   MergedModule = splitCodeGen(std::move(MergedModule), Out, {},
605                               [&]() { return createTargetMachine(); }, FileType,
606                               ShouldRestoreGlobalsLinkage);
607 
608   // If statistics were requested, save them to the specified file or
609   // print them out after codegen.
610   if (StatsFile)
611     PrintStatisticsJSON(StatsFile->os());
612   else if (AreStatisticsEnabled())
613     PrintStatistics();
614 
615   reportAndResetTimings();
616 
617   finishOptimizationRemarks();
618 
619   return true;
620 }
621 
622 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
623 /// LTO problems.
624 void LTOCodeGenerator::setCodeGenDebugOptions(StringRef Options) {
625   for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
626        o = getToken(o.second))
627     CodegenOptions.push_back(o.first);
628 }
629 
630 void LTOCodeGenerator::parseCodeGenDebugOptions() {
631   // if options were requested, set them
632   if (!CodegenOptions.empty()) {
633     // ParseCommandLineOptions() expects argv[0] to be program name.
634     std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
635     for (std::string &Arg : CodegenOptions)
636       CodegenArgv.push_back(Arg.c_str());
637     cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
638   }
639 }
640 
641 
642 void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI) {
643   // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
644   lto_codegen_diagnostic_severity_t Severity;
645   switch (DI.getSeverity()) {
646   case DS_Error:
647     Severity = LTO_DS_ERROR;
648     break;
649   case DS_Warning:
650     Severity = LTO_DS_WARNING;
651     break;
652   case DS_Remark:
653     Severity = LTO_DS_REMARK;
654     break;
655   case DS_Note:
656     Severity = LTO_DS_NOTE;
657     break;
658   }
659   // Create the string that will be reported to the external diagnostic handler.
660   std::string MsgStorage;
661   raw_string_ostream Stream(MsgStorage);
662   DiagnosticPrinterRawOStream DP(Stream);
663   DI.print(DP);
664   Stream.flush();
665 
666   // If this method has been called it means someone has set up an external
667   // diagnostic handler. Assert on that.
668   assert(DiagHandler && "Invalid diagnostic handler");
669   (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
670 }
671 
672 namespace {
673 struct LTODiagnosticHandler : public DiagnosticHandler {
674   LTOCodeGenerator *CodeGenerator;
675   LTODiagnosticHandler(LTOCodeGenerator *CodeGenPtr)
676       : CodeGenerator(CodeGenPtr) {}
677   bool handleDiagnostics(const DiagnosticInfo &DI) override {
678     CodeGenerator->DiagnosticHandler(DI);
679     return true;
680   }
681 };
682 }
683 
684 void
685 LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
686                                        void *Ctxt) {
687   this->DiagHandler = DiagHandler;
688   this->DiagContext = Ctxt;
689   if (!DiagHandler)
690     return Context.setDiagnosticHandler(nullptr);
691   // Register the LTOCodeGenerator stub in the LLVMContext to forward the
692   // diagnostic to the external DiagHandler.
693   Context.setDiagnosticHandler(llvm::make_unique<LTODiagnosticHandler>(this),
694                                true);
695 }
696 
697 namespace {
698 class LTODiagnosticInfo : public DiagnosticInfo {
699   const Twine &Msg;
700 public:
701   LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error)
702       : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
703   void print(DiagnosticPrinter &DP) const override { DP << Msg; }
704 };
705 }
706 
707 void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
708   if (DiagHandler)
709     (*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
710   else
711     Context.diagnose(LTODiagnosticInfo(ErrMsg));
712 }
713 
714 void LTOCodeGenerator::emitWarning(const std::string &ErrMsg) {
715   if (DiagHandler)
716     (*DiagHandler)(LTO_DS_WARNING, ErrMsg.c_str(), DiagContext);
717   else
718     Context.diagnose(LTODiagnosticInfo(ErrMsg, DS_Warning));
719 }
720