Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit 9d19900

Browse files
committed
Add initial LLVM compatability
1 parent 8ffd5cd commit 9d19900

File tree

2 files changed

+70
-18
lines changed

2 files changed

+70
-18
lines changed

tc/core/polyhedral/llvm_jit.cc

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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

6970
namespace tc {
7071

72+
#if LLVM_VERSION_MAJOR <= 6
7173
Jit::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

117163
JITSymbol Jit::findSymbol(const std::string Name) {

tc/core/polyhedral/llvm_jit.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
2222
#include "llvm/Target/TargetMachine.h"
2323

24+
#if LLVM_VERSION_MAJOR > 6
25+
#include "llvm/ExecutionEngine/Orc/Core.h"
26+
#endif
27+
2428
namespace tc {
2529

2630
namespace polyhedral {
@@ -29,6 +33,10 @@ class Scop;
2933

3034
class Jit {
3135
private:
36+
#if LLVM_VERSION_MAJOR > 6
37+
llvm::orc::ExecutionSession ES;
38+
std::shared_ptr<llvm::orc::SymbolResolver> Resolver;
39+
#endif
3240
std::unique_ptr<llvm::TargetMachine> TM_;
3341
const llvm::DataLayout DL_;
3442
llvm::orc::RTDyldObjectLinkingLayer objectLayer_;
@@ -38,12 +46,10 @@ class Jit {
3846
public:
3947
Jit();
4048

41-
using ModuleHandle = decltype(compileLayer_)::ModuleHandleT;
4249
std::shared_ptr<llvm::Module> codegenScop(
4350
const std::string& specializedName,
4451
const polyhedral::Scop& scop);
45-
ModuleHandle addModule(std::shared_ptr<llvm::Module> M);
46-
void removeModule(ModuleHandle H);
52+
void addModule(std::shared_ptr<llvm::Module> M);
4753

4854
llvm::JITSymbol findSymbol(const std::string name);
4955
llvm::JITTargetAddress getSymbolAddress(const std::string name);

0 commit comments

Comments
 (0)