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