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/IR/Argument.h" 40 #include "llvm/IR/BasicBlock.h" 41 #include "llvm/IR/Constants.h" 42 #include "llvm/IR/DerivedTypes.h" 43 #include "llvm/IR/Function.h" 44 #include "llvm/IR/IRBuilder.h" 45 #include "llvm/IR/Instructions.h" 46 #include "llvm/IR/LLVMContext.h" 47 #include "llvm/IR/Module.h" 48 #include "llvm/IR/Type.h" 49 #include "llvm/Support/Casting.h" 50 #include "llvm/Support/ManagedStatic.h" 51 #include "llvm/Support/TargetSelect.h" 52 #include "llvm/Support/raw_ostream.h" 53 #include <algorithm> 54 #include <cassert> 55 #include <memory> 56 #include <vector> 57 58 using namespace llvm; 59 60 int main() { 61 InitializeNativeTarget(); 62 63 LLVMContext Context; 64 65 // Create some module to put our function into it. 66 std::unique_ptr<Module> Owner = make_unique<Module>("test", Context); 67 Module *M = Owner.get(); 68 69 // Create the add1 function entry and insert this entry into module M. The 70 // function will have a return type of "int" and take an argument of "int". 71 Function *Add1F = 72 cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context), 73 Type::getInt32Ty(Context))); 74 75 // Add a basic block to the function. As before, it automatically inserts 76 // because of the last argument. 77 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F); 78 79 // Create a basic block builder with default parameters. The builder will 80 // automatically append instructions to the basic block `BB'. 81 IRBuilder<> builder(BB); 82 83 // Get pointers to the constant `1'. 84 Value *One = builder.getInt32(1); 85 86 // Get pointers to the integer argument of the add1 function... 87 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg 88 Argument *ArgX = &*Add1F->arg_begin(); // Get the arg 89 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. 90 91 // Create the add instruction, inserting it into the end of BB. 92 Value *Add = builder.CreateAdd(One, ArgX); 93 94 // Create the return instruction and add it to the basic block 95 builder.CreateRet(Add); 96 97 // Now, function add1 is ready. 98 99 // Now we're going to create function `foo', which returns an int and takes no 100 // arguments. 101 Function *FooF = 102 cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context))); 103 104 // Add a basic block to the FooF function. 105 BB = BasicBlock::Create(Context, "EntryBlock", FooF); 106 107 // Tell the basic block builder to attach itself to the new basic block 108 builder.SetInsertPoint(BB); 109 110 // Get pointer to the constant `10'. 111 Value *Ten = builder.getInt32(10); 112 113 // Pass Ten to the call to Add1F 114 CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten); 115 Add1CallRes->setTailCall(true); 116 117 // Create the return instruction and add it to the basic block. 118 builder.CreateRet(Add1CallRes); 119 120 // Now we create the JIT. 121 ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create(); 122 123 outs() << "We just constructed this LLVM module:\n\n" << *M; 124 outs() << "\n\nRunning foo: "; 125 outs().flush(); 126 127 // Call the `foo' function with no arguments: 128 std::vector<GenericValue> noargs; 129 GenericValue gv = EE->runFunction(FooF, noargs); 130 131 // Import result of execution: 132 outs() << "Result: " << gv.IntVal << "\n"; 133 delete EE; 134 llvm_shutdown(); 135 return 0; 136 } 137