]> git.lizzy.rs Git - rust.git/blob - src/rustllvm/PassWrapper.cpp
Rollup merge of #66826 - mlodato517:mlodato517-clarify-pr-message-assigner, r=Dylan-DPC
[rust.git] / src / rustllvm / PassWrapper.cpp
1 #include <stdio.h>
2
3 #include <vector>
4 #include <set>
5
6 #include "rustllvm.h"
7
8 #include "llvm/Analysis/TargetLibraryInfo.h"
9 #include "llvm/Analysis/TargetTransformInfo.h"
10 #include "llvm/CodeGen/TargetSubtargetInfo.h"
11 #include "llvm/IR/AutoUpgrade.h"
12 #include "llvm/IR/AssemblyAnnotationWriter.h"
13 #include "llvm/IR/IntrinsicInst.h"
14 #include "llvm/Support/CBindingWrapping.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/Host.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
19 #include "llvm/Transforms/IPO/AlwaysInliner.h"
20 #include "llvm/Transforms/IPO/FunctionImport.h"
21 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
22 #include "llvm/LTO/LTO.h"
23 #include "llvm-c/Transforms/PassManagerBuilder.h"
24
25 #include "llvm/Transforms/Instrumentation.h"
26 #if LLVM_VERSION_GE(9, 0)
27 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
28 #endif
29 #if LLVM_VERSION_GE(8, 0)
30 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
31 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
32 #endif
33
34 using namespace llvm;
35 using namespace llvm::legacy;
36
37 typedef struct LLVMOpaquePass *LLVMPassRef;
38 typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
39
40 DEFINE_STDCXX_CONVERSION_FUNCTIONS(Pass, LLVMPassRef)
41 DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
42 DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBuilder,
43                                    LLVMPassManagerBuilderRef)
44
45 extern "C" void LLVMInitializePasses() {
46   PassRegistry &Registry = *PassRegistry::getPassRegistry();
47   initializeCore(Registry);
48   initializeCodeGen(Registry);
49   initializeScalarOpts(Registry);
50   initializeVectorization(Registry);
51   initializeIPO(Registry);
52   initializeAnalysis(Registry);
53   initializeTransformUtils(Registry);
54   initializeInstCombine(Registry);
55   initializeInstrumentation(Registry);
56   initializeTarget(Registry);
57 }
58
59 enum class LLVMRustPassKind {
60   Other,
61   Function,
62   Module,
63 };
64
65 static LLVMRustPassKind toRust(PassKind Kind) {
66   switch (Kind) {
67   case PT_Function:
68     return LLVMRustPassKind::Function;
69   case PT_Module:
70     return LLVMRustPassKind::Module;
71   default:
72     return LLVMRustPassKind::Other;
73   }
74 }
75
76 extern "C" LLVMPassRef LLVMRustFindAndCreatePass(const char *PassName) {
77   StringRef SR(PassName);
78   PassRegistry *PR = PassRegistry::getPassRegistry();
79
80   const PassInfo *PI = PR->getPassInfo(SR);
81   if (PI) {
82     return wrap(PI->createPass());
83   }
84   return nullptr;
85 }
86
87 extern "C" LLVMPassRef LLVMRustCreateAddressSanitizerFunctionPass(bool Recover) {
88   const bool CompileKernel = false;
89
90   return wrap(createAddressSanitizerFunctionPass(CompileKernel, Recover));
91 }
92
93 extern "C" LLVMPassRef LLVMRustCreateModuleAddressSanitizerPass(bool Recover) {
94   const bool CompileKernel = false;
95
96 #if LLVM_VERSION_GE(9, 0)
97   return wrap(createModuleAddressSanitizerLegacyPassPass(CompileKernel, Recover));
98 #else
99   return wrap(createAddressSanitizerModulePass(CompileKernel, Recover));
100 #endif
101 }
102
103 extern "C" LLVMPassRef LLVMRustCreateMemorySanitizerPass(int TrackOrigins, bool Recover) {
104 #if LLVM_VERSION_GE(8, 0)
105   const bool CompileKernel = false;
106
107   return wrap(createMemorySanitizerLegacyPassPass(
108       MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel}));
109 #else
110   return wrap(createMemorySanitizerPass(TrackOrigins, Recover));
111 #endif
112 }
113
114 extern "C" LLVMPassRef LLVMRustCreateThreadSanitizerPass() {
115 #if LLVM_VERSION_GE(8, 0)
116   return wrap(createThreadSanitizerLegacyPassPass());
117 #else
118   return wrap(createThreadSanitizerPass());
119 #endif
120 }
121
122 extern "C" LLVMRustPassKind LLVMRustPassKind(LLVMPassRef RustPass) {
123   assert(RustPass);
124   Pass *Pass = unwrap(RustPass);
125   return toRust(Pass->getPassKind());
126 }
127
128 extern "C" void LLVMRustAddPass(LLVMPassManagerRef PMR, LLVMPassRef RustPass) {
129   assert(RustPass);
130   Pass *Pass = unwrap(RustPass);
131   PassManagerBase *PMB = unwrap(PMR);
132   PMB->add(Pass);
133 }
134
135 extern "C"
136 void LLVMRustPassManagerBuilderPopulateThinLTOPassManager(
137   LLVMPassManagerBuilderRef PMBR,
138   LLVMPassManagerRef PMR
139 ) {
140   unwrap(PMBR)->populateThinLTOPassManager(*unwrap(PMR));
141 }
142
143 extern "C"
144 void LLVMRustAddLastExtensionPasses(
145     LLVMPassManagerBuilderRef PMBR, LLVMPassRef *Passes, size_t NumPasses) {
146   auto AddExtensionPasses = [Passes, NumPasses](
147       const PassManagerBuilder &Builder, PassManagerBase &PM) {
148     for (size_t I = 0; I < NumPasses; I++) {
149       PM.add(unwrap(Passes[I]));
150     }
151   };
152   // Add the passes to both of the pre-finalization extension points,
153   // so they are run for optimized and non-optimized builds.
154   unwrap(PMBR)->addExtension(PassManagerBuilder::EP_OptimizerLast,
155                              AddExtensionPasses);
156   unwrap(PMBR)->addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
157                              AddExtensionPasses);
158 }
159
160 #ifdef LLVM_COMPONENT_X86
161 #define SUBTARGET_X86 SUBTARGET(X86)
162 #else
163 #define SUBTARGET_X86
164 #endif
165
166 #ifdef LLVM_COMPONENT_ARM
167 #define SUBTARGET_ARM SUBTARGET(ARM)
168 #else
169 #define SUBTARGET_ARM
170 #endif
171
172 #ifdef LLVM_COMPONENT_AARCH64
173 #define SUBTARGET_AARCH64 SUBTARGET(AArch64)
174 #else
175 #define SUBTARGET_AARCH64
176 #endif
177
178 #ifdef LLVM_COMPONENT_MIPS
179 #define SUBTARGET_MIPS SUBTARGET(Mips)
180 #else
181 #define SUBTARGET_MIPS
182 #endif
183
184 #ifdef LLVM_COMPONENT_POWERPC
185 #define SUBTARGET_PPC SUBTARGET(PPC)
186 #else
187 #define SUBTARGET_PPC
188 #endif
189
190 #ifdef LLVM_COMPONENT_SYSTEMZ
191 #define SUBTARGET_SYSTEMZ SUBTARGET(SystemZ)
192 #else
193 #define SUBTARGET_SYSTEMZ
194 #endif
195
196 #ifdef LLVM_COMPONENT_MSP430
197 #define SUBTARGET_MSP430 SUBTARGET(MSP430)
198 #else
199 #define SUBTARGET_MSP430
200 #endif
201
202 #ifdef LLVM_COMPONENT_RISCV
203 #define SUBTARGET_RISCV SUBTARGET(RISCV)
204 #else
205 #define SUBTARGET_RISCV
206 #endif
207
208 #ifdef LLVM_COMPONENT_SPARC
209 #define SUBTARGET_SPARC SUBTARGET(Sparc)
210 #else
211 #define SUBTARGET_SPARC
212 #endif
213
214 #ifdef LLVM_COMPONENT_HEXAGON
215 #define SUBTARGET_HEXAGON SUBTARGET(Hexagon)
216 #else
217 #define SUBTARGET_HEXAGON
218 #endif
219
220 #define GEN_SUBTARGETS                                                         \
221   SUBTARGET_X86                                                                \
222   SUBTARGET_ARM                                                                \
223   SUBTARGET_AARCH64                                                            \
224   SUBTARGET_MIPS                                                               \
225   SUBTARGET_PPC                                                                \
226   SUBTARGET_SYSTEMZ                                                            \
227   SUBTARGET_MSP430                                                             \
228   SUBTARGET_SPARC                                                              \
229   SUBTARGET_HEXAGON                                                            \
230   SUBTARGET_RISCV                                                              \
231
232 #define SUBTARGET(x)                                                           \
233   namespace llvm {                                                             \
234   extern const SubtargetFeatureKV x##FeatureKV[];                              \
235   extern const SubtargetFeatureKV x##SubTypeKV[];                              \
236   }
237
238 GEN_SUBTARGETS
239 #undef SUBTARGET
240
241 extern "C" bool LLVMRustHasFeature(LLVMTargetMachineRef TM,
242                                    const char *Feature) {
243   TargetMachine *Target = unwrap(TM);
244   const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
245   return MCInfo->checkFeatures(std::string("+") + Feature);
246 }
247
248 enum class LLVMRustCodeModel {
249   Other,
250   Small,
251   Kernel,
252   Medium,
253   Large,
254   None,
255 };
256
257 static CodeModel::Model fromRust(LLVMRustCodeModel Model) {
258   switch (Model) {
259   case LLVMRustCodeModel::Small:
260     return CodeModel::Small;
261   case LLVMRustCodeModel::Kernel:
262     return CodeModel::Kernel;
263   case LLVMRustCodeModel::Medium:
264     return CodeModel::Medium;
265   case LLVMRustCodeModel::Large:
266     return CodeModel::Large;
267   default:
268     report_fatal_error("Bad CodeModel.");
269   }
270 }
271
272 enum class LLVMRustCodeGenOptLevel {
273   Other,
274   None,
275   Less,
276   Default,
277   Aggressive,
278 };
279
280 static CodeGenOpt::Level fromRust(LLVMRustCodeGenOptLevel Level) {
281   switch (Level) {
282   case LLVMRustCodeGenOptLevel::None:
283     return CodeGenOpt::None;
284   case LLVMRustCodeGenOptLevel::Less:
285     return CodeGenOpt::Less;
286   case LLVMRustCodeGenOptLevel::Default:
287     return CodeGenOpt::Default;
288   case LLVMRustCodeGenOptLevel::Aggressive:
289     return CodeGenOpt::Aggressive;
290   default:
291     report_fatal_error("Bad CodeGenOptLevel.");
292   }
293 }
294
295 enum class LLVMRustRelocMode {
296   Default,
297   Static,
298   PIC,
299   DynamicNoPic,
300   ROPI,
301   RWPI,
302   ROPIRWPI,
303 };
304
305 static Optional<Reloc::Model> fromRust(LLVMRustRelocMode RustReloc) {
306   switch (RustReloc) {
307   case LLVMRustRelocMode::Default:
308     return None;
309   case LLVMRustRelocMode::Static:
310     return Reloc::Static;
311   case LLVMRustRelocMode::PIC:
312     return Reloc::PIC_;
313   case LLVMRustRelocMode::DynamicNoPic:
314     return Reloc::DynamicNoPIC;
315   case LLVMRustRelocMode::ROPI:
316     return Reloc::ROPI;
317   case LLVMRustRelocMode::RWPI:
318     return Reloc::RWPI;
319   case LLVMRustRelocMode::ROPIRWPI:
320     return Reloc::ROPI_RWPI;
321   }
322   report_fatal_error("Bad RelocModel.");
323 }
324
325 #ifdef LLVM_RUSTLLVM
326 /// getLongestEntryLength - Return the length of the longest entry in the table.
327 template<typename KV>
328 static size_t getLongestEntryLength(ArrayRef<KV> Table) {
329   size_t MaxLen = 0;
330   for (auto &I : Table)
331     MaxLen = std::max(MaxLen, std::strlen(I.Key));
332   return MaxLen;
333 }
334
335 extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM) {
336   const TargetMachine *Target = unwrap(TM);
337   const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
338   const Triple::ArchType HostArch = Triple(sys::getProcessTriple()).getArch();
339   const Triple::ArchType TargetArch = Target->getTargetTriple().getArch();
340   const ArrayRef<SubtargetSubTypeKV> CPUTable = MCInfo->getCPUTable();
341   unsigned MaxCPULen = getLongestEntryLength(CPUTable);
342
343   printf("Available CPUs for this target:\n");
344   if (HostArch == TargetArch) {
345     const StringRef HostCPU = sys::getHostCPUName();
346     printf("    %-*s - Select the CPU of the current host (currently %.*s).\n",
347       MaxCPULen, "native", (int)HostCPU.size(), HostCPU.data());
348   }
349   for (auto &CPU : CPUTable)
350     printf("    %-*s\n", MaxCPULen, CPU.Key);
351   printf("\n");
352 }
353
354 extern "C" void LLVMRustPrintTargetFeatures(LLVMTargetMachineRef TM) {
355   const TargetMachine *Target = unwrap(TM);
356   const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
357   const ArrayRef<SubtargetFeatureKV> FeatTable = MCInfo->getFeatureTable();
358   unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
359
360   printf("Available features for this target:\n");
361   for (auto &Feature : FeatTable)
362     printf("    %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
363   printf("\n");
364
365   printf("Use +feature to enable a feature, or -feature to disable it.\n"
366          "For example, rustc -C -target-cpu=mycpu -C "
367          "target-feature=+feature1,-feature2\n\n");
368 }
369
370 #else
371
372 extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef) {
373   printf("Target CPU help is not supported by this LLVM version.\n\n");
374 }
375
376 extern "C" void LLVMRustPrintTargetFeatures(LLVMTargetMachineRef) {
377   printf("Target features help is not supported by this LLVM version.\n\n");
378 }
379 #endif
380
381 extern "C" const char* LLVMRustGetHostCPUName(size_t *len) {
382   StringRef Name = sys::getHostCPUName();
383   *len = Name.size();
384   return Name.data();
385 }
386
387 extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
388     const char *TripleStr, const char *CPU, const char *Feature,
389     const char *ABIStr, LLVMRustCodeModel RustCM, LLVMRustRelocMode RustReloc,
390     LLVMRustCodeGenOptLevel RustOptLevel, bool UseSoftFloat,
391     bool PositionIndependentExecutable, bool FunctionSections,
392     bool DataSections,
393     bool TrapUnreachable,
394     bool Singlethread,
395     bool AsmComments,
396     bool EmitStackSizeSection) {
397
398   auto OptLevel = fromRust(RustOptLevel);
399   auto RM = fromRust(RustReloc);
400
401   std::string Error;
402   Triple Trip(Triple::normalize(TripleStr));
403   const llvm::Target *TheTarget =
404       TargetRegistry::lookupTarget(Trip.getTriple(), Error);
405   if (TheTarget == nullptr) {
406     LLVMRustSetLastError(Error.c_str());
407     return nullptr;
408   }
409
410   TargetOptions Options;
411
412   Options.FloatABIType = FloatABI::Default;
413   if (UseSoftFloat) {
414     Options.FloatABIType = FloatABI::Soft;
415   }
416   Options.DataSections = DataSections;
417   Options.FunctionSections = FunctionSections;
418   Options.MCOptions.AsmVerbose = AsmComments;
419   Options.MCOptions.PreserveAsmComments = AsmComments;
420   Options.MCOptions.ABIName = ABIStr;
421
422   if (TrapUnreachable) {
423     // Tell LLVM to codegen `unreachable` into an explicit trap instruction.
424     // This limits the extent of possible undefined behavior in some cases, as
425     // it prevents control flow from "falling through" into whatever code
426     // happens to be laid out next in memory.
427     Options.TrapUnreachable = true;
428   }
429
430   if (Singlethread) {
431     Options.ThreadModel = ThreadModel::Single;
432   }
433
434   Options.EmitStackSizeSection = EmitStackSizeSection;
435
436   Optional<CodeModel::Model> CM;
437   if (RustCM != LLVMRustCodeModel::None)
438     CM = fromRust(RustCM);
439   TargetMachine *TM = TheTarget->createTargetMachine(
440       Trip.getTriple(), CPU, Feature, Options, RM, CM, OptLevel);
441   return wrap(TM);
442 }
443
444 extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
445   delete unwrap(TM);
446 }
447
448 // Unfortunately, LLVM doesn't expose a C API to add the corresponding analysis
449 // passes for a target to a pass manager. We export that functionality through
450 // this function.
451 extern "C" void LLVMRustAddAnalysisPasses(LLVMTargetMachineRef TM,
452                                           LLVMPassManagerRef PMR,
453                                           LLVMModuleRef M) {
454   PassManagerBase *PM = unwrap(PMR);
455   PM->add(
456       createTargetTransformInfoWrapperPass(unwrap(TM)->getTargetIRAnalysis()));
457 }
458
459 extern "C" void LLVMRustConfigurePassManagerBuilder(
460     LLVMPassManagerBuilderRef PMBR, LLVMRustCodeGenOptLevel OptLevel,
461     bool MergeFunctions, bool SLPVectorize, bool LoopVectorize, bool PrepareForThinLTO,
462     const char* PGOGenPath, const char* PGOUsePath) {
463 #if LLVM_VERSION_GE(7, 0)
464   unwrap(PMBR)->MergeFunctions = MergeFunctions;
465 #endif
466   unwrap(PMBR)->SLPVectorize = SLPVectorize;
467   unwrap(PMBR)->OptLevel = fromRust(OptLevel);
468   unwrap(PMBR)->LoopVectorize = LoopVectorize;
469   unwrap(PMBR)->PrepareForThinLTO = PrepareForThinLTO;
470
471   if (PGOGenPath) {
472     assert(!PGOUsePath);
473     unwrap(PMBR)->EnablePGOInstrGen = true;
474     unwrap(PMBR)->PGOInstrGen = PGOGenPath;
475   }
476   if (PGOUsePath) {
477     assert(!PGOGenPath);
478     unwrap(PMBR)->PGOInstrUse = PGOUsePath;
479   }
480 }
481
482 // Unfortunately, the LLVM C API doesn't provide a way to set the `LibraryInfo`
483 // field of a PassManagerBuilder, we expose our own method of doing so.
484 extern "C" void LLVMRustAddBuilderLibraryInfo(LLVMPassManagerBuilderRef PMBR,
485                                               LLVMModuleRef M,
486                                               bool DisableSimplifyLibCalls) {
487   Triple TargetTriple(unwrap(M)->getTargetTriple());
488   TargetLibraryInfoImpl *TLI = new TargetLibraryInfoImpl(TargetTriple);
489   if (DisableSimplifyLibCalls)
490     TLI->disableAllFunctions();
491   unwrap(PMBR)->LibraryInfo = TLI;
492 }
493
494 // Unfortunately, the LLVM C API doesn't provide a way to create the
495 // TargetLibraryInfo pass, so we use this method to do so.
496 extern "C" void LLVMRustAddLibraryInfo(LLVMPassManagerRef PMR, LLVMModuleRef M,
497                                        bool DisableSimplifyLibCalls) {
498   Triple TargetTriple(unwrap(M)->getTargetTriple());
499   TargetLibraryInfoImpl TLII(TargetTriple);
500   if (DisableSimplifyLibCalls)
501     TLII.disableAllFunctions();
502   unwrap(PMR)->add(new TargetLibraryInfoWrapperPass(TLII));
503 }
504
505 // Unfortunately, the LLVM C API doesn't provide an easy way of iterating over
506 // all the functions in a module, so we do that manually here. You'll find
507 // similar code in clang's BackendUtil.cpp file.
508 extern "C" void LLVMRustRunFunctionPassManager(LLVMPassManagerRef PMR,
509                                                LLVMModuleRef M) {
510   llvm::legacy::FunctionPassManager *P =
511       unwrap<llvm::legacy::FunctionPassManager>(PMR);
512   P->doInitialization();
513
514   // Upgrade all calls to old intrinsics first.
515   for (Module::iterator I = unwrap(M)->begin(), E = unwrap(M)->end(); I != E;)
516     UpgradeCallsToIntrinsic(&*I++); // must be post-increment, as we remove
517
518   for (Module::iterator I = unwrap(M)->begin(), E = unwrap(M)->end(); I != E;
519        ++I)
520     if (!I->isDeclaration())
521       P->run(*I);
522
523   P->doFinalization();
524 }
525
526 extern "C" void LLVMRustSetLLVMOptions(int Argc, char **Argv) {
527   // Initializing the command-line options more than once is not allowed. So,
528   // check if they've already been initialized.  (This could happen if we're
529   // being called from rustpkg, for example). If the arguments change, then
530   // that's just kinda unfortunate.
531   static bool Initialized = false;
532   if (Initialized)
533     return;
534   Initialized = true;
535   cl::ParseCommandLineOptions(Argc, Argv);
536 }
537
538 enum class LLVMRustFileType {
539   Other,
540   AssemblyFile,
541   ObjectFile,
542 };
543
544 static TargetMachine::CodeGenFileType fromRust(LLVMRustFileType Type) {
545   switch (Type) {
546   case LLVMRustFileType::AssemblyFile:
547     return TargetMachine::CGFT_AssemblyFile;
548   case LLVMRustFileType::ObjectFile:
549     return TargetMachine::CGFT_ObjectFile;
550   default:
551     report_fatal_error("Bad FileType.");
552   }
553 }
554
555 extern "C" LLVMRustResult
556 LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMPassManagerRef PMR,
557                         LLVMModuleRef M, const char *Path,
558                         LLVMRustFileType RustFileType) {
559   llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
560   auto FileType = fromRust(RustFileType);
561
562   std::string ErrorInfo;
563   std::error_code EC;
564   raw_fd_ostream OS(Path, EC, sys::fs::F_None);
565   if (EC)
566     ErrorInfo = EC.message();
567   if (ErrorInfo != "") {
568     LLVMRustSetLastError(ErrorInfo.c_str());
569     return LLVMRustResult::Failure;
570   }
571
572 #if LLVM_VERSION_GE(7, 0)
573   buffer_ostream BOS(OS);
574   unwrap(Target)->addPassesToEmitFile(*PM, BOS, nullptr, FileType, false);
575 #else
576   unwrap(Target)->addPassesToEmitFile(*PM, OS, FileType, false);
577 #endif
578   PM->run(*unwrap(M));
579
580   // Apparently `addPassesToEmitFile` adds a pointer to our on-the-stack output
581   // stream (OS), so the only real safe place to delete this is here? Don't we
582   // wish this was written in Rust?
583   LLVMDisposePassManager(PMR);
584   return LLVMRustResult::Success;
585 }
586
587
588 // Callback to demangle function name
589 // Parameters:
590 // * name to be demangled
591 // * name len
592 // * output buffer
593 // * output buffer len
594 // Returns len of demangled string, or 0 if demangle failed.
595 typedef size_t (*DemangleFn)(const char*, size_t, char*, size_t);
596
597
598 namespace {
599
600 class RustAssemblyAnnotationWriter : public AssemblyAnnotationWriter {
601   DemangleFn Demangle;
602   std::vector<char> Buf;
603
604 public:
605   RustAssemblyAnnotationWriter(DemangleFn Demangle) : Demangle(Demangle) {}
606
607   // Return empty string if demangle failed
608   // or if name does not need to be demangled
609   StringRef CallDemangle(StringRef name) {
610     if (!Demangle) {
611       return StringRef();
612     }
613
614     if (Buf.size() < name.size() * 2) {
615       // Semangled name usually shorter than mangled,
616       // but allocate twice as much memory just in case
617       Buf.resize(name.size() * 2);
618     }
619
620     auto R = Demangle(name.data(), name.size(), Buf.data(), Buf.size());
621     if (!R) {
622       // Demangle failed.
623       return StringRef();
624     }
625
626     auto Demangled = StringRef(Buf.data(), R);
627     if (Demangled == name) {
628       // Do not print anything if demangled name is equal to mangled.
629       return StringRef();
630     }
631
632     return Demangled;
633   }
634
635   void emitFunctionAnnot(const Function *F,
636                          formatted_raw_ostream &OS) override {
637     StringRef Demangled = CallDemangle(F->getName());
638     if (Demangled.empty()) {
639         return;
640     }
641
642     OS << "; " << Demangled << "\n";
643   }
644
645   void emitInstructionAnnot(const Instruction *I,
646                             formatted_raw_ostream &OS) override {
647     const char *Name;
648     const Value *Value;
649     if (const CallInst *CI = dyn_cast<CallInst>(I)) {
650       Name = "call";
651       Value = CI->getCalledValue();
652     } else if (const InvokeInst* II = dyn_cast<InvokeInst>(I)) {
653       Name = "invoke";
654       Value = II->getCalledValue();
655     } else {
656       // Could demangle more operations, e. g.
657       // `store %place, @function`.
658       return;
659     }
660
661     if (!Value->hasName()) {
662       return;
663     }
664
665     StringRef Demangled = CallDemangle(Value->getName());
666     if (Demangled.empty()) {
667       return;
668     }
669
670     OS << "; " << Name << " " << Demangled << "\n";
671   }
672 };
673
674 class RustPrintModulePass : public ModulePass {
675   raw_ostream* OS;
676   DemangleFn Demangle;
677 public:
678   static char ID;
679   RustPrintModulePass() : ModulePass(ID), OS(nullptr), Demangle(nullptr) {}
680   RustPrintModulePass(raw_ostream &OS, DemangleFn Demangle)
681       : ModulePass(ID), OS(&OS), Demangle(Demangle) {}
682
683   bool runOnModule(Module &M) override {
684     RustAssemblyAnnotationWriter AW(Demangle);
685
686     M.print(*OS, &AW, false);
687
688     return false;
689   }
690
691   void getAnalysisUsage(AnalysisUsage &AU) const override {
692     AU.setPreservesAll();
693   }
694
695   static StringRef name() { return "RustPrintModulePass"; }
696 };
697
698 } // namespace
699
700 namespace llvm {
701   void initializeRustPrintModulePassPass(PassRegistry&);
702 }
703
704 char RustPrintModulePass::ID = 0;
705 INITIALIZE_PASS(RustPrintModulePass, "print-rust-module",
706                 "Print rust module to stderr", false, false)
707
708 extern "C" LLVMRustResult
709 LLVMRustPrintModule(LLVMPassManagerRef PMR, LLVMModuleRef M,
710                     const char *Path, DemangleFn Demangle) {
711   llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
712   std::string ErrorInfo;
713
714   std::error_code EC;
715   raw_fd_ostream OS(Path, EC, sys::fs::F_None);
716   if (EC)
717     ErrorInfo = EC.message();
718   if (ErrorInfo != "") {
719     LLVMRustSetLastError(ErrorInfo.c_str());
720     return LLVMRustResult::Failure;
721   }
722
723   formatted_raw_ostream FOS(OS);
724
725   PM->add(new RustPrintModulePass(FOS, Demangle));
726
727   PM->run(*unwrap(M));
728
729   return LLVMRustResult::Success;
730 }
731
732 extern "C" void LLVMRustPrintPasses() {
733   LLVMInitializePasses();
734   struct MyListener : PassRegistrationListener {
735     void passEnumerate(const PassInfo *Info) {
736       StringRef PassArg = Info->getPassArgument();
737       StringRef PassName = Info->getPassName();
738       if (!PassArg.empty()) {
739         // These unsigned->signed casts could theoretically overflow, but
740         // realistically never will (and even if, the result is implementation
741         // defined rather plain UB).
742         printf("%15.*s - %.*s\n", (int)PassArg.size(), PassArg.data(),
743                (int)PassName.size(), PassName.data());
744       }
745     }
746   } Listener;
747
748   PassRegistry *PR = PassRegistry::getPassRegistry();
749   PR->enumerateWith(&Listener);
750 }
751
752 extern "C" void LLVMRustAddAlwaysInlinePass(LLVMPassManagerBuilderRef PMBR,
753                                             bool AddLifetimes) {
754   unwrap(PMBR)->Inliner = llvm::createAlwaysInlinerLegacyPass(AddLifetimes);
755 }
756
757 extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
758                                            size_t Len) {
759   llvm::legacy::PassManager passes;
760
761   auto PreserveFunctions = [=](const GlobalValue &GV) {
762     for (size_t I = 0; I < Len; I++) {
763       if (GV.getName() == Symbols[I]) {
764         return true;
765       }
766     }
767     return false;
768   };
769
770   passes.add(llvm::createInternalizePass(PreserveFunctions));
771
772   passes.run(*unwrap(M));
773 }
774
775 extern "C" void LLVMRustMarkAllFunctionsNounwind(LLVMModuleRef M) {
776   for (Module::iterator GV = unwrap(M)->begin(), E = unwrap(M)->end(); GV != E;
777        ++GV) {
778     GV->setDoesNotThrow();
779     Function *F = dyn_cast<Function>(GV);
780     if (F == nullptr)
781       continue;
782
783     for (Function::iterator B = F->begin(), BE = F->end(); B != BE; ++B) {
784       for (BasicBlock::iterator I = B->begin(), IE = B->end(); I != IE; ++I) {
785         if (isa<InvokeInst>(I)) {
786           InvokeInst *CI = cast<InvokeInst>(I);
787           CI->setDoesNotThrow();
788         }
789       }
790     }
791   }
792 }
793
794 extern "C" void
795 LLVMRustSetDataLayoutFromTargetMachine(LLVMModuleRef Module,
796                                        LLVMTargetMachineRef TMR) {
797   TargetMachine *Target = unwrap(TMR);
798   unwrap(Module)->setDataLayout(Target->createDataLayout());
799 }
800
801 extern "C" void LLVMRustSetModulePICLevel(LLVMModuleRef M) {
802   unwrap(M)->setPICLevel(PICLevel::Level::BigPIC);
803 }
804
805 extern "C" void LLVMRustSetModulePIELevel(LLVMModuleRef M) {
806   unwrap(M)->setPIELevel(PIELevel::Level::Large);
807 }
808
809 // Here you'll find an implementation of ThinLTO as used by the Rust compiler
810 // right now. This ThinLTO support is only enabled on "recent ish" versions of
811 // LLVM, and otherwise it's just blanket rejected from other compilers.
812 //
813 // Most of this implementation is straight copied from LLVM. At the time of
814 // this writing it wasn't *quite* suitable to reuse more code from upstream
815 // for our purposes, but we should strive to upstream this support once it's
816 // ready to go! I figure we may want a bit of testing locally first before
817 // sending this upstream to LLVM. I hear though they're quite eager to receive
818 // feedback like this!
819 //
820 // If you're reading this code and wondering "what in the world" or you're
821 // working "good lord by LLVM upgrade is *still* failing due to these bindings"
822 // then fear not! (ok maybe fear a little). All code here is mostly based
823 // on `lib/LTO/ThinLTOCodeGenerator.cpp` in LLVM.
824 //
825 // You'll find that the general layout here roughly corresponds to the `run`
826 // method in that file as well as `ProcessThinLTOModule`. Functions are
827 // specifically commented below as well, but if you're updating this code
828 // or otherwise trying to understand it, the LLVM source will be useful in
829 // interpreting the mysteries within.
830 //
831 // Otherwise I'll apologize in advance, it probably requires a relatively
832 // significant investment on your part to "truly understand" what's going on
833 // here. Not saying I do myself, but it took me awhile staring at LLVM's source
834 // and various online resources about ThinLTO to make heads or tails of all
835 // this.
836
837 // This is a shared data structure which *must* be threadsafe to share
838 // read-only amongst threads. This also corresponds basically to the arguments
839 // of the `ProcessThinLTOModule` function in the LLVM source.
840 struct LLVMRustThinLTOData {
841   // The combined index that is the global analysis over all modules we're
842   // performing ThinLTO for. This is mostly managed by LLVM.
843   ModuleSummaryIndex Index;
844
845   // All modules we may look at, stored as in-memory serialized versions. This
846   // is later used when inlining to ensure we can extract any module to inline
847   // from.
848   StringMap<MemoryBufferRef> ModuleMap;
849
850   // A set that we manage of everything we *don't* want internalized. Note that
851   // this includes all transitive references right now as well, but it may not
852   // always!
853   DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
854
855   // Not 100% sure what these are, but they impact what's internalized and
856   // what's inlined across modules, I believe.
857   StringMap<FunctionImporter::ImportMapTy> ImportLists;
858   StringMap<FunctionImporter::ExportSetTy> ExportLists;
859   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
860
861 #if LLVM_VERSION_GE(7, 0)
862   LLVMRustThinLTOData() : Index(/* HaveGVs = */ false) {}
863 #endif
864 };
865
866 // Just an argument to the `LLVMRustCreateThinLTOData` function below.
867 struct LLVMRustThinLTOModule {
868   const char *identifier;
869   const char *data;
870   size_t len;
871 };
872
873 // This is copied from `lib/LTO/ThinLTOCodeGenerator.cpp`, not sure what it
874 // does.
875 static const GlobalValueSummary *
876 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
877   auto StrongDefForLinker = llvm::find_if(
878       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
879         auto Linkage = Summary->linkage();
880         return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
881                !GlobalValue::isWeakForLinker(Linkage);
882       });
883   if (StrongDefForLinker != GVSummaryList.end())
884     return StrongDefForLinker->get();
885
886   auto FirstDefForLinker = llvm::find_if(
887       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
888         auto Linkage = Summary->linkage();
889         return !GlobalValue::isAvailableExternallyLinkage(Linkage);
890       });
891   if (FirstDefForLinker == GVSummaryList.end())
892     return nullptr;
893   return FirstDefForLinker->get();
894 }
895
896 // The main entry point for creating the global ThinLTO analysis. The structure
897 // here is basically the same as before threads are spawned in the `run`
898 // function of `lib/LTO/ThinLTOCodeGenerator.cpp`.
899 extern "C" LLVMRustThinLTOData*
900 LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
901                           int num_modules,
902                           const char **preserved_symbols,
903                           int num_symbols) {
904   auto Ret = llvm::make_unique<LLVMRustThinLTOData>();
905
906   // Load each module's summary and merge it into one combined index
907   for (int i = 0; i < num_modules; i++) {
908     auto module = &modules[i];
909     StringRef buffer(module->data, module->len);
910     MemoryBufferRef mem_buffer(buffer, module->identifier);
911
912     Ret->ModuleMap[module->identifier] = mem_buffer;
913
914     if (Error Err = readModuleSummaryIndex(mem_buffer, Ret->Index, i)) {
915       LLVMRustSetLastError(toString(std::move(Err)).c_str());
916       return nullptr;
917     }
918   }
919
920   // Collect for each module the list of function it defines (GUID -> Summary)
921   Ret->Index.collectDefinedGVSummariesPerModule(Ret->ModuleToDefinedGVSummaries);
922
923   // Convert the preserved symbols set from string to GUID, this is then needed
924   // for internalization.
925   for (int i = 0; i < num_symbols; i++) {
926     auto GUID = GlobalValue::getGUID(preserved_symbols[i]);
927     Ret->GUIDPreservedSymbols.insert(GUID);
928   }
929
930   // Collect the import/export lists for all modules from the call-graph in the
931   // combined index
932   //
933   // This is copied from `lib/LTO/ThinLTOCodeGenerator.cpp`
934 #if LLVM_VERSION_GE(7, 0)
935   auto deadIsPrevailing = [&](GlobalValue::GUID G) {
936     return PrevailingType::Unknown;
937   };
938 #if LLVM_VERSION_GE(8, 0)
939   // We don't have a complete picture in our use of ThinLTO, just our immediate
940   // crate, so we need `ImportEnabled = false` to limit internalization.
941   // Otherwise, we sometimes lose `static` values -- see #60184.
942   computeDeadSymbolsWithConstProp(Ret->Index, Ret->GUIDPreservedSymbols,
943                                   deadIsPrevailing, /* ImportEnabled = */ false);
944 #else
945   computeDeadSymbols(Ret->Index, Ret->GUIDPreservedSymbols, deadIsPrevailing);
946 #endif
947 #else
948   computeDeadSymbols(Ret->Index, Ret->GUIDPreservedSymbols);
949 #endif
950   ComputeCrossModuleImport(
951     Ret->Index,
952     Ret->ModuleToDefinedGVSummaries,
953     Ret->ImportLists,
954     Ret->ExportLists
955   );
956
957   // Resolve LinkOnce/Weak symbols, this has to be computed early be cause it
958   // impacts the caching.
959   //
960   // This is copied from `lib/LTO/ThinLTOCodeGenerator.cpp` with some of this
961   // being lifted from `lib/LTO/LTO.cpp` as well
962   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
963   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
964   for (auto &I : Ret->Index) {
965     if (I.second.SummaryList.size() > 1)
966       PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second.SummaryList);
967   }
968   auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
969     const auto &Prevailing = PrevailingCopy.find(GUID);
970     if (Prevailing == PrevailingCopy.end())
971       return true;
972     return Prevailing->second == S;
973   };
974   auto recordNewLinkage = [&](StringRef ModuleIdentifier,
975                               GlobalValue::GUID GUID,
976                               GlobalValue::LinkageTypes NewLinkage) {
977     ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
978   };
979 #if LLVM_VERSION_GE(9, 0)
980   thinLTOResolvePrevailingInIndex(Ret->Index, isPrevailing, recordNewLinkage,
981                                   Ret->GUIDPreservedSymbols);
982 #elif LLVM_VERSION_GE(8, 0)
983   thinLTOResolvePrevailingInIndex(Ret->Index, isPrevailing, recordNewLinkage);
984 #else
985   thinLTOResolveWeakForLinkerInIndex(Ret->Index, isPrevailing, recordNewLinkage);
986 #endif
987
988   // Here we calculate an `ExportedGUIDs` set for use in the `isExported`
989   // callback below. This callback below will dictate the linkage for all
990   // summaries in the index, and we basically just only want to ensure that dead
991   // symbols are internalized. Otherwise everything that's already external
992   // linkage will stay as external, and internal will stay as internal.
993   std::set<GlobalValue::GUID> ExportedGUIDs;
994   for (auto &List : Ret->Index) {
995     for (auto &GVS: List.second.SummaryList) {
996       if (GlobalValue::isLocalLinkage(GVS->linkage()))
997         continue;
998       auto GUID = GVS->getOriginalName();
999       if (GVS->flags().Live)
1000         ExportedGUIDs.insert(GUID);
1001     }
1002   }
1003   auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
1004     const auto &ExportList = Ret->ExportLists.find(ModuleIdentifier);
1005     return (ExportList != Ret->ExportLists.end() &&
1006       ExportList->second.count(GUID)) ||
1007       ExportedGUIDs.count(GUID);
1008   };
1009   thinLTOInternalizeAndPromoteInIndex(Ret->Index, isExported);
1010
1011   return Ret.release();
1012 }
1013
1014 extern "C" void
1015 LLVMRustFreeThinLTOData(LLVMRustThinLTOData *Data) {
1016   delete Data;
1017 }
1018
1019 // Below are the various passes that happen *per module* when doing ThinLTO.
1020 //
1021 // In other words, these are the functions that are all run concurrently
1022 // with one another, one per module. The passes here correspond to the analysis
1023 // passes in `lib/LTO/ThinLTOCodeGenerator.cpp`, currently found in the
1024 // `ProcessThinLTOModule` function. Here they're split up into separate steps
1025 // so rustc can save off the intermediate bytecode between each step.
1026
1027 extern "C" bool
1028 LLVMRustPrepareThinLTORename(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1029   Module &Mod = *unwrap(M);
1030   if (renameModuleForThinLTO(Mod, Data->Index)) {
1031     LLVMRustSetLastError("renameModuleForThinLTO failed");
1032     return false;
1033   }
1034   return true;
1035 }
1036
1037 extern "C" bool
1038 LLVMRustPrepareThinLTOResolveWeak(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1039   Module &Mod = *unwrap(M);
1040   const auto &DefinedGlobals = Data->ModuleToDefinedGVSummaries.lookup(Mod.getModuleIdentifier());
1041 #if LLVM_VERSION_GE(8, 0)
1042   thinLTOResolvePrevailingInModule(Mod, DefinedGlobals);
1043 #else
1044   thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
1045 #endif
1046   return true;
1047 }
1048
1049 extern "C" bool
1050 LLVMRustPrepareThinLTOInternalize(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1051   Module &Mod = *unwrap(M);
1052   const auto &DefinedGlobals = Data->ModuleToDefinedGVSummaries.lookup(Mod.getModuleIdentifier());
1053   thinLTOInternalizeModule(Mod, DefinedGlobals);
1054   return true;
1055 }
1056
1057 extern "C" bool
1058 LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1059   Module &Mod = *unwrap(M);
1060
1061   const auto &ImportList = Data->ImportLists.lookup(Mod.getModuleIdentifier());
1062   auto Loader = [&](StringRef Identifier) {
1063     const auto &Memory = Data->ModuleMap.lookup(Identifier);
1064     auto &Context = Mod.getContext();
1065     auto MOrErr = getLazyBitcodeModule(Memory, Context, true, true);
1066
1067     if (!MOrErr)
1068       return MOrErr;
1069
1070     // The rest of this closure is a workaround for
1071     // https://bugs.llvm.org/show_bug.cgi?id=38184 where during ThinLTO imports
1072     // we accidentally import wasm custom sections into different modules,
1073     // duplicating them by in the final output artifact.
1074     //
1075     // The issue is worked around here by manually removing the
1076     // `wasm.custom_sections` named metadata node from any imported module. This
1077     // we know isn't used by any optimization pass so there's no need for it to
1078     // be imported.
1079     //
1080     // Note that the metadata is currently lazily loaded, so we materialize it
1081     // here before looking up if there's metadata inside. The `FunctionImporter`
1082     // will immediately materialize metadata anyway after an import, so this
1083     // shouldn't be a perf hit.
1084     if (Error Err = (*MOrErr)->materializeMetadata()) {
1085       Expected<std::unique_ptr<Module>> Ret(std::move(Err));
1086       return Ret;
1087     }
1088
1089     auto *WasmCustomSections = (*MOrErr)->getNamedMetadata("wasm.custom_sections");
1090     if (WasmCustomSections)
1091       WasmCustomSections->eraseFromParent();
1092
1093     return MOrErr;
1094   };
1095   FunctionImporter Importer(Data->Index, Loader);
1096   Expected<bool> Result = Importer.importFunctions(Mod, ImportList);
1097   if (!Result) {
1098     LLVMRustSetLastError(toString(Result.takeError()).c_str());
1099     return false;
1100   }
1101   return true;
1102 }
1103
1104 extern "C" typedef void (*LLVMRustModuleNameCallback)(void*, // payload
1105                                                       const char*, // importing module name
1106                                                       const char*); // imported module name
1107
1108 // Calls `module_name_callback` for each module import done by ThinLTO.
1109 // The callback is provided with regular null-terminated C strings.
1110 extern "C" void
1111 LLVMRustGetThinLTOModuleImports(const LLVMRustThinLTOData *data,
1112                                 LLVMRustModuleNameCallback module_name_callback,
1113                                 void* callback_payload) {
1114   for (const auto& importing_module : data->ImportLists) {
1115     const std::string importing_module_id = importing_module.getKey().str();
1116     const auto& imports = importing_module.getValue();
1117     for (const auto& imported_module : imports) {
1118       const std::string imported_module_id = imported_module.getKey().str();
1119       module_name_callback(callback_payload,
1120                            importing_module_id.c_str(),
1121                            imported_module_id.c_str());
1122     }
1123   }
1124 }
1125
1126 // This struct and various functions are sort of a hack right now, but the
1127 // problem is that we've got in-memory LLVM modules after we generate and
1128 // optimize all codegen-units for one compilation in rustc. To be compatible
1129 // with the LTO support above we need to serialize the modules plus their
1130 // ThinLTO summary into memory.
1131 //
1132 // This structure is basically an owned version of a serialize module, with
1133 // a ThinLTO summary attached.
1134 struct LLVMRustThinLTOBuffer {
1135   std::string data;
1136 };
1137
1138 extern "C" LLVMRustThinLTOBuffer*
1139 LLVMRustThinLTOBufferCreate(LLVMModuleRef M) {
1140   auto Ret = llvm::make_unique<LLVMRustThinLTOBuffer>();
1141   {
1142     raw_string_ostream OS(Ret->data);
1143     {
1144       legacy::PassManager PM;
1145       PM.add(createWriteThinLTOBitcodePass(OS));
1146       PM.run(*unwrap(M));
1147     }
1148   }
1149   return Ret.release();
1150 }
1151
1152 extern "C" void
1153 LLVMRustThinLTOBufferFree(LLVMRustThinLTOBuffer *Buffer) {
1154   delete Buffer;
1155 }
1156
1157 extern "C" const void*
1158 LLVMRustThinLTOBufferPtr(const LLVMRustThinLTOBuffer *Buffer) {
1159   return Buffer->data.data();
1160 }
1161
1162 extern "C" size_t
1163 LLVMRustThinLTOBufferLen(const LLVMRustThinLTOBuffer *Buffer) {
1164   return Buffer->data.length();
1165 }
1166
1167 // This is what we used to parse upstream bitcode for actual ThinLTO
1168 // processing.  We'll call this once per module optimized through ThinLTO, and
1169 // it'll be called concurrently on many threads.
1170 extern "C" LLVMModuleRef
1171 LLVMRustParseBitcodeForLTO(LLVMContextRef Context,
1172                            const char *data,
1173                            size_t len,
1174                            const char *identifier) {
1175   StringRef Data(data, len);
1176   MemoryBufferRef Buffer(Data, identifier);
1177   unwrap(Context)->enableDebugTypeODRUniquing();
1178   Expected<std::unique_ptr<Module>> SrcOrError =
1179       parseBitcodeFile(Buffer, *unwrap(Context));
1180   if (!SrcOrError) {
1181     LLVMRustSetLastError(toString(SrcOrError.takeError()).c_str());
1182     return nullptr;
1183   }
1184   return wrap(std::move(*SrcOrError).release());
1185 }
1186
1187 // Rewrite all `DICompileUnit` pointers to the `DICompileUnit` specified. See
1188 // the comment in `back/lto.rs` for why this exists.
1189 extern "C" void
1190 LLVMRustThinLTOGetDICompileUnit(LLVMModuleRef Mod,
1191                                 DICompileUnit **A,
1192                                 DICompileUnit **B) {
1193   Module *M = unwrap(Mod);
1194   DICompileUnit **Cur = A;
1195   DICompileUnit **Next = B;
1196   for (DICompileUnit *CU : M->debug_compile_units()) {
1197     *Cur = CU;
1198     Cur = Next;
1199     Next = nullptr;
1200     if (Cur == nullptr)
1201       break;
1202   }
1203 }
1204
1205 // Rewrite all `DICompileUnit` pointers to the `DICompileUnit` specified. See
1206 // the comment in `back/lto.rs` for why this exists.
1207 extern "C" void
1208 LLVMRustThinLTOPatchDICompileUnit(LLVMModuleRef Mod, DICompileUnit *Unit) {
1209   Module *M = unwrap(Mod);
1210
1211   // If the original source module didn't have a `DICompileUnit` then try to
1212   // merge all the existing compile units. If there aren't actually any though
1213   // then there's not much for us to do so return.
1214   if (Unit == nullptr) {
1215     for (DICompileUnit *CU : M->debug_compile_units()) {
1216       Unit = CU;
1217       break;
1218     }
1219     if (Unit == nullptr)
1220       return;
1221   }
1222
1223   // Use LLVM's built-in `DebugInfoFinder` to find a bunch of debuginfo and
1224   // process it recursively. Note that we specifically iterate over instructions
1225   // to ensure we feed everything into it.
1226   DebugInfoFinder Finder;
1227   Finder.processModule(*M);
1228   for (Function &F : M->functions()) {
1229     for (auto &FI : F) {
1230       for (Instruction &BI : FI) {
1231         if (auto Loc = BI.getDebugLoc())
1232           Finder.processLocation(*M, Loc);
1233         if (auto DVI = dyn_cast<DbgValueInst>(&BI))
1234           Finder.processValue(*M, DVI);
1235         if (auto DDI = dyn_cast<DbgDeclareInst>(&BI))
1236           Finder.processDeclare(*M, DDI);
1237       }
1238     }
1239   }
1240
1241   // After we've found all our debuginfo, rewrite all subprograms to point to
1242   // the same `DICompileUnit`.
1243   for (auto &F : Finder.subprograms()) {
1244     F->replaceUnit(Unit);
1245   }
1246
1247   // Erase any other references to other `DICompileUnit` instances, the verifier
1248   // will later ensure that we don't actually have any other stale references to
1249   // worry about.
1250   auto *MD = M->getNamedMetadata("llvm.dbg.cu");
1251   MD->clearOperands();
1252   MD->addOperand(Unit);
1253 }