@@ -640,6 +640,62 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMPassManagerRef PMR,
640
640
return LLVMRustResult::Success;
641
641
}
642
642
643
+ extern " C" typedef void (*LLVMRustSelfProfileBeforePassCallback)(void *, // LlvmSelfProfiler
644
+ const char *, // pass name
645
+ const char *); // IR name
646
+ extern " C" typedef void (*LLVMRustSelfProfileAfterPassCallback)(void *); // LlvmSelfProfiler
647
+
648
+ #if LLVM_VERSION_GE(9, 0)
649
+
650
+ std::string LLVMRustwrappedIrGetName (const llvm::Any &WrappedIr) {
651
+ if (any_isa<const Module *>(WrappedIr))
652
+ return any_cast<const Module *>(WrappedIr)->getName ().str ();
653
+ if (any_isa<const Function *>(WrappedIr))
654
+ return any_cast<const Function *>(WrappedIr)->getName ().str ();
655
+ if (any_isa<const Loop *>(WrappedIr))
656
+ return any_cast<const Loop *>(WrappedIr)->getName ().str ();
657
+ if (any_isa<const LazyCallGraph::SCC *>(WrappedIr))
658
+ return any_cast<const LazyCallGraph::SCC *>(WrappedIr)->getName ();
659
+ return " <UNKNOWN>" ;
660
+ }
661
+
662
+
663
+ void LLVMSelfProfileInitializeCallbacks (
664
+ PassInstrumentationCallbacks& PIC, void * LlvmSelfProfiler,
665
+ LLVMRustSelfProfileBeforePassCallback BeforePassCallback,
666
+ LLVMRustSelfProfileAfterPassCallback AfterPassCallback) {
667
+ PIC.registerBeforePassCallback ([LlvmSelfProfiler, BeforePassCallback](
668
+ StringRef Pass, llvm::Any Ir) {
669
+ std::string PassName = Pass.str ();
670
+ std::string IrName = LLVMRustwrappedIrGetName (Ir);
671
+ BeforePassCallback (LlvmSelfProfiler, PassName.c_str (), IrName.c_str ());
672
+ return true ;
673
+ });
674
+
675
+ PIC.registerAfterPassCallback (
676
+ [LlvmSelfProfiler, AfterPassCallback](StringRef Pass, llvm::Any Ir) {
677
+ AfterPassCallback (LlvmSelfProfiler);
678
+ });
679
+
680
+ PIC.registerAfterPassInvalidatedCallback (
681
+ [LlvmSelfProfiler, AfterPassCallback](StringRef Pass) {
682
+ AfterPassCallback (LlvmSelfProfiler);
683
+ });
684
+
685
+ PIC.registerBeforeAnalysisCallback ([LlvmSelfProfiler, BeforePassCallback](
686
+ StringRef Pass, llvm::Any Ir) {
687
+ std::string PassName = Pass.str ();
688
+ std::string IrName = LLVMRustwrappedIrGetName (Ir);
689
+ BeforePassCallback (LlvmSelfProfiler, PassName.c_str (), IrName.c_str ());
690
+ });
691
+
692
+ PIC.registerAfterAnalysisCallback (
693
+ [LlvmSelfProfiler, AfterPassCallback](StringRef Pass, llvm::Any Ir) {
694
+ AfterPassCallback (LlvmSelfProfiler);
695
+ });
696
+ }
697
+ #endif
698
+
643
699
enum class LLVMRustOptStage {
644
700
PreLinkNoLTO,
645
701
PreLinkThinLTO,
@@ -666,7 +722,10 @@ LLVMRustOptimizeWithNewPassManager(
666
722
bool MergeFunctions, bool UnrollLoops, bool SLPVectorize, bool LoopVectorize,
667
723
bool DisableSimplifyLibCalls,
668
724
LLVMRustSanitizerOptions *SanitizerOptions,
669
- const char *PGOGenPath, const char *PGOUsePath) {
725
+ const char *PGOGenPath, const char *PGOUsePath,
726
+ void * LlvmSelfProfiler,
727
+ LLVMRustSelfProfileBeforePassCallback BeforePassCallback,
728
+ LLVMRustSelfProfileAfterPassCallback AfterPassCallback) {
670
729
#if LLVM_VERSION_GE(9, 0)
671
730
Module *TheModule = unwrap (ModuleRef);
672
731
TargetMachine *TM = unwrap (TMRef);
@@ -685,6 +744,10 @@ LLVMRustOptimizeWithNewPassManager(
685
744
StandardInstrumentations SI;
686
745
SI.registerCallbacks (PIC);
687
746
747
+ if (LlvmSelfProfiler){
748
+ LLVMSelfProfileInitializeCallbacks (PIC,LlvmSelfProfiler,BeforePassCallback,AfterPassCallback);
749
+ }
750
+
688
751
Optional<PGOOptions> PGOOpt;
689
752
if (PGOGenPath) {
690
753
assert (!PGOUsePath);
0 commit comments