diff --git a/include/llvm/ExecutionEngine/Orc/RPCUtils.h b/include/llvm/ExecutionEngine/Orc/RPCUtils.h index 966a4968434..2e7b7b8b25c 100644 --- a/include/llvm/ExecutionEngine/Orc/RPCUtils.h +++ b/include/llvm/ExecutionEngine/Orc/RPCUtils.h @@ -17,7 +17,6 @@ #include #include -#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ExecutionEngine/Orc/OrcError.h" @@ -61,6 +60,71 @@ public: // partially specialized. class RPCBase { protected: + + // FIXME: Remove MSVCPError/MSVCPExpected once MSVC's future implementation + // supports classes without default constructors. +#ifdef _MSC_VER + + // Work around MSVC's future implementation's use of default constructors: + // A default constructed value in the promise will be overwritten when the + // real error is set - so the default constructed Error has to be checked + // already. + class MSVCPError : public Error { + public: + + MSVCPError() { + (void)!!*this; + } + + MSVCPError(MSVCPError &&Other) : Error(std::move(Other)) {} + + MSVCPError& operator=(MSVCPError Other) { + Error::operator=(std::move(Other)); + return *this; + } + + MSVCPError(Error Err) : Error(std::move(Err)) {} + }; + + // Likewise for Expected: + template + class MSVCPExpected : public Expected { + public: + + MSVCPExpected() + : Expected(make_error("", inconvertibleErrorCode())) { + consumeError(this->takeError()); + } + + MSVCPExpected(MSVCPExpected &&Other) : Expected(std::move(Other)) {} + + MSVCPExpected& operator=(MSVCPExpected &&Other) { + Expected::operator=(std::move(Other)); + return *this; + } + + MSVCPExpected(Error Err) : Expected(std::move(Err)) {} + + template + MSVCPExpected(OtherT &&Val, + typename std::enable_if::value>::type + * = nullptr) : Expected(std::move(Val)) {} + + template + MSVCPExpected( + Expected &&Other, + typename std::enable_if::value>::type * = + nullptr) : Expected(std::move(Other)) {} + + template + explicit MSVCPExpected( + Expected &&Other, + typename std::enable_if::value>::type * = + nullptr) : Expected(std::move(Other)) {} + }; + +#endif // _MSC_VER + // RPC Function description type. // // This class provides the information and operations needed to support the @@ -69,12 +133,9 @@ protected: // betwen the two. Both specializations have the same interface: // // Id - The function's unique identifier. - // OptionalReturn - The return type for asyncronous calls. - // ErrorReturn - The return type for synchronous calls. - // optionalToErrorReturn - Conversion from a valid OptionalReturn to an - // ErrorReturn. + // ErrorReturn - The return type for blocking calls. // readResult - Deserialize a result from a channel. - // abandon - Abandon a promised (asynchronous) result. + // abandon - Abandon a promised result. // respond - Retun a result on the channel. template class FunctionHelper {}; @@ -91,32 +152,38 @@ protected: static const FunctionIdT Id = FuncId; - typedef Optional OptionalReturn; - typedef Expected ErrorReturn; - static ErrorReturn optionalToErrorReturn(OptionalReturn &&V) { - assert(V && "Return value not available"); - return std::move(*V); - } + // FIXME: Ditch PErrorReturn (replace it with plain ErrorReturn) once MSVC's + // std::future implementation supports types without default + // constructors. +#ifdef _MSC_VER + typedef MSVCPExpected PErrorReturn; +#else + typedef Expected PErrorReturn; +#endif template - static Error readResult(ChannelT &C, std::promise &P) { + static Error readResult(ChannelT &C, std::promise &P) { RetT Val; auto Err = deserialize(C, Val); auto Err2 = endReceiveMessage(C); Err = joinErrors(std::move(Err), std::move(Err2)); - - if (Err) { - P.set_value(OptionalReturn()); + if (Err) return Err; - } + P.set_value(std::move(Val)); return Error::success(); } - static void abandon(std::promise &P) { - P.set_value(OptionalReturn()); + static void abandon(std::promise &P) { + P.set_value( + make_error("RPC function call failed to return", + inconvertibleErrorCode())); + } + + static void consumeAbandoned(std::future &P) { + consumeError(P.get().takeError()); } template @@ -148,22 +215,33 @@ protected: static const FunctionIdT Id = FuncId; - typedef bool OptionalReturn; typedef Error ErrorReturn; - static ErrorReturn optionalToErrorReturn(OptionalReturn &&V) { - assert(V && "Return value not available"); - return Error::success(); - } + // FIXME: Ditch PErrorReturn (replace it with plain ErrorReturn) once MSVC's + // std::future implementation supports types without default + // constructors. +#ifdef _MSC_VER + typedef MSVCPError PErrorReturn; +#else + typedef Error PErrorReturn; +#endif template - static Error readResult(ChannelT &C, std::promise &P) { + static Error readResult(ChannelT &C, std::promise &P) { // Void functions don't have anything to deserialize, so we're good. - P.set_value(true); + P.set_value(Error::success()); return endReceiveMessage(C); } - static void abandon(std::promise &P) { P.set_value(false); } + static void abandon(std::promise &P) { + P.set_value( + make_error("RPC function call failed to return", + inconvertibleErrorCode())); + } + + static void consumeAbandoned(std::future &P) { + consumeError(P.get()); + } template static Error respond(ChannelT &C, SequenceNumberT SeqNo, @@ -378,30 +456,27 @@ public: template using Function = FunctionHelper; - /// Return type for asynchronous call primitives. + /// Return type for non-blocking call primitives. template - using AsyncCallResult = std::future; + using NonBlockingCallResult = std::future; - /// Return type for asynchronous call-with-seq primitives. + /// Return type for non-blocking call-with-seq primitives. template - using AsyncCallWithSeqResult = - std::pair, SequenceNumberT>; + using NonBlockingCallWithSeqResult = + std::pair, SequenceNumberT>; - /// Serialize Args... to channel C, but do not call C.send(). - /// - /// Returns an error (on serialization failure) or a pair of: - /// (1) A future Optional (or future for void functions), and - /// (2) A sequence number. + /// Call Func on Channel C. Does not block, does not call send. Returns a pair + /// of a future result and the sequence number assigned to the result. /// /// This utility function is primarily used for single-threaded mode support, /// where the sequence number can be used to wait for the corresponding - /// result. In multi-threaded mode the appendCallAsync method, which does not + /// result. In multi-threaded mode the appendCallNB method, which does not /// return the sequence numeber, should be preferred. template - Expected> - appendCallAsyncWithSeq(ChannelT &C, const ArgTs &... Args) { + Expected> + appendCallNBWithSeq(ChannelT &C, const ArgTs &... Args) { auto SeqNo = SequenceNumberMgr.getSequenceNumber(); - std::promise Promise; + std::promise Promise; auto Result = Promise.get_future(); OutstandingResults[SeqNo] = createOutstandingResult(std::move(Promise)); @@ -409,21 +484,23 @@ public: if (auto Err = CallHelper::call(C, SeqNo, Args...)) { abandonOutstandingResults(); + Func::consumeAbandoned(Result); return std::move(Err); } else - return AsyncCallWithSeqResult(std::move(Result), SeqNo); + return NonBlockingCallWithSeqResult(std::move(Result), SeqNo); } - /// The same as appendCallAsyncWithSeq, except that it calls C.send() to + /// The same as appendCallNBWithSeq, except that it calls C.send() to /// flush the channel after serializing the call. template - Expected> - callAsyncWithSeq(ChannelT &C, const ArgTs &... Args) { - auto Result = appendCallAsyncWithSeq(C, Args...); + Expected> + callNBWithSeq(ChannelT &C, const ArgTs &... Args) { + auto Result = appendCallNBWithSeq(C, Args...); if (!Result) return Result; if (auto Err = C.send()) { abandonOutstandingResults(); + Func::consumeAbandoned(Result->first); return std::move(Err); } return Result; @@ -431,41 +508,66 @@ public: /// Serialize Args... to channel C, but do not call send. /// Returns an error if serialization fails, otherwise returns a - /// std::future> (or a future for void functions). + /// std::future> (or a future for void functions). template - Expected> appendCallAsync(ChannelT &C, - const ArgTs &... Args) { - auto ResAndSeqOrErr = appendCallAsyncWithSeq(C, Args...); - if (ResAndSeqOrErr) - return std::move(ResAndSeqOrErr->first); - return ResAndSeqOrErr.getError(); + Expected> appendCallNB(ChannelT &C, + const ArgTs &... Args) { + auto FutureResAndSeqOrErr = appendCallNBWithSeq(C, Args...); + if (FutureResAndSeqOrErr) + return std::move(FutureResAndSeqOrErr->first); + return FutureResAndSeqOrErr.takeError(); } - /// The same as appendCallAsync, except that it calls C.send to flush the + /// The same as appendCallNB, except that it calls C.send to flush the /// channel after serializing the call. template - Expected> callAsync(ChannelT &C, - const ArgTs &... Args) { - auto ResAndSeqOrErr = callAsyncWithSeq(C, Args...); - if (ResAndSeqOrErr) - return std::move(ResAndSeqOrErr->first); - return ResAndSeqOrErr.getError(); + Expected> callNB(ChannelT &C, + const ArgTs &... Args) { + auto FutureResAndSeqOrErr = callNBWithSeq(C, Args...); + if (FutureResAndSeqOrErr) + return std::move(FutureResAndSeqOrErr->first); + return FutureResAndSeqOrErr.takeError(); } - /// This can be used in single-threaded mode. + /// Call Func on Channel C. Blocks waiting for a result. Returns an Error + /// for void functions or an Expected for functions returning a T. + /// + /// This function is for use in threaded code where another thread is + /// handling responses and incoming calls. + template + typename Func::ErrorReturn callB(ChannelT &C, const ArgTs &... Args) { + if (auto FutureResOrErr = callNBWithSeq(C, Args...)) { + if (auto Err = C.send()) { + abandonOutstandingResults(); + Func::consumeAbandoned(*FutureResOrErr); + return std::move(Err); + } + return FutureResOrErr->get(); + } else + return FutureResOrErr.takeError(); + } + + /// Call Func on Channel C. Block waiting for a result. While blocked, run + /// HandleOther to handle incoming calls (Response calls will be handled + /// implicitly before calling HandleOther). Returns an Error for void + /// functions or an Expected for functions returning a T. + /// + /// This function is for use in single threaded mode when the calling thread + /// must act as both sender and receiver. template typename Func::ErrorReturn callSTHandling(ChannelT &C, HandleFtor &HandleOther, const ArgTs &... Args) { - if (auto ResultAndSeqNoOrErr = callAsyncWithSeq(C, Args...)) { + if (auto ResultAndSeqNoOrErr = callNBWithSeq(C, Args...)) { auto &ResultAndSeqNo = *ResultAndSeqNoOrErr; if (auto Err = waitForResult(C, ResultAndSeqNo.second, HandleOther)) return std::move(Err); - return Func::optionalToErrorReturn(ResultAndSeqNo.first.get()); + return ResultAndSeqNo.first.get(); } else return ResultAndSeqNoOrErr.takeError(); } - // This can be used in single-threaded mode. + /// Call Func on Channel C. Block waiting for a result. Returns an Error for + /// void functions or an Expected for functions returning a T. template typename Func::ErrorReturn callST(ChannelT &C, const ArgTs &... Args) { return callSTHandling(C, handleNone, Args...); @@ -656,7 +758,7 @@ private: class OutstandingResultImpl : public OutstandingResult { private: public: - OutstandingResultImpl(std::promise &&P) + OutstandingResultImpl(std::promise &&P) : P(std::move(P)) {} Error readResult(ChannelT &C) override { return Func::readResult(C, P); } @@ -664,13 +766,13 @@ private: void abandon() override { Func::abandon(P); } private: - std::promise P; + std::promise P; }; // Create an outstanding result for the given function. template std::unique_ptr - createOutstandingResult(std::promise &&P) { + createOutstandingResult(std::promise &&P) { return llvm::make_unique>(std::move(P)); } diff --git a/unittests/ExecutionEngine/Orc/RPCUtilsTest.cpp b/unittests/ExecutionEngine/Orc/RPCUtilsTest.cpp index 7d55641e4ce..babea5ca73a 100644 --- a/unittests/ExecutionEngine/Orc/RPCUtilsTest.cpp +++ b/unittests/ExecutionEngine/Orc/RPCUtilsTest.cpp @@ -83,7 +83,7 @@ TEST_F(DummyRPC, TestAsyncVoidBool) { QueueChannel C2(Q2, Q1); // Make an async call. - auto ResOrErr = callAsyncWithSeq(C1, true); + auto ResOrErr = callNBWithSeq(C1, true); EXPECT_TRUE(!!ResOrErr) << "Simple call over queue failed"; { @@ -102,8 +102,8 @@ TEST_F(DummyRPC, TestAsyncVoidBool) { } // Verify that the function returned ok. - auto Val = ResOrErr->first.get(); - EXPECT_TRUE(Val) << "Remote void function failed to execute."; + auto Err = ResOrErr->first.get(); + EXPECT_FALSE(!!Err) << "Remote void function failed to execute."; } TEST_F(DummyRPC, TestAsyncIntInt) { @@ -112,7 +112,7 @@ TEST_F(DummyRPC, TestAsyncIntInt) { QueueChannel C2(Q2, Q1); // Make an async call. - auto ResOrErr = callAsyncWithSeq(C1, 21); + auto ResOrErr = callNBWithSeq(C1, 21); EXPECT_TRUE(!!ResOrErr) << "Simple call over queue failed"; { @@ -143,7 +143,7 @@ TEST_F(DummyRPC, TestSerialization) { // Make a call to Proc1. std::vector v({42, 7}); - auto ResOrErr = callAsyncWithSeq( + auto ResOrErr = callNBWithSeq( C1, -101, 250, -10000, 10000, -1000000000, 1000000000, -10000000000, 10000000000, true, "foo", v); EXPECT_TRUE(!!ResOrErr) << "Big (serialization test) call over queue failed"; @@ -179,8 +179,8 @@ TEST_F(DummyRPC, TestSerialization) { } // Verify that the function returned ok. - auto Val = ResOrErr->first.get(); - EXPECT_TRUE(Val) << "Remote void function failed to execute."; + auto Err = ResOrErr->first.get(); + EXPECT_FALSE(!!Err) << "Remote void function failed to execute."; } // Test the synchronous call API.