
This patch introduces new APIs to support resource tracking and removal in Orc. It is intended as a thread-safe generalization of the removeModule concept from OrcV1. Clients can now create ResourceTracker objects (using JITDylib::createResourceTracker) to track resources for each MaterializationUnit (code, data, aliases, absolute symbols, etc.) added to the JIT. Every MaterializationUnit will be associated with a ResourceTracker, and ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib has a default ResourceTracker that will be used for MaterializationUnits added to that JITDylib if no ResourceTracker is explicitly specified. Two operations can be performed on ResourceTrackers: transferTo and remove. The transferTo operation transfers tracking of the resources to a different ResourceTracker object, allowing ResourceTrackers to be merged to reduce administrative overhead (the source tracker is invalidated in the process). The remove operation removes all resources associated with a ResourceTracker, including any symbols defined by MaterializationUnits associated with the tracker, and also invalidates the tracker. These operations are thread safe, and should work regardless of the the state of the MaterializationUnits. In the case of resource transfer any existing resources associated with the source tracker will be transferred to the destination tracker, and all future resources for those units will be automatically associated with the destination tracker. In the case of resource removal all already-allocated resources will be deallocated, any if any program representations associated with the tracker have not been compiled yet they will be destroyed. If any program representations are currently being compiled then they will be prevented from completing: their MaterializationResponsibility will return errors on any attempt to update the JIT state. Clients (usually Layer writers) wishing to track resources can implement the ResourceManager API to receive notifications when ResourceTrackers are transferred or removed. The MaterializationResponsibility::withResourceKeyDo method can be used to create associations between the key for a ResourceTracker and an allocated resource in a thread-safe way. RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the ResourceManager API to enable tracking and removal of memory allocated by the JIT linker. The new JITDylib::clear method can be used to trigger removal of every ResourceTracker associated with the JITDylib (note that this will only remove resources for the JITDylib, it does not run static destructors). This patch includes unit tests showing basic usage. A follow-up patch will update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will use this API to release code associated with anonymous expressions.
168 lines
5.7 KiB
C++
168 lines
5.7 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/JITLink.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;
|
|
|
|
const llvm::StringRef TestMod =
|
|
R"(
|
|
define i32 @callee() {
|
|
entry:
|
|
ret i32 7
|
|
}
|
|
|
|
define i32 @entry() {
|
|
entry:
|
|
%0 = call i32 @callee()
|
|
ret i32 %0
|
|
}
|
|
)";
|
|
|
|
class MyPlugin : public ObjectLinkingLayer::Plugin {
|
|
public:
|
|
// The modifyPassConfig callback gives us a chance to inspect the
|
|
// MaterializationResponsibility and target triple for the object being
|
|
// linked, then add any JITLink passes that we would like to run on the
|
|
// link graph. A pass is just a function object that is callable as
|
|
// Error(jitlink::LinkGraph&). In this case we will add two passes
|
|
// defined as lambdas that call the printLinkerGraph method on our
|
|
// plugin: One to run before the linker applies fixups and another to
|
|
// run afterwards.
|
|
void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT,
|
|
jitlink::PassConfiguration &Config) override {
|
|
Config.PostPrunePasses.push_back([this](jitlink::LinkGraph &G) -> Error {
|
|
printLinkGraph(G, "Before fixup:");
|
|
return Error::success();
|
|
});
|
|
Config.PostFixupPasses.push_back([this](jitlink::LinkGraph &G) -> Error {
|
|
printLinkGraph(G, "After fixup:");
|
|
return Error::success();
|
|
});
|
|
}
|
|
|
|
void notifyLoaded(MaterializationResponsibility &MR) override {
|
|
dbgs() << "Loading object defining " << MR.getSymbols() << "\n";
|
|
}
|
|
|
|
Error notifyEmitted(MaterializationResponsibility &MR) override {
|
|
dbgs() << "Emitted object defining " << MR.getSymbols() << "\n";
|
|
return Error::success();
|
|
}
|
|
|
|
Error notifyFailed(MaterializationResponsibility &MR) override {
|
|
return Error::success();
|
|
}
|
|
|
|
Error notifyRemovingResources(ResourceKey K) override {
|
|
return Error::success();
|
|
}
|
|
|
|
void notifyTransferringResources(ResourceKey DstKey,
|
|
ResourceKey SrcKey) override {}
|
|
|
|
private:
|
|
void printLinkGraph(jitlink::LinkGraph &G, StringRef Title) {
|
|
constexpr JITTargetAddress LineWidth = 16;
|
|
|
|
dbgs() << "--- " << Title << "---\n";
|
|
for (auto &S : G.sections()) {
|
|
dbgs() << " section: " << S.getName() << "\n";
|
|
for (auto *B : S.blocks()) {
|
|
dbgs() << " block@" << formatv("{0:x16}", B->getAddress()) << ":\n";
|
|
|
|
if (B->isZeroFill())
|
|
continue;
|
|
|
|
JITTargetAddress InitAddr = B->getAddress() & ~(LineWidth - 1);
|
|
JITTargetAddress StartAddr = B->getAddress();
|
|
JITTargetAddress EndAddr = B->getAddress() + B->getSize();
|
|
auto *Data = reinterpret_cast<const uint8_t *>(B->getContent().data());
|
|
|
|
for (JITTargetAddress CurAddr = InitAddr; CurAddr != EndAddr;
|
|
++CurAddr) {
|
|
if (CurAddr % LineWidth == 0)
|
|
dbgs() << " " << formatv("{0:x16}", CurAddr) << ": ";
|
|
if (CurAddr < StartAddr)
|
|
dbgs() << " ";
|
|
else
|
|
dbgs() << formatv("{0:x-2}", Data[CurAddr - StartAddr]) << " ";
|
|
if (CurAddr % LineWidth == LineWidth - 1)
|
|
dbgs() << "\n";
|
|
}
|
|
if (EndAddr % LineWidth != 0)
|
|
dbgs() << "\n";
|
|
dbgs() << "\n";
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
int main(int argc, char *argv[]) {
|
|
// Initialize LLVM.
|
|
InitLLVM X(argc, argv);
|
|
|
|
InitializeNativeTarget();
|
|
InitializeNativeTargetAsmPrinter();
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "LLJITWithObjectLinkingLayerPlugin");
|
|
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.
|
|
// We attach our plugin in to the newly created ObjectLinkingLayer before
|
|
// returning it.
|
|
auto J = ExitOnErr(
|
|
LLJITBuilder()
|
|
.setJITTargetMachineBuilder(std::move(JTMB))
|
|
.setObjectLinkingLayerCreator(
|
|
[&](ExecutionSession &ES, const Triple &TT) {
|
|
// Create ObjectLinkingLayer.
|
|
auto ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>(
|
|
ES, std::make_unique<jitlink::InProcessMemoryManager>());
|
|
// Add an instance of our plugin.
|
|
ObjLinkingLayer->addPlugin(std::make_unique<MyPlugin>());
|
|
return ObjLinkingLayer;
|
|
})
|
|
.create());
|
|
|
|
auto M = ExitOnErr(parseExampleModule(TestMod, "test-module"));
|
|
|
|
ExitOnErr(J->addIRModule(std::move(M)));
|
|
|
|
// Look up the JIT'd function, cast it to a function pointer, then call it.
|
|
auto EntrySym = ExitOnErr(J->lookup("entry"));
|
|
auto *Entry = (int (*)())EntrySym.getAddress();
|
|
|
|
int Result = Entry();
|
|
outs() << "---Result---\n"
|
|
<< "entry() = " << Result << "\n";
|
|
|
|
return 0;
|
|
}
|