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 21 static llvm::orc::shared::detail::CWrapperFunctionResult 22 addWrapper(const char *ArgData, 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 28 static void addAsyncWrapper(unique_function<void(int32_t)> SendResult, 29 int32_t X, int32_t Y) { 30 SendResult(X + Y); 31 } 32 33 TEST(ExecutionSessionWrapperFunctionCalls, RunWrapperTemplate) { 34 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create())); 35 36 int32_t Result; 37 EXPECT_THAT_ERROR(ES.callSPSWrapper<int32_t(int32_t, int32_t)>( 38 pointerToJITTargetAddress(addWrapper), Result, 2, 3), 39 Succeeded()); 40 EXPECT_EQ(Result, 5); 41 } 42 43 TEST(ExecutionSessionWrapperFunctionCalls, RunWrapperAsyncTemplate) { 44 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create())); 45 46 std::promise<MSVCPExpected<int32_t>> RP; 47 using Sig = int32_t(int32_t, int32_t); 48 ES.callSPSWrapperAsync<Sig>( 49 [&](Error SerializationErr, int32_t R) { 50 if (SerializationErr) 51 RP.set_value(std::move(SerializationErr)); 52 RP.set_value(std::move(R)); 53 }, 54 pointerToJITTargetAddress(addWrapper), 2, 3); 55 Expected<int32_t> Result = RP.get_future().get(); 56 EXPECT_THAT_EXPECTED(Result, HasValue(5)); 57 } 58 59 TEST(ExecutionSessionWrapperFunctionCalls, RegisterAsyncHandlerAndRun) { 60 61 constexpr JITTargetAddress AddAsyncTagAddr = 0x01; 62 63 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create())); 64 auto &JD = ES.createBareJITDylib("JD"); 65 66 auto AddAsyncTag = ES.intern("addAsync_tag"); 67 cantFail(JD.define(absoluteSymbols( 68 {{AddAsyncTag, 69 JITEvaluatedSymbol(AddAsyncTagAddr, JITSymbolFlags::Exported)}}))); 70 71 ExecutionSession::JITDispatchHandlerAssociationMap Associations; 72 73 Associations[AddAsyncTag] = 74 ES.wrapAsyncWithSPS<int32_t(int32_t, int32_t)>(addAsyncWrapper); 75 76 cantFail(ES.registerJITDispatchHandlers(JD, std::move(Associations))); 77 78 std::promise<int32_t> RP; 79 auto RF = RP.get_future(); 80 81 using ArgSerialization = SPSArgList<int32_t, int32_t>; 82 size_t ArgBufferSize = ArgSerialization::size(1, 2); 83 WrapperFunctionResult ArgBuffer; 84 char *ArgBufferData = 85 WrapperFunctionResult::allocate(ArgBuffer, ArgBufferSize); 86 SPSOutputBuffer OB(ArgBufferData, ArgBufferSize); 87 EXPECT_TRUE(ArgSerialization::serialize(OB, 1, 2)); 88 89 ES.runJITDispatchHandler( 90 [&](WrapperFunctionResult ResultBuffer) { 91 int32_t Result; 92 SPSInputBuffer IB(ResultBuffer.data(), ResultBuffer.size()); 93 EXPECT_TRUE(SPSArgList<int32_t>::deserialize(IB, Result)); 94 RP.set_value(Result); 95 }, 96 AddAsyncTagAddr, ArrayRef<char>(ArgBuffer.data(), ArgBuffer.size())); 97 98 EXPECT_EQ(RF.get(), (int32_t)3); 99 100 cantFail(ES.endSession()); 101 } 102