@@ -31,14 +31,23 @@ __pragma(warning(push))
3131#include " llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
3232#include " llvm/ExecutionEngine/SectionMemoryManager.h"
3333#include " llvm/IR/DiagnosticInfo.h"
34- #include " llvm/IR/LegacyPassManager.h"
34+ #include " llvm/IR/PassManager.h"
35+ #include " llvm/Passes/PassBuilder.h"
3536#include " llvm/Support/CommandLine.h"
3637#include " llvm/Support/Host.h"
3738#include " llvm/Support/TargetSelect.h"
3839#include " llvm/Transforms/InstCombine/InstCombine.h"
3940#include " llvm/Transforms/Instrumentation/MemorySanitizer.h"
40- #include " llvm/Transforms/Scalar.h"
41+ #include " llvm/Transforms/Scalar/ADCE.h"
42+ #include " llvm/Transforms/Scalar/DeadStoreElimination.h"
43+ #include " llvm/Transforms/Scalar/EarlyCSE.h"
4144#include " llvm/Transforms/Scalar/GVN.h"
45+ #include " llvm/Transforms/Scalar/LICM.h"
46+ #include " llvm/Transforms/Scalar/Reassociate.h"
47+ #include " llvm/Transforms/Scalar/SCCP.h"
48+ #include " llvm/Transforms/Scalar/SROA.h"
49+ #include " llvm/Transforms/Scalar/SimplifyCFG.h"
50+
4251#include " llvm/Transforms/IPO.h"
4352
4453#ifdef _MSC_VER
@@ -204,8 +213,12 @@ JITGlobals *JITGlobals::get()
204213 // jitTargetMachineBuilder.setCodeModel(llvm::CodeModel::Small);
205214
206215 // Reactor's MemorySanitizer support depends on intercepting __emutls_get_address calls.
216+ #if LLVM_VERSION_MAJOR < 17
207217 ASSERT (!__has_feature (memory_sanitizer) || (jitTargetMachineBuilder.getOptions ().ExplicitEmulatedTLS &&
208218 jitTargetMachineBuilder.getOptions ().EmulatedTLS ));
219+ #else
220+ ASSERT (!__has_feature (memory_sanitizer) || jitTargetMachineBuilder.getOptions ().EmulatedTLS );
221+ #endif
209222
210223 auto dataLayout = jitTargetMachineBuilder.getDefaultDataLayoutForTarget ();
211224 if (!dataLayout) {
@@ -606,13 +619,25 @@ class ExternalSymbolGenerator : public llvm::orc::JITDylib::DefinitionGenerator
606619 // to append these on macOS.
607620 auto trimmed = (*name).drop_while ([](char c) { return c == ' _' ; });
608621
622+ #if LLVM_VERSION_MAJOR < 17
623+ auto toSymbol = [](void *ptr) {
624+ return llvm::JITEvaluatedSymbol (
625+ static_cast <llvm::JITTargetAddress>(reinterpret_cast <uintptr_t >(ptr)),
626+ llvm::JITSymbolFlags::Exported);
627+ };
628+ #else
629+ auto toSymbol = [](void *ptr) {
630+ return llvm::orc::ExecutorSymbolDef{
631+ llvm::orc::ExecutorAddr (reinterpret_cast <uintptr_t >(ptr)),
632+ llvm::JITSymbolFlags::Exported,
633+ };
634+ };
635+ #endif
636+
609637 auto it = resolver.functions .find (trimmed.str ());
610638 if (it != resolver.functions .end ())
611639 {
612- symbols[name] = llvm::JITEvaluatedSymbol (
613- static_cast <llvm::JITTargetAddress>(reinterpret_cast <uintptr_t >(it->second )),
614- llvm::JITSymbolFlags::Exported);
615-
640+ symbols[name] = toSymbol (it->second );
616641 continue ;
617642 }
618643
@@ -627,10 +652,7 @@ class ExternalSymbolGenerator : public llvm::orc::JITDylib::DefinitionGenerator
627652
628653 if (address)
629654 {
630- symbols[name] = llvm::JITEvaluatedSymbol (
631- static_cast <llvm::JITTargetAddress>(reinterpret_cast <uintptr_t >(address)),
632- llvm::JITSymbolFlags::Exported);
633-
655+ symbols[name] = toSymbol (it->second );
634656 continue ;
635657 }
636658#endif
@@ -809,7 +831,11 @@ class JITRoutine : public rr::Routine
809831 }
810832 else // Successful compilation
811833 {
834+ #if LLVM_VERSION_MAJOR < 17
812835 addresses[i] = reinterpret_cast <void *>(static_cast <intptr_t >(symbol->getAddress ()));
836+ #else
837+ addresses[i] = reinterpret_cast <void *>(static_cast <intptr_t >(symbol->getAddress ().getValue ()));
838+ #endif
813839 }
814840 }
815841
@@ -866,12 +892,27 @@ void JITBuilder::optimize(const rr::Config &cfg)
866892 }
867893#endif // ENABLE_RR_DEBUG_INFO
868894
869- llvm::legacy::PassManager passManager;
895+ llvm::LoopAnalysisManager lam;
896+ llvm::FunctionAnalysisManager fam;
897+ llvm::CGSCCAnalysisManager cgam;
898+ llvm::ModuleAnalysisManager mam;
899+ llvm::PassBuilder pb;
900+
901+ pb.registerModuleAnalyses (mam);
902+ pb.registerCGSCCAnalyses (cgam);
903+ pb.registerFunctionAnalyses (fam);
904+ pb.registerLoopAnalyses (lam);
905+ pb.crossRegisterProxies (lam, fam, cgam, mam);
906+
907+ llvm::ModulePassManager pm;
908+ llvm::FunctionPassManager fpm;
870909
871910#if REACTOR_ENABLE_MEMORY_SANITIZER_INSTRUMENTATION
872911 if (__has_feature (memory_sanitizer))
873912 {
874- passManager.add (llvm::createMemorySanitizerLegacyPassPass ());
913+ llvm::MemorySanitizerOptions msanOpts;
914+ pm.addPass (llvm::ModuleMemorySanitizerPass (msanOpts));
915+ pm.addPass (llvm::createModuleToFunctionPassAdaptor (llvm::MemorySanitizerPass (msanOpts)));
875916 }
876917#endif
877918
@@ -880,23 +921,30 @@ void JITBuilder::optimize(const rr::Config &cfg)
880921 switch (pass)
881922 {
882923 case rr::Optimization::Pass::Disabled: break ;
883- case rr::Optimization::Pass::CFGSimplification: passManager.add (llvm::createCFGSimplificationPass ()); break ;
884- case rr::Optimization::Pass::LICM: passManager.add (llvm::createLICMPass ()); break ;
885- case rr::Optimization::Pass::AggressiveDCE: passManager.add (llvm::createAggressiveDCEPass ()); break ;
886- case rr::Optimization::Pass::GVN: passManager.add (llvm::createGVNPass ()); break ;
887- case rr::Optimization::Pass::InstructionCombining: passManager.add (llvm::createInstructionCombiningPass ()); break ;
888- case rr::Optimization::Pass::Reassociate: passManager.add (llvm::createReassociatePass ()); break ;
889- case rr::Optimization::Pass::DeadStoreElimination: passManager.add (llvm::createDeadStoreEliminationPass ()); break ;
890- case rr::Optimization::Pass::SCCP: passManager.add (llvm::createSCCPPass ()); break ;
891- case rr::Optimization::Pass::ScalarReplAggregates: passManager.add (llvm::createSROAPass ()); break ;
892- case rr::Optimization::Pass::EarlyCSEPass: passManager.add (llvm::createEarlyCSEPass ()); break ;
893- case rr::Optimization::Pass::Inline: passManager.add (llvm::createFunctionInliningPass ()); break ;
924+ case rr::Optimization::Pass::CFGSimplification: fpm.addPass (llvm::SimplifyCFGPass ()); break ;
925+ case rr::Optimization::Pass::LICM: fpm.addPass (llvm::createFunctionToLoopPassAdaptor (
926+ llvm::LICMPass (llvm::SetLicmMssaOptCap, llvm::SetLicmMssaNoAccForPromotionCap, true )));
927+ break ;
928+ case rr::Optimization::Pass::AggressiveDCE: fpm.addPass (llvm::ADCEPass ()); break ;
929+ case rr::Optimization::Pass::GVN: fpm.addPass (llvm::GVNPass ()); break ;
930+ case rr::Optimization::Pass::InstructionCombining: fpm.addPass (llvm::InstCombinePass ()); break ;
931+ case rr::Optimization::Pass::Reassociate: fpm.addPass (llvm::ReassociatePass ()); break ;
932+ case rr::Optimization::Pass::DeadStoreElimination: fpm.addPass (llvm::DSEPass ()); break ;
933+ case rr::Optimization::Pass::SCCP: fpm.addPass (llvm::SCCPPass ()); break ;
934+ case rr::Optimization::Pass::ScalarReplAggregates: fpm.addPass (llvm::SROAPass (llvm::SROAOptions::PreserveCFG)); break ;
935+ case rr::Optimization::Pass::EarlyCSEPass: fpm.addPass (llvm::EarlyCSEPass ()); break ;
936+ case rr::Optimization::Pass::Inline: break ;
894937 default :
895938 UNREACHABLE (" pass: %d" , int (pass));
896939 }
897940 }
898941
899- passManager.run (*module );
942+ if (!fpm.isEmpty ())
943+ {
944+ pm.addPass (llvm::createModuleToFunctionPassAdaptor (std::move (fpm)));
945+ }
946+
947+ pm.run (*module , mam);
900948}
901949
902950std::shared_ptr<rr::Routine> JITBuilder::acquireRoutine (const char *name, llvm::Function **funcs, size_t count, const rr::Config &cfg)
0 commit comments