Files
llvm-project/llvm/examples/OrcV2Examples/LLJITWithCustomObjectLinkingLayer/LLJITWithCustomObjectLinkingLayer.cpp
Lang Hames b78e9faa70 [ORC] Add MemMgr arg to LLJITBuilder::ObjectLinkingLayerCreator. (#192214)
LinkGraphLinkingLayer and ObjectLinkingLayer will start requiring a
jitlink::JITLinkMemoryManager argument in an upcoming commit. In
preparation for that, this patch threads a MemMgr argument through the
LLJITBuilder::ObjectLinkingLayerCreator factory type.

Note: This patch does not thread the argument through the C API
(LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction) yet so as to not
break compatibility. All current users of the C API construct
RuntimeDyld instances, which would have to ignore this argument anyway.
If we don't update the
LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction type before
RuntimeDyld is removed then that will be a good time to update it, since
all existing users were going to have to rewrite their code anyway.
2026-04-15 12:26:58 +01:00

66 lines
2.2 KiB
C++

//===--------------- 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, "LLJITWithCustomObjectLinkingLayer");
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, jitlink::JITLinkMemoryManager &MemMgr) {
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 Add1Addr = ExitOnErr(J->lookup("add1"));
int (*Add1)(int) = Add1Addr.toPtr<int(int)>();
int Result = Add1(42);
outs() << "add1(42) = " << Result << "\n";
return 0;
}