llvm.org GIT mirror llvm / 21dbb43
Add a HowToUseLLJIT example project. A very minimal demo of how to use the LLJIT class, along the lines of the old HowToUseJIT example. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361435 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames a month ago
3 changed file(s) with 85 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 add_subdirectory(BrainF)
11 add_subdirectory(Fibonacci)
22 add_subdirectory(HowToUseJIT)
3 add_subdirectory(HowToUseLLJIT)
34 add_subdirectory(Kaleidoscope)
45 add_subdirectory(ModuleMaker)
56
0 set(LLVM_LINK_COMPONENTS
1 Core
2 OrcJIT
3 Support
4 nativecodegen
5 )
6
7 add_llvm_example(HowToUseLLJIT
8 HowToUseLLJIT.cpp
9 )
0 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
1 #include "llvm/IR/Function.h"
2 #include "llvm/IR/IRBuilder.h"
3 #include "llvm/IR/Module.h"
4 #include "llvm/Support/InitLLVM.h"
5 #include "llvm/Support/TargetSelect.h"
6 #include "llvm/Support/raw_ostream.h"
7
8 using namespace llvm;
9 using namespace llvm::orc;
10
11 ExitOnError ExitOnErr;
12
13 ThreadSafeModule createDemoModule() {
14 auto Context = llvm::make_unique();
15 auto M = make_unique("test", *Context);
16
17 // Create the add1 function entry and insert this entry into module M. The
18 // function will have a return type of "int" and take an argument of "int".
19 Function *Add1F =
20 Function::Create(FunctionType::get(Type::getInt32Ty(*Context),
21 {Type::getInt32Ty(*Context)}, false),
22 Function::ExternalLinkage, "add1", M.get());
23
24 // Add a basic block to the function. As before, it automatically inserts
25 // because of the last argument.
26 BasicBlock *BB = BasicBlock::Create(*Context, "EntryBlock", Add1F);
27
28 // Create a basic block builder with default parameters. The builder will
29 // automatically append instructions to the basic block `BB'.
30 IRBuilder<> builder(BB);
31
32 // Get pointers to the constant `1'.
33 Value *One = builder.getInt32(1);
34
35 // Get pointers to the integer argument of the add1 function...
36 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
37 Argument *ArgX = &*Add1F->arg_begin(); // Get the arg
38 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
39
40 // Create the add instruction, inserting it into the end of BB.
41 Value *Add = builder.CreateAdd(One, ArgX);
42
43 // Create the return instruction and add it to the basic block
44 builder.CreateRet(Add);
45
46 return ThreadSafeModule(std::move(M), std::move(Context));
47 }
48
49 int main(int argc, char *argv[]) {
50 // Initialize LLVM.
51 InitLLVM X(argc, argv);
52
53 InitializeNativeTarget();
54 InitializeNativeTargetAsmPrinter();
55
56 cl::ParseCommandLineOptions(argc, argv, "HowToUseLLJIT");
57 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
58
59 // Create an LLJIT instance.
60 auto J = ExitOnErr(LLJITBuilder().create());
61 auto M = createDemoModule();
62
63 ExitOnErr(J->addIRModule(std::move(M)));
64
65 // Look up the JIT'd function, cast it to a function pointer, then call it.
66 auto Add1Sym = ExitOnErr(J->lookup("add1"));
67 int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress();
68
69 int Result = Add1(42);
70 outs() << "add1(42) = " << Result << "\n";
71
72 return 0;
73 }