Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / examples / OrcV2Examples / LLJITWithCustomObjectLinkingLayer / LLJITWithCustomObjectLinkingLayer.cpp
blob16c81de54c86f6f96efe5c732d1602bd73727389
1 //===--------------- LLJITWithCustomObjectLinkingLayer.cpp ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file shows how to switch LLJIT to use a custom object linking layer (we
10 // use ObjectLinkingLayer, which is backed by JITLink, as an example).
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
16 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
17 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
18 #include "llvm/Support/InitLLVM.h"
19 #include "llvm/Support/TargetSelect.h"
20 #include "llvm/Support/raw_ostream.h"
22 #include "../ExampleModules.h"
24 using namespace llvm;
25 using namespace llvm::orc;
27 ExitOnError ExitOnErr;
29 int main(int argc, char *argv[]) {
30 // Initialize LLVM.
31 InitLLVM X(argc, argv);
33 InitializeNativeTarget();
34 InitializeNativeTargetAsmPrinter();
36 cl::ParseCommandLineOptions(argc, argv, "LLJITWithCustomObjectLinkingLayer");
37 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
39 // Detect the host and set code model to small.
40 auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
41 JTMB.setCodeModel(CodeModel::Small);
43 // Create an LLJIT instance with an ObjectLinkingLayer as the base layer.
44 auto J = ExitOnErr(
45 LLJITBuilder()
46 .setJITTargetMachineBuilder(std::move(JTMB))
47 .setObjectLinkingLayerCreator(
48 [&](ExecutionSession &ES, const Triple &TT) {
49 return std::make_unique<ObjectLinkingLayer>(
50 ES, ExitOnErr(jitlink::InProcessMemoryManager::Create()));
52 .create());
54 auto M = ExitOnErr(parseExampleModule(Add1Example, "add1"));
56 ExitOnErr(J->addIRModule(std::move(M)));
58 // Look up the JIT'd function, cast it to a function pointer, then call it.
59 auto Add1Addr = ExitOnErr(J->lookup("add1"));
60 int (*Add1)(int) = Add1Addr.toPtr<int(int)>();
62 int Result = Add1(42);
63 outs() << "add1(42) = " << Result << "\n";
65 return 0;