summaryrefslogtreecommitdiffstats
path: root/llvm/examples/LLJITExamples/LLJITWithCustomObjectLinkingLayer/LLJITWithCustomObjectLinkingLayer.cpp
diff options
context:
space:
mode:
authorLang Hames <lhames@gmail.com>2019-12-15 20:30:01 -0800
committerLang Hames <lhames@gmail.com>2019-12-15 21:57:11 -0800
commit3c9bf77b2e610856e23237746c0ff51a01d1d79f (patch)
tree64c692a7bffd35d647394e8d8b3be865825af52c /llvm/examples/LLJITExamples/LLJITWithCustomObjectLinkingLayer/LLJITWithCustomObjectLinkingLayer.cpp
parent67a1b7f05379f2686f858fc103debcac0aed3973 (diff)
downloadbcm5719-llvm-3c9bf77b2e610856e23237746c0ff51a01d1d79f.tar.gz
bcm5719-llvm-3c9bf77b2e610856e23237746c0ff51a01d1d79f.zip
[examples] Rename LLJITWithJITLink to LLJITWithCustomObjectLinkingLayer.
LLJIT now uses JITLink/ObjectLinkingLayer by default where available, so these steps aren't required to use it. The tutorial is still useful though: Clients can use it to test altervative linking layer implementations (e.g. handing off to the system linker) or to test implementations of JITLink that are still under development.
Diffstat (limited to 'llvm/examples/LLJITExamples/LLJITWithCustomObjectLinkingLayer/LLJITWithCustomObjectLinkingLayer.cpp')
-rw-r--r--llvm/examples/LLJITExamples/LLJITWithCustomObjectLinkingLayer/LLJITWithCustomObjectLinkingLayer.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/llvm/examples/LLJITExamples/LLJITWithCustomObjectLinkingLayer/LLJITWithCustomObjectLinkingLayer.cpp b/llvm/examples/LLJITExamples/LLJITWithCustomObjectLinkingLayer/LLJITWithCustomObjectLinkingLayer.cpp
new file mode 100644
index 00000000000..04607d743ad
--- /dev/null
+++ b/llvm/examples/LLJITExamples/LLJITWithCustomObjectLinkingLayer/LLJITWithCustomObjectLinkingLayer.cpp
@@ -0,0 +1,66 @@
+//===--------------- LLJITWithCustomObjectLinkingLayer.cpp ----------------===//
+//
+// 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 a custom object linking layer (we
+// use ObjectLinkingLayer, which is backed by JITLink, as an example).
+//
+//===----------------------------------------------------------------------===//
+
+#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]) + ": ");
+
+ // Detect the host and set code model to small.
+ auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
+ JTMB.setCodeModel(CodeModel::Small);
+
+ // Create an LLJIT instance with an ObjectLinkingLayer as the base layer.
+ auto J = ExitOnErr(
+ LLJITBuilder()
+ .setJITTargetMachineBuilder(std::move(JTMB))
+ .setObjectLinkingLayerCreator(
+ [&](ExecutionSession &ES, const Triple &TT) {
+ return std::make_unique<ObjectLinkingLayer>(
+ ES, std::make_unique<jitlink::InProcessMemoryManager>());
+ })
+ .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