Skip to content

Commit 28b3f2f

Browse files
authored
[orc-rt] Add SPSExecutorAddr <-> T* serialization. (#162992)
This replaces SPS transparent conversion for pointers. Transparent conversion only applies to argument/return types, not nested types. We want to be able to serialize / deserialize structs containing pointers. We may need to replace this in the near future with a new SPSPointer tag type, since SPSExecutorAddr is meant to be serialization for pure addresses, and pointers may carry other information (e.g. tag bits), but we can do that in a follow-up commit.
1 parent 24ac506 commit 28b3f2f

File tree

4 files changed

+26
-77
lines changed

4 files changed

+26
-77
lines changed

orc-rt/include/orc-rt/SPSWrapperFunction.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ template <typename... SPSArgTs> struct WFSPSHelper {
4242
static T &&from(T &&Arg) noexcept { return std::forward<T>(Arg); }
4343
};
4444

45-
template <typename T> struct Serializable<T *> {
46-
typedef ExecutorAddr serializable_type;
47-
static ExecutorAddr to(T *Arg) { return ExecutorAddr::fromPtr(Arg); }
48-
static T *from(ExecutorAddr A) { return A.toPtr<T *>(); }
49-
};
50-
5145
template <> struct Serializable<Error> {
5246
typedef SPSSerializableError serializable_type;
5347
static SPSSerializableError to(Error Err) {
@@ -66,21 +60,6 @@ template <typename... SPSArgTs> struct WFSPSHelper {
6660
}
6761
};
6862

69-
template <typename T> struct Serializable<Expected<T *>> {
70-
typedef SPSSerializableExpected<ExecutorAddr> serializable_type;
71-
static SPSSerializableExpected<ExecutorAddr> to(Expected<T *> Val) {
72-
return SPSSerializableExpected<ExecutorAddr>(
73-
Val ? Expected<ExecutorAddr>(ExecutorAddr::fromPtr(*Val))
74-
: Expected<ExecutorAddr>(Val.takeError()));
75-
}
76-
static Expected<T *> from(SPSSerializableExpected<ExecutorAddr> Val) {
77-
if (auto Tmp = Val.toExpected())
78-
return Tmp->toPtr<T *>();
79-
else
80-
return Tmp.takeError();
81-
}
82-
};
83-
8463
template <typename... Ts> struct DeserializableTuple;
8564

8665
template <typename... Ts> struct DeserializableTuple<std::tuple<Ts...>> {

orc-rt/include/orc-rt/SimplePackedSerialization.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,26 @@ template <> class SPSSerializationTraits<SPSExecutorAddr, ExecutorAddr> {
556556
}
557557
};
558558

559+
/// Allow SPSExectorAddr serialization to/from T*.
560+
template <typename T> class SPSSerializationTraits<SPSExecutorAddr, T *> {
561+
public:
562+
static size_t size(T *const &P) {
563+
return SPSArgList<SPSExecutorAddr>::size(ExecutorAddr::fromPtr(P));
564+
}
565+
566+
static bool serialize(SPSOutputBuffer &OB, T *const &P) {
567+
return SPSArgList<SPSExecutorAddr>::serialize(OB, ExecutorAddr::fromPtr(P));
568+
}
569+
570+
static bool deserialize(SPSInputBuffer &IB, T *&P) {
571+
ExecutorAddr Value;
572+
if (!SPSArgList<SPSExecutorAddr>::deserialize(IB, Value))
573+
return false;
574+
P = Value.toPtr<T *>();
575+
return true;
576+
}
577+
};
578+
559579
/// Helper type for serializing Errors.
560580
///
561581
/// llvm::Errors are move-only, and not inspectable except by consuming them.

orc-rt/unittests/SPSWrapperFunctionTest.cpp

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -192,62 +192,6 @@ TEST(SPSWrapperFunctionUtilsTest, TransparentConversionExpectedFailureCase) {
192192
EXPECT_EQ(ErrMsg, "N is not a multiple of 2");
193193
}
194194

195-
static void
196-
round_trip_int_pointer_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx,
197-
orc_rt_WrapperFunctionReturn Return,
198-
orc_rt_WrapperFunctionBuffer ArgBytes) {
199-
SPSWrapperFunction<SPSExecutorAddr(SPSExecutorAddr)>::handle(
200-
Session, CallCtx, Return, ArgBytes,
201-
[](move_only_function<void(int32_t *)> Return, int32_t *P) {
202-
Return(P);
203-
});
204-
}
205-
206-
TEST(SPSWrapperFunctionUtilsTest, TransparentConversionPointers) {
207-
int X = 42;
208-
int *P = nullptr;
209-
SPSWrapperFunction<SPSExecutorAddr(SPSExecutorAddr)>::call(
210-
DirectCaller(nullptr, round_trip_int_pointer_sps_wrapper),
211-
[&](Expected<int32_t *> R) { P = cantFail(std::move(R)); }, &X);
212-
213-
EXPECT_EQ(P, &X);
214-
}
215-
216-
TEST(SPSWrapperFunctionUtilsTest, TransparentConversionReferenceArguments) {
217-
int X = 42;
218-
int *P = nullptr;
219-
SPSWrapperFunction<SPSExecutorAddr(SPSExecutorAddr)>::call(
220-
DirectCaller(nullptr, round_trip_int_pointer_sps_wrapper),
221-
[&](Expected<int32_t *> R) { P = cantFail(std::move(R)); },
222-
static_cast<int *const &>(&X));
223-
224-
EXPECT_EQ(P, &X);
225-
}
226-
227-
static void
228-
expected_int_pointer_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx,
229-
orc_rt_WrapperFunctionReturn Return,
230-
orc_rt_WrapperFunctionBuffer ArgBytes) {
231-
SPSWrapperFunction<SPSExpected<SPSExecutorAddr>(SPSExecutorAddr)>::handle(
232-
Session, CallCtx, Return, ArgBytes,
233-
[](move_only_function<void(Expected<int32_t *>)> Return, int32_t *P) {
234-
Return(P);
235-
});
236-
}
237-
238-
TEST(SPSWrapperFunctionUtilsTest, TransparentConversionExpectedPointers) {
239-
int X = 42;
240-
int *P = nullptr;
241-
SPSWrapperFunction<SPSExpected<SPSExecutorAddr>(SPSExecutorAddr)>::call(
242-
DirectCaller(nullptr, expected_int_pointer_sps_wrapper),
243-
[&](Expected<Expected<int32_t *>> R) {
244-
P = cantFail(cantFail(std::move(R)));
245-
},
246-
&X);
247-
248-
EXPECT_EQ(P, &X);
249-
}
250-
251195
template <size_t N> struct SPSOpCounter {};
252196

253197
namespace orc_rt {

orc-rt/unittests/SimplePackedSerializationTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ TEST(SimplePackedSerializationTest, StdOptionalValueSerialization) {
169169
blobSerializationRoundTrip<SPSOptional<int64_t>>(Value);
170170
}
171171

172+
TEST(SimplePackedSerializationTest, Pointers) {
173+
int X = 42;
174+
int *P = &X;
175+
blobSerializationRoundTrip<SPSExecutorAddr>(P);
176+
}
177+
172178
TEST(SimplePackedSerializationTest, ArgListSerialization) {
173179
using BAL = SPSArgList<bool, int32_t, SPSString>;
174180

0 commit comments

Comments
 (0)