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 static llvm::orc::shared::detail::CWrapperFunctionResult 34 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 pointerToJITTargetAddress(addWrapper), Result, 2, 3), 44 Succeeded()); 45 EXPECT_EQ(Result, 5); 46 } 47 48 TEST(ExecutionSessionWrapperFunctionCalls, RunVoidWrapperAsyncTemplate) { 49 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create())); 50 51 std::promise<MSVCPError> RP; 52 ES.callSPSWrapperAsync<void()>( 53 [&](Error SerializationErr) { 54 RP.set_value(std::move(SerializationErr)); 55 }, 56 pointerToJITTargetAddress(voidWrapper)); 57 Error Err = RP.get_future().get(); 58 EXPECT_THAT_ERROR(std::move(Err), Succeeded()); 59 } 60 61 TEST(ExecutionSessionWrapperFunctionCalls, RunNonVoidWrapperAsyncTemplate) { 62 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create())); 63 64 std::promise<MSVCPExpected<int32_t>> RP; 65 ES.callSPSWrapperAsync<int32_t(int32_t, int32_t)>( 66 [&](Error SerializationErr, int32_t R) { 67 if (SerializationErr) 68 RP.set_value(std::move(SerializationErr)); 69 RP.set_value(std::move(R)); 70 }, 71 pointerToJITTargetAddress(addWrapper), 2, 3); 72 Expected<int32_t> Result = RP.get_future().get(); 73 EXPECT_THAT_EXPECTED(Result, HasValue(5)); 74 } 75 76 TEST(ExecutionSessionWrapperFunctionCalls, RegisterAsyncHandlerAndRun) { 77 78 constexpr JITTargetAddress AddAsyncTagAddr = 0x01; 79 80 ExecutionSession ES(cantFail(SelfExecutorProcessControl::Create())); 81 auto &JD = ES.createBareJITDylib("JD"); 82 83 auto AddAsyncTag = ES.intern("addAsync_tag"); 84 cantFail(JD.define(absoluteSymbols( 85 {{AddAsyncTag, 86 JITEvaluatedSymbol(AddAsyncTagAddr, JITSymbolFlags::Exported)}}))); 87 88 ExecutionSession::JITDispatchHandlerAssociationMap Associations; 89 90 Associations[AddAsyncTag] = 91 ES.wrapAsyncWithSPS<int32_t(int32_t, int32_t)>(addAsyncWrapper); 92 93 cantFail(ES.registerJITDispatchHandlers(JD, std::move(Associations))); 94 95 std::promise<int32_t> RP; 96 auto RF = RP.get_future(); 97 98 using ArgSerialization = SPSArgList<int32_t, int32_t>; 99 size_t ArgBufferSize = ArgSerialization::size(1, 2); 100 auto ArgBuffer = WrapperFunctionResult::allocate(ArgBufferSize); 101 SPSOutputBuffer OB(ArgBuffer.data(), ArgBuffer.size()); 102 EXPECT_TRUE(ArgSerialization::serialize(OB, 1, 2)); 103 104 ES.runJITDispatchHandler( 105 [&](WrapperFunctionResult ResultBuffer) { 106 int32_t Result; 107 SPSInputBuffer IB(ResultBuffer.data(), ResultBuffer.size()); 108 EXPECT_TRUE(SPSArgList<int32_t>::deserialize(IB, Result)); 109 RP.set_value(Result); 110 }, 111 AddAsyncTagAddr, ArrayRef<char>(ArgBuffer.data(), ArgBuffer.size())); 112 113 EXPECT_EQ(RF.get(), (int32_t)3); 114 115 cantFail(ES.endSession()); 116 } 117