[llvm] [orc-rt] Refactor WrapperFunction to simplify Serializer classes. (PR #161763)

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 2 19:07:52 PDT 2025


https://github.com/lhames created https://github.com/llvm/llvm-project/pull/161763

Serializers only need to provide two methods now, rather than four. The first method should return an argument serializer / deserializer, the second a result value serializer / deserializer. The interfaces for these are now more uniform (deserialize now returns a tuple, rather than taking its output location(s) by reference). The intent is to simplify Serializer helper code. NFCI.

>From ebaa151b976fda05e547c73ffcd6143f253b70d2 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Thu, 2 Oct 2025 15:52:32 +1000
Subject: [PATCH] [orc-rt] Refactor WrapperFunction to simplify Serializer
 classes.

Serializers only need to provide two methods now, rather than four. The first
method should return an argument serializer / deserializer, the second a result
value serializer / deserializer. The interfaces for these are now more uniform
(deserialize now returns a tuple, rather than taking its output location(s) by
reference). The intent is to simplify Serializer helper code. NFCI.
---
 orc-rt/include/orc-rt/SPSWrapperFunction.h | 31 ++++++++--------------
 orc-rt/include/orc-rt/WrapperFunction.h    | 17 ++++++------
 2 files changed, 19 insertions(+), 29 deletions(-)

diff --git a/orc-rt/include/orc-rt/SPSWrapperFunction.h b/orc-rt/include/orc-rt/SPSWrapperFunction.h
index d08176f676289..3ea6406b69a37 100644
--- a/orc-rt/include/orc-rt/SPSWrapperFunction.h
+++ b/orc-rt/include/orc-rt/SPSWrapperFunction.h
@@ -20,9 +20,9 @@
 namespace orc_rt {
 namespace detail {
 
-template <typename... SPSArgTs> struct WFSPSSerializer {
+template <typename... SPSArgTs> struct WFSPSHelper {
   template <typename... ArgTs>
-  std::optional<WrapperFunctionBuffer> operator()(const ArgTs &...Args) {
+  std::optional<WrapperFunctionBuffer> serialize(const ArgTs &...Args) {
     auto R =
         WrapperFunctionBuffer::allocate(SPSArgList<SPSArgTs...>::size(Args...));
     SPSOutputBuffer OB(R.data(), R.size());
@@ -30,15 +30,17 @@ template <typename... SPSArgTs> struct WFSPSSerializer {
       return std::nullopt;
     return std::move(R);
   }
-};
 
-template <typename... SPSArgTs> struct WFSPSDeserializer {
-  template <typename... ArgTs>
-  bool operator()(WrapperFunctionBuffer &ArgBytes, ArgTs &...Args) {
+  template <typename ArgTuple>
+  std::optional<ArgTuple> deserialize(WrapperFunctionBuffer ArgBytes) {
     assert(!ArgBytes.getOutOfBandError() &&
            "Should not attempt to deserialize out-of-band error");
     SPSInputBuffer IB(ArgBytes.data(), ArgBytes.size());
-    return SPSArgList<SPSArgTs...>::deserialize(IB, Args...);
+    ArgTuple Args;
+    if (!SPSSerializationTraits<SPSTuple<SPSArgTs...>, ArgTuple>::deserialize(
+            IB, Args))
+      return std::nullopt;
+    return Args;
   }
 };
 
@@ -48,19 +50,8 @@ template <typename SPSSig> struct WrapperFunctionSPSSerializer;
 
 template <typename SPSRetT, typename... SPSArgTs>
 struct WrapperFunctionSPSSerializer<SPSRetT(SPSArgTs...)> {
-  static detail::WFSPSSerializer<SPSArgTs...> argumentSerializer() noexcept {
-    return {};
-  }
-  static detail::WFSPSDeserializer<SPSArgTs...>
-  argumentDeserializer() noexcept {
-    return {};
-  }
-  static detail::WFSPSSerializer<SPSRetT> resultSerializer() noexcept {
-    return {};
-  }
-  static detail::WFSPSDeserializer<SPSRetT> resultDeserializer() noexcept {
-    return {};
-  }
+  static detail::WFSPSHelper<SPSArgTs...> arguments() noexcept { return {}; }
+  static detail::WFSPSHelper<SPSRetT> result() noexcept { return {}; }
 };
 
 /// Provides call and handle utilities to simplify writing and invocation of
diff --git a/orc-rt/include/orc-rt/WrapperFunction.h b/orc-rt/include/orc-rt/WrapperFunction.h
index 41960d24de04e..233c3b21e041d 100644
--- a/orc-rt/include/orc-rt/WrapperFunction.h
+++ b/orc-rt/include/orc-rt/WrapperFunction.h
@@ -139,7 +139,7 @@ class StructuredYield<std::tuple<RetT>, Serializer>
 public:
   using StructuredYieldBase<Serializer>::StructuredYieldBase;
   void operator()(RetT &&R) {
-    if (auto ResultBytes = this->S.resultSerializer()(std::forward<RetT>(R)))
+    if (auto ResultBytes = this->S.result().serialize(std::forward<RetT>(R)))
       this->Return(this->Session, this->CallCtx, ResultBytes->release());
     else
       this->Return(this->Session, this->CallCtx,
@@ -166,9 +166,9 @@ template <typename T, typename Serializer>
 struct ResultDeserializer<std::tuple<Expected<T>>, Serializer> {
   static Expected<T> deserialize(WrapperFunctionBuffer ResultBytes,
                                  Serializer &S) {
-    T Val;
-    if (S.resultDeserializer()(ResultBytes, Val))
-      return std::move(Val);
+    if (auto Val = S.result().template deserialize<std::tuple<T>>(
+            std::move(ResultBytes)))
+      return std::move(std::get<0>(*Val));
     else
       return make_error<StringError>("Could not deserialize result");
   }
@@ -205,7 +205,7 @@ struct WrapperFunction {
         "Result-handler should have exactly one argument");
     typedef typename ResultHandlerTraits::args_tuple_type ResultTupleType;
 
-    if (auto ArgBytes = S.argumentSerializer()(std::forward<ArgTs>(Args)...)) {
+    if (auto ArgBytes = S.arguments().serialize(std::forward<ArgTs>(Args)...)) {
       C(
           [RH = std::move(RH),
            S = std::move(S)](orc_rt_SessionRef Session,
@@ -241,13 +241,12 @@ struct WrapperFunction {
     if (ArgBytes.getOutOfBandError())
       return Return(Session, CallCtx, ArgBytes.release());
 
-    ArgTuple Args;
-    if (std::apply(bind_front(S.argumentDeserializer(), std::move(ArgBytes)),
-                   Args))
+    if (auto Args =
+            S.arguments().template deserialize<ArgTuple>(std::move(ArgBytes)))
       std::apply(bind_front(std::forward<Handler>(H),
                             detail::StructuredYield<RetTupleType, Serializer>(
                                 Session, CallCtx, Return, std::move(S))),
-                 std::move(Args));
+                 std::move(*Args));
     else
       Return(Session, CallCtx,
              WrapperFunctionBuffer::createOutOfBandError(



More information about the llvm-commits mailing list