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.
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.
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"
28 #if LLVM_VERSION_GE(6, 0)
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/Target/TargetSubtargetInfo.h"
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"
45 #include "llvm-c/Transforms/PassManagerBuilder.h"
47 #if LLVM_VERSION_GE(4, 0)
52 using namespace llvm::legacy;
54 extern cl::opt<bool> EnableARMEHABI;
56 typedef struct LLVMOpaquePass *LLVMPassRef;
57 typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
59 DEFINE_STDCXX_CONVERSION_FUNCTIONS(Pass, LLVMPassRef)
60 DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
61 DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBuilder,
62 LLVMPassManagerBuilderRef)
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);
78 enum class LLVMRustPassKind {
84 static LLVMRustPassKind toRust(PassKind Kind) {
87 return LLVMRustPassKind::Function;
89 return LLVMRustPassKind::Module;
91 return LLVMRustPassKind::Other;
95 extern "C" LLVMPassRef LLVMRustFindAndCreatePass(const char *PassName) {
96 StringRef SR(PassName);
97 PassRegistry *PR = PassRegistry::getPassRegistry();
99 const PassInfo *PI = PR->getPassInfo(SR);
101 return wrap(PI->createPass());
106 extern "C" LLVMRustPassKind LLVMRustPassKind(LLVMPassRef RustPass) {
108 Pass *Pass = unwrap(RustPass);
109 return toRust(Pass->getPassKind());
112 extern "C" void LLVMRustAddPass(LLVMPassManagerRef PMR, LLVMPassRef RustPass) {
114 Pass *Pass = unwrap(RustPass);
115 PassManagerBase *PMB = unwrap(PMR);
120 bool LLVMRustPassManagerBuilderPopulateThinLTOPassManager(
121 LLVMPassManagerBuilderRef PMBR,
122 LLVMPassManagerRef PMR
124 #if LLVM_VERSION_GE(4, 0)
125 unwrap(PMBR)->populateThinLTOPassManager(*unwrap(PMR));
132 #ifdef LLVM_COMPONENT_X86
133 #define SUBTARGET_X86 SUBTARGET(X86)
135 #define SUBTARGET_X86
138 #ifdef LLVM_COMPONENT_ARM
139 #define SUBTARGET_ARM SUBTARGET(ARM)
141 #define SUBTARGET_ARM
144 #ifdef LLVM_COMPONENT_AARCH64
145 #define SUBTARGET_AARCH64 SUBTARGET(AArch64)
147 #define SUBTARGET_AARCH64
150 #ifdef LLVM_COMPONENT_MIPS
151 #define SUBTARGET_MIPS SUBTARGET(Mips)
153 #define SUBTARGET_MIPS
156 #ifdef LLVM_COMPONENT_POWERPC
157 #define SUBTARGET_PPC SUBTARGET(PPC)
159 #define SUBTARGET_PPC
162 #ifdef LLVM_COMPONENT_SYSTEMZ
163 #define SUBTARGET_SYSTEMZ SUBTARGET(SystemZ)
165 #define SUBTARGET_SYSTEMZ
168 #ifdef LLVM_COMPONENT_MSP430
169 #define SUBTARGET_MSP430 SUBTARGET(MSP430)
171 #define SUBTARGET_MSP430
174 #ifdef LLVM_COMPONENT_RISCV
175 #define SUBTARGET_RISCV SUBTARGET(RISCV)
177 #define SUBTARGET_RISCV
180 #ifdef LLVM_COMPONENT_SPARC
181 #define SUBTARGET_SPARC SUBTARGET(Sparc)
183 #define SUBTARGET_SPARC
186 #ifdef LLVM_COMPONENT_HEXAGON
187 #define SUBTARGET_HEXAGON SUBTARGET(Hexagon)
189 #define SUBTARGET_HEXAGON
192 #define GEN_SUBTARGETS \
204 #define SUBTARGET(x) \
206 extern const SubtargetFeatureKV x##FeatureKV[]; \
207 extern const SubtargetFeatureKV x##SubTypeKV[]; \
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);
224 enum class LLVMRustCodeModel {
233 static CodeModel::Model fromRust(LLVMRustCodeModel 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;
244 report_fatal_error("Bad CodeModel.");
248 enum class LLVMRustCodeGenOptLevel {
256 static CodeGenOpt::Level fromRust(LLVMRustCodeGenOptLevel 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;
267 report_fatal_error("Bad CodeGenOptLevel.");
271 enum class LLVMRustRelocMode {
281 static Optional<Reloc::Model> fromRust(LLVMRustRelocMode RustReloc) {
283 case LLVMRustRelocMode::Default:
285 case LLVMRustRelocMode::Static:
286 return Reloc::Static;
287 case LLVMRustRelocMode::PIC:
289 case LLVMRustRelocMode::DynamicNoPic:
290 return Reloc::DynamicNoPIC;
291 #if LLVM_VERSION_GE(4, 0)
292 case LLVMRustRelocMode::ROPI:
294 case LLVMRustRelocMode::RWPI:
296 case LLVMRustRelocMode::ROPIRWPI:
297 return Reloc::ROPI_RWPI;
303 report_fatal_error("Bad RelocModel.");
307 /// getLongestEntryLength - Return the length of the longest entry in the table.
309 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
311 for (auto &I : Table)
312 MaxLen = std::max(MaxLen, std::strlen(I.Key));
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);
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());
330 for (auto &CPU : CPUTable)
331 printf(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
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);
341 printf("Available features for this target:\n");
342 for (auto &Feature : FeatTable)
343 printf(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
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");
353 extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef) {
354 printf("Target CPU help is not supported by this LLVM version.\n\n");
357 extern "C" void LLVMRustPrintTargetFeatures(LLVMTargetMachineRef) {
358 printf("Target features help is not supported by this LLVM version.\n\n");
362 extern "C" const char* LLVMRustGetHostCPUName(size_t *len) {
363 StringRef Name = sys::getHostCPUName();
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,
374 bool TrapUnreachable,
377 bool EmitStackSizeSection) {
379 auto OptLevel = fromRust(RustOptLevel);
380 auto RM = fromRust(RustReloc);
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());
391 TargetOptions Options;
393 Options.FloatABIType = FloatABI::Default;
395 Options.FloatABIType = FloatABI::Soft;
397 Options.DataSections = DataSections;
398 Options.FunctionSections = FunctionSections;
399 Options.MCOptions.AsmVerbose = AsmComments;
400 Options.MCOptions.PreserveAsmComments = AsmComments;
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;
411 Options.ThreadModel = ThreadModel::Single;
414 #if LLVM_VERSION_GE(6, 0)
415 Options.EmitStackSizeSection = EmitStackSizeSection;
417 Optional<CodeModel::Model> CM;
419 CodeModel::Model CM = CodeModel::Model::Default;
421 if (RustCM != LLVMRustCodeModel::None)
422 CM = fromRust(RustCM);
423 TargetMachine *TM = TheTarget->createTargetMachine(
424 Trip.getTriple(), CPU, Feature, Options, RM, CM, OptLevel);
428 extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
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
435 extern "C" void LLVMRustAddAnalysisPasses(LLVMTargetMachineRef TM,
436 LLVMPassManagerRef PMR,
438 PassManagerBase *PM = unwrap(PMR);
440 createTargetTransformInfoWrapperPass(unwrap(TM)->getTargetIRAnalysis()));
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) {
448 unwrap(PMBR)->MergeFunctions = MergeFunctions;
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;
460 unwrap(PMBR)->EnablePGOInstrGen = true;
461 unwrap(PMBR)->PGOInstrGen = PGOGenPath;
465 unwrap(PMBR)->PGOInstrUse = PGOUsePath;
468 assert(!PGOGenPath && !PGOUsePath && "Should've caught earlier");
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,
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;
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));
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,
500 llvm::legacy::FunctionPassManager *P =
501 unwrap<llvm::legacy::FunctionPassManager>(PMR);
502 P->doInitialization();
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
508 for (Module::iterator I = unwrap(M)->begin(), E = unwrap(M)->end(); I != E;
510 if (!I->isDeclaration())
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;
525 cl::ParseCommandLineOptions(Argc, Argv);
528 enum class LLVMRustFileType {
534 static TargetMachine::CodeGenFileType fromRust(LLVMRustFileType Type) {
536 case LLVMRustFileType::AssemblyFile:
537 return TargetMachine::CGFT_AssemblyFile;
538 case LLVMRustFileType::ObjectFile:
539 return TargetMachine::CGFT_ObjectFile;
541 report_fatal_error("Bad FileType.");
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);
552 std::string ErrorInfo;
554 raw_fd_ostream OS(Path, EC, sys::fs::F_None);
556 ErrorInfo = EC.message();
557 if (ErrorInfo != "") {
558 LLVMRustSetLastError(ErrorInfo.c_str());
559 return LLVMRustResult::Failure;
562 #if LLVM_VERSION_GE(7, 0)
563 buffer_ostream BOS(OS);
564 unwrap(Target)->addPassesToEmitFile(*PM, BOS, nullptr, FileType, false);
566 unwrap(Target)->addPassesToEmitFile(*PM, OS, FileType, false);
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?
574 return LLVMRustResult::Success;
578 // Callback to demangle function name
580 // * name to be demangled
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);
590 class RustAssemblyAnnotationWriter : public AssemblyAnnotationWriter {
592 std::vector<char> Buf;
595 RustAssemblyAnnotationWriter(DemangleFn Demangle) : Demangle(Demangle) {}
597 // Return empty string if demangle failed
598 // or if name does not need to be demangled
599 StringRef CallDemangle(StringRef name) {
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);
610 auto R = Demangle(name.data(), name.size(), Buf.data(), Buf.size());
616 auto Demangled = StringRef(Buf.data(), R);
617 if (Demangled == name) {
618 // Do not print anything if demangled name is equal to mangled.
625 void emitFunctionAnnot(const Function *F,
626 formatted_raw_ostream &OS) override {
627 StringRef Demangled = CallDemangle(F->getName());
628 if (Demangled.empty()) {
632 OS << "; " << Demangled << "\n";
635 void emitInstructionAnnot(const Instruction *I,
636 formatted_raw_ostream &OS) override {
639 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
641 Value = CI->getCalledValue();
642 } else if (const InvokeInst* II = dyn_cast<InvokeInst>(I)) {
644 Value = II->getCalledValue();
646 // Could demangle more operations, e. g.
647 // `store %place, @function`.
651 if (!Value->hasName()) {
655 StringRef Demangled = CallDemangle(Value->getName());
656 if (Demangled.empty()) {
660 OS << "; " << Name << " " << Demangled << "\n";
664 class RustPrintModulePass : public ModulePass {
669 RustPrintModulePass() : ModulePass(ID), OS(nullptr), Demangle(nullptr) {}
670 RustPrintModulePass(raw_ostream &OS, DemangleFn Demangle)
671 : ModulePass(ID), OS(&OS), Demangle(Demangle) {}
673 bool runOnModule(Module &M) override {
674 RustAssemblyAnnotationWriter AW(Demangle);
676 M.print(*OS, &AW, false);
681 void getAnalysisUsage(AnalysisUsage &AU) const override {
682 AU.setPreservesAll();
685 static StringRef name() { return "RustPrintModulePass"; }
691 void initializeRustPrintModulePassPass(PassRegistry&);
694 char RustPrintModulePass::ID = 0;
695 INITIALIZE_PASS(RustPrintModulePass, "print-rust-module",
696 "Print rust module to stderr", false, false)
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;
704 raw_fd_ostream OS(Path, EC, sys::fs::F_None);
706 ErrorInfo = EC.message();
708 formatted_raw_ostream FOS(OS);
710 PM->add(new RustPrintModulePass(FOS, Demangle));
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());
730 if (Info->getPassArgument() && *Info->getPassArgument()) {
731 printf("%15s - %s\n", Info->getPassArgument(), Info->getPassName());
737 PassRegistry *PR = PassRegistry::getPassRegistry();
738 PR->enumerateWith(&Listener);
741 extern "C" void LLVMRustAddAlwaysInlinePass(LLVMPassManagerBuilderRef PMBR,
743 #if LLVM_VERSION_GE(4, 0)
744 unwrap(PMBR)->Inliner = llvm::createAlwaysInlinerLegacyPass(AddLifetimes);
746 unwrap(PMBR)->Inliner = createAlwaysInlinerPass(AddLifetimes);
750 extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
752 llvm::legacy::PassManager passes;
754 auto PreserveFunctions = [=](const GlobalValue &GV) {
755 for (size_t I = 0; I < Len; I++) {
756 if (GV.getName() == Symbols[I]) {
763 passes.add(llvm::createInternalizePass(PreserveFunctions));
765 passes.run(*unwrap(M));
768 extern "C" void LLVMRustMarkAllFunctionsNounwind(LLVMModuleRef M) {
769 for (Module::iterator GV = unwrap(M)->begin(), E = unwrap(M)->end(); GV != E;
771 GV->setDoesNotThrow();
772 Function *F = dyn_cast<Function>(GV);
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();
788 LLVMRustSetDataLayoutFromTargetMachine(LLVMModuleRef Module,
789 LLVMTargetMachineRef TMR) {
790 TargetMachine *Target = unwrap(TMR);
791 unwrap(Module)->setDataLayout(Target->createDataLayout());
794 extern "C" void LLVMRustSetModulePIELevel(LLVMModuleRef M) {
795 unwrap(M)->setPIELevel(PIELevel::Level::Large);
799 LLVMRustThinLTOAvailable() {
800 #if LLVM_VERSION_GE(4, 0)
808 LLVMRustPGOAvailable() {
816 #if LLVM_VERSION_GE(4, 0)
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.
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!
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.
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.
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
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;
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
857 StringMap<MemoryBufferRef> ModuleMap;
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
862 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
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;
870 #if LLVM_VERSION_GE(7, 0)
871 LLVMRustThinLTOData() : Index(/* isPerformingAnalysis = */ false) {}
875 // Just an argument to the `LLVMRustCreateThinLTOData` function below.
876 struct LLVMRustThinLTOModule {
877 const char *identifier;
882 // This is copied from `lib/LTO/ThinLTOCodeGenerator.cpp`, not sure what it
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);
892 if (StrongDefForLinker != GVSummaryList.end())
893 return StrongDefForLinker->get();
895 auto FirstDefForLinker = llvm::find_if(
896 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
897 auto Linkage = Summary->linkage();
898 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
900 if (FirstDefForLinker == GVSummaryList.end())
902 return FirstDefForLinker->get();
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,
911 const char **preserved_symbols,
913 auto Ret = llvm::make_unique<LLVMRustThinLTOData>();
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);
921 Ret->ModuleMap[module->identifier] = mem_buffer;
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());
929 Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
930 object::ModuleSummaryIndexObjectFile::create(mem_buffer);
932 LLVMRustSetLastError(toString(ObjOrErr.takeError()).c_str());
935 auto Index = (*ObjOrErr)->takeIndex();
936 Ret->Index.mergeFrom(std::move(Index), i);
940 // Collect for each module the list of function it defines (GUID -> Summary)
941 Ret->Index.collectDefinedGVSummariesPerModule(Ret->ModuleToDefinedGVSummaries);
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);
950 // Collect the import/export lists for all modules from the call-graph in the
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;
959 computeDeadSymbols(Ret->Index, Ret->GUIDPreservedSymbols, deadIsPrevailing);
961 computeDeadSymbols(Ret->Index, Ret->GUIDPreservedSymbols);
963 ComputeCrossModuleImport(
965 Ret->ModuleToDefinedGVSummaries,
970 auto DeadSymbols = computeDeadSymbols(Ret->Index, Ret->GUIDPreservedSymbols);
971 ComputeCrossModuleImport(
973 Ret->ModuleToDefinedGVSummaries,
980 // Resolve LinkOnce/Weak symbols, this has to be computed early be cause it
981 // impacts the caching.
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);
992 if (I.second.size() > 1)
993 PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
996 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
997 const auto &Prevailing = PrevailingCopy.find(GUID);
998 if (Prevailing == PrevailingCopy.end())
1000 return Prevailing->second == S;
1002 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
1003 GlobalValue::GUID GUID,
1004 GlobalValue::LinkageTypes NewLinkage) {
1005 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
1007 thinLTOResolveWeakForLinkerInIndex(Ret->Index, isPrevailing, recordNewLinkage);
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) {
1019 for (auto &GVS: List.second) {
1021 if (GlobalValue::isLocalLinkage(GVS->linkage()))
1023 auto GUID = GVS->getOriginalName();
1024 #if LLVM_VERSION_GE(5, 0)
1025 if (GVS->flags().Live)
1027 if (!DeadSymbols.count(GUID))
1029 ExportedGUIDs.insert(GUID);
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);
1038 thinLTOInternalizeAndPromoteInIndex(Ret->Index, isExported);
1040 return Ret.release();
1044 LLVMRustFreeThinLTOData(LLVMRustThinLTOData *Data) {
1048 // Below are the various passes that happen *per module* when doing ThinLTO.
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.
1057 LLVMRustPrepareThinLTORename(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1058 Module &Mod = *unwrap(M);
1059 if (renameModuleForThinLTO(Mod, Data->Index)) {
1060 LLVMRustSetLastError("renameModuleForThinLTO failed");
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);
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);
1083 LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1084 Module &Mod = *unwrap(M);
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);
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.
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
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));
1114 auto *WasmCustomSections = (*MOrErr)->getNamedMetadata("wasm.custom_sections");
1115 if (WasmCustomSections)
1116 WasmCustomSections->eraseFromParent();
1120 FunctionImporter Importer(Data->Index, Loader);
1121 Expected<bool> Result = Importer.importFunctions(Mod, ImportList);
1123 LLVMRustSetLastError(toString(Result.takeError()).c_str());
1129 extern "C" typedef void (*LLVMRustModuleNameCallback)(void*, // payload
1130 const char*, // importing module name
1131 const char*); // imported module name
1133 // Calls `module_name_callback` for each module import done by ThinLTO.
1134 // The callback is provided with regular null-terminated C strings.
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());
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.
1157 // This structure is basically an owned version of a serialize module, with
1158 // a ThinLTO summary attached.
1159 struct LLVMRustThinLTOBuffer {
1163 extern "C" LLVMRustThinLTOBuffer*
1164 LLVMRustThinLTOBufferCreate(LLVMModuleRef M) {
1165 auto Ret = llvm::make_unique<LLVMRustThinLTOBuffer>();
1167 raw_string_ostream OS(Ret->data);
1169 legacy::PassManager PM;
1170 PM.add(createWriteThinLTOBitcodePass(OS));
1174 return Ret.release();
1178 LLVMRustThinLTOBufferFree(LLVMRustThinLTOBuffer *Buffer) {
1182 extern "C" const void*
1183 LLVMRustThinLTOBufferPtr(const LLVMRustThinLTOBuffer *Buffer) {
1184 return Buffer->data.data();
1188 LLVMRustThinLTOBufferLen(const LLVMRustThinLTOBuffer *Buffer) {
1189 return Buffer->data.length();
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,
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));
1206 LLVMRustSetLastError(toString(SrcOrError.takeError()).c_str());
1209 return wrap(std::move(*SrcOrError).release());
1212 // Rewrite all `DICompileUnit` pointers to the `DICompileUnit` specified. See
1213 // the comment in `back/lto.rs` for why this exists.
1215 LLVMRustThinLTOGetDICompileUnit(LLVMModuleRef Mod,
1217 DICompileUnit **B) {
1218 Module *M = unwrap(Mod);
1219 DICompileUnit **Cur = A;
1220 DICompileUnit **Next = B;
1221 for (DICompileUnit *CU : M->debug_compile_units()) {
1230 // Rewrite all `DICompileUnit` pointers to the `DICompileUnit` specified. See
1231 // the comment in `back/lto.rs` for why this exists.
1233 LLVMRustThinLTOPatchDICompileUnit(LLVMModuleRef Mod, DICompileUnit *Unit) {
1234 Module *M = unwrap(Mod);
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()) {
1244 if (Unit == nullptr)
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);
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);
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
1275 auto *MD = M->getNamedMetadata("llvm.dbg.cu");
1276 MD->clearOperands();
1277 MD->addOperand(Unit);
1282 struct LLVMRustThinLTOData {
1285 struct LLVMRustThinLTOModule {
1288 extern "C" LLVMRustThinLTOData*
1289 LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
1291 const char **preserved_symbols,
1293 report_fatal_error("ThinLTO not available");
1297 LLVMRustPrepareThinLTORename(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1298 report_fatal_error("ThinLTO not available");
1302 LLVMRustPrepareThinLTOResolveWeak(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1303 report_fatal_error("ThinLTO not available");
1307 LLVMRustPrepareThinLTOInternalize(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1308 report_fatal_error("ThinLTO not available");
1312 LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1313 report_fatal_error("ThinLTO not available");
1316 extern "C" LLVMRustThinLTOModuleImports
1317 LLVMRustGetLLVMRustThinLTOModuleImports(const LLVMRustThinLTOData *Data) {
1318 report_fatal_error("ThinLTO not available");
1322 LLVMRustFreeThinLTOData(LLVMRustThinLTOData *Data) {
1323 report_fatal_error("ThinLTO not available");
1326 struct LLVMRustThinLTOBuffer {
1329 extern "C" LLVMRustThinLTOBuffer*
1330 LLVMRustThinLTOBufferCreate(LLVMModuleRef M) {
1331 report_fatal_error("ThinLTO not available");
1335 LLVMRustThinLTOBufferFree(LLVMRustThinLTOBuffer *Buffer) {
1336 report_fatal_error("ThinLTO not available");
1339 extern "C" const void*
1340 LLVMRustThinLTOBufferPtr(const LLVMRustThinLTOBuffer *Buffer) {
1341 report_fatal_error("ThinLTO not available");
1345 LLVMRustThinLTOBufferLen(const LLVMRustThinLTOBuffer *Buffer) {
1346 report_fatal_error("ThinLTO not available");
1349 extern "C" LLVMModuleRef
1350 LLVMRustParseBitcodeForThinLTO(LLVMContextRef Context,
1353 const char *identifier) {
1354 report_fatal_error("ThinLTO not available");
1358 LLVMRustThinLTOGetDICompileUnit(LLVMModuleRef Mod,
1360 DICompileUnit **B) {
1361 report_fatal_error("ThinLTO not available");
1365 LLVMRustThinLTOPatchDICompileUnit(LLVMModuleRef Mod) {
1366 report_fatal_error("ThinLTO not available");
1369 #endif // LLVM_VERSION_GE(4, 0)