Skip to content

Commit 7cf94e7

Browse files
lhamesgithub-actions[bot]
authored andcommitted
Automerge: [orc-rt] Hoist DirectCaller test utility into header to enable re-use. (#162405)
The DirectCaller utility allows "direct" calls (with arguments serialized into, and then immediately back out of a WrapperFunctionBuffer) to wrapper functions. It was introduced for the SPSWrapperFunction tests, but will be useful for testing WrapperFunction interfaces for various orc-rt APIs too, so this commit hoists it somewhere where it can be reused.
2 parents 74f708b + 70b7a35 commit 7cf94e7

File tree

2 files changed

+73
-54
lines changed

2 files changed

+73
-54
lines changed

orc-rt/unittests/DirectCaller.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===- DirectCaller.h -----------------------------------------------------===//
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+
#ifndef ORC_RT_UNITTEST_DIRECTCALLER_H
10+
#define ORC_RT_UNITTEST_DIRECTCALLER_H
11+
12+
#include "orc-rt/WrapperFunction.h"
13+
14+
#include <memory>
15+
#include <utility>
16+
17+
/// Make calls and call result handlers directly on the current thread.
18+
class DirectCaller {
19+
private:
20+
class DirectResultSender {
21+
public:
22+
virtual ~DirectResultSender() {}
23+
virtual void send(orc_rt_SessionRef Session,
24+
orc_rt::WrapperFunctionBuffer ResultBytes) = 0;
25+
static void send(orc_rt_SessionRef Session, void *CallCtx,
26+
orc_rt_WrapperFunctionBuffer ResultBytes) {
27+
std::unique_ptr<DirectResultSender>(
28+
reinterpret_cast<DirectResultSender *>(CallCtx))
29+
->send(Session, ResultBytes);
30+
}
31+
};
32+
33+
template <typename ImplFn>
34+
class DirectResultSenderImpl : public DirectResultSender {
35+
public:
36+
DirectResultSenderImpl(ImplFn &&Fn) : Fn(std::forward<ImplFn>(Fn)) {}
37+
void send(orc_rt_SessionRef Session,
38+
orc_rt::WrapperFunctionBuffer ResultBytes) override {
39+
Fn(Session, std::move(ResultBytes));
40+
}
41+
42+
private:
43+
std::decay_t<ImplFn> Fn;
44+
};
45+
46+
template <typename ImplFn>
47+
static std::unique_ptr<DirectResultSender>
48+
makeDirectResultSender(ImplFn &&Fn) {
49+
return std::make_unique<DirectResultSenderImpl<ImplFn>>(
50+
std::forward<ImplFn>(Fn));
51+
}
52+
53+
public:
54+
DirectCaller(orc_rt_SessionRef Session, orc_rt_WrapperFunction Fn)
55+
: Session(Session), Fn(Fn) {}
56+
57+
template <typename HandleResultFn>
58+
void operator()(HandleResultFn &&HandleResult,
59+
orc_rt::WrapperFunctionBuffer ArgBytes) {
60+
auto DR =
61+
makeDirectResultSender(std::forward<HandleResultFn>(HandleResult));
62+
Fn(Session, reinterpret_cast<void *>(DR.release()),
63+
DirectResultSender::send, ArgBytes.release());
64+
}
65+
66+
private:
67+
orc_rt_SessionRef Session;
68+
orc_rt_WrapperFunction Fn;
69+
};
70+
71+
#endif // ORC_RT_UNITTEST_DIRECTCALLER_H

orc-rt/unittests/SPSWrapperFunctionTest.cpp

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,64 +16,12 @@
1616
#include "orc-rt/WrapperFunction.h"
1717
#include "orc-rt/move_only_function.h"
1818

19+
#include "DirectCaller.h"
20+
1921
#include "gtest/gtest.h"
2022

2123
using namespace orc_rt;
2224

23-
/// Make calls and call result handlers directly on the current thread.
24-
class DirectCaller {
25-
private:
26-
class DirectResultSender {
27-
public:
28-
virtual ~DirectResultSender() {}
29-
virtual void send(orc_rt_SessionRef Session,
30-
WrapperFunctionBuffer ResultBytes) = 0;
31-
static void send(orc_rt_SessionRef Session, void *CallCtx,
32-
orc_rt_WrapperFunctionBuffer ResultBytes) {
33-
std::unique_ptr<DirectResultSender>(
34-
reinterpret_cast<DirectResultSender *>(CallCtx))
35-
->send(Session, ResultBytes);
36-
}
37-
};
38-
39-
template <typename ImplFn>
40-
class DirectResultSenderImpl : public DirectResultSender {
41-
public:
42-
DirectResultSenderImpl(ImplFn &&Fn) : Fn(std::forward<ImplFn>(Fn)) {}
43-
void send(orc_rt_SessionRef Session,
44-
WrapperFunctionBuffer ResultBytes) override {
45-
Fn(Session, std::move(ResultBytes));
46-
}
47-
48-
private:
49-
std::decay_t<ImplFn> Fn;
50-
};
51-
52-
template <typename ImplFn>
53-
static std::unique_ptr<DirectResultSender>
54-
makeDirectResultSender(ImplFn &&Fn) {
55-
return std::make_unique<DirectResultSenderImpl<ImplFn>>(
56-
std::forward<ImplFn>(Fn));
57-
}
58-
59-
public:
60-
DirectCaller(orc_rt_SessionRef Session, orc_rt_WrapperFunction Fn)
61-
: Session(Session), Fn(Fn) {}
62-
63-
template <typename HandleResultFn>
64-
void operator()(HandleResultFn &&HandleResult,
65-
WrapperFunctionBuffer ArgBytes) {
66-
auto DR =
67-
makeDirectResultSender(std::forward<HandleResultFn>(HandleResult));
68-
Fn(Session, reinterpret_cast<void *>(DR.release()),
69-
DirectResultSender::send, ArgBytes.release());
70-
}
71-
72-
private:
73-
orc_rt_SessionRef Session;
74-
orc_rt_WrapperFunction Fn;
75-
};
76-
7725
static void void_noop_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx,
7826
orc_rt_WrapperFunctionReturn Return,
7927
orc_rt_WrapperFunctionBuffer ArgBytes) {

0 commit comments

Comments
 (0)