Skip to content

Commit 7fbfd1c

Browse files
authored
[orc-rt] Add SPS serialization for WrapperFunctionBuffer. (#157609)
Also adds orc_rt_WrapperFunctionBufferConstData to the C API to get a constant pointer to a wrapper function buffer's data.
1 parent 9bbf22c commit 7fbfd1c

File tree

7 files changed

+106
-3
lines changed

7 files changed

+106
-3
lines changed

orc-rt/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(ORC_RT_HEADERS
1919
orc-rt/SPSAllocAction.h
2020
orc-rt/SPSMemoryFlags.h
2121
orc-rt/SPSWrapperFunction.h
22+
orc-rt/SPSWrapperFunctionBuffer.h
2223
orc-rt/bind.h
2324
orc-rt/bit.h
2425
orc-rt/move_only_function.h

orc-rt/include/orc-rt-c/WrapperFunction.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ orc_rt_WrapperFunctionBufferData(orc_rt_WrapperFunctionBuffer *B) {
160160
return B->Size > sizeof(B->Data.Value) ? B->Data.ValuePtr : B->Data.Value;
161161
}
162162

163+
static inline const char *
164+
orc_rt_WrapperFunctionBufferConstData(const orc_rt_WrapperFunctionBuffer *B) {
165+
assert((B->Size != 0 || B->Data.ValuePtr == NULL) &&
166+
"Cannot get data for out-of-band error value");
167+
return B->Size > sizeof(B->Data.Value) ? B->Data.ValuePtr : B->Data.Value;
168+
}
169+
163170
/**
164171
* Safely get the size of the given orc_rt_WrapperFunctionBuffer.
165172
*
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===-- SPSWrapperFunctionBuffer.h - SPS serialization for WFB --*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// SPS serialization for WrapperFunctionBuffer.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef ORC_RT_SPSWRAPPERFUNCTIONBUFFER_H
14+
#define ORC_RT_SPSWRAPPERFUNCTIONBUFFER_H
15+
16+
#include "orc-rt/SimplePackedSerialization.h"
17+
#include "orc-rt/WrapperFunction.h"
18+
19+
namespace orc_rt {
20+
21+
struct SPSWrapperFunctionBuffer;
22+
23+
template <>
24+
class SPSSerializationTraits<SPSWrapperFunctionBuffer, WrapperFunctionBuffer> {
25+
public:
26+
static size_t size(const WrapperFunctionBuffer &WFB) {
27+
return SPSArgList<uint64_t>::size(static_cast<uint64_t>(WFB.size())) +
28+
WFB.size();
29+
}
30+
31+
static bool serialize(SPSOutputBuffer &OB, const WrapperFunctionBuffer &WFB) {
32+
if (!SPSArgList<uint64_t>::serialize(OB, static_cast<uint64_t>(WFB.size())))
33+
return false;
34+
return OB.write(WFB.data(), WFB.size());
35+
}
36+
37+
static bool deserialize(SPSInputBuffer &IB, WrapperFunctionBuffer &WFB) {
38+
uint64_t Size;
39+
if (!SPSArgList<uint64_t>::deserialize(IB, Size))
40+
return false;
41+
WFB = WrapperFunctionBuffer::allocate(Size);
42+
return IB.read(WFB.data(), WFB.size());
43+
}
44+
};
45+
46+
} // namespace orc_rt
47+
48+
#endif // ORC_RT_SPSWRAPPERFUNCTIONBUFFER_H

orc-rt/include/orc-rt/WrapperFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class WrapperFunctionBuffer {
6262
/// Get a pointer to the data contained in this instance.
6363
char *data() { return orc_rt_WrapperFunctionBufferData(&B); }
6464

65+
/// Get a pointer to the data contained is this instance.
66+
const char *data() const { return orc_rt_WrapperFunctionBufferConstData(&B); }
67+
6568
/// Returns the size of the data contained in this instance.
6669
size_t size() const { return orc_rt_WrapperFunctionBufferSize(&B); }
6770

orc-rt/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_orc_rt_unittest(CoreTests
2626
SimplePackedSerializationTest.cpp
2727
SPSMemoryFlagsTest.cpp
2828
SPSWrapperFunctionTest.cpp
29+
SPSWrapperFunctionBufferTest.cpp
2930
WrapperFunctionBufferTest.cpp
3031
bind-test.cpp
3132
bit-test.cpp
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===-- SPSWrapperFunctionBufferTest.cpp ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Test SPS serialization for WrapperFunctionBuffers.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "orc-rt/SPSWrapperFunctionBuffer.h"
14+
15+
#include "SimplePackedSerializationTestUtils.h"
16+
#include "gtest/gtest.h"
17+
18+
using namespace orc_rt;
19+
20+
static bool WFBEQ(const WrapperFunctionBuffer &LHS,
21+
const WrapperFunctionBuffer &RHS) {
22+
if (LHS.size() != RHS.size())
23+
return false;
24+
return memcmp(LHS.data(), RHS.data(), LHS.size()) == 0;
25+
}
26+
27+
TEST(SPSWrapperFunctionBufferTest, EmptyBuffer) {
28+
WrapperFunctionBuffer EB;
29+
blobSerializationRoundTrip<SPSWrapperFunctionBuffer>(EB, WFBEQ);
30+
}
31+
32+
TEST(SPSWrapperFunctionBufferTest, SmallBuffer) {
33+
const char *Source = "foo";
34+
auto EB = WrapperFunctionBuffer::copyFrom(Source);
35+
blobSerializationRoundTrip<SPSWrapperFunctionBuffer>(EB, WFBEQ);
36+
}
37+
38+
TEST(SPSWrapperFunctionBufferTest, BigBuffer) {
39+
const char *Source = "The quick brown fox jumps over the lazy dog";
40+
auto EB = WrapperFunctionBuffer::copyFrom(Source);
41+
blobSerializationRoundTrip<SPSWrapperFunctionBuffer>(EB, WFBEQ);
42+
}

orc-rt/unittests/SimplePackedSerializationTestUtils.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ static bool spsDeserialize(orc_rt::WrapperFunctionBuffer &B, ArgTs &...Args) {
3131
return SPSTraitsT::deserialize(IB, Args...);
3232
}
3333

34-
template <typename SPSTagT, typename T>
35-
static inline void blobSerializationRoundTrip(const T &Value) {
34+
template <typename SPSTagT, typename T, typename Comparator = std::equal_to<T>>
35+
static inline void blobSerializationRoundTrip(const T &Value,
36+
Comparator &&C = Comparator()) {
3637
using BST = orc_rt::SPSSerializationTraits<SPSTagT, T>;
3738

3839
size_t Size = BST::size(Value);
@@ -46,7 +47,7 @@ static inline void blobSerializationRoundTrip(const T &Value) {
4647
T DSValue;
4748
EXPECT_TRUE(BST::deserialize(IB, DSValue));
4849

49-
EXPECT_EQ(Value, DSValue)
50+
EXPECT_TRUE(C(Value, DSValue))
5051
<< "Incorrect value after serialization/deserialization round-trip";
5152
}
5253

0 commit comments

Comments
 (0)