Debian Codegraph @

llvm/unittests/ExecutionEngine/Orc/ExecutionSessionWrapperFunctionCallsTest.cpp

raw
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/ExecutionEngine/Orc/SelfExecutorProcessControl.h"
13 #include "llvm/Support/MSVCErrorWorkarounds.h"
14 #include "llvm/Testing/Support/Error.h"
15 #include "gtest/gtest.h"
16
17 #include <future>
18
19 using namespace llvm;
20 using namespace llvm::orc;
21 using namespace llvm::orc::shared;
22
23 static CWrapperFunctionResult addWrapper(const char *ArgData, 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 CWrapperFunctionResult voidWrapper(const char *ArgData, size_t ArgSize) {
35 return WrapperFunction<void()>::handle(ArgData, ArgSize, []() {}).release();
36 }
37
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
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
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
79 TEST(ExecutionSessionWrapperFunctionCalls, RegisterAsyncHandlerAndRun) {
80
81 constexpr ExecutorAddr 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, {AddAsyncTagAddr, JITSymbolFlags::Exported}}})));
89
90 ExecutionSession::JITDispatchHandlerAssociationMap Associations;
91
92 Associations[AddAsyncTag] =
93 ES.wrapAsyncWithSPS<int32_t(int32_t, int32_t)>(addAsyncWrapper);
94
95 cantFail(ES.registerJITDispatchHandlers(JD, std::move(Associations)));
96
97 std::promise<int32_t> RP;
98 auto RF = RP.get_future();
99
100 using ArgSerialization = SPSArgList<int32_t, int32_t>;
101 size_t ArgBufferSize = ArgSerialization::size(1, 2);
102 auto ArgBuffer = WrapperFunctionResult::allocate(ArgBufferSize);
103 SPSOutputBuffer OB(ArgBuffer.data(), ArgBuffer.size());
104 EXPECT_TRUE(ArgSerialization::serialize(OB, 1, 2));
105
106 ES.runJITDispatchHandler(
107 [&](WrapperFunctionResult ResultBuffer) {
108 int32_t Result;
109 SPSInputBuffer IB(ResultBuffer.data(), ResultBuffer.size());
110 EXPECT_TRUE(SPSArgList<int32_t>::deserialize(IB, Result));
111 RP.set_value(Result);
112 },
113 AddAsyncTagAddr, ArrayRef<char>(ArgBuffer));
114
115 EXPECT_EQ(RF.get(), (int32_t)3);
116
117 cantFail(ES.endSession());
118 }