From 90e330dbb3550860486f69d0b8eac550d36b294c Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Tue, 24 Jan 2017 05:30:08 +0000 Subject: [PATCH] [Orc][RPC] Refactor some common remote-function-id negotiation code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@292886 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ExecutionEngine/Orc/RPCUtils.h | 114 ++++++-------------- 1 file changed, 33 insertions(+), 81 deletions(-) diff --git a/include/llvm/ExecutionEngine/Orc/RPCUtils.h b/include/llvm/ExecutionEngine/Orc/RPCUtils.h index b6945b50bd9..fcebc418f4c 100644 --- a/include/llvm/ExecutionEngine/Orc/RPCUtils.h +++ b/include/llvm/ExecutionEngine/Orc/RPCUtils.h @@ -762,6 +762,12 @@ public: LaunchPolicy()); } + + /// Negotiate a function id for Func with the other end of the channel. + template Error negotiateFunction(bool Retry = false) { + return getRemoteFunctionId(true, Retry).takeError(); + } + /// Append a call Func, does not call send on the channel. /// The first argument specifies a user-defined handler to be run when the /// function returns. The handler should take an Expected, @@ -777,7 +783,7 @@ public: // Look up the function ID. FunctionIdT FnId; - if (auto FnIdOrErr = getRemoteFunctionId()) + if (auto FnIdOrErr = getRemoteFunctionId(LazyAutoNegotiation, false)) FnId = *FnIdOrErr; else { // This isn't a channel error so we don't want to abandon other pending @@ -969,41 +975,39 @@ protected: return I->second; } - // Find the remote FunctionId for the given function, which must be in the - // RemoteFunctionIds map. - template Expected getRemoteFunctionId() { - // Try to find the id for the given function. - auto I = RemoteFunctionIds.find(Func::getPrototype()); + // Find the remote FunctionId for the given function. + template + Expected getRemoteFunctionId(bool NegotiateIfNotInMap, + bool NegotiateIfInvalid) { + bool DoNegotiate; - // If we have it in the map, return it. - if (I != RemoteFunctionIds.end()) - return I->second; + // Check if we already have a function id... + auto I = RemoteFunctionIds.find(Func::getPrototype()); + if (I != RemoteFunctionIds.end()) { + // If it's valid there's nothing left to do. + if (I->second != getInvalidFunctionId()) + return I->second; + DoNegotiate = NegotiateIfInvalid; + } else + DoNegotiate = NegotiateIfNotInMap; - // Otherwise, if we have auto-negotiation enabled, try to negotiate it. - if (LazyAutoNegotiation) { + // We don't have a function id for Func yet, but we're allowed to try to + // negotiate one. + if (DoNegotiate) { auto &Impl = static_cast(*this); if (auto RemoteIdOrErr = - Impl.template callB(Func::getPrototype())) { - auto &RemoteId = *RemoteIdOrErr; - - // If autonegotiation indicates that the remote end doesn't support this - // function, return an unknown function error. - if (RemoteId == getInvalidFunctionId()) - return orcError(OrcErrorCode::UnknownRPCFunction); - - // Autonegotiation succeeded and returned a valid id. Update the map and - // return the id. - RemoteFunctionIds[Func::getPrototype()] = RemoteId; - return RemoteId; - } else { - // Autonegotiation failed. Return the error. + Impl.template callB(Func::getPrototype())) { + RemoteFunctionIds[Func::getPrototype()] = *RemoteIdOrErr; + if (*RemoteIdOrErr == getInvalidFunctionId()) + return make_error(Func::getPrototype()); + return *RemoteIdOrErr; + } else return RemoteIdOrErr.takeError(); - } } - // No key was available in the map and autonegotiation wasn't enabled. - // Return an unknown function error. - return orcError(OrcErrorCode::UnknownRPCFunction); + // No key was available in the map and we weren't allowed to try to + // negotiate one, so return an unknown function error. + return make_error(Func::getPrototype()); } using WrappedHandlerFn = std::function; @@ -1122,32 +1126,6 @@ public: Launch); } - /// Negotiate a function id for Func with the other end of the channel. - template Error negotiateFunction(bool Retry = false) { - using OrcRPCNegotiate = typename BaseClass::OrcRPCNegotiate; - - // Check if we already have a function id... - auto I = this->RemoteFunctionIds.find(Func::getPrototype()); - if (I != this->RemoteFunctionIds.end()) { - // If it's valid there's nothing left to do. - if (I->second != this->getInvalidFunctionId()) - return Error::success(); - // If it's invalid and we can't re-attempt negotiation, throw an error. - if (!Retry) - return make_error(Func::getPrototype()); - } - - // We don't have a function id for Func yet, call the remote to try to - // negotiate one. - if (auto RemoteIdOrErr = callB(Func::getPrototype())) { - this->RemoteFunctionIds[Func::getPrototype()] = *RemoteIdOrErr; - if (*RemoteIdOrErr == this->getInvalidFunctionId()) - return make_error(Func::getPrototype()); - return Error::success(); - } else - return RemoteIdOrErr.takeError(); - } - /// Return type for non-blocking call primitives. template using NonBlockingCallResult = typename detail::ResultTraits< @@ -1260,32 +1238,6 @@ public: detail::MemberFnWrapper(Object, Method)); } - /// Negotiate a function id for Func with the other end of the channel. - template Error negotiateFunction(bool Retry = false) { - using OrcRPCNegotiate = typename BaseClass::OrcRPCNegotiate; - - // Check if we already have a function id... - auto I = this->RemoteFunctionIds.find(Func::getPrototype()); - if (I != this->RemoteFunctionIds.end()) { - // If it's valid there's nothing left to do. - if (I->second != this->getInvalidFunctionId()) - return Error::success(); - // If it's invalid and we can't re-attempt negotiation, throw an error. - if (!Retry) - return make_error(Func::getPrototype()); - } - - // We don't have a function id for Func yet, call the remote to try to - // negotiate one. - if (auto RemoteIdOrErr = callB(Func::getPrototype())) { - this->RemoteFunctionIds[Func::getPrototype()] = *RemoteIdOrErr; - if (*RemoteIdOrErr == this->getInvalidFunctionId()) - return make_error(Func::getPrototype()); - return Error::success(); - } else - return RemoteIdOrErr.takeError(); - } - template typename detail::ResultTraits::ErrorReturnType -- 2.40.0