[compiler-rt] 870fc84 - [ORC-RT] Add SPS serialization for span<const char> / SPSSequence<char>.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 3 13:43:56 PDT 2021
Author: Lang Hames
Date: 2021-11-03T13:43:49-07:00
New Revision: 870fc844d11e3e7b75bf62858953e08df1b1d94c
URL: https://github.com/llvm/llvm-project/commit/870fc844d11e3e7b75bf62858953e08df1b1d94c
DIFF: https://github.com/llvm/llvm-project/commit/870fc844d11e3e7b75bf62858953e08df1b1d94c.diff
LOG: [ORC-RT] Add SPS serialization for span<const char> / SPSSequence<char>.
Added:
Modified:
compiler-rt/lib/orc/simple_packed_serialization.h
compiler-rt/lib/orc/unittests/simple_packed_serialization_test.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/orc/simple_packed_serialization.h b/compiler-rt/lib/orc/simple_packed_serialization.h
index 3a6d245ba8227..ec43130a2ef58 100644
--- a/compiler-rt/lib/orc/simple_packed_serialization.h
+++ b/compiler-rt/lib/orc/simple_packed_serialization.h
@@ -354,6 +354,27 @@ class SPSSerializationTraits<SPSSequence<SPSElementTagT>, SequenceT,
}
};
+/// Trivial serialization / deserialization for span<char>
+template <> class SPSSerializationTraits<SPSSequence<char>, span<const char>> {
+public:
+ static size_t size(const span<const char> &S) {
+ return SPSArgList<uint64_t>::size(static_cast<uint64_t>(S.size())) +
+ S.size();
+ }
+ static bool serialize(SPSOutputBuffer &OB, const span<const char> &S) {
+ if (!SPSArgList<uint64_t>::serialize(OB, static_cast<uint64_t>(S.size())))
+ return false;
+ return OB.write(S.data(), S.size());
+ }
+ static bool deserialize(SPSInputBuffer &IB, span<const char> &S) {
+ uint64_t Size;
+ if (!SPSArgList<uint64_t>::deserialize(IB, Size))
+ return false;
+ S = span<const char>(IB.data(), Size);
+ return IB.skip(Size);
+ }
+};
+
/// SPSTuple serialization for std::pair.
template <typename SPSTagT1, typename SPSTagT2, typename T1, typename T2>
class SPSSerializationTraits<SPSTuple<SPSTagT1, SPSTagT2>, std::pair<T1, T2>> {
diff --git a/compiler-rt/lib/orc/unittests/simple_packed_serialization_test.cpp b/compiler-rt/lib/orc/unittests/simple_packed_serialization_test.cpp
index 3b55aa9662c73..92aa58aafe3ef 100644
--- a/compiler-rt/lib/orc/unittests/simple_packed_serialization_test.cpp
+++ b/compiler-rt/lib/orc/unittests/simple_packed_serialization_test.cpp
@@ -130,6 +130,30 @@ TEST(SimplePackedSerializationTest, StringViewCharSequenceSerialization) {
blobSerializationRoundTrip<SPSString, string_view>(string_view(HW));
}
+TEST(SimplePackedSerializationTest, SpanSerialization) {
+ const char Data[] = {3, 2, 1, 0, 1, 2, 3}; // Span should handle nulls.
+ span<const char> OutS(Data, sizeof(Data));
+
+ size_t Size = SPSArgList<SPSSequence<char>>::size(OutS);
+ auto Buffer = std::make_unique<char[]>(Size);
+ SPSOutputBuffer OB(Buffer.get(), Size);
+
+ EXPECT_TRUE(SPSArgList<SPSSequence<char>>::serialize(OB, OutS));
+
+ SPSInputBuffer IB(Buffer.get(), Size);
+
+ span<const char> InS;
+
+ EXPECT_TRUE(SPSArgList<SPSSequence<char>>::deserialize(IB, InS));
+
+ // Check that the serialized and deserialized values match.
+ EXPECT_EQ(InS.size(), OutS.size());
+ EXPECT_EQ(memcmp(OutS.data(), InS.data(), InS.size()), 0);
+
+ // Check that the span points directly to the input buffer.
+ EXPECT_EQ(InS.data(), Buffer.get() + sizeof(uint64_t));
+}
+
TEST(SimplePackedSerializationTest, StdPairSerialization) {
std::pair<int32_t, std::string> P(42, "foo");
blobSerializationRoundTrip<SPSTuple<int32_t, SPSString>,
More information about the llvm-commits
mailing list