14 #ifndef LLVM_EXECUTIONENGINE_ORC_REMOTEOBJECTLAYER_H 15 #define LLVM_EXECUTIONENGINE_ORC_REMOTEOBJECTLAYER_H 47 :
public rpc::Function<AddObject, Expected<ObjHandleT>(std::string)> {
49 static const char *
getName() {
return "AddObject"; }
55 static const char *
getName() {
return "RemoveObject"; }
59 :
public rpc::Function<FindSymbol, Expected<RemoteSymbol>(std::string,
62 static const char *
getName() {
return "FindSymbol"; }
67 Expected<RemoteSymbol>(ObjHandleT, std::string,
70 static const char *
getName() {
return "FindSymbolIn"; }
77 static const char *
getName() {
return "EmitAndFinalize"; }
82 Expected<RemoteSymbol>(ObjHandleT, std::string)> {
84 static const char *
getName() {
return "Lookup"; }
89 Expected<RemoteSymbol>(ObjHandleT, std::string)> {
91 static const char *
getName() {
return "LookupInLogicalDylib"; }
95 :
public rpc::Function<ReleaseRemoteSymbol, Error(RemoteSymbolId)> {
97 static const char *
getName() {
return "ReleaseRemoteSymbol"; }
102 Expected<JITTargetAddress>(RemoteSymbolId)> {
104 static const char *
getName() {
return "MaterializeRemoteSymbol"; }
110 template <
typename RPCEndpo
int>
119 Remote.template addHandler<ReleaseRemoteSymbol>(
120 *
this, &ThisT::handleReleaseRemoteSymbol);
121 Remote.template addHandler<MaterializeRemoteSymbol>(
122 *
this, &ThisT::handleMaterializeRemoteSymbol);
141 :
C(Other.
C),
Id(Other.
Id) {
155 C.releaseRemoteSymbol(
Id);
160 auto Addr =
C.materializeRemoteSymbol(
Id);
182 [
this](std::unique_ptr<ErrorInfoBase> EIB) {
185 EIB->convertToErrorCode()));
186 return Error(std::move(EIB));
191 return make_error<BadSymbolHandleError>(
Id,
"Remote JIT Symbol");
195 return make_error<RemoteObjectLayerAPI::BadObjectHandleError>(
196 H,
"Bad object handle");
202 auto Id = SymbolIdMgr.getNext();
204 assert(!InUseSymbols.count(
Id) &&
"Symbol id already in use");
205 InUseSymbols.insert(std::make_pair(
Id, std::move(Sym)));
208 return teeLog(std::move(Err));
210 return nullRemoteSymbol();
215 if (RemoteSymOrErr) {
216 auto &RemoteSym = *RemoteSymOrErr;
217 if (RemoteSym == nullRemoteSymbol())
236 if (
auto Err = Remote.template callB<ReleaseRemoteSymbol>(Id))
243 return Remote.template callB<MaterializeRemoteSymbol>(
Id);
248 auto SI = InUseSymbols.find(Id);
249 if (
SI != InUseSymbols.end()) {
250 InUseSymbols.erase(
SI);
253 return teeLog(badRemoteSymbolIdError(Id));
259 auto SI = InUseSymbols.find(Id);
260 if (
SI != InUseSymbols.end()) {
261 auto AddrOrErr =
SI->second.getAddress();
262 InUseSymbols.erase(
SI);
263 SymbolIdMgr.release(Id);
267 return teeLog(AddrOrErr.takeError());
269 return teeLog(badRemoteSymbolIdError(Id));
274 std::map<RemoteSymbolId, JITSymbol> InUseSymbols;
288 template <
typename RPCEndpo
int>
321 Remote.template addHandler<LookupInLogicalDylib>(
322 *
this, &ThisT::lookupInLogicalDylib);
331 std::shared_ptr<LegacyJITSymbolResolver>
Resolver) {
332 if (
auto HandleOrErr =
333 this->Remote.template callB<AddObject>(ObjBuffer->getBuffer())) {
334 auto &Handle = *HandleOrErr;
336 assert(!Resolvers.count(Handle) &&
"Handle already in use?");
337 Resolvers[Handle] = std::move(Resolver);
340 return HandleOrErr.takeError();
345 return this->Remote.template callB<RemoveObject>(
H);
350 return remoteToJITSymbol(
351 this->Remote.template callB<FindSymbol>(Name,
352 ExportedSymbolsOnly));
357 return remoteToJITSymbol(
358 this->Remote.template callB<FindSymbolIn>(H, Name,
359 ExportedSymbolsOnly));
364 return this->Remote.template callB<EmitAndFinalize>(
H);
370 auto RI = Resolvers.find(H);
371 if (RI != Resolvers.end()) {
372 return this->jitSymbolToRemote(RI->second->findSymbol(Name));
374 return teeLog(badObjectHandleError(H));
378 const std::string &Name) {
379 auto RI = Resolvers.find(H);
380 if (RI != Resolvers.end())
381 return this->jitSymbolToRemote(
382 RI->second->findSymbolInLogicalDylib(Name));
384 return teeLog(badObjectHandleError(H));
388 std::shared_ptr<LegacyJITSymbolResolver>>
398 template <
typename BaseLayerT,
typename RPCEndpo
int>
425 BaseLayer(BaseLayer), HandleIdMgr(1) {
428 Remote.template addHandler<AddObject>(*
this, &ThisT::addObject);
429 Remote.template addHandler<RemoveObject>(*
this, &ThisT::removeObject);
430 Remote.template addHandler<FindSymbol>(*
this, &ThisT::findSymbol);
431 Remote.template addHandler<FindSymbolIn>(*
this, &ThisT::findSymbolIn);
432 Remote.template addHandler<EmitAndFinalize>(*
this, &ThisT::emitAndFinalize);
439 StringMemoryBuffer(std::string Buffer)
440 : Buffer(std::move(Buffer)) {
441 init(this->Buffer.data(), this->Buffer.data() + this->Buffer.size(),
445 BufferKind getBufferKind()
const override {
return MemoryBuffer_Malloc; }
451 return remoteToJITSymbol(
452 this->Remote.template callB<Lookup>(Id, Name));
456 return remoteToJITSymbol(
457 this->Remote.template callB<LookupInLogicalDylib>(Id, Name));
461 auto Buffer = llvm::make_unique<StringMemoryBuffer>(std::move(ObjBuffer));
462 auto Id = HandleIdMgr.getNext();
463 assert(!BaseLayerHandles.count(Id) &&
"Id already in use?");
466 [
this, Id](
const std::string &Name) {
return lookup(Id, Name); },
467 [
this,
Id](
const std::string &
Name) {
468 return lookupInLogicalDylib(Id, Name);
471 if (
auto HandleOrErr =
472 BaseLayer.addObject(std::move(Buffer), std::move(Resolver))) {
473 BaseLayerHandles[
Id] = std::move(*HandleOrErr);
476 return teeLog(HandleOrErr.takeError());
480 auto HI = BaseLayerHandles.find(H);
481 if (
HI != BaseLayerHandles.end()) {
482 if (
auto Err = BaseLayer.removeObject(
HI->second))
483 return teeLog(std::move(Err));
486 return teeLog(badObjectHandleError(H));
490 bool ExportedSymbolsOnly) {
491 if (
auto Sym = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
492 return this->jitSymbolToRemote(std::move(Sym));
493 else if (
auto Err = Sym.takeError())
494 return teeLog(std::move(Err));
495 return this->nullRemoteSymbol();
499 bool ExportedSymbolsOnly) {
500 auto HI = BaseLayerHandles.find(H);
501 if (
HI != BaseLayerHandles.end()) {
502 if (
auto Sym = BaseLayer.findSymbolIn(
HI->second, Name, ExportedSymbolsOnly))
503 return this->jitSymbolToRemote(std::move(Sym));
504 else if (
auto Err = Sym.takeError())
505 return teeLog(std::move(Err));
506 return this->nullRemoteSymbol();
508 return teeLog(badObjectHandleError(H));
512 auto HI = BaseLayerHandles.find(H);
513 if (
HI != BaseLayerHandles.end()) {
514 if (
auto Err = BaseLayer.emitAndFinalize(
HI->second))
515 return teeLog(std::move(Err));
518 return teeLog(badObjectHandleError(H));
521 BaseLayerT &BaseLayer;
523 std::map<ObjHandleT, typename BaseLayerT::ObjHandleT> BaseLayerHandles;
529 #endif // LLVM_EXECUTIONENGINE_ORC_REMOTEOBJECTLAYER_H
Represents a symbol in the JIT.
This class represents lattice values for constants.
RemoteSymbol nullRemoteSymbol()
Convenience function for getting a null remote symbol value.
RemoteObjectClientLayer(RPCEndpoint &Remote, std::function< void(Error)> ReportError)
Create a RemoteObjectClientLayer that communicates with a RemoteObjectServerLayer instance via the gi...
Error badObjectHandleError(ObjHandleT H)
static const char * getName()
Error takeError()
Take ownership of the stored error.
RemoteSymbolMaterializer(const RemoteSymbolMaterializer &Other)
remote::ResourceIdMgr::ResourceId ObjHandleT
JITSymbolFlags getFlags() const
std::unique_ptr< MemoryBuffer > ObjectPtr
amdgpu Simplify well known AMD library false Value Value const Twine & Name
RemoteObjectLayer(RPCEndpoint &Remote, std::function< void(Error)> ReportError)
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
~RemoteSymbolMaterializer()
Release the remote symbol.
Tagged union holding either a T or a Error.
Error emitAndFinalize(ObjHandleT H)
Immediately emit and finalize the object with the given handle.
static const uint16_t * lookup(unsigned opcode, unsigned domain, ArrayRef< uint16_t[3]> Table)
static const char * getName()
static const char * getName()
static const char * getName()
JITSymbol remoteToJITSymbol(Expected< RemoteSymbol > RemoteSymOrErr)
Convert an Expected<RemoteSymbol> to a JITSymbol.
initializer< Ty > init(const Ty &Val)
Expected< RemoteSymbol > jitSymbolToRemote(JITSymbol Sym)
Create a RemoteSymbol wrapping the given JITSymbol.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
This class is used as the symbol materializer for JITSymbols returned by RemoteObjectLayerClient/Remo...
RemoteSymbolMaterializer(RemoteObjectLayer &C, RemoteSymbolId Id)
Construct a RemoteSymbolMaterializer for the given RemoteObjectLayer with the given Id...
Error badRemoteSymbolIdError(RemoteSymbolId Id)
static const RemoteSymbolId NullSymbolId
Flags for symbols in the JIT.
remote::ResourceIdMgr::ResourceId RemoteSymbolId
JITSymbol findSymbolIn(ObjHandleT H, StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol within the given context.
RPC API needed by RemoteObjectClientLayer and RemoteObjectServerLayer.
std::function< void(Error)> ReportError
Expected< ObjHandleT > addObject(ObjectPtr ObjBuffer, std::shared_ptr< LegacyJITSymbolResolver > Resolver)
Add an object to the JIT.
Template error for missing resources.
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
static ErrorSuccess success()
Create a success value.
std::pair< RemoteSymbolId, JITSymbolFlags > RemoteSymbol
Base class containing common utilities for RemoteObjectClientLayer and RemoteObjectServerLayer.
RemoteObjectServerLayer(BaseLayerT &BaseLayer, RPCEndpoint &Remote, std::function< void(Error)> ReportError)
Create a RemoteObjectServerLayer with the given base layer (which must be an object layer)...
RemoteObjectServerLayer acts as a server and handling RPC calls for the object layer API from the giv...
static const char * getName()
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static const char * getName()
static const char * getName()
static const char * getName()
Error teeLog(Error Err)
Creates a StringError that contains a copy of Err's log message, then sends that StringError to Repor...
static const ObjHandleT InvalidObjectHandleId
RemoteObjectClientLayer forwards the ORC Object Layer API over an RPC connection. ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Error removeObject(ObjHandleT H)
Remove the given object from the JIT.
std::shared_ptr< LambdaResolver< DylibLookupFtorT, ExternalLookupFtorT > > createLambdaResolver(DylibLookupFtorT DylibLookupFtor, ExternalLookupFtorT ExternalLookupFtor)
Lightweight error class with error context and mandatory checking.
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Error takeError()
Move the error field value out of this JITSymbol.
print Print MemDeps of function
static const char * getName()
StringRef - Represent a constant reference to a string, i.e.
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset, const char *ErrorMsg)
Expected< JITTargetAddress > materialize()
Materialize the symbol on the remote and get its address.