1 //===- ExecutionSessionWrapperFunctionCallsTest.cpp -- Test wrapper calls -===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/Core.h"
10 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
11 #include "llvm/Support/MSVCErrorWorkarounds.h"
12 #include "llvm/Testing/Support/Error.h"
13 #include "gtest/gtest.h"
18 using namespace llvm::orc
;
19 using namespace llvm::orc::shared
;
21 static llvm::orc::shared::detail::CWrapperFunctionResult
22 addWrapper(const char *ArgData
, size_t ArgSize
) {
23 return WrapperFunction
<int32_t(int32_t, int32_t)>::handle(
24 ArgData
, ArgSize
, [](int32_t X
, int32_t Y
) { return X
+ Y
; })
28 static void addAsyncWrapper(unique_function
<void(int32_t)> SendResult
,
29 int32_t X
, int32_t Y
) {
33 static llvm::orc::shared::detail::CWrapperFunctionResult
34 voidWrapper(const char *ArgData
, size_t ArgSize
) {
35 return WrapperFunction
<void()>::handle(ArgData
, ArgSize
, []() {}).release();
38 TEST(ExecutionSessionWrapperFunctionCalls
, RunWrapperTemplate
) {
39 ExecutionSession
ES(cantFail(SelfExecutorProcessControl::Create()));
42 EXPECT_THAT_ERROR(ES
.callSPSWrapper
<int32_t(int32_t, int32_t)>(
43 pointerToJITTargetAddress(addWrapper
), Result
, 2, 3),
48 TEST(ExecutionSessionWrapperFunctionCalls
, RunVoidWrapperAsyncTemplate
) {
49 ExecutionSession
ES(cantFail(SelfExecutorProcessControl::Create()));
51 std::promise
<MSVCPError
> RP
;
52 ES
.callSPSWrapperAsync
<void()>(
53 [&](Error SerializationErr
) {
54 RP
.set_value(std::move(SerializationErr
));
56 pointerToJITTargetAddress(voidWrapper
));
57 Error Err
= RP
.get_future().get();
58 EXPECT_THAT_ERROR(std::move(Err
), Succeeded());
61 TEST(ExecutionSessionWrapperFunctionCalls
, RunNonVoidWrapperAsyncTemplate
) {
62 ExecutionSession
ES(cantFail(SelfExecutorProcessControl::Create()));
64 std::promise
<MSVCPExpected
<int32_t>> RP
;
65 ES
.callSPSWrapperAsync
<int32_t(int32_t, int32_t)>(
66 [&](Error SerializationErr
, int32_t R
) {
68 RP
.set_value(std::move(SerializationErr
));
69 RP
.set_value(std::move(R
));
71 pointerToJITTargetAddress(addWrapper
), 2, 3);
72 Expected
<int32_t> Result
= RP
.get_future().get();
73 EXPECT_THAT_EXPECTED(Result
, HasValue(5));
76 TEST(ExecutionSessionWrapperFunctionCalls
, RegisterAsyncHandlerAndRun
) {
78 constexpr JITTargetAddress AddAsyncTagAddr
= 0x01;
80 ExecutionSession
ES(cantFail(SelfExecutorProcessControl::Create()));
81 auto &JD
= ES
.createBareJITDylib("JD");
83 auto AddAsyncTag
= ES
.intern("addAsync_tag");
84 cantFail(JD
.define(absoluteSymbols(
86 JITEvaluatedSymbol(AddAsyncTagAddr
, JITSymbolFlags::Exported
)}})));
88 ExecutionSession::JITDispatchHandlerAssociationMap Associations
;
90 Associations
[AddAsyncTag
] =
91 ES
.wrapAsyncWithSPS
<int32_t(int32_t, int32_t)>(addAsyncWrapper
);
93 cantFail(ES
.registerJITDispatchHandlers(JD
, std::move(Associations
)));
95 std::promise
<int32_t> RP
;
96 auto RF
= RP
.get_future();
98 using ArgSerialization
= SPSArgList
<int32_t, int32_t>;
99 size_t ArgBufferSize
= ArgSerialization::size(1, 2);
100 WrapperFunctionResult ArgBuffer
;
101 char *ArgBufferData
=
102 WrapperFunctionResult::allocate(ArgBuffer
, ArgBufferSize
);
103 SPSOutputBuffer
OB(ArgBufferData
, ArgBufferSize
);
104 EXPECT_TRUE(ArgSerialization::serialize(OB
, 1, 2));
106 ES
.runJITDispatchHandler(
107 [&](WrapperFunctionResult ResultBuffer
) {
109 SPSInputBuffer
IB(ResultBuffer
.data(), ResultBuffer
.size());
110 EXPECT_TRUE(SPSArgList
<int32_t>::deserialize(IB
, Result
));
111 RP
.set_value(Result
);
113 AddAsyncTagAddr
, ArrayRef
<char>(ArgBuffer
.data(), ArgBuffer
.size()));
115 EXPECT_EQ(RF
.get(), (int32_t)3);
117 cantFail(ES
.endSession());