1 //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // WARNING: This example demonstrates how to use LLVM's older ExecutionEngine
10 // JIT APIs. The newer LLJIT APIs should be preferred for new
11 // projects. See llvm/examples/HowToUseLLJIT.
12 //
13 // This small program provides an example of how to quickly build a small
14 // module with two functions and execute it with the JIT.
15 //
16 // Goal:
17 // The goal of this snippet is to create in the memory
18 // the LLVM module consisting of two functions as follow:
19 //
20 // int add1(int x) {
21 // return x+1;
22 // }
23 //
24 // int foo() {
25 // return add1(10);
26 // }
27 //
28 // then compile the module via JIT, then execute the `foo'
29 // function and return result to a driver, i.e. to a "host program".
30 //
31 // Some remarks and questions:
32 //
33 // - could we invoke some code using noname functions too?
34 // e.g. evaluate "foo()+foo()" without fears to introduce
35 // conflict of temporary function name with some real
36 // existing function name?
37 //
38 //===----------------------------------------------------------------------===//
39
40 #include "llvm/ADT/STLExtras.h"
41 #include "llvm/ExecutionEngine/ExecutionEngine.h"
42 #include "llvm/ExecutionEngine/GenericValue.h"
43 #include "llvm/ExecutionEngine/MCJIT.h"
44 #include "llvm/IR/Argument.h"
45 #include "llvm/IR/BasicBlock.h"
46 #include "llvm/IR/Constants.h"
47 #include "llvm/IR/DerivedTypes.h"
48 #include "llvm/IR/Function.h"
49 #include "llvm/IR/IRBuilder.h"
50 #include "llvm/IR/Instructions.h"
51 #include "llvm/IR/LLVMContext.h"
52 #include "llvm/IR/Module.h"
53 #include "llvm/IR/Type.h"
54 #include "llvm/Support/Casting.h"
55 #include "llvm/Support/ManagedStatic.h"
56 #include "llvm/Support/TargetSelect.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include <algorithm>
59 #include <cassert>
60 #include <memory>
61 #include <vector>
62
63 using namespace llvm;
64
main()65 int main() {
66 InitializeNativeTarget();
67 LLVMInitializeNativeAsmPrinter();
68
69 LLVMContext Context;
70
71 // Create some module to put our function into it.
72 std::unique_ptr<Module> Owner = std::make_unique<Module>("test", Context);
73 Module *M = Owner.get();
74
75 // Create the add1 function entry and insert this entry into module M. The
76 // function will have a return type of "int" and take an argument of "int".
77 Function *Add1F =
78 Function::Create(FunctionType::get(Type::getInt32Ty(Context),
79 {Type::getInt32Ty(Context)}, false),
80 Function::ExternalLinkage, "add1", M);
81
82 // Add a basic block to the function. As before, it automatically inserts
83 // because of the last argument.
84 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
85
86 // Create a basic block builder with default parameters. The builder will
87 // automatically append instructions to the basic block `BB'.
88 IRBuilder<> builder(BB);
89
90 // Get pointers to the constant `1'.
91 Value *One = builder.getInt32(1);
92
93 // Get pointers to the integer argument of the add1 function...
94 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
95 Argument *ArgX = &*Add1F->arg_begin(); // Get the arg
96 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
97
98 // Create the add instruction, inserting it into the end of BB.
99 Value *Add = builder.CreateAdd(One, ArgX);
100
101 // Create the return instruction and add it to the basic block
102 builder.CreateRet(Add);
103
104 // Now, function add1 is ready.
105
106 // Now we're going to create function `foo', which returns an int and takes no
107 // arguments.
108 Function *FooF =
109 Function::Create(FunctionType::get(Type::getInt32Ty(Context), {}, false),
110 Function::ExternalLinkage, "foo", M);
111
112 // Add a basic block to the FooF function.
113 BB = BasicBlock::Create(Context, "EntryBlock", FooF);
114
115 // Tell the basic block builder to attach itself to the new basic block
116 builder.SetInsertPoint(BB);
117
118 // Get pointer to the constant `10'.
119 Value *Ten = builder.getInt32(10);
120
121 // Pass Ten to the call to Add1F
122 CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
123 Add1CallRes->setTailCall(true);
124
125 // Create the return instruction and add it to the basic block.
126 builder.CreateRet(Add1CallRes);
127
128 // Now we create the JIT.
129 ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
130
131 outs() << "We just constructed this LLVM module:\n\n" << *M;
132 outs() << "\n\nRunning foo: ";
133 outs().flush();
134
135 // Call the `foo' function with no arguments:
136 std::vector<GenericValue> noargs;
137 GenericValue gv = EE->runFunction(FooF, noargs);
138
139 // Import result of execution:
140 outs() << "Result: " << gv.IntVal << "\n";
141 delete EE;
142 llvm_shutdown();
143 return 0;
144 }
145