xref: /llvm-project/llvm/unittests/ExecutionEngine/Orc/ExecutionSessionWrapperFunctionCallsTest.cpp (revision dc11c0601577afb8f67513d041ee25dabe3555b9)
1 //===- ExecutionSessionWrapperFunctionCallsTest.cpp -- Test wrapper calls -===//
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 #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"
15 
16 #include <future>
17 
18 using namespace llvm;
19 using namespace llvm::orc;
20 using namespace llvm::orc::shared;
21 
22 static llvm::orc::shared::CWrapperFunctionResult addWrapper(const char *ArgData,
23                                                             size_t ArgSize) {
24   return WrapperFunction<int32_t(int32_t, int32_t)>::handle(
25              ArgData, ArgSize, [](int32_t X, int32_t Y) { return X + Y; })
26       .release();
27 }
28 
29 static void addAsyncWrapper(unique_function<void(int32_t)> SendResult,
30                             int32_t X, int32_t Y) {
31   SendResult(X + Y);
32 }
33 
34 static llvm::orc::shared::CWrapperFunctionResult
35 voidWrapper(const char *ArgData, size_t ArgSize) {
36   return WrapperFunction<void()>::handle(ArgData, ArgSize, []() {}).release();
37 }
38 
39 TEST(ExecutionSessionWrapperFunctionCalls, RunWrapperTemplate) {
40   ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
41 
42   int32_t Result;
43   EXPECT_THAT_ERROR(ES.callSPSWrapper<int32_t(int32_t, int32_t)>(
44                         ExecutorAddr::fromPtr(addWrapper), Result, 2, 3),
45                     Succeeded());
46   EXPECT_EQ(Result, 5);
47   cantFail(ES.endSession());
48 }
49 
50 TEST(ExecutionSessionWrapperFunctionCalls, RunVoidWrapperAsyncTemplate) {
51   ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
52 
53   std::promise<MSVCPError> RP;
54   ES.callSPSWrapperAsync<void()>(ExecutorAddr::fromPtr(voidWrapper),
55                                  [&](Error SerializationErr) {
56                                    RP.set_value(std::move(SerializationErr));
57                                  });
58   Error Err = RP.get_future().get();
59   EXPECT_THAT_ERROR(std::move(Err), Succeeded());
60   cantFail(ES.endSession());
61 }
62 
63 TEST(ExecutionSessionWrapperFunctionCalls, RunNonVoidWrapperAsyncTemplate) {
64   ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
65 
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) {
70         if (SerializationErr)
71           RP.set_value(std::move(SerializationErr));
72         RP.set_value(std::move(R));
73       },
74       2, 3);
75   Expected<int32_t> Result = RP.get_future().get();
76   EXPECT_THAT_EXPECTED(Result, HasValue(5));
77   cantFail(ES.endSession());
78 }
79 
80 TEST(ExecutionSessionWrapperFunctionCalls, RegisterAsyncHandlerAndRun) {
81 
82   constexpr ExecutorAddr AddAsyncTagAddr(0x01);
83 
84   ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
85   auto &JD = ES.createBareJITDylib("JD");
86 
87   auto AddAsyncTag = ES.intern("addAsync_tag");
88   cantFail(JD.define(absoluteSymbols(
89       {{AddAsyncTag, {AddAsyncTagAddr, JITSymbolFlags::Exported}}})));
90 
91   ExecutionSession::JITDispatchHandlerAssociationMap Associations;
92 
93   Associations[AddAsyncTag] =
94       ES.wrapAsyncWithSPS<int32_t(int32_t, int32_t)>(addAsyncWrapper);
95 
96   cantFail(ES.registerJITDispatchHandlers(JD, std::move(Associations)));
97 
98   std::promise<int32_t> RP;
99   auto RF = RP.get_future();
100 
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));
106 
107   ES.runJITDispatchHandler(
108       [&](WrapperFunctionResult ResultBuffer) {
109         int32_t Result;
110         SPSInputBuffer IB(ResultBuffer.data(), ResultBuffer.size());
111         EXPECT_TRUE(SPSArgList<int32_t>::deserialize(IB, Result));
112         RP.set_value(Result);
113       },
114       AddAsyncTagAddr, ArrayRef<char>(ArgBuffer.data(), ArgBuffer.size()));
115 
116   EXPECT_EQ(RF.get(), (int32_t)3);
117 
118   cantFail(ES.endSession());
119 }
120