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