[llvm] f38cfda - [ORC] Add specialized SPSSerializationTraits for ArrayRef<char>.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 2 15:29:17 PDT 2021


Author: Lang Hames
Date: 2021-09-03T08:28:29+10:00
New Revision: f38cfdabd1ec53eb4278e02af98a76620f33e260

URL: https://github.com/llvm/llvm-project/commit/f38cfdabd1ec53eb4278e02af98a76620f33e260
DIFF: https://github.com/llvm/llvm-project/commit/f38cfdabd1ec53eb4278e02af98a76620f33e260.diff

LOG: [ORC] Add specialized SPSSerializationTraits for ArrayRef<char>.

Deserializing from an SPSSequence<char> to an an ArrayRef<char> will point the
ArrayRef<char> at the input buffer.

Added: 
    

Modified: 
    llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h
    llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h
index dfa2307b2f200..e444a6352c518 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h
@@ -296,6 +296,31 @@ class TrivialSPSSequenceSerialization<SPSElementTagT, ArrayRef<T>> {
   static constexpr bool available = true;
 };
 
+/// Specialized SPSSequence<char> -> ArrayRef<char> serialization.
+///
+/// On deserialize, points directly into the input buffer.
+template <> class SPSSerializationTraits<SPSSequence<char>, ArrayRef<char>> {
+public:
+  static size_t size(const ArrayRef<char> &A) {
+    return SPSArgList<uint64_t>::size(static_cast<uint64_t>(A.size())) +
+           A.size();
+  }
+
+  static bool serialize(SPSOutputBuffer &OB, const ArrayRef<char> &A) {
+    if (!SPSArgList<uint64_t>::serialize(OB, static_cast<uint64_t>(A.size())))
+      return false;
+    return OB.write(A.data(), A.size());
+  }
+
+  static bool deserialize(SPSInputBuffer &IB, ArrayRef<char> &A) {
+    uint64_t Size;
+    if (!SPSArgList<uint64_t>::deserialize(IB, Size))
+      return false;
+    A = {IB.data(), Size};
+    return IB.skip(Size);
+  }
+};
+
 /// 'Trivial' sequence serialization: Sequence is serialized as a uint64_t size
 /// followed by a for-earch loop over the elements of the sequence to serialize
 /// each of them.

diff  --git a/llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp b/llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp
index b5455e6580bd2..b43735d6a0949 100644
--- a/llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp
@@ -168,3 +168,23 @@ TEST(SimplePackedSerialization, StringMap) {
   StringMap<int32_t> M({{"A", 1}, {"B", 2}});
   spsSerializationRoundTrip<SPSSequence<SPSTuple<SPSString, int32_t>>>(M);
 }
+
+TEST(SimplePackedSerializationTest, ArrayRef) {
+  constexpr unsigned BufferSize = 6 + 8; // "hello\0" + sizeof(uint64_t)
+  ArrayRef<char> HelloOut = "hello";
+  char Buffer[BufferSize];
+  memset(Buffer, 0, BufferSize);
+
+  SPSOutputBuffer OB(Buffer, BufferSize);
+  EXPECT_TRUE(SPSArgList<SPSSequence<char>>::serialize(OB, HelloOut));
+
+  ArrayRef<char> HelloIn;
+  SPSInputBuffer IB(Buffer, BufferSize);
+  EXPECT_TRUE(SPSArgList<SPSSequence<char>>::deserialize(IB, HelloIn));
+
+  // Output should be copied to buffer.
+  EXPECT_NE(HelloOut.data(), Buffer);
+
+  // Input should reference buffer.
+  EXPECT_LT(HelloIn.data() - Buffer, BufferSize);
+}


        


More information about the llvm-commits mailing list