1843f198aSLang Hames //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===// 2843f198aSLang Hames // 3843f198aSLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4843f198aSLang Hames // See https://llvm.org/LICENSE.txt for license information. 5843f198aSLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6843f198aSLang Hames // 7843f198aSLang Hames //===----------------------------------------------------------------------===// 8843f198aSLang Hames 9e3748b5aSLang Hames #include "llvm/ExecutionEngine/Orc/LLJIT.h" 10e3748b5aSLang Hames #include "llvm/IR/Function.h" 11e3748b5aSLang Hames #include "llvm/IR/IRBuilder.h" 12e3748b5aSLang Hames #include "llvm/IR/Module.h" 13e3748b5aSLang Hames #include "llvm/Support/InitLLVM.h" 14e3748b5aSLang Hames #include "llvm/Support/TargetSelect.h" 15e3748b5aSLang Hames #include "llvm/Support/raw_ostream.h" 16e3748b5aSLang Hames 17e3748b5aSLang Hames using namespace llvm; 18e3748b5aSLang Hames using namespace llvm::orc; 19e3748b5aSLang Hames 20e3748b5aSLang Hames ExitOnError ExitOnErr; 21e3748b5aSLang Hames 22e3748b5aSLang Hames ThreadSafeModule createDemoModule() { 23*0eaee545SJonas Devlieghere auto Context = std::make_unique<LLVMContext>(); 24*0eaee545SJonas Devlieghere auto M = std::make_unique<Module>("test", *Context); 25e3748b5aSLang Hames 26e3748b5aSLang Hames // Create the add1 function entry and insert this entry into module M. The 27e3748b5aSLang Hames // function will have a return type of "int" and take an argument of "int". 28e3748b5aSLang Hames Function *Add1F = 29e3748b5aSLang Hames Function::Create(FunctionType::get(Type::getInt32Ty(*Context), 30e3748b5aSLang Hames {Type::getInt32Ty(*Context)}, false), 31e3748b5aSLang Hames Function::ExternalLinkage, "add1", M.get()); 32e3748b5aSLang Hames 33e3748b5aSLang Hames // Add a basic block to the function. As before, it automatically inserts 34e3748b5aSLang Hames // because of the last argument. 35e3748b5aSLang Hames BasicBlock *BB = BasicBlock::Create(*Context, "EntryBlock", Add1F); 36e3748b5aSLang Hames 37e3748b5aSLang Hames // Create a basic block builder with default parameters. The builder will 38e3748b5aSLang Hames // automatically append instructions to the basic block `BB'. 39e3748b5aSLang Hames IRBuilder<> builder(BB); 40e3748b5aSLang Hames 41e3748b5aSLang Hames // Get pointers to the constant `1'. 42e3748b5aSLang Hames Value *One = builder.getInt32(1); 43e3748b5aSLang Hames 44e3748b5aSLang Hames // Get pointers to the integer argument of the add1 function... 45e3748b5aSLang Hames assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg 46e3748b5aSLang Hames Argument *ArgX = &*Add1F->arg_begin(); // Get the arg 47e3748b5aSLang Hames ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. 48e3748b5aSLang Hames 49e3748b5aSLang Hames // Create the add instruction, inserting it into the end of BB. 50e3748b5aSLang Hames Value *Add = builder.CreateAdd(One, ArgX); 51e3748b5aSLang Hames 52e3748b5aSLang Hames // Create the return instruction and add it to the basic block 53e3748b5aSLang Hames builder.CreateRet(Add); 54e3748b5aSLang Hames 55e3748b5aSLang Hames return ThreadSafeModule(std::move(M), std::move(Context)); 56e3748b5aSLang Hames } 57e3748b5aSLang Hames 58e3748b5aSLang Hames int main(int argc, char *argv[]) { 59e3748b5aSLang Hames // Initialize LLVM. 60e3748b5aSLang Hames InitLLVM X(argc, argv); 61e3748b5aSLang Hames 62e3748b5aSLang Hames InitializeNativeTarget(); 63e3748b5aSLang Hames InitializeNativeTargetAsmPrinter(); 64e3748b5aSLang Hames 65e3748b5aSLang Hames cl::ParseCommandLineOptions(argc, argv, "HowToUseLLJIT"); 66e3748b5aSLang Hames ExitOnErr.setBanner(std::string(argv[0]) + ": "); 67e3748b5aSLang Hames 68e3748b5aSLang Hames // Create an LLJIT instance. 69e3748b5aSLang Hames auto J = ExitOnErr(LLJITBuilder().create()); 70e3748b5aSLang Hames auto M = createDemoModule(); 71e3748b5aSLang Hames 72e3748b5aSLang Hames ExitOnErr(J->addIRModule(std::move(M))); 73e3748b5aSLang Hames 74e3748b5aSLang Hames // Look up the JIT'd function, cast it to a function pointer, then call it. 75e3748b5aSLang Hames auto Add1Sym = ExitOnErr(J->lookup("add1")); 76e3748b5aSLang Hames int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress(); 77e3748b5aSLang Hames 78e3748b5aSLang Hames int Result = Add1(42); 79e3748b5aSLang Hames outs() << "add1(42) = " << Result << "\n"; 80e3748b5aSLang Hames 81e3748b5aSLang Hames return 0; 82e3748b5aSLang Hames } 83