mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-02-01 05:01:59 +01:00
efc9f3486a
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.
213 lines
7.4 KiB
C++
213 lines
7.4 KiB
C++
//===-------------------- Layer.cpp - Layer interfaces --------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/ExecutionEngine/Orc/Layer.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
|
|
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/Object/MachO.h"
|
|
#include "llvm/Object/ObjectFile.h"
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#define DEBUG_TYPE "orc"
|
|
|
|
namespace llvm {
|
|
namespace orc {
|
|
|
|
IRLayer::~IRLayer() {}
|
|
|
|
Error IRLayer::add(ResourceTrackerSP RT, ThreadSafeModule TSM) {
|
|
assert(RT && "RT can not be null");
|
|
auto &JD = RT->getJITDylib();
|
|
return JD.define(std::make_unique<BasicIRLayerMaterializationUnit>(
|
|
*this, *getManglingOptions(), std::move(TSM)),
|
|
std::move(RT));
|
|
}
|
|
|
|
IRMaterializationUnit::IRMaterializationUnit(
|
|
ExecutionSession &ES, const IRSymbolMapper::ManglingOptions &MO,
|
|
ThreadSafeModule TSM)
|
|
: MaterializationUnit(SymbolFlagsMap(), nullptr), TSM(std::move(TSM)) {
|
|
|
|
assert(this->TSM && "Module must not be null");
|
|
|
|
MangleAndInterner Mangle(ES, this->TSM.getModuleUnlocked()->getDataLayout());
|
|
this->TSM.withModuleDo([&](Module &M) {
|
|
for (auto &G : M.global_values()) {
|
|
// Skip globals that don't generate symbols.
|
|
|
|
if (!G.hasName() || G.isDeclaration() || G.hasLocalLinkage() ||
|
|
G.hasAvailableExternallyLinkage() || G.hasAppendingLinkage())
|
|
continue;
|
|
|
|
// thread locals generate different symbols depending on whether or not
|
|
// emulated TLS is enabled.
|
|
if (G.isThreadLocal() && MO.EmulatedTLS) {
|
|
auto &GV = cast<GlobalVariable>(G);
|
|
|
|
auto Flags = JITSymbolFlags::fromGlobalValue(GV);
|
|
|
|
auto EmuTLSV = Mangle(("__emutls_v." + GV.getName()).str());
|
|
SymbolFlags[EmuTLSV] = Flags;
|
|
SymbolToDefinition[EmuTLSV] = &GV;
|
|
|
|
// If this GV has a non-zero initializer we'll need to emit an
|
|
// __emutls.t symbol too.
|
|
if (GV.hasInitializer()) {
|
|
const auto *InitVal = GV.getInitializer();
|
|
|
|
// Skip zero-initializers.
|
|
if (isa<ConstantAggregateZero>(InitVal))
|
|
continue;
|
|
const auto *InitIntValue = dyn_cast<ConstantInt>(InitVal);
|
|
if (InitIntValue && InitIntValue->isZero())
|
|
continue;
|
|
|
|
auto EmuTLST = Mangle(("__emutls_t." + GV.getName()).str());
|
|
SymbolFlags[EmuTLST] = Flags;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
// Otherwise we just need a normal linker mangling.
|
|
auto MangledName = Mangle(G.getName());
|
|
SymbolFlags[MangledName] = JITSymbolFlags::fromGlobalValue(G);
|
|
SymbolToDefinition[MangledName] = &G;
|
|
}
|
|
|
|
// If we need an init symbol for this module then create one.
|
|
if (!llvm::empty(getStaticInitGVs(M))) {
|
|
size_t Counter = 0;
|
|
|
|
do {
|
|
std::string InitSymbolName;
|
|
raw_string_ostream(InitSymbolName)
|
|
<< "$." << M.getModuleIdentifier() << ".__inits." << Counter++;
|
|
InitSymbol = ES.intern(InitSymbolName);
|
|
} while (SymbolFlags.count(InitSymbol));
|
|
|
|
SymbolFlags[InitSymbol] = JITSymbolFlags::MaterializationSideEffectsOnly;
|
|
}
|
|
});
|
|
}
|
|
|
|
IRMaterializationUnit::IRMaterializationUnit(
|
|
ThreadSafeModule TSM, SymbolFlagsMap SymbolFlags,
|
|
SymbolStringPtr InitSymbol, SymbolNameToDefinitionMap SymbolToDefinition)
|
|
: MaterializationUnit(std::move(SymbolFlags), std::move(InitSymbol)),
|
|
TSM(std::move(TSM)), SymbolToDefinition(std::move(SymbolToDefinition)) {}
|
|
|
|
StringRef IRMaterializationUnit::getName() const {
|
|
if (TSM)
|
|
return TSM.withModuleDo(
|
|
[](const Module &M) -> StringRef { return M.getModuleIdentifier(); });
|
|
return "<null module>";
|
|
}
|
|
|
|
void IRMaterializationUnit::discard(const JITDylib &JD,
|
|
const SymbolStringPtr &Name) {
|
|
LLVM_DEBUG(JD.getExecutionSession().runSessionLocked([&]() {
|
|
dbgs() << "In " << JD.getName() << " discarding " << *Name << " from MU@"
|
|
<< this << " (" << getName() << ")\n";
|
|
}););
|
|
|
|
auto I = SymbolToDefinition.find(Name);
|
|
assert(I != SymbolToDefinition.end() &&
|
|
"Symbol not provided by this MU, or previously discarded");
|
|
assert(!I->second->isDeclaration() &&
|
|
"Discard should only apply to definitions");
|
|
I->second->setLinkage(GlobalValue::AvailableExternallyLinkage);
|
|
SymbolToDefinition.erase(I);
|
|
}
|
|
|
|
BasicIRLayerMaterializationUnit::BasicIRLayerMaterializationUnit(
|
|
IRLayer &L, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM)
|
|
: IRMaterializationUnit(L.getExecutionSession(), MO, std::move(TSM)), L(L) {
|
|
}
|
|
|
|
void BasicIRLayerMaterializationUnit::materialize(
|
|
std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
// Throw away the SymbolToDefinition map: it's not usable after we hand
|
|
// off the module.
|
|
SymbolToDefinition.clear();
|
|
|
|
// If cloneToNewContextOnEmit is set, clone the module now.
|
|
if (L.getCloneToNewContextOnEmit())
|
|
TSM = cloneToNewContext(TSM);
|
|
|
|
#ifndef NDEBUG
|
|
auto &ES = R->getTargetJITDylib().getExecutionSession();
|
|
auto &N = R->getTargetJITDylib().getName();
|
|
#endif // NDEBUG
|
|
|
|
LLVM_DEBUG(ES.runSessionLocked(
|
|
[&]() { dbgs() << "Emitting, for " << N << ", " << *this << "\n"; }););
|
|
L.emit(std::move(R), std::move(TSM));
|
|
LLVM_DEBUG(ES.runSessionLocked([&]() {
|
|
dbgs() << "Finished emitting, for " << N << ", " << *this << "\n";
|
|
}););
|
|
}
|
|
|
|
ObjectLayer::ObjectLayer(ExecutionSession &ES) : ES(ES) {}
|
|
|
|
ObjectLayer::~ObjectLayer() {}
|
|
|
|
Error ObjectLayer::add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O) {
|
|
assert(RT && "RT can not be null");
|
|
auto ObjMU = BasicObjectLayerMaterializationUnit::Create(*this, std::move(O));
|
|
if (!ObjMU)
|
|
return ObjMU.takeError();
|
|
auto &JD = RT->getJITDylib();
|
|
return JD.define(std::move(*ObjMU), std::move(RT));
|
|
}
|
|
|
|
Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>>
|
|
BasicObjectLayerMaterializationUnit::Create(ObjectLayer &L,
|
|
std::unique_ptr<MemoryBuffer> O) {
|
|
auto ObjSymInfo =
|
|
getObjectSymbolInfo(L.getExecutionSession(), O->getMemBufferRef());
|
|
|
|
if (!ObjSymInfo)
|
|
return ObjSymInfo.takeError();
|
|
|
|
auto &SymbolFlags = ObjSymInfo->first;
|
|
auto &InitSymbol = ObjSymInfo->second;
|
|
|
|
return std::unique_ptr<BasicObjectLayerMaterializationUnit>(
|
|
new BasicObjectLayerMaterializationUnit(
|
|
L, std::move(O), std::move(SymbolFlags), std::move(InitSymbol)));
|
|
}
|
|
|
|
BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit(
|
|
ObjectLayer &L, std::unique_ptr<MemoryBuffer> O, SymbolFlagsMap SymbolFlags,
|
|
SymbolStringPtr InitSymbol)
|
|
: MaterializationUnit(std::move(SymbolFlags), std::move(InitSymbol)), L(L),
|
|
O(std::move(O)) {}
|
|
|
|
StringRef BasicObjectLayerMaterializationUnit::getName() const {
|
|
if (O)
|
|
return O->getBufferIdentifier();
|
|
return "<null object>";
|
|
}
|
|
|
|
void BasicObjectLayerMaterializationUnit::materialize(
|
|
std::unique_ptr<MaterializationResponsibility> R) {
|
|
L.emit(std::move(R), std::move(O));
|
|
}
|
|
|
|
void BasicObjectLayerMaterializationUnit::discard(const JITDylib &JD,
|
|
const SymbolStringPtr &Name) {
|
|
// This is a no-op for object files: Having removed 'Name' from SymbolFlags
|
|
// the symbol will be dead-stripped by the JIT linker.
|
|
}
|
|
|
|
} // End namespace orc.
|
|
} // End namespace llvm.
|