1 //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
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
7 //===----------------------------------------------------------------------===//
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.
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.
17 // The goal of this snippet is to create in the memory
18 // the LLVM module consisting of two functions as follow:
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".
31 // Some remarks and questions:
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?
38 //===----------------------------------------------------------------------===//
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"
66 InitializeNativeTarget();
67 LLVMInitializeNativeAsmPrinter();
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();
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".
78 Function::Create(FunctionType::get(Type::getInt32Ty(Context
),
79 {Type::getInt32Ty(Context
)}, false),
80 Function::ExternalLinkage
, "add1", M
);
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
);
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
);
90 // Get pointers to the constant `1'.
91 Value
*One
= builder
.getInt32(1);
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.
98 // Create the add instruction, inserting it into the end of BB.
99 Value
*Add
= builder
.CreateAdd(One
, ArgX
);
101 // Create the return instruction and add it to the basic block
102 builder
.CreateRet(Add
);
104 // Now, function add1 is ready.
106 // Now we're going to create function `foo', which returns an int and takes no
109 Function::Create(FunctionType::get(Type::getInt32Ty(Context
), {}, false),
110 Function::ExternalLinkage
, "foo", M
);
112 // Add a basic block to the FooF function.
113 BB
= BasicBlock::Create(Context
, "EntryBlock", FooF
);
115 // Tell the basic block builder to attach itself to the new basic block
116 builder
.SetInsertPoint(BB
);
118 // Get pointer to the constant `10'.
119 Value
*Ten
= builder
.getInt32(10);
121 // Pass Ten to the call to Add1F
122 CallInst
*Add1CallRes
= builder
.CreateCall(Add1F
, Ten
);
123 Add1CallRes
->setTailCall(true);
125 // Create the return instruction and add it to the basic block.
126 builder
.CreateRet(Add1CallRes
);
128 // Now we create the JIT.
129 ExecutionEngine
* EE
= EngineBuilder(std::move(Owner
)).create();
131 outs() << "We just constructed this LLVM module:\n\n" << *M
;
132 outs() << "\n\nRunning foo: ";
135 // Call the `foo' function with no arguments:
136 std::vector
<GenericValue
> noargs
;
137 GenericValue gv
= EE
->runFunction(FooF
, noargs
);
139 // Import result of execution:
140 outs() << "Result: " << gv
.IntVal
<< "\n";