[llvm] [orc-rt] Add SPS serialization support for AllocGroup. (PR #157415)
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 8 03:00:51 PDT 2025
https://github.com/lhames created https://github.com/llvm/llvm-project/pull/157415
None
>From 40017f8fa55feaf453a458b9a4563fc8cd41dd62 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Mon, 8 Sep 2025 19:55:13 +1000
Subject: [PATCH] [orc-rt] Add SPS serialization support for AllocGroup.
---
orc-rt/include/CMakeLists.txt | 1 +
orc-rt/include/orc-rt/MemoryFlags.h | 20 ++++++++++
orc-rt/include/orc-rt/SPSMemoryFlags.h | 49 +++++++++++++++++++++++++
orc-rt/unittests/CMakeLists.txt | 1 +
orc-rt/unittests/SPSMemoryFlagsTest.cpp | 35 ++++++++++++++++++
5 files changed, 106 insertions(+)
create mode 100644 orc-rt/include/orc-rt/SPSMemoryFlags.h
create mode 100644 orc-rt/unittests/SPSMemoryFlagsTest.cpp
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<MemProt>;
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<SPSAllocGroup, AllocGroup> {
+private:
+ typedef detail::AllocGroupInternals::underlying_type UT;
+
+public:
+ static size_t size(const AllocGroup &AG) {
+ return SPSSerializationTraits<UT, UT>::size(
+ detail::AllocGroupInternals::getId(AG));
+ }
+
+ static bool serialize(SPSOutputBuffer &OB, const AllocGroup &AG) {
+ return SPSSerializationTraits<UT, UT>::serialize(
+ OB, detail::AllocGroupInternals::getId(AG));
+ }
+
+ static bool deserialize(SPSInputBuffer &IB, AllocGroup &AG) {
+ UT Id = 0;
+ if (!SPSSerializationTraits<UT, UT>::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<SPSAllocGroup, AllocGroup>(AG);
+ }
+ }
+ }
+ }
+}
More information about the llvm-commits
mailing list