xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- EPCGenericJITLinkMemoryManager.h - EPC-based mem manager -*- 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 // Implements JITLinkMemoryManager by making remove calls via
10 // ExecutorProcessControl::callWrapperAsync.
11 //
12 // This simplifies the implementaton of new ExecutorProcessControl instances,
13 // as this implementation will always work (at the cost of some performance
14 // overhead for the calls).
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_EXECUTIONENGINE_ORC_EPCGENERICJITLINKMEMORYMANAGER_H
19 #define LLVM_EXECUTIONENGINE_ORC_EPCGENERICJITLINKMEMORYMANAGER_H
20 
21 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
22 #include "llvm/ExecutionEngine/Orc/Core.h"
23 #include "llvm/Support/Compiler.h"
24 
25 namespace llvm {
26 namespace orc {
27 
28 class LLVM_ABI EPCGenericJITLinkMemoryManager
29     : public jitlink::JITLinkMemoryManager {
30 public:
31   /// Function addresses for memory access.
32   struct SymbolAddrs {
33     ExecutorAddr Allocator;
34     ExecutorAddr Reserve;
35     ExecutorAddr Finalize;
36     ExecutorAddr Deallocate;
37   };
38 
39   /// Create an EPCGenericJITLinkMemoryManager instance from a given set of
40   /// function addrs.
EPCGenericJITLinkMemoryManager(ExecutorProcessControl & EPC,SymbolAddrs SAs)41   EPCGenericJITLinkMemoryManager(ExecutorProcessControl &EPC, SymbolAddrs SAs)
42       : EPC(EPC), SAs(SAs) {}
43 
44   void allocate(const jitlink::JITLinkDylib *JD, jitlink::LinkGraph &G,
45                 OnAllocatedFunction OnAllocated) override;
46 
47   // Use overloads from base class.
48   using JITLinkMemoryManager::allocate;
49 
50   void deallocate(std::vector<FinalizedAlloc> Allocs,
51                   OnDeallocatedFunction OnDeallocated) override;
52 
53   // Use overloads from base class.
54   using JITLinkMemoryManager::deallocate;
55 
56 private:
57   class InFlightAlloc;
58 
59   void completeAllocation(ExecutorAddr AllocAddr, jitlink::BasicLayout BL,
60                           OnAllocatedFunction OnAllocated);
61 
62   ExecutorProcessControl &EPC;
63   SymbolAddrs SAs;
64 };
65 
66 namespace shared {
67 
68 /// FIXME: This specialization should be moved into TargetProcessControlTypes.h
69 ///        (or wherever those types get merged to) once ORC depends on JITLink.
70 template <>
71 class SPSSerializationTraits<SPSExecutorAddr,
72                              jitlink::JITLinkMemoryManager::FinalizedAlloc> {
73 public:
size(const jitlink::JITLinkMemoryManager::FinalizedAlloc & FA)74   static size_t size(const jitlink::JITLinkMemoryManager::FinalizedAlloc &FA) {
75     return SPSArgList<SPSExecutorAddr>::size(ExecutorAddr(FA.getAddress()));
76   }
77 
78   static bool
serialize(SPSOutputBuffer & OB,const jitlink::JITLinkMemoryManager::FinalizedAlloc & FA)79   serialize(SPSOutputBuffer &OB,
80             const jitlink::JITLinkMemoryManager::FinalizedAlloc &FA) {
81     return SPSArgList<SPSExecutorAddr>::serialize(
82         OB, ExecutorAddr(FA.getAddress()));
83   }
84 
deserialize(SPSInputBuffer & IB,jitlink::JITLinkMemoryManager::FinalizedAlloc & FA)85   static bool deserialize(SPSInputBuffer &IB,
86                           jitlink::JITLinkMemoryManager::FinalizedAlloc &FA) {
87     ExecutorAddr A;
88     if (!SPSArgList<SPSExecutorAddr>::deserialize(IB, A))
89       return false;
90     FA = jitlink::JITLinkMemoryManager::FinalizedAlloc(A);
91     return true;
92   }
93 };
94 
95 } // end namespace shared
96 } // end namespace orc
97 } // end namespace llvm
98 
99 #endif // LLVM_EXECUTIONENGINE_ORC_EPCGENERICJITLINKMEMORYMANAGER_H
100