Skip to content
This repository was archived by the owner on Jul 3, 2021. It is now read-only.

Commit 402e9d1

Browse files
Request and run trivial function
1 parent 8a4bba8 commit 402e9d1

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

JitFromScratch.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ Error JitFromScratch::submitModule(std::unique_ptr<Module> M,
2020
LLVM_DEBUG(dbgs() << "Submit IR module:\n\n" << *M << "\n\n");
2121
return LLJIT->addIRModule(ThreadSafeModule(std::move(M), std::move(C)));
2222
}
23+
24+
Expected<JITTargetAddress> JitFromScratch::getFunctionAddr(StringRef Name) {
25+
Expected<JITEvaluatedSymbol> S = LLJIT->lookup(Name);
26+
if (!S)
27+
return S.takeError();
28+
29+
JITTargetAddress A = S->getAddress();
30+
if (!A)
31+
return createStringError(inconvertibleErrorCode(),
32+
"'%s' evaluated to nullptr", Name.data());
33+
34+
return A;
35+
}

JitFromScratch.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#pragma once
22

3+
#include <llvm/ADT/StringRef.h>
34
#include <llvm/ADT/Triple.h>
5+
#include <llvm/ExecutionEngine/JITSymbol.h>
46
#include <llvm/ExecutionEngine/Orc/LLJIT.h>
57
#include <llvm/IR/DataLayout.h>
68
#include <llvm/IR/LLVMContext.h>
79
#include <llvm/IR/Module.h>
810
#include <llvm/Support/Error.h>
911

12+
#include <functional>
1013
#include <memory>
1114

1215
class JitFromScratch {
@@ -29,7 +32,19 @@ class JitFromScratch {
2932

3033
llvm::Error submitModule(std::unique_ptr<llvm::Module> M,
3134
std::unique_ptr<llvm::LLVMContext> C);
35+
36+
template <class Signature_t>
37+
llvm::Expected<std::function<Signature_t>> getFunction(llvm::StringRef Name) {
38+
if (auto A = getFunctionAddr(Name))
39+
return std::function<Signature_t>(
40+
llvm::jitTargetAddressToPointer<Signature_t *>(*A));
41+
else
42+
return A.takeError();
43+
}
44+
3245
private:
3346
std::unique_ptr<llvm::orc::LLJIT> LLJIT;
3447
llvm::Triple TT;
48+
49+
llvm::Expected<llvm::JITTargetAddress> getFunctionAddr(llvm::StringRef Name);
3550
};

main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,14 @@ int main(int argc, char **argv) {
109109
auto M = std::make_unique<Module>("JitFromScratch", *C);
110110
M->setDataLayout(Jit.getDataLayout());
111111

112-
ExitOnErr(codegenIR(*M, arrayElements(x)));
112+
std::string JitedFnName = ExitOnErr(codegenIR(*M, arrayElements(x)));
113113
ExitOnErr(Jit.submitModule(std::move(M), std::move(C)));
114114

115+
// Request function; this compiles to machine code and links.
116+
auto getZero = ExitOnErr(Jit.getFunction<int(int, int)>(JitedFnName));
117+
115118
int *z = integerDistances(x, y);
119+
z[1] = getZero(0, 0);
116120

117121
outs() << format("Integer Distances: %d, %d, %d\n\n", z[0], z[1], z[2]);
118122
outs().flush();

0 commit comments

Comments
 (0)