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,63 @@ 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+ : SSP(),
107+ ES(SSP),
108+ Resolver(llvm::orc::createLegacyLookupResolver(
109+ [this ](const std::string& Name) -> JITSymbol {
110+ if (auto Sym = compileLayer_.findSymbol (Name, false ))
111+ return Sym;
112+ else if (auto Err = Sym.takeError ())
113+ return std::move (Err);
114+ if (auto SymAddr =
115+ RTDyldMemoryManager::getSymbolAddressInProcess (Name))
116+ return JITSymbol (SymAddr, JITSymbolFlags::Exported);
117+ return nullptr ;
118+ },
119+ [](Error err) { throw std::runtime_error (" Lookup failed!" ); })),
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+ sys::DynamicLibrary::LoadLibraryPermanently (nullptr , &err);
132+ if (err != " " ) {
133+ throw std::runtime_error (" Failed to find cilkrts: " + err);
134+ }
135+ }
136+
137+ // Note that this copy may cause tapir tests to fail
138+ // However, this code will never use tapir code
139+ // and once the LLVM API churn stops, will be modified
140+ // to be properly compatable.
141+ void Jit::addModule (std::shared_ptr<Module> M) {
142+ M->setTargetTriple (TM_->getTargetTriple ().str ());
143+ auto K = ES.allocateVModule ();
144+ llvm::Error res = compileLayer_.addModule (K, CloneModule (*M));
145+ CHECK (!res) << " Failed to jit compile." ;
146+ }
147+ #endif
148+
149+ std::shared_ptr<Module> Jit::codegenScop (
150+ const std::string& specializedName,
151+ const polyhedral::Scop& scop) {
152+ std::shared_ptr<Module> mod = emitLLVMKernel (
153+ specializedName, scop, getTargetMachine ().createDataLayout ());
154+ addModule (mod);
155+ return mod;
156+ }
157+
158+ TargetMachine& Jit::getTargetMachine () {
159+ return *TM_;
115160}
116161
117162JITSymbol Jit::findSymbol (const std::string Name) {
0 commit comments