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/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"
14
15 #include <future>
16
17 using namespace llvm;
18 using namespace llvm::orc;
19 using namespace llvm::orc::shared;
20
addWrapper(const char * ArgData,size_t ArgSize)21 static llvm::orc::shared::CWrapperFunctionResult addWrapper(const char *ArgData,
22 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; })
25 .release();
26 }
27
addAsyncWrapper(unique_function<void (int32_t)> SendResult,int32_t X,int32_t Y)28 static void addAsyncWrapper(unique_function<void(int32_t)> SendResult,
29 int32_t X, int32_t Y) {
30 SendResult(X + Y);
31 }
32
33 static llvm::orc::shared::CWrapperFunctionResult
voidWrapper(const char * ArgData,size_t ArgSize)34 voidWrapper(const char *ArgData, size_t ArgSize) {
35 return WrapperFunction<void()>::handle(ArgData, ArgSize, []() {}).release();
36 }
37
TEST(ExecutionSessionWrapperFunctionCalls,RunWrapperTemplate)38 TEST(ExecutionSessionWrapperFunctionCalls, RunWrapperTemplate) {
39 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
40
41 int32_t Result;
42 EXPECT_THAT_ERROR(ES.callSPSWrapper<int32_t(int32_t, int32_t)>(
43 ExecutorAddr::fromPtr(addWrapper), Result, 2, 3),
44 Succeeded());
45 EXPECT_EQ(Result, 5);
46 cantFail(ES.endSession());
47 }
48
TEST(ExecutionSessionWrapperFunctionCalls,RunVoidWrapperAsyncTemplate)49 TEST(ExecutionSessionWrapperFunctionCalls, RunVoidWrapperAsyncTemplate) {
50 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
51
52 std::promise<MSVCPError> RP;
53 ES.callSPSWrapperAsync<void()>(ExecutorAddr::fromPtr(voidWrapper),
54 [&](Error SerializationErr) {
55 RP.set_value(std::move(SerializationErr));
56 });
57 Error Err = RP.get_future().get();
58 EXPECT_THAT_ERROR(std::move(Err), Succeeded());
59 cantFail(ES.endSession());
60 }
61
TEST(ExecutionSessionWrapperFunctionCalls,RunNonVoidWrapperAsyncTemplate)62 TEST(ExecutionSessionWrapperFunctionCalls, RunNonVoidWrapperAsyncTemplate) {
63 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
64
65 std::promise<MSVCPExpected<int32_t>> RP;
66 ES.callSPSWrapperAsync<int32_t(int32_t, int32_t)>(
67 ExecutorAddr::fromPtr(addWrapper),
68 [&](Error SerializationErr, int32_t R) {
69 if (SerializationErr)
70 RP.set_value(std::move(SerializationErr));
71 RP.set_value(std::move(R));
72 },
73 2, 3);
74 Expected<int32_t> Result = RP.get_future().get();
75 EXPECT_THAT_EXPECTED(Result, HasValue(5));
76 cantFail(ES.endSession());
77 }
78
TEST(ExecutionSessionWrapperFunctionCalls,RegisterAsyncHandlerAndRun)79 TEST(ExecutionSessionWrapperFunctionCalls, RegisterAsyncHandlerAndRun) {
80
81 constexpr JITTargetAddress AddAsyncTagAddr = 0x01;
82
83 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
84 auto &JD = ES.createBareJITDylib("JD");
85
86 auto AddAsyncTag = ES.intern("addAsync_tag");
87 cantFail(JD.define(absoluteSymbols(
88 {{AddAsyncTag,
89 JITEvaluatedSymbol(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