diff --git a/orc-rt/include/CMakeLists.txt b/orc-rt/include/CMakeLists.txt index bcbee39e647ea..17a24aa02e2f6 100644 --- a/orc-rt/include/CMakeLists.txt +++ b/orc-rt/include/CMakeLists.txt @@ -17,6 +17,7 @@ set(ORC_RT_HEADERS orc-rt/ScopeExit.h orc-rt/SimplePackedSerialization.h orc-rt/SPSAllocAction.h + orc-rt/SPSMemoryFlags.h orc-rt/SPSWrapperFunction.h orc-rt/bind.h orc-rt/bit.h diff --git a/orc-rt/include/orc-rt/MemoryFlags.h b/orc-rt/include/orc-rt/MemoryFlags.h index 24384e62d8b09..128fbfc6c3bfd 100644 --- a/orc-rt/include/orc-rt/MemoryFlags.h +++ b/orc-rt/include/orc-rt/MemoryFlags.h @@ -42,8 +42,14 @@ enum class MemLifetime : unsigned { Finalize }; +namespace detail { +struct AllocGroupInternals; +} // namespace detail + /// A pair of memory protections and lifetime policy. class AllocGroup { + friend struct detail::AllocGroupInternals; + private: static constexpr int NumProtBits = bitmask_enum_num_bits_v; static constexpr int NumLifetimeBits = 1; @@ -87,6 +93,20 @@ class AllocGroup { underlying_type Id = 0; }; +namespace detail { +/// Helper for serializers that need access to the underlying representation of +/// AllocGroup. +struct AllocGroupInternals { + typedef AllocGroup::underlying_type underlying_type; + static underlying_type getId(const AllocGroup &AG) noexcept { return AG.Id; } + static AllocGroup fromId(underlying_type Id) noexcept { + AllocGroup AG; + AG.Id = Id; + return AG; + } +}; +} // namespace detail + /// A specialized small-map for AllocGroups. /// /// Iteration order is guaranteed to match key ordering. diff --git a/orc-rt/include/orc-rt/SPSMemoryFlags.h b/orc-rt/include/orc-rt/SPSMemoryFlags.h new file mode 100644 index 0000000000000..133875bb0d838 --- /dev/null +++ b/orc-rt/include/orc-rt/SPSMemoryFlags.h @@ -0,0 +1,49 @@ +//===-- SPSMemoryFlags.h - SPS-serialization for MemoryFlags.h --*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// SPSSerialization for relevant types in MemoryFlags.h. +// +//===----------------------------------------------------------------------===// + +#ifndef ORC_RT_SPSMEMORYFLAGS_H +#define ORC_RT_SPSMEMORYFLAGS_H + +#include "orc-rt/MemoryFlags.h" +#include "orc-rt/SimplePackedSerialization.h" + +namespace orc_rt { + +struct SPSAllocGroup; + +template <> class SPSSerializationTraits { +private: + typedef detail::AllocGroupInternals::underlying_type UT; + +public: + static size_t size(const AllocGroup &AG) { + return SPSSerializationTraits::size( + detail::AllocGroupInternals::getId(AG)); + } + + static bool serialize(SPSOutputBuffer &OB, const AllocGroup &AG) { + return SPSSerializationTraits::serialize( + OB, detail::AllocGroupInternals::getId(AG)); + } + + static bool deserialize(SPSInputBuffer &IB, AllocGroup &AG) { + UT Id = 0; + if (!SPSSerializationTraits::deserialize(IB, Id)) + return false; + AG = detail::AllocGroupInternals::fromId(Id); + return true; + } +}; + +} // namespace orc_rt + +#endif // ORC_RT_SPSMEMORYFLAGS_H diff --git a/orc-rt/unittests/CMakeLists.txt b/orc-rt/unittests/CMakeLists.txt index f8556401a9350..54430587dd27b 100644 --- a/orc-rt/unittests/CMakeLists.txt +++ b/orc-rt/unittests/CMakeLists.txt @@ -24,6 +24,7 @@ add_orc_rt_unittest(CoreTests RTTITest.cpp ScopeExitTest.cpp SimplePackedSerializationTest.cpp + SPSMemoryFlagsTest.cpp SPSWrapperFunctionTest.cpp WrapperFunctionBufferTest.cpp bind-test.cpp diff --git a/orc-rt/unittests/SPSMemoryFlagsTest.cpp b/orc-rt/unittests/SPSMemoryFlagsTest.cpp new file mode 100644 index 0000000000000..b58fda9374af3 --- /dev/null +++ b/orc-rt/unittests/SPSMemoryFlagsTest.cpp @@ -0,0 +1,35 @@ +//===-- SPSMemoryFlagsTest.cpp --------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Test SPS serialization for MemoryFlags APIs. +// +//===----------------------------------------------------------------------===// + +#include "orc-rt/SPSMemoryFlags.h" + +#include "SimplePackedSerializationTestUtils.h" +#include "gtest/gtest.h" + +using namespace orc_rt; + +TEST(SPSMemoryFlags, TestAllocGroupSerialization) { + for (bool Read : {false, true}) { + for (bool Write : {false, true}) { + for (bool Exec : {false, true}) { + for (bool FinalizeLifetime : {false, true}) { + AllocGroup AG((Read ? MemProt::Read : MemProt::None) | + (Write ? MemProt::Write : MemProt::None) | + (Exec ? MemProt::Exec : MemProt::None), + FinalizeLifetime ? MemLifetime::Finalize + : MemLifetime::Standard); + blobSerializationRoundTrip(AG); + } + } + } + } +}