xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ExecutionEngine/Orc/MapperJITLinkMemoryManager.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===--------------- MapperJITLinkMemoryManager.h -*- 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 using MemoryMapper
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_MAPPERJITLINKMEMORYMANAGER_H
14 #define LLVM_EXECUTIONENGINE_ORC_MAPPERJITLINKMEMORYMANAGER_H
15 
16 #include "llvm/ADT/IntervalMap.h"
17 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
18 #include "llvm/ExecutionEngine/Orc/MemoryMapper.h"
19 #include "llvm/Support/Compiler.h"
20 
21 namespace llvm {
22 namespace orc {
23 
24 class LLVM_ABI MapperJITLinkMemoryManager
25     : public jitlink::JITLinkMemoryManager {
26 public:
27   MapperJITLinkMemoryManager(size_t ReservationGranularity,
28                              std::unique_ptr<MemoryMapper> Mapper);
29 
30   template <class MemoryMapperType, class... Args>
31   static Expected<std::unique_ptr<MapperJITLinkMemoryManager>>
CreateWithMapper(size_t ReservationGranularity,Args &&...A)32   CreateWithMapper(size_t ReservationGranularity, Args &&...A) {
33     auto Mapper = MemoryMapperType::Create(std::forward<Args>(A)...);
34     if (!Mapper)
35       return Mapper.takeError();
36 
37     return std::make_unique<MapperJITLinkMemoryManager>(ReservationGranularity,
38                                                         std::move(*Mapper));
39   }
40 
41   void allocate(const jitlink::JITLinkDylib *JD, jitlink::LinkGraph &G,
42                 OnAllocatedFunction OnAllocated) override;
43   // synchronous overload
44   using JITLinkMemoryManager::allocate;
45 
46   void deallocate(std::vector<FinalizedAlloc> Allocs,
47                   OnDeallocatedFunction OnDeallocated) override;
48   // synchronous overload
49   using JITLinkMemoryManager::deallocate;
50 
51 private:
52   class InFlightAlloc;
53 
54   std::mutex Mutex;
55 
56   // We reserve multiples of this from the executor address space
57   size_t ReservationUnits;
58 
59   // Ranges that have been reserved in executor but not yet allocated
60   using AvailableMemoryMap = IntervalMap<ExecutorAddr, bool>;
61   AvailableMemoryMap::Allocator AMAllocator;
62   IntervalMap<ExecutorAddr, bool> AvailableMemory;
63 
64   // Ranges that have been reserved in executor and already allocated
65   DenseMap<ExecutorAddr, ExecutorAddrDiff> UsedMemory;
66 
67   std::unique_ptr<MemoryMapper> Mapper;
68 };
69 
70 } // end namespace orc
71 } // end namespace llvm
72 
73 #endif // LLVM_EXECUTIONENGINE_ORC_MAPPERJITLINKMEMORYMANAGER_H
74