xref: /llvm-project/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp (revision 78b083dbb725e1ec568d1b8ee523f5f025d25798)
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 #include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h"
11 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
12 
13 #include <limits>
14 
15 namespace llvm {
16 namespace orc {
17 
18 class EPCGenericJITLinkMemoryManager::Alloc
19     : public jitlink::JITLinkMemoryManager::Allocation {
20 public:
21   struct SegInfo {
22     char *WorkingMem = nullptr;
23     ExecutorAddress TargetAddr;
24     uint64_t ContentSize = 0;
25     uint64_t ZeroFillSize = 0;
26   };
27   using SegInfoMap = DenseMap<unsigned, SegInfo>;
28 
29   Alloc(EPCGenericJITLinkMemoryManager &Parent, ExecutorAddress TargetAddr,
30         std::unique_ptr<char[]> WorkingBuffer, SegInfoMap Segs)
31       : Parent(Parent), TargetAddr(TargetAddr),
32         WorkingBuffer(std::move(WorkingBuffer)), Segs(std::move(Segs)) {}
33 
34   MutableArrayRef<char> getWorkingMemory(ProtectionFlags Seg) override {
35     auto I = Segs.find(Seg);
36     assert(I != Segs.end() && "No allocation for seg");
37     assert(I->second.ContentSize <= std::numeric_limits<size_t>::max());
38     return {I->second.WorkingMem, static_cast<size_t>(I->second.ContentSize)};
39   }
40 
41   JITTargetAddress getTargetMemory(ProtectionFlags Seg) override {
42     auto I = Segs.find(Seg);
43     assert(I != Segs.end() && "No allocation for seg");
44     return I->second.TargetAddr.getValue();
45   }
46 
47   void finalizeAsync(FinalizeContinuation OnFinalize) override {
48     char *WorkingMem = WorkingBuffer.get();
49     tpctypes::FinalizeRequest FR;
50     for (auto &KV : Segs) {
51       assert(KV.second.ContentSize <= std::numeric_limits<size_t>::max());
52       FR.Segments.push_back(tpctypes::SegFinalizeRequest{
53           tpctypes::toWireProtectionFlags(
54               static_cast<sys::Memory::ProtectionFlags>(KV.first)),
55           KV.second.TargetAddr,
56           alignTo(KV.second.ContentSize + KV.second.ZeroFillSize,
57                   Parent.EPC.getPageSize()),
58           {WorkingMem, static_cast<size_t>(KV.second.ContentSize)}});
59       WorkingMem += KV.second.ContentSize;
60     }
61     Parent.EPC.callSPSWrapperAsync<
62         rt::SPSSimpleExecutorMemoryManagerFinalizeSignature>(
63         [OnFinalize = std::move(OnFinalize)](Error SerializationErr,
64                                              Error FinalizeErr) {
65           if (SerializationErr)
66             OnFinalize(std::move(SerializationErr));
67           else
68             OnFinalize(std::move(FinalizeErr));
69         },
70         Parent.SAs.Finalize.getValue(), Parent.SAs.Allocator, std::move(FR));
71   }
72 
73   Error deallocate() override {
74     Error Err = Error::success();
75     if (auto E2 = Parent.EPC.callSPSWrapper<
76                   rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>(
77             Parent.SAs.Deallocate.getValue(), Err, Parent.SAs.Allocator,
78             ArrayRef<ExecutorAddress>(TargetAddr)))
79       return E2;
80     return Err;
81   }
82 
83 private:
84   EPCGenericJITLinkMemoryManager &Parent;
85   ExecutorAddress TargetAddr;
86   std::unique_ptr<char[]> WorkingBuffer;
87   SegInfoMap Segs;
88 };
89 
90 Expected<std::unique_ptr<jitlink::JITLinkMemoryManager::Allocation>>
91 EPCGenericJITLinkMemoryManager::allocate(const jitlink::JITLinkDylib *JD,
92                                          const SegmentsRequestMap &Request) {
93   Alloc::SegInfoMap Segs;
94   uint64_t AllocSize = 0;
95   size_t WorkingSize = 0;
96   for (auto &KV : Request) {
97     if (!isPowerOf2_64(KV.second.getAlignment()))
98       return make_error<StringError>("Alignment is not a power of two",
99                                      inconvertibleErrorCode());
100     if (KV.second.getAlignment() > EPC.getPageSize())
101       return make_error<StringError>("Alignment exceeds page size",
102                                      inconvertibleErrorCode());
103 
104     auto &Seg = Segs[KV.first];
105     Seg.ContentSize = KV.second.getContentSize();
106     Seg.ZeroFillSize = KV.second.getZeroFillSize();
107     AllocSize += alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize());
108     WorkingSize += Seg.ContentSize;
109   }
110 
111   std::unique_ptr<char[]> WorkingBuffer;
112   if (WorkingSize > 0)
113     WorkingBuffer = std::make_unique<char[]>(WorkingSize);
114   Expected<ExecutorAddress> TargetAllocAddr((ExecutorAddress()));
115   if (auto Err = EPC.callSPSWrapper<
116                  rt::SPSSimpleExecutorMemoryManagerReserveSignature>(
117           SAs.Reserve.getValue(), TargetAllocAddr, SAs.Allocator, AllocSize))
118     return std::move(Err);
119   if (!TargetAllocAddr)
120     return TargetAllocAddr.takeError();
121 
122   char *WorkingMem = WorkingBuffer.get();
123   JITTargetAddress SegAddr = TargetAllocAddr->getValue();
124   for (auto &KV : Segs) {
125     auto &Seg = KV.second;
126     Seg.TargetAddr.setValue(SegAddr);
127     SegAddr += alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize());
128     Seg.WorkingMem = WorkingMem;
129     WorkingMem += Seg.ContentSize;
130   }
131 
132   return std::make_unique<Alloc>(*this, *TargetAllocAddr,
133                                  std::move(WorkingBuffer), std::move(Segs));
134 }
135 
136 } // end namespace orc
137 } // end namespace llvm
138