Skip to content

Commit fc1a44f

Browse files
authored
[orc-rt] Add SPS serialization support for AllocGroup. (#157415)
1 parent 6c3f18e commit fc1a44f

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

orc-rt/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set(ORC_RT_HEADERS
1717
orc-rt/ScopeExit.h
1818
orc-rt/SimplePackedSerialization.h
1919
orc-rt/SPSAllocAction.h
20+
orc-rt/SPSMemoryFlags.h
2021
orc-rt/SPSWrapperFunction.h
2122
orc-rt/bind.h
2223
orc-rt/bit.h

orc-rt/include/orc-rt/MemoryFlags.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ enum class MemLifetime : unsigned {
4242
Finalize
4343
};
4444

45+
namespace detail {
46+
struct AllocGroupInternals;
47+
} // namespace detail
48+
4549
/// A pair of memory protections and lifetime policy.
4650
class AllocGroup {
51+
friend struct detail::AllocGroupInternals;
52+
4753
private:
4854
static constexpr int NumProtBits = bitmask_enum_num_bits_v<MemProt>;
4955
static constexpr int NumLifetimeBits = 1;
@@ -87,6 +93,20 @@ class AllocGroup {
8793
underlying_type Id = 0;
8894
};
8995

96+
namespace detail {
97+
/// Helper for serializers that need access to the underlying representation of
98+
/// AllocGroup.
99+
struct AllocGroupInternals {
100+
typedef AllocGroup::underlying_type underlying_type;
101+
static underlying_type getId(const AllocGroup &AG) noexcept { return AG.Id; }
102+
static AllocGroup fromId(underlying_type Id) noexcept {
103+
AllocGroup AG;
104+
AG.Id = Id;
105+
return AG;
106+
}
107+
};
108+
} // namespace detail
109+
90110
/// A specialized small-map for AllocGroups.
91111
///
92112
/// Iteration order is guaranteed to match key ordering.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- SPSMemoryFlags.h - SPS-serialization for MemoryFlags.h --*- 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+
// SPSSerialization for relevant types in MemoryFlags.h.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef ORC_RT_SPSMEMORYFLAGS_H
14+
#define ORC_RT_SPSMEMORYFLAGS_H
15+
16+
#include "orc-rt/MemoryFlags.h"
17+
#include "orc-rt/SimplePackedSerialization.h"
18+
19+
namespace orc_rt {
20+
21+
struct SPSAllocGroup;
22+
23+
template <> class SPSSerializationTraits<SPSAllocGroup, AllocGroup> {
24+
private:
25+
typedef detail::AllocGroupInternals::underlying_type UT;
26+
27+
public:
28+
static size_t size(const AllocGroup &AG) {
29+
return SPSSerializationTraits<UT, UT>::size(
30+
detail::AllocGroupInternals::getId(AG));
31+
}
32+
33+
static bool serialize(SPSOutputBuffer &OB, const AllocGroup &AG) {
34+
return SPSSerializationTraits<UT, UT>::serialize(
35+
OB, detail::AllocGroupInternals::getId(AG));
36+
}
37+
38+
static bool deserialize(SPSInputBuffer &IB, AllocGroup &AG) {
39+
UT Id = 0;
40+
if (!SPSSerializationTraits<UT, UT>::deserialize(IB, Id))
41+
return false;
42+
AG = detail::AllocGroupInternals::fromId(Id);
43+
return true;
44+
}
45+
};
46+
47+
} // namespace orc_rt
48+
49+
#endif // ORC_RT_SPSMEMORYFLAGS_H

orc-rt/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_orc_rt_unittest(CoreTests
2424
RTTITest.cpp
2525
ScopeExitTest.cpp
2626
SimplePackedSerializationTest.cpp
27+
SPSMemoryFlagsTest.cpp
2728
SPSWrapperFunctionTest.cpp
2829
WrapperFunctionBufferTest.cpp
2930
bind-test.cpp
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===-- SPSMemoryFlagsTest.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 MemoryFlags APIs.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "orc-rt/SPSMemoryFlags.h"
14+
15+
#include "SimplePackedSerializationTestUtils.h"
16+
#include "gtest/gtest.h"
17+
18+
using namespace orc_rt;
19+
20+
TEST(SPSMemoryFlags, TestAllocGroupSerialization) {
21+
for (bool Read : {false, true}) {
22+
for (bool Write : {false, true}) {
23+
for (bool Exec : {false, true}) {
24+
for (bool FinalizeLifetime : {false, true}) {
25+
AllocGroup AG((Read ? MemProt::Read : MemProt::None) |
26+
(Write ? MemProt::Write : MemProt::None) |
27+
(Exec ? MemProt::Exec : MemProt::None),
28+
FinalizeLifetime ? MemLifetime::Finalize
29+
: MemLifetime::Standard);
30+
blobSerializationRoundTrip<SPSAllocGroup, AllocGroup>(AG);
31+
}
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)