From 21dbb437f800308cfe14b28fe08035c9c1b74d0b Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Wed, 22 May 2019 21:38:41 +0000 Subject: [PATCH] Add a HowToUseLLJIT example project. A very minimal demo of how to use the LLJIT class, along the lines of the old HowToUseJIT example. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361435 91177308-0d34-0410-b5e6-96231b3b80d8 --- examples/CMakeLists.txt | 1 + examples/HowToUseLLJIT/CMakeLists.txt | 10 +++++ examples/HowToUseLLJIT/HowToUseLLJIT.cpp | 74 ++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 examples/HowToUseLLJIT/CMakeLists.txt create mode 100644 examples/HowToUseLLJIT/HowToUseLLJIT.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5727066d622..f8d4ee908bb 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,6 +1,7 @@ add_subdirectory(BrainF) add_subdirectory(Fibonacci) add_subdirectory(HowToUseJIT) +add_subdirectory(HowToUseLLJIT) add_subdirectory(Kaleidoscope) add_subdirectory(ModuleMaker) diff --git a/examples/HowToUseLLJIT/CMakeLists.txt b/examples/HowToUseLLJIT/CMakeLists.txt new file mode 100644 index 00000000000..77a8e8c704c --- /dev/null +++ b/examples/HowToUseLLJIT/CMakeLists.txt @@ -0,0 +1,10 @@ +set(LLVM_LINK_COMPONENTS + Core + OrcJIT + Support + nativecodegen + ) + +add_llvm_example(HowToUseLLJIT + HowToUseLLJIT.cpp + ) diff --git a/examples/HowToUseLLJIT/HowToUseLLJIT.cpp b/examples/HowToUseLLJIT/HowToUseLLJIT.cpp new file mode 100644 index 00000000000..372643d7d1a --- /dev/null +++ b/examples/HowToUseLLJIT/HowToUseLLJIT.cpp @@ -0,0 +1,74 @@ +#include "llvm/ExecutionEngine/Orc/LLJIT.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; +using namespace llvm::orc; + +ExitOnError ExitOnErr; + +ThreadSafeModule createDemoModule() { + auto Context = llvm::make_unique(); + auto M = make_unique("test", *Context); + + // Create the add1 function entry and insert this entry into module M. The + // function will have a return type of "int" and take an argument of "int". + Function *Add1F = + Function::Create(FunctionType::get(Type::getInt32Ty(*Context), + {Type::getInt32Ty(*Context)}, false), + Function::ExternalLinkage, "add1", M.get()); + + // Add a basic block to the function. As before, it automatically inserts + // because of the last argument. + BasicBlock *BB = BasicBlock::Create(*Context, "EntryBlock", Add1F); + + // Create a basic block builder with default parameters. The builder will + // automatically append instructions to the basic block `BB'. + IRBuilder<> builder(BB); + + // Get pointers to the constant `1'. + Value *One = builder.getInt32(1); + + // Get pointers to the integer argument of the add1 function... + assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg + Argument *ArgX = &*Add1F->arg_begin(); // Get the arg + ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. + + // Create the add instruction, inserting it into the end of BB. + Value *Add = builder.CreateAdd(One, ArgX); + + // Create the return instruction and add it to the basic block + builder.CreateRet(Add); + + return ThreadSafeModule(std::move(M), std::move(Context)); +} + +int main(int argc, char *argv[]) { + // Initialize LLVM. + InitLLVM X(argc, argv); + + InitializeNativeTarget(); + InitializeNativeTargetAsmPrinter(); + + cl::ParseCommandLineOptions(argc, argv, "HowToUseLLJIT"); + ExitOnErr.setBanner(std::string(argv[0]) + ": "); + + // Create an LLJIT instance. + auto J = ExitOnErr(LLJITBuilder().create()); + auto M = createDemoModule(); + + ExitOnErr(J->addIRModule(std::move(M))); + + // Look up the JIT'd function, cast it to a function pointer, then call it. + auto Add1Sym = ExitOnErr(J->lookup("add1")); + int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress(); + + int Result = Add1(42); + outs() << "add1(42) = " << Result << "\n"; + + return 0; +} -- 2.11.0