[Orc] Fix copy elision warning in RPCUtils
authorStefan Gränitz <stefan.graenitz@gmail.com>
Fri, 19 Mar 2021 13:46:05 +0000 (14:46 +0100)
committerStefan Gränitz <stefan.graenitz@gmail.com>
Mon, 22 Mar 2021 16:47:33 +0000 (17:47 +0100)
The `callB()` template function always moved errors on return, because in the majority of cases its return type is an `Expected<T>` and the error must be moved into the implicit ctor.
For the special case of a `void` result, however, the `ResultTraits` class is specialized and the return type is a raw `Error`. Some build bots complain, that in favor of NRVO errors should not be moved in this case.

```
llvm/include/llvm/ExecutionEngine/Orc/Shared/RPCUtils.h:1513:27:
llvm/include/llvm/ExecutionEngine/Orc/Shared/RPCUtils.h:1519:27:
llvm/include/llvm/ExecutionEngine/Orc/Shared/RPCUtils.h:1526:29:
  warning: moving a local object in a return statement prevents copy elision [-Wpessimizing-move]
```

The warning is reasonable from a type-system point of view. For performance it's entirely insignificant.

Differential Revision: https://reviews.llvm.org/D98947

llvm/include/llvm/ExecutionEngine/Orc/Shared/RPCUtils.h

index e0ac640..1ff47ce 100644 (file)
@@ -243,6 +243,8 @@ public:
   static void consumeAbandoned(ErrorReturnType RetOrErr) {
     consumeError(RetOrErr.takeError());
   }
+
+  static ErrorReturnType returnError(Error Err) { return std::move(Err); }
 };
 
 // ResultTraits specialization for void functions.
@@ -275,6 +277,8 @@ public:
   static void consumeAbandoned(ErrorReturnType Err) {
     consumeError(std::move(Err));
   }
+
+  static ErrorReturnType returnError(Error Err) { return Err; }
 };
 
 // ResultTraits<Error> is equivalent to ResultTraits<void>. This allows
@@ -1494,36 +1498,34 @@ public:
   typename detail::ResultTraits<AltRetT>::ErrorReturnType
   callB(const ArgTs &...Args) {
     bool ReceivedResponse = false;
-    using ResultType = typename detail::ResultTraits<AltRetT>::ErrorReturnType;
-    auto Result = detail::ResultTraits<AltRetT>::createBlankErrorReturnValue();
+    using AltRetTraits = detail::ResultTraits<AltRetT>;
+    using ResultType = typename AltRetTraits::ErrorReturnType;
+    ResultType Result = AltRetTraits::createBlankErrorReturnValue();
 
     // We have to 'Check' result (which we know is in a success state at this
     // point) so that it can be overwritten in the async handler.
     (void)!!Result;
 
-    if (auto Err = this->template appendCallAsync<Func>(
+    if (Error Err = this->template appendCallAsync<Func>(
             [&](ResultType R) {
               Result = std::move(R);
               ReceivedResponse = true;
               return Error::success();
             },
             Args...)) {
-      detail::ResultTraits<typename Func::ReturnType>::consumeAbandoned(
-          std::move(Result));
-      return std::move(Err);
+      AltRetTraits::consumeAbandoned(std::move(Result));
+      return AltRetTraits::returnError(std::move(Err));
     }
 
-    if (auto Err = this->C.send()) {
-      detail::ResultTraits<typename Func::ReturnType>::consumeAbandoned(
-          std::move(Result));
-      return std::move(Err);
+    if (Error Err = this->C.send()) {
+      AltRetTraits::consumeAbandoned(std::move(Result));
+      return AltRetTraits::returnError(std::move(Err));
     }
 
     while (!ReceivedResponse) {
-      if (auto Err = this->handleOne()) {
-        detail::ResultTraits<typename Func::ReturnType>::consumeAbandoned(
-            std::move(Result));
-        return std::move(Err);
+      if (Error Err = this->handleOne()) {
+        AltRetTraits::consumeAbandoned(std::move(Result));
+        return AltRetTraits::returnError(std::move(Err));
       }
     }