2525#include " llvm/IR/Verifier.h"
2626#include " llvm/Passes/PassBuilder.h"
2727#include " llvm/Support/DynamicLibrary.h"
28+ #include " llvm/Transforms/Utils/Cloning.h"
2829
2930#include " tc/core/flags.h"
3031#include " tc/core/polyhedral/codegen_llvm.h"
@@ -68,6 +69,7 @@ std::string find_library_path(std::string library) {
6869
6970namespace tc {
7071
72+ #if LLVM_VERSION_MAJOR <= 6
7173Jit::Jit ()
7274 : TM_(EngineBuilder().selectTarget()),
7375 DL_(TM_->createDataLayout ()),
@@ -82,20 +84,7 @@ Jit::Jit()
8284 }
8385}
8486
85- std::shared_ptr<Module> Jit::codegenScop (
86- const std::string& specializedName,
87- const polyhedral::Scop& scop) {
88- std::shared_ptr<Module> mod = emitLLVMKernel (
89- specializedName, scop, getTargetMachine ().createDataLayout ());
90- addModule (mod);
91- return mod;
92- }
93-
94- TargetMachine& Jit::getTargetMachine () {
95- return *TM_;
96- }
97-
98- Jit::ModuleHandle Jit::addModule (std::shared_ptr<Module> M) {
87+ void Jit::addModule (std::shared_ptr<Module> M) {
9988 M->setTargetTriple (TM_->getTargetTriple ().str ());
10089 auto Resolver = orc::createLambdaResolver (
10190 [&](const std::string& Name) {
@@ -111,7 +100,64 @@ Jit::ModuleHandle Jit::addModule(std::shared_ptr<Module> M) {
111100
112101 auto res = compileLayer_.addModule (M, std::move (Resolver));
113102 CHECK (res) << " Failed to jit compile." ;
114- return *res;
103+ }
104+ #else
105+ Jit::Jit ()
106+ : Resolver(createLegacyLookupResolver(
107+ [this ](const std::string& Name) -> JITSymbol {
108+ if (auto Sym = compileLayer_.findSymbol (Name, false ))
109+ return Sym;
110+ else if (auto Err = Sym.takeError ())
111+ return std::move (Err);
112+ if (auto SymAddr =
113+ RTDyldMemoryManager::getSymbolAddressInProcess (Name))
114+ return JITSymbol (SymAddr, JITSymbolFlags::Exported);
115+ return nullptr ;
116+ },
117+ [](Error err) {
118+ throw std::runtime_error (" Lookup failed: " + err);
119+ })),
120+ TM_ (EngineBuilder().selectTarget()),
121+ DL_(TM_->createDataLayout ()),
122+ objectLayer_(
123+ ES,
124+ [this ](llvm::orc::VModuleKey) {
125+ return llvm::orc::RTDyldObjectLinkingLayer::Resources{
126+ std::make_shared<SectionMemoryManager>(), Resolver};
127+ }),
128+ compileLayer_(objectLayer_, orc::SimpleCompiler(*TM_)) {
129+ std::string err;
130+
131+ auto path = find_library_path (" libcilkrts.so" );
132+ sys::DynamicLibrary::LoadLibraryPermanently (path.c_str (), &err);
133+ if (err != " " ) {
134+ throw std::runtime_error (" Failed to find cilkrts: " + err);
135+ }
136+ }
137+
138+ // Note that this copy may cause tapir tests to fail
139+ // However, this code will never use tapir code
140+ // and once the LLVM API churn stops, will be modified
141+ // to be properly compatable.
142+ void Jit::addModule (std::shared_ptr<Module> M) {
143+ M->setTargetTriple (TM_->getTargetTriple ().str ());
144+ auto K = ES.allocateVModule ();
145+ llvm::Error res = compileLayer_.addModule (K, CloneModule (*M));
146+ CHECK (!res) << " Failed to jit compile." ;
147+ }
148+ #endif
149+
150+ std::shared_ptr<Module> Jit::codegenScop (
151+ const std::string& specializedName,
152+ const polyhedral::Scop& scop) {
153+ std::shared_ptr<Module> mod = emitLLVMKernel (
154+ specializedName, scop, getTargetMachine ().createDataLayout ());
155+ addModule (mod);
156+ return mod;
157+ }
158+
159+ TargetMachine& Jit::getTargetMachine () {
160+ return *TM_;
115161}
116162
117163JITSymbol Jit::findSymbol (const std::string Name) {
0 commit comments