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.
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"
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"
38 #include "llvm-c/Transforms/PassManagerBuilder.h"
41 using namespace llvm::legacy;
43 extern cl::opt<bool> EnableARMEHABI;
45 typedef struct LLVMOpaquePass *LLVMPassRef;
46 typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
48 DEFINE_STDCXX_CONVERSION_FUNCTIONS(Pass, LLVMPassRef)
49 DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
50 DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBuilder,
51 LLVMPassManagerBuilderRef)
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);
64 initializeTransformUtils(Registry);
65 initializeInstCombine(Registry);
66 initializeInstrumentation(Registry);
67 initializeTarget(Registry);
70 enum class LLVMRustPassKind {
76 static LLVMRustPassKind toRust(PassKind Kind) {
79 return LLVMRustPassKind::Function;
81 return LLVMRustPassKind::Module;
83 return LLVMRustPassKind::Other;
87 extern "C" LLVMPassRef LLVMRustFindAndCreatePass(const char *PassName) {
88 StringRef SR(PassName);
89 PassRegistry *PR = PassRegistry::getPassRegistry();
91 const PassInfo *PI = PR->getPassInfo(SR);
93 return wrap(PI->createPass());
98 extern "C" LLVMRustPassKind LLVMRustPassKind(LLVMPassRef RustPass) {
100 Pass *Pass = unwrap(RustPass);
101 return toRust(Pass->getPassKind());
104 extern "C" void LLVMRustAddPass(LLVMPassManagerRef PMR, LLVMPassRef RustPass) {
106 Pass *Pass = unwrap(RustPass);
107 PassManagerBase *PMB = unwrap(PMR);
112 bool LLVMRustPassManagerBuilderPopulateThinLTOPassManager(
113 LLVMPassManagerBuilderRef PMBR,
114 LLVMPassManagerRef PMR
116 #if LLVM_VERSION_GE(4, 0)
117 unwrap(PMBR)->populateThinLTOPassManager(*unwrap(PMR));
124 #ifdef LLVM_COMPONENT_X86
125 #define SUBTARGET_X86 SUBTARGET(X86)
127 #define SUBTARGET_X86
130 #ifdef LLVM_COMPONENT_ARM
131 #define SUBTARGET_ARM SUBTARGET(ARM)
133 #define SUBTARGET_ARM
136 #ifdef LLVM_COMPONENT_AARCH64
137 #define SUBTARGET_AARCH64 SUBTARGET(AArch64)
139 #define SUBTARGET_AARCH64
142 #ifdef LLVM_COMPONENT_MIPS
143 #define SUBTARGET_MIPS SUBTARGET(Mips)
145 #define SUBTARGET_MIPS
148 #ifdef LLVM_COMPONENT_POWERPC
149 #define SUBTARGET_PPC SUBTARGET(PPC)
151 #define SUBTARGET_PPC
154 #ifdef LLVM_COMPONENT_SYSTEMZ
155 #define SUBTARGET_SYSTEMZ SUBTARGET(SystemZ)
157 #define SUBTARGET_SYSTEMZ
160 #ifdef LLVM_COMPONENT_MSP430
161 #define SUBTARGET_MSP430 SUBTARGET(MSP430)
163 #define SUBTARGET_MSP430
166 #ifdef LLVM_COMPONENT_SPARC
167 #define SUBTARGET_SPARC SUBTARGET(Sparc)
169 #define SUBTARGET_SPARC
172 #ifdef LLVM_COMPONENT_HEXAGON
173 #define SUBTARGET_HEXAGON SUBTARGET(Hexagon)
175 #define SUBTARGET_HEXAGON
178 #define GEN_SUBTARGETS \
189 #define SUBTARGET(x) \
191 extern const SubtargetFeatureKV x##FeatureKV[]; \
192 extern const SubtargetFeatureKV x##SubTypeKV[]; \
198 extern "C" bool LLVMRustHasFeature(LLVMTargetMachineRef TM,
199 const char *Feature) {
201 TargetMachine *Target = unwrap(TM);
202 const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
203 const FeatureBitset &Bits = MCInfo->getFeatureBits();
204 const ArrayRef<SubtargetFeatureKV> FeatTable = MCInfo->getFeatureTable();
206 for (auto &FeatureEntry : FeatTable)
207 if (!strcmp(FeatureEntry.Key, Feature))
208 return (Bits & FeatureEntry.Value) == FeatureEntry.Value;
213 enum class LLVMRustCodeModel {
223 static CodeModel::Model fromRust(LLVMRustCodeModel 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;
238 report_fatal_error("Bad CodeModel.");
242 enum class LLVMRustCodeGenOptLevel {
250 static CodeGenOpt::Level fromRust(LLVMRustCodeGenOptLevel 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;
261 report_fatal_error("Bad CodeGenOptLevel.");
265 enum class LLVMRustRelocMode {
275 #if LLVM_VERSION_LE(3, 8)
276 static Reloc::Model fromRust(LLVMRustRelocMode RustReloc) {
278 static Optional<Reloc::Model> fromRust(LLVMRustRelocMode RustReloc) {
281 case LLVMRustRelocMode::Default:
282 #if LLVM_VERSION_LE(3, 8)
283 return Reloc::Default;
287 case LLVMRustRelocMode::Static:
288 return Reloc::Static;
289 case LLVMRustRelocMode::PIC:
291 case LLVMRustRelocMode::DynamicNoPic:
292 return Reloc::DynamicNoPIC;
293 #if LLVM_VERSION_GE(4, 0)
294 case LLVMRustRelocMode::ROPI:
296 case LLVMRustRelocMode::RWPI:
298 case LLVMRustRelocMode::ROPIRWPI:
299 return Reloc::ROPI_RWPI;
305 report_fatal_error("Bad RelocModel.");
309 /// getLongestEntryLength - Return the length of the longest entry in the table.
311 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
313 for (auto &I : Table)
314 MaxLen = std::max(MaxLen, std::strlen(I.Key));
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);
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());
332 for (auto &CPU : CPUTable)
333 printf(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
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);
343 printf("Available features for this target:\n");
344 for (auto &Feature : FeatTable)
345 printf(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
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");
355 extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef) {
356 printf("Target CPU help is not supported by this LLVM version.\n\n");
359 extern "C" void LLVMRustPrintTargetFeatures(LLVMTargetMachineRef) {
360 printf("Target features help is not supported by this LLVM version.\n\n");
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,
370 bool TrapUnreachable,
373 auto CM = fromRust(RustCM);
374 auto OptLevel = fromRust(RustOptLevel);
375 auto RM = fromRust(RustReloc);
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());
386 StringRef RealCPU = CPU;
387 if (RealCPU == "native") {
388 RealCPU = sys::getHostCPUName();
391 TargetOptions Options;
392 #if LLVM_VERSION_LE(3, 8)
393 Options.PositionIndependentExecutable = PositionIndependentExecutable;
396 Options.FloatABIType = FloatABI::Default;
398 Options.FloatABIType = FloatABI::Soft;
400 Options.DataSections = DataSections;
401 Options.FunctionSections = FunctionSections;
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;
412 Options.ThreadModel = ThreadModel::Single;
415 TargetMachine *TM = TheTarget->createTargetMachine(
416 Trip.getTriple(), RealCPU, Feature, Options, RM, CM, OptLevel);
420 extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
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
427 extern "C" void LLVMRustAddAnalysisPasses(LLVMTargetMachineRef TM,
428 LLVMPassManagerRef PMR,
430 PassManagerBase *PM = unwrap(PMR);
432 createTargetTransformInfoWrapperPass(unwrap(TM)->getTargetIRAnalysis()));
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;
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,
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;
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));
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,
473 llvm::legacy::FunctionPassManager *P =
474 unwrap<llvm::legacy::FunctionPassManager>(PMR);
475 P->doInitialization();
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
481 for (Module::iterator I = unwrap(M)->begin(), E = unwrap(M)->end(); I != E;
483 if (!I->isDeclaration())
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;
498 cl::ParseCommandLineOptions(Argc, Argv);
501 enum class LLVMRustFileType {
507 static TargetMachine::CodeGenFileType fromRust(LLVMRustFileType Type) {
509 case LLVMRustFileType::AssemblyFile:
510 return TargetMachine::CGFT_AssemblyFile;
511 case LLVMRustFileType::ObjectFile:
512 return TargetMachine::CGFT_ObjectFile;
514 report_fatal_error("Bad FileType.");
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);
525 std::string ErrorInfo;
527 raw_fd_ostream OS(Path, EC, sys::fs::F_None);
529 ErrorInfo = EC.message();
530 if (ErrorInfo != "") {
531 LLVMRustSetLastError(ErrorInfo.c_str());
532 return LLVMRustResult::Failure;
535 unwrap(Target)->addPassesToEmitFile(*PM, OS, FileType, false);
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?
542 return LLVMRustResult::Success;
546 // Callback to demangle function name
548 // * name to be demangled
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);
558 class RustAssemblyAnnotationWriter : public AssemblyAnnotationWriter {
560 std::vector<char> Buf;
563 RustAssemblyAnnotationWriter(DemangleFn Demangle) : Demangle(Demangle) {}
565 // Return empty string if demangle failed
566 // or if name does not need to be demangled
567 StringRef CallDemangle(StringRef name) {
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);
578 auto R = Demangle(name.data(), name.size(), Buf.data(), Buf.size());
584 auto Demangled = StringRef(Buf.data(), R);
585 if (Demangled == name) {
586 // Do not print anything if demangled name is equal to mangled.
593 void emitFunctionAnnot(const Function *F,
594 formatted_raw_ostream &OS) override {
595 StringRef Demangled = CallDemangle(F->getName());
596 if (Demangled.empty()) {
600 OS << "; " << Demangled << "\n";
603 void emitInstructionAnnot(const Instruction *I,
604 formatted_raw_ostream &OS) override {
607 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
609 Value = CI->getCalledValue();
610 } else if (const InvokeInst* II = dyn_cast<InvokeInst>(I)) {
612 Value = II->getCalledValue();
614 // Could demangle more operations, e. g.
615 // `store %place, @function`.
619 if (!Value->hasName()) {
623 StringRef Demangled = CallDemangle(Value->getName());
624 if (Demangled.empty()) {
628 OS << "; " << Name << " " << Demangled << "\n";
632 class RustPrintModulePass : public ModulePass {
637 RustPrintModulePass() : ModulePass(ID), OS(nullptr), Demangle(nullptr) {}
638 RustPrintModulePass(raw_ostream &OS, DemangleFn Demangle)
639 : ModulePass(ID), OS(&OS), Demangle(Demangle) {}
641 bool runOnModule(Module &M) override {
642 RustAssemblyAnnotationWriter AW(Demangle);
644 M.print(*OS, &AW, false);
649 void getAnalysisUsage(AnalysisUsage &AU) const override {
650 AU.setPreservesAll();
653 static StringRef name() { return "RustPrintModulePass"; }
659 void initializeRustPrintModulePassPass(PassRegistry&);
662 char RustPrintModulePass::ID = 0;
663 INITIALIZE_PASS(RustPrintModulePass, "print-rust-module",
664 "Print rust module to stderr", false, false)
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;
672 raw_fd_ostream OS(Path, EC, sys::fs::F_None);
674 ErrorInfo = EC.message();
676 formatted_raw_ostream FOS(OS);
678 PM->add(new RustPrintModulePass(FOS, Demangle));
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());
698 if (Info->getPassArgument() && *Info->getPassArgument()) {
699 printf("%15s - %s\n", Info->getPassArgument(), Info->getPassName());
705 PassRegistry *PR = PassRegistry::getPassRegistry();
706 PR->enumerateWith(&Listener);
709 extern "C" void LLVMRustAddAlwaysInlinePass(LLVMPassManagerBuilderRef PMBR,
711 #if LLVM_VERSION_GE(4, 0)
712 unwrap(PMBR)->Inliner = llvm::createAlwaysInlinerLegacyPass(AddLifetimes);
714 unwrap(PMBR)->Inliner = createAlwaysInlinerPass(AddLifetimes);
718 extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
720 llvm::legacy::PassManager passes;
722 #if LLVM_VERSION_LE(3, 8)
723 ArrayRef<const char *> Ref(Symbols, Len);
724 passes.add(llvm::createInternalizePass(Ref));
726 auto PreserveFunctions = [=](const GlobalValue &GV) {
727 for (size_t I = 0; I < Len; I++) {
728 if (GV.getName() == Symbols[I]) {
735 passes.add(llvm::createInternalizePass(PreserveFunctions));
738 passes.run(*unwrap(M));
741 extern "C" void LLVMRustMarkAllFunctionsNounwind(LLVMModuleRef M) {
742 for (Module::iterator GV = unwrap(M)->begin(), E = unwrap(M)->end(); GV != E;
744 GV->setDoesNotThrow();
745 Function *F = dyn_cast<Function>(GV);
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();
761 LLVMRustSetDataLayoutFromTargetMachine(LLVMModuleRef Module,
762 LLVMTargetMachineRef TMR) {
763 TargetMachine *Target = unwrap(TMR);
764 unwrap(Module)->setDataLayout(Target->createDataLayout());
767 extern "C" LLVMTargetDataRef LLVMRustGetModuleDataLayout(LLVMModuleRef M) {
768 return wrap(&unwrap(M)->getDataLayout());
771 extern "C" void LLVMRustSetModulePIELevel(LLVMModuleRef M) {
772 #if LLVM_VERSION_GE(3, 9)
773 unwrap(M)->setPIELevel(PIELevel::Level::Large);
778 LLVMRustThinLTOAvailable() {
779 #if LLVM_VERSION_GE(4, 0)
786 #if LLVM_VERSION_GE(4, 0)
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.
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!
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.
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.
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
817 LLVMRustWriteThinBitcodeToFile(LLVMPassManagerRef PMR,
819 const char *BcFile) {
820 llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
822 llvm::raw_fd_ostream bc(BcFile, EC, llvm::sys::fs::F_None);
824 LLVMRustSetLastError(EC.message().c_str());
827 PM->add(createWriteThinLTOBitcodePass(bc));
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;
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
844 StringMap<MemoryBufferRef> ModuleMap;
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
849 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
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;
858 // Just an argument to the `LLVMRustCreateThinLTOData` function below.
859 struct LLVMRustThinLTOModule {
860 const char *identifier;
865 // This is copied from `lib/LTO/ThinLTOCodeGenerator.cpp`, not sure what it
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);
875 if (StrongDefForLinker != GVSummaryList.end())
876 return StrongDefForLinker->get();
878 auto FirstDefForLinker = llvm::find_if(
879 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
880 auto Linkage = Summary->linkage();
881 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
883 if (FirstDefForLinker == GVSummaryList.end())
885 return FirstDefForLinker->get();
888 // This is a helper function we added that isn't present in LLVM's source.
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.
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.
900 addPreservedGUID(const ModuleSummaryIndex &Index,
901 DenseSet<GlobalValue::GUID> &Preserved,
902 GlobalValue::GUID GUID) {
903 if (Preserved.count(GUID))
905 Preserved.insert(GUID);
907 #if LLVM_VERSION_GE(5, 0)
908 auto Info = Index.getValueInfo(GUID);
912 for (auto &Summary : Info.getSummaryList()) {
913 for (auto &Ref : Summary->refs()) {
914 addPreservedGUID(Index, Preserved, Ref.getGUID());
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());
923 for (auto &GUID: FS->type_tests()) {
924 addPreservedGUID(Index, Preserved, GUID);
927 if (isa<AliasSummary>(GVSummary)) {
928 auto *AS = cast<AliasSummary>(GVSummary);
929 auto GUID = AS->getAliasee().getOriginalName();
930 addPreservedGUID(Index, Preserved, GUID);
934 auto SummaryList = Index.findGlobalValueSummaryList(GUID);
935 if (SummaryList == Index.end())
937 for (auto &Summary : SummaryList->second) {
938 for (auto &Ref : Summary->refs()) {
940 addPreservedGUID(Index, Preserved, Ref.getGUID());
942 auto Value = Ref.getValue();
943 addPreservedGUID(Index, Preserved, Value->getGUID());
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());
952 auto Value = Call.first.getValue();
953 addPreservedGUID(Index, Preserved, Value->getGUID());
956 for (auto &GUID: FS->type_tests()) {
957 addPreservedGUID(Index, Preserved, GUID);
960 if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
961 auto GUID = AS->getAliasee().getOriginalName();
962 addPreservedGUID(Index, Preserved, GUID);
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,
974 const char **preserved_symbols,
976 auto Ret = llvm::make_unique<LLVMRustThinLTOData>();
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);
984 Ret->ModuleMap[module->identifier] = mem_buffer;
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());
992 Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
993 object::ModuleSummaryIndexObjectFile::create(mem_buffer);
995 LLVMRustSetLastError(toString(ObjOrErr.takeError()).c_str());
998 auto Index = (*ObjOrErr)->takeIndex();
999 Ret->Index.mergeFrom(std::move(Index), i);
1003 // Collect for each module the list of function it defines (GUID -> Summary)
1004 Ret->Index.collectDefinedGVSummariesPerModule(Ret->ModuleToDefinedGVSummaries);
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]));
1015 // Collect the import/export lists for all modules from the call-graph in the
1018 // This is copied from `lib/LTO/ThinLTOCodeGenerator.cpp`
1019 #if LLVM_VERSION_GE(5, 0)
1020 computeDeadSymbols(Ret->Index, Ret->GUIDPreservedSymbols);
1021 ComputeCrossModuleImport(
1023 Ret->ModuleToDefinedGVSummaries,
1028 auto DeadSymbols = computeDeadSymbols(Ret->Index, Ret->GUIDPreservedSymbols);
1029 ComputeCrossModuleImport(
1031 Ret->ModuleToDefinedGVSummaries,
1038 // Resolve LinkOnce/Weak symbols, this has to be computed early be cause it
1039 // impacts the caching.
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);
1049 if (I.second.size() > 1)
1050 PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
1053 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
1054 const auto &Prevailing = PrevailingCopy.find(GUID);
1055 if (Prevailing == PrevailingCopy.end())
1057 return Prevailing->second == S;
1059 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
1060 GlobalValue::GUID GUID,
1061 GlobalValue::LinkageTypes NewLinkage) {
1062 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
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);
1071 thinLTOInternalizeAndPromoteInIndex(Ret->Index, isExported);
1073 return Ret.release();
1077 LLVMRustFreeThinLTOData(LLVMRustThinLTOData *Data) {
1081 // Below are the various passes that happen *per module* when doing ThinLTO.
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.
1090 LLVMRustPrepareThinLTORename(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1091 Module &Mod = *unwrap(M);
1092 if (renameModuleForThinLTO(Mod, Data->Index)) {
1093 LLVMRustSetLastError("renameModuleForThinLTO failed");
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);
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);
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);
1124 FunctionImporter Importer(Data->Index, Loader);
1125 Expected<bool> Result = Importer.importFunctions(Mod, ImportList);
1127 LLVMRustSetLastError(toString(Result.takeError()).c_str());
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.
1139 // This structure is basically an owned version of a serialize module, with
1140 // a ThinLTO summary attached.
1141 struct LLVMRustThinLTOBuffer {
1145 extern "C" LLVMRustThinLTOBuffer*
1146 LLVMRustThinLTOBufferCreate(LLVMModuleRef M) {
1147 auto Ret = llvm::make_unique<LLVMRustThinLTOBuffer>();
1149 raw_string_ostream OS(Ret->data);
1151 legacy::PassManager PM;
1152 PM.add(createWriteThinLTOBitcodePass(OS));
1156 return Ret.release();
1160 LLVMRustThinLTOBufferFree(LLVMRustThinLTOBuffer *Buffer) {
1164 extern "C" const void*
1165 LLVMRustThinLTOBufferPtr(const LLVMRustThinLTOBuffer *Buffer) {
1166 return Buffer->data.data();
1170 LLVMRustThinLTOBufferLen(const LLVMRustThinLTOBuffer *Buffer) {
1171 return Buffer->data.length();
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,
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));
1188 LLVMRustSetLastError(toString(SrcOrError.takeError()).c_str());
1191 return wrap(std::move(*SrcOrError).release());
1197 LLVMRustWriteThinBitcodeToFile(LLVMPassManagerRef PMR,
1199 const char *BcFile) {
1200 report_fatal_error("ThinLTO not available");
1203 struct LLVMRustThinLTOData {
1206 struct LLVMRustThinLTOModule {
1209 extern "C" LLVMRustThinLTOData*
1210 LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
1212 const char **preserved_symbols,
1214 report_fatal_error("ThinLTO not available");
1218 LLVMRustPrepareThinLTORename(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1219 report_fatal_error("ThinLTO not available");
1223 LLVMRustPrepareThinLTOResolveWeak(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1224 report_fatal_error("ThinLTO not available");
1228 LLVMRustPrepareThinLTOInternalize(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1229 report_fatal_error("ThinLTO not available");
1233 LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1234 report_fatal_error("ThinLTO not available");
1238 LLVMRustFreeThinLTOData(LLVMRustThinLTOData *Data) {
1239 report_fatal_error("ThinLTO not available");
1242 struct LLVMRustThinLTOBuffer {
1245 extern "C" LLVMRustThinLTOBuffer*
1246 LLVMRustThinLTOBufferCreate(LLVMModuleRef M) {
1247 report_fatal_error("ThinLTO not available");
1251 LLVMRustThinLTOBufferFree(LLVMRustThinLTOBuffer *Buffer) {
1252 report_fatal_error("ThinLTO not available");
1255 extern "C" const void*
1256 LLVMRustThinLTOBufferPtr(const LLVMRustThinLTOBuffer *Buffer) {
1257 report_fatal_error("ThinLTO not available");
1261 LLVMRustThinLTOBufferLen(const LLVMRustThinLTOBuffer *Buffer) {
1262 report_fatal_error("ThinLTO not available");
1265 extern "C" LLVMModuleRef
1266 LLVMRustParseBitcodeForThinLTO(LLVMContextRef Context,
1269 const char *identifier) {
1270 report_fatal_error("ThinLTO not available");
1272 #endif // LLVM_VERSION_GE(4, 0)