1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 04:02:41 +01:00
llvm-mirror/unittests/ExecutionEngine/Orc/ExecutionSessionWrapperFunctionCallsTest.cpp
Lang Hames d08c2fabb8 [ORC] Require ExecutorProcessControl when constructing an ExecutionSession.
Wrapper function call and dispatch handler helpers are moved to
ExecutionSession, and existing EPC-based tools are re-written to take an
ExecutionSession argument instead.

Requiring an ExecutorProcessControl instance simplifies existing EPC based
utilities (which only need to take an ES now), and should encourage more
utilities to use the EPC interface. It also simplifies process termination,
since the session can automatically call ExecutorProcessControl::disconnect
(previously this had to be done manually, and carefully ordered with the
rest of JIT tear-down to work correctly).
2021-07-27 16:53:49 +10:00

102 lines
3.5 KiB
C++

//===- ExecutionSessionWrapperFunctionCallsTest.cpp -- Test wrapper calls -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
#include "llvm/Support/MSVCErrorWorkarounds.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <future>
using namespace llvm;
using namespace llvm::orc;
using namespace llvm::orc::shared;
static llvm::orc::shared::detail::CWrapperFunctionResult
addWrapper(const char *ArgData, size_t ArgSize) {
return WrapperFunction<int32_t(int32_t, int32_t)>::handle(
ArgData, ArgSize, [](int32_t X, int32_t Y) { return X + Y; })
.release();
}
static void addAsyncWrapper(unique_function<void(int32_t)> SendResult,
int32_t X, int32_t Y) {
SendResult(X + Y);
}
TEST(ExecutionSessionWrapperFunctionCalls, RunWrapperTemplate) {
ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
int32_t Result;
EXPECT_THAT_ERROR(ES.callSPSWrapper<int32_t(int32_t, int32_t)>(
pointerToJITTargetAddress(addWrapper), Result, 2, 3),
Succeeded());
EXPECT_EQ(Result, 5);
}
TEST(ExecutionSessionWrapperFunctionCalls, RunWrapperAsyncTemplate) {
ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
std::promise<MSVCPExpected<int32_t>> RP;
using Sig = int32_t(int32_t, int32_t);
ES.callSPSWrapperAsync<Sig>(
[&](Error SerializationErr, int32_t R) {
if (SerializationErr)
RP.set_value(std::move(SerializationErr));
RP.set_value(std::move(R));
},
pointerToJITTargetAddress(addWrapper), 2, 3);
Expected<int32_t> Result = RP.get_future().get();
EXPECT_THAT_EXPECTED(Result, HasValue(5));
}
TEST(ExecutionSessionWrapperFunctionCalls, RegisterAsyncHandlerAndRun) {
constexpr JITTargetAddress AddAsyncTagAddr = 0x01;
ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create()));
auto &JD = ES.createBareJITDylib("JD");
auto AddAsyncTag = ES.intern("addAsync_tag");
cantFail(JD.define(absoluteSymbols(
{{AddAsyncTag,
JITEvaluatedSymbol(AddAsyncTagAddr, JITSymbolFlags::Exported)}})));
ExecutionSession::JITDispatchHandlerAssociationMap Associations;
Associations[AddAsyncTag] =
ES.wrapAsyncWithSPS<int32_t(int32_t, int32_t)>(addAsyncWrapper);
cantFail(ES.registerJITDispatchHandlers(JD, std::move(Associations)));
std::promise<int32_t> RP;
auto RF = RP.get_future();
using ArgSerialization = SPSArgList<int32_t, int32_t>;
size_t ArgBufferSize = ArgSerialization::size(1, 2);
WrapperFunctionResult ArgBuffer;
char *ArgBufferData =
WrapperFunctionResult::allocate(ArgBuffer, ArgBufferSize);
SPSOutputBuffer OB(ArgBufferData, ArgBufferSize);
EXPECT_TRUE(ArgSerialization::serialize(OB, 1, 2));
ES.runJITDispatchHandler(
[&](WrapperFunctionResult ResultBuffer) {
int32_t Result;
SPSInputBuffer IB(ResultBuffer.data(), ResultBuffer.size());
EXPECT_TRUE(SPSArgList<int32_t>::deserialize(IB, Result));
RP.set_value(Result);
},
AddAsyncTagAddr, ArrayRef<char>(ArgBuffer.data(), ArgBuffer.size()));
EXPECT_EQ(RF.get(), (int32_t)3);
cantFail(ES.endSession());
}