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