[llvm] [orc-rt] Add unit test utility: MakeAllocAction. (PR #162229)

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 6 23:39:29 PDT 2025


https://github.com/lhames created https://github.com/llvm/llvm-project/pull/162229

MakeAllocAction can be used to construct AllocActions with less boilerplate than the previous option (spsSerialize + AllocAction constructor call). This will be used to simplify upcoming unit tests that use AllocActions.

The existing AllocActionsTest is updated to use the new utility.

>From 66a49441ac93ae637069aa9935b8507e4042c980 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Tue, 7 Oct 2025 17:34:29 +1100
Subject: [PATCH] [orc-rt] Add unit test utility: MakeAllocAction.

MakeAllocAction can be used to construct AllocActions with less boilerplate
than the previous option (spsSerialize + AllocAction constructor call). This
will be used to simplify upcoming unit tests that use AllocActions.

The existing AllocActionsTest is updated to use the new utility.
---
 orc-rt/unittests/AllocActionTest.cpp    | 54 +++++++++++++++----------
 orc-rt/unittests/AllocActionTestUtils.h | 30 ++++++++++++++
 2 files changed, 63 insertions(+), 21 deletions(-)
 create mode 100644 orc-rt/unittests/AllocActionTestUtils.h

diff --git a/orc-rt/unittests/AllocActionTest.cpp b/orc-rt/unittests/AllocActionTest.cpp
index 46a3fdb6d79cc..08e2a64cde331 100644
--- a/orc-rt/unittests/AllocActionTest.cpp
+++ b/orc-rt/unittests/AllocActionTest.cpp
@@ -14,7 +14,7 @@
 #include "orc-rt/ExecutorAddress.h"
 #include "orc-rt/SPSAllocAction.h"
 
-#include "SimplePackedSerializationTestUtils.h"
+#include "AllocActionTestUtils.h"
 #include "gtest/gtest.h"
 
 using namespace orc_rt;
@@ -77,11 +77,14 @@ TEST(AllocActionTest, RunFinalizationActionsComplete) {
 
   std::vector<AllocActionPair> InitialActions;
 
-  auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); };
-  InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
-                            {decrement_sps_allocaction, MakeArgBuffer()}});
-  InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
-                            {decrement_sps_allocaction, MakeArgBuffer()}});
+  auto MakeAAOnVal = [&](AllocActionFn Fn) {
+    return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
+                                                   ExecutorAddr::fromPtr(&Val));
+  };
+  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
+                            MakeAAOnVal(decrement_sps_allocaction)});
+  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
+                            MakeAAOnVal(decrement_sps_allocaction)});
 
   auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions)));
 
@@ -102,11 +105,14 @@ TEST(AllocActionTest, RunFinalizeActionsFail) {
 
   std::vector<AllocActionPair> InitialActions;
 
-  auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); };
-  InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
-                            {decrement_sps_allocaction, MakeArgBuffer()}});
-  InitialActions.push_back({{fail_sps_allocaction, MakeArgBuffer()},
-                            {decrement_sps_allocaction, MakeArgBuffer()}});
+  auto MakeAAOnVal = [&](AllocActionFn Fn) {
+    return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
+                                                   ExecutorAddr::fromPtr(&Val));
+  };
+  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
+                            MakeAAOnVal(decrement_sps_allocaction)});
+  InitialActions.push_back({*MakeAllocAction<>::from(fail_sps_allocaction),
+                            MakeAAOnVal(decrement_sps_allocaction)});
 
   auto DeallocActions = runFinalizeActions(std::move(InitialActions));
 
@@ -126,11 +132,14 @@ TEST(AllocActionTest, RunFinalizeActionsNullFinalize) {
 
   std::vector<AllocActionPair> InitialActions;
 
-  auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); };
-  InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
-                            {decrement_sps_allocaction, MakeArgBuffer()}});
-  InitialActions.push_back({{nullptr, WrapperFunctionBuffer()},
-                            {decrement_sps_allocaction, MakeArgBuffer()}});
+  auto MakeAAOnVal = [&](AllocActionFn Fn) {
+    return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
+                                                   ExecutorAddr::fromPtr(&Val));
+  };
+  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
+                            MakeAAOnVal(decrement_sps_allocaction)});
+  InitialActions.push_back({*MakeAllocAction<>::from(nullptr),
+                            MakeAAOnVal(decrement_sps_allocaction)});
 
   auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions)));
 
@@ -148,11 +157,14 @@ TEST(AllocActionTest, RunFinalizeActionsNullDealloc) {
 
   std::vector<AllocActionPair> InitialActions;
 
-  auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); };
-  InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
-                            {decrement_sps_allocaction, MakeArgBuffer()}});
-  InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()},
-                            {nullptr, WrapperFunctionBuffer()}});
+  auto MakeAAOnVal = [&](AllocActionFn Fn) {
+    return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
+                                                   ExecutorAddr::fromPtr(&Val));
+  };
+  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
+                            MakeAAOnVal(decrement_sps_allocaction)});
+  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
+                            *MakeAllocAction<>::from(nullptr)});
 
   auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions)));
 
diff --git a/orc-rt/unittests/AllocActionTestUtils.h b/orc-rt/unittests/AllocActionTestUtils.h
new file mode 100644
index 0000000000000..18f8c9b7cbdca
--- /dev/null
+++ b/orc-rt/unittests/AllocActionTestUtils.h
@@ -0,0 +1,30 @@
+//===- AllocActionTestUtils.h ---------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef ORC_RT_UNITTEST_ALLOCACTIONTESTUTILS_H
+#define ORC_RT_UNITTEST_ALLOCACTIONTESTUTILS_H
+
+#include "SimplePackedSerializationTestUtils.h"
+#include "orc-rt/AllocAction.h"
+
+#include <optional>
+
+template <typename... SPSArgTs> struct MakeAllocAction {
+  template <typename... ArgTs>
+  static std::optional<orc_rt::AllocAction> from(orc_rt::AllocActionFn Fn,
+                                                 ArgTs &&...Args) {
+    using SPS = orc_rt::SPSArgList<SPSArgTs...>;
+    auto B = orc_rt::WrapperFunctionBuffer::allocate(SPS::size(Args...));
+    orc_rt::SPSOutputBuffer OB(B.data(), B.size());
+    if (!SPS::serialize(OB, Args...))
+      return std::nullopt;
+    return orc_rt::AllocAction(Fn, std::move(B));
+  }
+};
+
+#endif // ORC_RT_UNITTEST_ALLOCACTIONTESTUTILS_H



More information about the llvm-commits mailing list