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