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