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/AbsoluteSymbols.h"
10 #include "llvm/ExecutionEngine/Orc/Core.h"
11 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
12 #include "llvm/Support/MSVCErrorWorkarounds.h"
13 #include "llvm/Testing/Support/Error.h"
14 #include "gtest/gtest.h"
19 using namespace llvm::orc
;
20 using namespace llvm::orc::shared
;
22 static llvm::orc::shared::CWrapperFunctionResult
addWrapper(const char *ArgData
,
24 return WrapperFunction
<int32_t(int32_t, int32_t)>::handle(
25 ArgData
, ArgSize
, [](int32_t X
, int32_t Y
) { return X
+ Y
; })
29 static void addAsyncWrapper(unique_function
<void(int32_t)> SendResult
,
30 int32_t X
, int32_t Y
) {
34 static llvm::orc::shared::CWrapperFunctionResult
35 voidWrapper(const char *ArgData
, size_t ArgSize
) {
36 return WrapperFunction
<void()>::handle(ArgData
, ArgSize
, []() {}).release();
39 TEST(ExecutionSessionWrapperFunctionCalls
, RunWrapperTemplate
) {
40 ExecutionSession
ES(cantFail(SelfExecutorProcessControl::Create()));
43 EXPECT_THAT_ERROR(ES
.callSPSWrapper
<int32_t(int32_t, int32_t)>(
44 ExecutorAddr::fromPtr(addWrapper
), Result
, 2, 3),
47 cantFail(ES
.endSession());
50 TEST(ExecutionSessionWrapperFunctionCalls
, RunVoidWrapperAsyncTemplate
) {
51 ExecutionSession
ES(cantFail(SelfExecutorProcessControl::Create()));
53 std::promise
<MSVCPError
> RP
;
54 ES
.callSPSWrapperAsync
<void()>(ExecutorAddr::fromPtr(voidWrapper
),
55 [&](Error SerializationErr
) {
56 RP
.set_value(std::move(SerializationErr
));
58 Error Err
= RP
.get_future().get();
59 EXPECT_THAT_ERROR(std::move(Err
), Succeeded());
60 cantFail(ES
.endSession());
63 TEST(ExecutionSessionWrapperFunctionCalls
, RunNonVoidWrapperAsyncTemplate
) {
64 ExecutionSession
ES(cantFail(SelfExecutorProcessControl::Create()));
66 std::promise
<MSVCPExpected
<int32_t>> RP
;
67 ES
.callSPSWrapperAsync
<int32_t(int32_t, int32_t)>(
68 ExecutorAddr::fromPtr(addWrapper
),
69 [&](Error SerializationErr
, int32_t R
) {
71 RP
.set_value(std::move(SerializationErr
));
72 RP
.set_value(std::move(R
));
75 Expected
<int32_t> Result
= RP
.get_future().get();
76 EXPECT_THAT_EXPECTED(Result
, HasValue(5));
77 cantFail(ES
.endSession());
80 TEST(ExecutionSessionWrapperFunctionCalls
, RegisterAsyncHandlerAndRun
) {
82 constexpr ExecutorAddr
AddAsyncTagAddr(0x01);
84 ExecutionSession
ES(cantFail(SelfExecutorProcessControl::Create()));
85 auto &JD
= ES
.createBareJITDylib("JD");
87 auto AddAsyncTag
= ES
.intern("addAsync_tag");
88 cantFail(JD
.define(absoluteSymbols(
89 {{AddAsyncTag
, {AddAsyncTagAddr
, JITSymbolFlags::Exported
}}})));
91 ExecutionSession::JITDispatchHandlerAssociationMap Associations
;
93 Associations
[AddAsyncTag
] =
94 ES
.wrapAsyncWithSPS
<int32_t(int32_t, int32_t)>(addAsyncWrapper
);
96 cantFail(ES
.registerJITDispatchHandlers(JD
, std::move(Associations
)));
98 std::promise
<int32_t> RP
;
99 auto RF
= RP
.get_future();
101 using ArgSerialization
= SPSArgList
<int32_t, int32_t>;
102 size_t ArgBufferSize
= ArgSerialization::size(1, 2);
103 auto ArgBuffer
= WrapperFunctionResult::allocate(ArgBufferSize
);
104 SPSOutputBuffer
OB(ArgBuffer
.data(), ArgBuffer
.size());
105 EXPECT_TRUE(ArgSerialization::serialize(OB
, 1, 2));
107 ES
.runJITDispatchHandler(
108 [&](WrapperFunctionResult ResultBuffer
) {
110 SPSInputBuffer
IB(ResultBuffer
.data(), ResultBuffer
.size());
111 EXPECT_TRUE(SPSArgList
<int32_t>::deserialize(IB
, Result
));
112 RP
.set_value(Result
);
114 AddAsyncTagAddr
, ArrayRef
<char>(ArgBuffer
.data(), ArgBuffer
.size()));
116 EXPECT_EQ(RF
.get(), (int32_t)3);
118 cantFail(ES
.endSession());