summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLang Hames <lhames@gmail.com>2019-10-18 18:25:15 +0000
committerLang Hames <lhames@gmail.com>2019-10-18 18:25:15 +0000
commit1ac3f80a6a097afd776e925fee4b1176b4383d7f (patch)
tree78df555bfd47ae86b6123e225d2c950e2b8e7d0e
parent2f41a023afdb68364ea490135874425e85faa574 (diff)
downloadbcm5719-llvm-1ac3f80a6a097afd776e925fee4b1176b4383d7f.tar.gz
bcm5719-llvm-1ac3f80a6a097afd776e925fee4b1176b4383d7f.zip
[examples] Add an example of how to use JITLink and small-code-model with LLJIT.
JITLink is LLVM's newer jit-linker. It is an alternative to (and hopefully eventually a replacement for) LLVM's older jit-linker, RuntimeDyld. Unlike RuntimeDyld which requries JIT'd code to be complied with the large code model, JITlink can link code compiled with the small code model, which is the native code model for a number of targets (including all supported MachO targets). This example shows how to: -- Create a JITLink InProcessMemoryManager -- Set the code model to small -- Use a JITLink backed ObjectLinkingLayer as the linking layer for LLJIT (rather than the default RTDyldObjectLinkingLayer). Note: This example will only work on platforms supported by JITLink. As of this commit that's MachO/x86-64 and MachO/arm64. llvm-svn: 375266
-rw-r--r--llvm/examples/LLJITExamples/CMakeLists.txt1
-rw-r--r--llvm/examples/LLJITExamples/LLJITWithJITLink/CMakeLists.txt11
-rw-r--r--llvm/examples/LLJITExamples/LLJITWithJITLink/LLJITWithJITLink.cpp70
3 files changed, 82 insertions, 0 deletions
diff --git a/llvm/examples/LLJITExamples/CMakeLists.txt b/llvm/examples/LLJITExamples/CMakeLists.txt
index 2ca888e3054..d1d53028f89 100644
--- a/llvm/examples/LLJITExamples/CMakeLists.txt
+++ b/llvm/examples/LLJITExamples/CMakeLists.txt
@@ -1 +1,2 @@
add_subdirectory(LLJITWithObjectCache)
+add_subdirectory(LLJITWithJITLink)
diff --git a/llvm/examples/LLJITExamples/LLJITWithJITLink/CMakeLists.txt b/llvm/examples/LLJITExamples/LLJITWithJITLink/CMakeLists.txt
new file mode 100644
index 00000000000..5f9bf394a72
--- /dev/null
+++ b/llvm/examples/LLJITExamples/LLJITWithJITLink/CMakeLists.txt
@@ -0,0 +1,11 @@
+set(LLVM_LINK_COMPONENTS
+ Core
+ IRReader
+ OrcJIT
+ Support
+ nativecodegen
+ )
+
+add_llvm_example(LLJITWithJITLink
+ LLJITWithJITLink.cpp
+ )
diff --git a/llvm/examples/LLJITExamples/LLJITWithJITLink/LLJITWithJITLink.cpp b/llvm/examples/LLJITExamples/LLJITWithJITLink/LLJITWithJITLink.cpp
new file mode 100644
index 00000000000..ed79b90e328
--- /dev/null
+++ b/llvm/examples/LLJITExamples/LLJITWithJITLink/LLJITWithJITLink.cpp
@@ -0,0 +1,70 @@
+//===-- LLJITWithJITLink.cpp - Configure LLJIT to use ObjectLinkingLayer --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file shows how to switch LLJIT to use ObjectLinkingLayer (which is
+// backed by JITLink) rather than RTDyldObjectLinkingLayer (which is backed by
+// RuntimeDyld). Using JITLink as the underlying allocator enables use of
+// small code model in JIT'd code.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
+#include "llvm/ExecutionEngine/Orc/LLJIT.h"
+#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include "../ExampleModules.h"
+
+using namespace llvm;
+using namespace llvm::orc;
+
+ExitOnError ExitOnErr;
+
+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]) + ": ");
+
+ // Declare an in-process JITLink memory manager.
+ jitlink::InProcessMemoryManager MemMgr;
+
+ // Detect the host and set code model to small.
+ auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
+ JTMB.setCodeModel(CodeModel::Small);
+
+ // Create an LLJIT instance with a custom CompileFunction.
+ auto J =
+ ExitOnErr(LLJITBuilder()
+ .setJITTargetMachineBuilder(std::move(JTMB))
+ .setObjectLinkingLayerCreator([&](ExecutionSession &ES,
+ const Triple &TT) {
+ return std::make_unique<ObjectLinkingLayer>(ES, MemMgr);
+ })
+ .create());
+
+ auto M = ExitOnErr(parseExampleModule(Add1Example, "add1"));
+
+ 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;
+}
OpenPOWER on IntegriCloud