Skip to content

Commit aceb408

Browse files
committed
Support LLVM 17
This is almost completely stolen from google/swiftshader@9fbca2d and google/swiftshader@ea5f37f I have no idea how to do a function inlining pass with the new pass manager and I don't really want to spend more time on this so I just left it as a no-op. Surely it won't break anything!
1 parent 689cba7 commit aceb408

File tree

2 files changed

+73
-25
lines changed

2 files changed

+73
-25
lines changed

expr2/reactor/LLVMJIT.cpp

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

902950
std::shared_ptr<rr::Routine> JITBuilder::acquireRoutine(const char *name, llvm::Function **funcs, size_t count, const rr::Config &cfg)

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ if use_asmjit
8787
else
8888
sources = sources_common + sources_expr2
8989
incdir += include_directories('expr2/reactor')
90-
deps += dependency('llvm', version: ['>= 10.0', '< 17'], method: 'config-tool', static: get_option('static-llvm'),
90+
deps += dependency('llvm', version: ['>= 10.0', '< 18'], method: 'config-tool', static: get_option('static-llvm'),
9191
modules: [
9292
'asmprinter', 'executionengine', 'target', 'orcjit', 'native',
9393
])

0 commit comments

Comments
 (0)