brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · 37fb92d Raw
67 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file shows how to switch LLJIT to use a custom object linking layer (we10// use ObjectLinkingLayer, which is backed by JITLink, as an example).11//12//===----------------------------------------------------------------------===//13 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"21 22#include "../ExampleModules.h"23 24using namespace llvm;25using namespace llvm::orc;26 27ExitOnError ExitOnErr;28 29int main(int argc, char *argv[]) {30  // Initialize LLVM.31  InitLLVM X(argc, argv);32 33  InitializeNativeTarget();34  InitializeNativeTargetAsmPrinter();35 36  cl::ParseCommandLineOptions(argc, argv, "LLJITWithCustomObjectLinkingLayer");37  ExitOnErr.setBanner(std::string(argv[0]) + ": ");38 39  // Detect the host and set code model to small.40  auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());41  JTMB.setCodeModel(CodeModel::Small);42 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) {49                return std::make_unique<ObjectLinkingLayer>(50                    ES, ExitOnErr(jitlink::InProcessMemoryManager::Create()));51              })52          .create());53 54  auto M = ExitOnErr(parseExampleModule(Add1Example, "add1"));55 56  ExitOnErr(J->addIRModule(std::move(M)));57 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)>();61 62  int Result = Add1(42);63  outs() << "add1(42) = " << Result << "\n";64 65  return 0;66}67