Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp
35266 views
//===---- EPCGenericJITLinkMemoryManager.cpp -- Mem management via EPC ----===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h"910#include "llvm/ExecutionEngine/JITLink/JITLink.h"11#include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h"12#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"1314#include <limits>1516using namespace llvm::jitlink;1718namespace llvm {19namespace orc {2021class EPCGenericJITLinkMemoryManager::InFlightAlloc22: public jitlink::JITLinkMemoryManager::InFlightAlloc {23public:2425// FIXME: The C++98 initializer is an attempt to work around compile failures26// due to http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1397.27// We should be able to switch this back to member initialization once that28// issue is fixed.29struct SegInfo {30SegInfo() : WorkingMem(nullptr), ContentSize(0), ZeroFillSize(0) {}3132char *WorkingMem;33ExecutorAddr Addr;34uint64_t ContentSize;35uint64_t ZeroFillSize;36};3738using SegInfoMap = AllocGroupSmallMap<SegInfo>;3940InFlightAlloc(EPCGenericJITLinkMemoryManager &Parent, LinkGraph &G,41ExecutorAddr AllocAddr, SegInfoMap Segs)42: Parent(Parent), G(G), AllocAddr(AllocAddr), Segs(std::move(Segs)) {}4344void finalize(OnFinalizedFunction OnFinalize) override {45tpctypes::FinalizeRequest FR;46for (auto &KV : Segs) {47assert(KV.second.ContentSize <= std::numeric_limits<size_t>::max());48FR.Segments.push_back(tpctypes::SegFinalizeRequest{49KV.first,50KV.second.Addr,51alignTo(KV.second.ContentSize + KV.second.ZeroFillSize,52Parent.EPC.getPageSize()),53{KV.second.WorkingMem, static_cast<size_t>(KV.second.ContentSize)}});54}5556// Transfer allocation actions.57std::swap(FR.Actions, G.allocActions());5859Parent.EPC.callSPSWrapperAsync<60rt::SPSSimpleExecutorMemoryManagerFinalizeSignature>(61Parent.SAs.Finalize,62[OnFinalize = std::move(OnFinalize), AllocAddr = this->AllocAddr](63Error SerializationErr, Error FinalizeErr) mutable {64// FIXME: Release abandoned alloc.65if (SerializationErr) {66cantFail(std::move(FinalizeErr));67OnFinalize(std::move(SerializationErr));68} else if (FinalizeErr)69OnFinalize(std::move(FinalizeErr));70else71OnFinalize(FinalizedAlloc(AllocAddr));72},73Parent.SAs.Allocator, std::move(FR));74}7576void abandon(OnAbandonedFunction OnAbandoned) override {77// FIXME: Return memory to pool instead.78Parent.EPC.callSPSWrapperAsync<79rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>(80Parent.SAs.Deallocate,81[OnAbandoned = std::move(OnAbandoned)](Error SerializationErr,82Error DeallocateErr) mutable {83if (SerializationErr) {84cantFail(std::move(DeallocateErr));85OnAbandoned(std::move(SerializationErr));86} else87OnAbandoned(std::move(DeallocateErr));88},89Parent.SAs.Allocator, ArrayRef<ExecutorAddr>(AllocAddr));90}9192private:93EPCGenericJITLinkMemoryManager &Parent;94LinkGraph &G;95ExecutorAddr AllocAddr;96SegInfoMap Segs;97};9899void EPCGenericJITLinkMemoryManager::allocate(const JITLinkDylib *JD,100LinkGraph &G,101OnAllocatedFunction OnAllocated) {102BasicLayout BL(G);103104auto Pages = BL.getContiguousPageBasedLayoutSizes(EPC.getPageSize());105if (!Pages)106return OnAllocated(Pages.takeError());107108EPC.callSPSWrapperAsync<rt::SPSSimpleExecutorMemoryManagerReserveSignature>(109SAs.Reserve,110[this, BL = std::move(BL), OnAllocated = std::move(OnAllocated)](111Error SerializationErr, Expected<ExecutorAddr> AllocAddr) mutable {112if (SerializationErr) {113cantFail(AllocAddr.takeError());114return OnAllocated(std::move(SerializationErr));115}116if (!AllocAddr)117return OnAllocated(AllocAddr.takeError());118119completeAllocation(*AllocAddr, std::move(BL), std::move(OnAllocated));120},121SAs.Allocator, Pages->total());122}123124void EPCGenericJITLinkMemoryManager::deallocate(125std::vector<FinalizedAlloc> Allocs, OnDeallocatedFunction OnDeallocated) {126EPC.callSPSWrapperAsync<127rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>(128SAs.Deallocate,129[OnDeallocated = std::move(OnDeallocated)](Error SerErr,130Error DeallocErr) mutable {131if (SerErr) {132cantFail(std::move(DeallocErr));133OnDeallocated(std::move(SerErr));134} else135OnDeallocated(std::move(DeallocErr));136},137SAs.Allocator, Allocs);138for (auto &A : Allocs)139A.release();140}141142void EPCGenericJITLinkMemoryManager::completeAllocation(143ExecutorAddr AllocAddr, BasicLayout BL, OnAllocatedFunction OnAllocated) {144145InFlightAlloc::SegInfoMap SegInfos;146147ExecutorAddr NextSegAddr = AllocAddr;148for (auto &KV : BL.segments()) {149const auto &AG = KV.first;150auto &Seg = KV.second;151152Seg.Addr = NextSegAddr;153KV.second.WorkingMem = BL.getGraph().allocateBuffer(Seg.ContentSize).data();154NextSegAddr += ExecutorAddrDiff(155alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize()));156157auto &SegInfo = SegInfos[AG];158SegInfo.ContentSize = Seg.ContentSize;159SegInfo.ZeroFillSize = Seg.ZeroFillSize;160SegInfo.Addr = Seg.Addr;161SegInfo.WorkingMem = Seg.WorkingMem;162}163164if (auto Err = BL.apply())165return OnAllocated(std::move(Err));166167OnAllocated(std::make_unique<InFlightAlloc>(*this, BL.getGraph(), AllocAddr,168std::move(SegInfos)));169}170171} // end namespace orc172} // end namespace llvm173174175