[llvm] [orc-rt] Enable span<const char> use in SPSWrapperFunctions. (PR #162792)

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 10 00:01:26 PDT 2025


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

SPS deserialization for span<const char> produces a value that points directly into the argument buffer for efficiency. This was broken by an unnecessary std::move of the buffer inside WrapperFunction, which this commit removes.

>From 6d60b8b7f1f51a12b8d12d190687465845a2c806 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Fri, 10 Oct 2025 17:54:50 +1100
Subject: [PATCH] [orc-rt] Enable span<const char> use in SPSWrapperFunctions.

SPS deserialization for span<const char> produces a value that points
directly into the argument buffer for efficiency. This was broken by an
unnecessary std::move of the buffer inside WrapperFunction, which this
commit removes.
---
 orc-rt/include/orc-rt/SPSWrapperFunction.h  |  2 +-
 orc-rt/include/orc-rt/WrapperFunction.h     |  3 +--
 orc-rt/unittests/SPSWrapperFunctionTest.cpp | 23 +++++++++++++++++++++
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/orc-rt/include/orc-rt/SPSWrapperFunction.h b/orc-rt/include/orc-rt/SPSWrapperFunction.h
index 824e009306e2a..dc688223d9924 100644
--- a/orc-rt/include/orc-rt/SPSWrapperFunction.h
+++ b/orc-rt/include/orc-rt/SPSWrapperFunction.h
@@ -110,7 +110,7 @@ template <typename... SPSArgTs> struct WFSPSHelper {
   }
 
   template <typename ArgTuple>
-  std::optional<ArgTuple> deserialize(WrapperFunctionBuffer ArgBytes) {
+  std::optional<ArgTuple> deserialize(const WrapperFunctionBuffer &ArgBytes) {
     assert(!ArgBytes.getOutOfBandError() &&
            "Should not attempt to deserialize out-of-band error");
     SPSInputBuffer IB(ArgBytes.data(), ArgBytes.size());
diff --git a/orc-rt/include/orc-rt/WrapperFunction.h b/orc-rt/include/orc-rt/WrapperFunction.h
index c43f1c5b4a753..6e8b84e980dc0 100644
--- a/orc-rt/include/orc-rt/WrapperFunction.h
+++ b/orc-rt/include/orc-rt/WrapperFunction.h
@@ -382,8 +382,7 @@ struct WrapperFunction {
     if (ArgBytes.getOutOfBandError())
       return Return(Session, CallCtx, ArgBytes.release());
 
-    if (auto Args =
-            S.arguments().template deserialize<ArgTuple>(std::move(ArgBytes)))
+    if (auto Args = S.arguments().template deserialize<ArgTuple>(ArgBytes))
       std::apply(HandlerTraits::forwardArgsAsRequested(bind_front(
                      std::forward<Handler>(H),
                      detail::StructuredYield<RetTupleType, Serializer>(
diff --git a/orc-rt/unittests/SPSWrapperFunctionTest.cpp b/orc-rt/unittests/SPSWrapperFunctionTest.cpp
index d0f06e8d31451..ed085f2fef76c 100644
--- a/orc-rt/unittests/SPSWrapperFunctionTest.cpp
+++ b/orc-rt/unittests/SPSWrapperFunctionTest.cpp
@@ -95,6 +95,29 @@ TEST(SPSWrapperFunctionUtilsTest, BinaryOpViaFunctionPointer) {
   EXPECT_EQ(Result, 42);
 }
 
+static void
+round_trip_string_via_span_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx,
+                                       orc_rt_WrapperFunctionReturn Return,
+                                       orc_rt_WrapperFunctionBuffer ArgBytes) {
+  SPSWrapperFunction<SPSString(SPSString)>::handle(
+      Session, CallCtx, Return, ArgBytes,
+      [](move_only_function<void(std::string)> Return, span<const char> S) {
+        Return({S.data(), S.size()});
+      });
+}
+
+TEST(SPSWrapperFunctionUtilsTest, RoundTripStringViaSpan) {
+  /// Test that the SPSWrapperFunction<...>::handle call in
+  /// round_trip_string_via_span_sps_wrapper can deserialize into a usable
+  /// span<const char>.
+  std::string Result;
+  SPSWrapperFunction<SPSString(SPSString)>::call(
+      DirectCaller(nullptr, round_trip_string_via_span_sps_wrapper),
+      [&](Expected<std::string> R) { Result = cantFail(std::move(R)); },
+      std::string_view("hello, world!"));
+  EXPECT_EQ(Result, "hello, world!");
+}
+
 static void improbable_feat_sps_wrapper(orc_rt_SessionRef Session,
                                         void *CallCtx,
                                         orc_rt_WrapperFunctionReturn Return,



More information about the llvm-commits mailing list