[llvm] 50e499a - [Orc] Fix copy elision warning in RPCUtils

Stefan Gränitz via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 22 09:48:02 PDT 2021


Author: Stefan Gränitz
Date: 2021-03-22T17:47:33+01:00
New Revision: 50e499a56d66d8041d39ef1f8047533d50c2d165

URL: https://github.com/llvm/llvm-project/commit/50e499a56d66d8041d39ef1f8047533d50c2d165
DIFF: https://github.com/llvm/llvm-project/commit/50e499a56d66d8041d39ef1f8047533d50c2d165.diff

LOG: [Orc] Fix copy elision warning in RPCUtils

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/RPCUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/RPCUtils.h
index e0ac640ebcdd0..1ff47ce427585 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/RPCUtils.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/RPCUtils.h
@@ -243,6 +243,8 @@ template <typename RetT> class ResultTraits {
   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 @@ template <> class ResultTraits<void> {
   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 @@ class SingleThreadedRPCEndpoint
   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));
       }
     }
 


        


More information about the llvm-commits mailing list