1 //===---- EPCGenericJITLinkMemoryManager.cpp -- Mem management via EPC ----===//
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/EPCGenericJITLinkMemoryManager.h"
10 
11 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
12 #include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h"
13 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
14 
15 #include <limits>
16 
17 using namespace llvm::jitlink;
18 
19 namespace llvm {
20 namespace orc {
21 
22 class EPCGenericJITLinkMemoryManager::InFlightAlloc
23     : public jitlink::JITLinkMemoryManager::InFlightAlloc {
24 public:
25 
26   // FIXME: The C++98 initializer is an attempt to work around compile failures
27   // due to http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1397.
28   // We should be able to switch this back to member initialization once that
29   // issue is fixed.
30   struct SegInfo {
31     SegInfo() : WorkingMem(nullptr), ContentSize(0), ZeroFillSize(0) {}
32 
33     char *WorkingMem;
34     ExecutorAddr Addr;
35     uint64_t ContentSize;
36     uint64_t ZeroFillSize;
37   };
38 
39   using SegInfoMap = AllocGroupSmallMap<SegInfo>;
40 
41   InFlightAlloc(EPCGenericJITLinkMemoryManager &Parent, LinkGraph &G,
42                 ExecutorAddr AllocAddr, SegInfoMap Segs)
43       : Parent(Parent), G(G), AllocAddr(AllocAddr), Segs(std::move(Segs)) {}
44 
45   void finalize(OnFinalizedFunction OnFinalize) override {
46     tpctypes::FinalizeRequest FR;
47     for (auto &KV : Segs) {
48       assert(KV.second.ContentSize <= std::numeric_limits<size_t>::max());
49       FR.Segments.push_back(tpctypes::SegFinalizeRequest{
50           tpctypes::toWireProtectionFlags(
51               toSysMemoryProtectionFlags(KV.first.getMemProt())),
52           KV.second.Addr,
53           alignTo(KV.second.ContentSize + KV.second.ZeroFillSize,
54                   Parent.EPC.getPageSize()),
55           {KV.second.WorkingMem, static_cast<size_t>(KV.second.ContentSize)}});
56     }
57 
58     // Transfer allocation actions.
59     // FIXME: Merge JITLink and ORC SupportFunctionCall and Action list types,
60     //        turn this into a std::swap.
61     FR.Actions.reserve(G.allocActions().size());
62     for (auto &ActPair : G.allocActions())
63       FR.Actions.push_back(
64           {{ExecutorAddr(ActPair.Finalize.FnAddr),
65             ExecutorAddr(ActPair.Finalize.CtxAddr), ActPair.Finalize.CtxSize},
66            {ExecutorAddr(ActPair.Dealloc.FnAddr),
67             ExecutorAddr(ActPair.Dealloc.CtxAddr), ActPair.Dealloc.CtxSize}});
68     G.allocActions().clear();
69 
70     Parent.EPC.callSPSWrapperAsync<
71         rt::SPSSimpleExecutorMemoryManagerFinalizeSignature>(
72         Parent.SAs.Finalize,
73         [OnFinalize = std::move(OnFinalize), AllocAddr = this->AllocAddr](
74             Error SerializationErr, Error FinalizeErr) mutable {
75           // FIXME: Release abandoned alloc.
76           if (SerializationErr) {
77             cantFail(std::move(FinalizeErr));
78             OnFinalize(std::move(SerializationErr));
79           } else if (FinalizeErr)
80             OnFinalize(std::move(FinalizeErr));
81           else
82             OnFinalize(FinalizedAlloc(AllocAddr.getValue()));
83         },
84         Parent.SAs.Allocator, std::move(FR));
85   }
86 
87   void abandon(OnAbandonedFunction OnAbandoned) override {
88     // FIXME: Return memory to pool instead.
89     Parent.EPC.callSPSWrapperAsync<
90         rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>(
91         Parent.SAs.Deallocate,
92         [OnAbandoned = std::move(OnAbandoned)](Error SerializationErr,
93                                                Error DeallocateErr) mutable {
94           if (SerializationErr) {
95             cantFail(std::move(DeallocateErr));
96             OnAbandoned(std::move(SerializationErr));
97           } else
98             OnAbandoned(std::move(DeallocateErr));
99         },
100         Parent.SAs.Allocator, ArrayRef<ExecutorAddr>(AllocAddr));
101   }
102 
103 private:
104   EPCGenericJITLinkMemoryManager &Parent;
105   LinkGraph &G;
106   ExecutorAddr AllocAddr;
107   SegInfoMap Segs;
108 };
109 
110 void EPCGenericJITLinkMemoryManager::allocate(const JITLinkDylib *JD,
111                                               LinkGraph &G,
112                                               OnAllocatedFunction OnAllocated) {
113   BasicLayout BL(G);
114 
115   auto Pages = BL.getContiguousPageBasedLayoutSizes(EPC.getPageSize());
116   if (!Pages)
117     return OnAllocated(Pages.takeError());
118 
119   EPC.callSPSWrapperAsync<rt::SPSSimpleExecutorMemoryManagerReserveSignature>(
120       SAs.Reserve,
121       [this, BL = std::move(BL), OnAllocated = std::move(OnAllocated)](
122           Error SerializationErr, Expected<ExecutorAddr> AllocAddr) mutable {
123         if (SerializationErr) {
124           cantFail(AllocAddr.takeError());
125           return OnAllocated(std::move(SerializationErr));
126         }
127         if (!AllocAddr)
128           return OnAllocated(AllocAddr.takeError());
129 
130         completeAllocation(*AllocAddr, std::move(BL), std::move(OnAllocated));
131       },
132       SAs.Allocator, Pages->total());
133 }
134 
135 void EPCGenericJITLinkMemoryManager::deallocate(
136     std::vector<FinalizedAlloc> Allocs, OnDeallocatedFunction OnDeallocated) {
137   EPC.callSPSWrapperAsync<
138       rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>(
139       SAs.Deallocate,
140       [OnDeallocated = std::move(OnDeallocated)](Error SerErr,
141                                                  Error DeallocErr) mutable {
142         if (SerErr) {
143           cantFail(std::move(DeallocErr));
144           OnDeallocated(std::move(SerErr));
145         } else
146           OnDeallocated(std::move(DeallocErr));
147       },
148       SAs.Allocator, Allocs);
149   for (auto &A : Allocs)
150     A.release();
151 }
152 
153 void EPCGenericJITLinkMemoryManager::completeAllocation(
154     ExecutorAddr AllocAddr, BasicLayout BL, OnAllocatedFunction OnAllocated) {
155 
156   InFlightAlloc::SegInfoMap SegInfos;
157 
158   ExecutorAddr NextSegAddr = AllocAddr;
159   for (auto &KV : BL.segments()) {
160     const auto &AG = KV.first;
161     auto &Seg = KV.second;
162 
163     Seg.Addr = NextSegAddr.getValue();
164     KV.second.WorkingMem = BL.getGraph().allocateBuffer(Seg.ContentSize).data();
165     NextSegAddr += ExecutorAddrDiff(
166         alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize()));
167 
168     auto &SegInfo = SegInfos[AG];
169     SegInfo.ContentSize = Seg.ContentSize;
170     SegInfo.ZeroFillSize = Seg.ZeroFillSize;
171     SegInfo.Addr = ExecutorAddr(Seg.Addr);
172     SegInfo.WorkingMem = Seg.WorkingMem;
173   }
174 
175   if (auto Err = BL.apply())
176     return OnAllocated(std::move(Err));
177 
178   OnAllocated(std::make_unique<InFlightAlloc>(*this, BL.getGraph(), AllocAddr,
179                                               std::move(SegInfos)));
180 }
181 
182 } // end namespace orc
183 } // end namespace llvm
184