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