| 1 | //===---- SimpleRemoteMemoryMapper.cpp - Remote memory mapper ----*- C++ -*-==// |
| 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/SimpleRemoteMemoryMapper.h" |
| 10 | |
| 11 | #include "llvm/ExecutionEngine/JITLink/JITLink.h" |
| 12 | #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h" |
| 13 | |
| 14 | namespace llvm::orc { |
| 15 | |
| 16 | SimpleRemoteMemoryMapper::SimpleRemoteMemoryMapper(ExecutorProcessControl &EPC, |
| 17 | SymbolAddrs SAs) |
| 18 | : EPC(EPC), SAs(SAs) {} |
| 19 | |
| 20 | void SimpleRemoteMemoryMapper::reserve(size_t NumBytes, |
| 21 | OnReservedFunction OnReserved) { |
| 22 | EPC.callSPSWrapperAsync<rt::SPSSimpleRemoteMemoryMapReserveSignature>( |
| 23 | WrapperFnAddr: SAs.Reserve, |
| 24 | SendResult: [NumBytes, OnReserved = std::move(OnReserved)]( |
| 25 | Error SerializationErr, Expected<ExecutorAddr> Result) mutable { |
| 26 | if (SerializationErr) { |
| 27 | cantFail(Err: Result.takeError()); |
| 28 | return OnReserved(std::move(SerializationErr)); |
| 29 | } |
| 30 | |
| 31 | if (Result) |
| 32 | OnReserved(ExecutorAddrRange(*Result, NumBytes)); |
| 33 | else |
| 34 | OnReserved(Result.takeError()); |
| 35 | }, |
| 36 | Args: SAs.Instance, Args: static_cast<uint64_t>(NumBytes)); |
| 37 | } |
| 38 | |
| 39 | char *SimpleRemoteMemoryMapper::prepare(jitlink::LinkGraph &G, |
| 40 | ExecutorAddr Addr, size_t ContentSize) { |
| 41 | return G.allocateBuffer(Size: ContentSize).data(); |
| 42 | } |
| 43 | |
| 44 | void SimpleRemoteMemoryMapper::initialize(MemoryMapper::AllocInfo &AI, |
| 45 | OnInitializedFunction OnInitialized) { |
| 46 | |
| 47 | tpctypes::FinalizeRequest FR; |
| 48 | |
| 49 | std::swap(x&: FR.Actions, y&: AI.Actions); |
| 50 | FR.Segments.reserve(n: AI.Segments.size()); |
| 51 | |
| 52 | for (auto Seg : AI.Segments) |
| 53 | FR.Segments.push_back(x: {.RAG: Seg.AG, .Addr: AI.MappingBase + Seg.Offset, |
| 54 | .Size: Seg.ContentSize + Seg.ZeroFillSize, |
| 55 | .Content: ArrayRef<char>(Seg.WorkingMem, Seg.ContentSize)}); |
| 56 | |
| 57 | EPC.callSPSWrapperAsync<rt::SPSSimpleRemoteMemoryMapInitializeSignature>( |
| 58 | WrapperFnAddr: SAs.Initialize, |
| 59 | SendResult: [OnInitialized = std::move(OnInitialized)]( |
| 60 | Error SerializationErr, Expected<ExecutorAddr> Result) mutable { |
| 61 | if (SerializationErr) { |
| 62 | cantFail(Err: Result.takeError()); |
| 63 | return OnInitialized(std::move(SerializationErr)); |
| 64 | } |
| 65 | |
| 66 | OnInitialized(std::move(Result)); |
| 67 | }, |
| 68 | Args: SAs.Instance, Args: std::move(FR)); |
| 69 | } |
| 70 | |
| 71 | void SimpleRemoteMemoryMapper::deinitialize( |
| 72 | ArrayRef<ExecutorAddr> Allocations, |
| 73 | MemoryMapper::OnDeinitializedFunction OnDeinitialized) { |
| 74 | EPC.callSPSWrapperAsync<rt::SPSSimpleRemoteMemoryMapDeinitializeSignature>( |
| 75 | WrapperFnAddr: SAs.Deinitialize, |
| 76 | SendResult: [OnDeinitialized = std::move(OnDeinitialized)](Error SerializationErr, |
| 77 | Error Result) mutable { |
| 78 | if (SerializationErr) { |
| 79 | cantFail(Err: std::move(Result)); |
| 80 | return OnDeinitialized(std::move(SerializationErr)); |
| 81 | } |
| 82 | |
| 83 | OnDeinitialized(std::move(Result)); |
| 84 | }, |
| 85 | Args: SAs.Instance, Args: Allocations); |
| 86 | } |
| 87 | |
| 88 | void SimpleRemoteMemoryMapper::release(ArrayRef<ExecutorAddr> Bases, |
| 89 | OnReleasedFunction OnReleased) { |
| 90 | EPC.callSPSWrapperAsync<rt::SPSSimpleRemoteMemoryMapReleaseSignature>( |
| 91 | WrapperFnAddr: SAs.Release, |
| 92 | SendResult: [OnReleased = std::move(OnReleased)](Error SerializationErr, |
| 93 | Error Result) mutable { |
| 94 | if (SerializationErr) { |
| 95 | cantFail(Err: std::move(Result)); |
| 96 | return OnReleased(std::move(SerializationErr)); |
| 97 | } |
| 98 | |
| 99 | return OnReleased(std::move(Result)); |
| 100 | }, |
| 101 | Args: SAs.Instance, Args: Bases); |
| 102 | } |
| 103 | |
| 104 | } // namespace llvm::orc |
| 105 | |