LLVM 24.0.0git
MemoryMapper.h
Go to the documentation of this file.
1//===- MemoryMapper.h - Cross-process memory mapper -------------*- 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// Cross-process (and in-process) memory mapping and transfer
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
14#define LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
15
21
22#include <mutex>
23
24namespace llvm {
25namespace jitlink {
26class LinkGraph;
27} // namespace jitlink
28
29namespace orc {
30
31/// Manages mapping, content transfer and protections for JIT memory
33public:
34 /// Represents a single allocation containing multiple segments and
35 /// initialization and deinitialization actions
49
51
52 // Page size of the target process
53 virtual unsigned int getPageSize() = 0;
54
55 /// Reserves address space in executor process
56 virtual void reserve(size_t NumBytes, OnReservedFunction OnReserved) = 0;
57
58 /// Provides working memory
59 /// The LinkGraph parameter is included to allow implementations to allocate
60 /// working memory from the LinkGraph's allocator, in which case it will be
61 /// deallocated when the LinkGraph is destroyed.
63 size_t ContentSize) = 0;
64
66
67 /// Ensures executor memory is synchronized with working copy memory, sends
68 /// functions to be called after initilization and before deinitialization and
69 /// applies memory protections
70 /// Returns a unique address identifying the allocation. This address should
71 /// be passed to deinitialize to run deallocation actions (and reset
72 /// permissions where possible).
73 virtual void initialize(AllocInfo &AI,
74 OnInitializedFunction OnInitialized) = 0;
75
77
78 /// Runs previously specified deinitialization actions
79 /// Executor addresses returned by initialize should be passed
80 virtual void deinitialize(ArrayRef<ExecutorAddr> Allocations,
81 OnDeinitializedFunction OnDeInitialized) = 0;
82
84
85 /// Release address space acquired through reserve()
86 virtual void release(ArrayRef<ExecutorAddr> Reservations,
87 OnReleasedFunction OnRelease) = 0;
88
89 virtual ~MemoryMapper();
90};
91
93public:
94 InProcessMemoryMapper(size_t PageSize);
95
97
98 unsigned int getPageSize() override { return PageSize; }
99
100 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
101
102 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
103
105 size_t ContentSize) override;
106
107 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
108 OnDeinitializedFunction OnDeInitialized) override;
109
110 void release(ArrayRef<ExecutorAddr> Reservations,
111 OnReleasedFunction OnRelease) override;
112
113 ~InProcessMemoryMapper() override;
114
115private:
116 struct Allocation {
117 size_t Size;
118 std::vector<shared::WrapperFunctionCall> DeinitializationActions;
119 };
120 using AllocationMap = DenseMap<ExecutorAddr, Allocation>;
121
122 struct Reservation {
123 size_t Size;
124 std::vector<ExecutorAddr> Allocations;
125 };
126 using ReservationMap = DenseMap<void *, Reservation>;
127
128 std::mutex Mutex;
129 ReservationMap Reservations;
130 AllocationMap Allocations;
131
132 size_t PageSize;
133};
134
136public:
138 size_t PageSize);
139
142
143 unsigned int getPageSize() override { return PageSize; }
144
145 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
146
148 size_t ContentSize) override;
149
150 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
151
152 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
153 OnDeinitializedFunction OnDeInitialized) override;
154
155 void release(ArrayRef<ExecutorAddr> Reservations,
156 OnReleasedFunction OnRelease) override;
157
158 ~SharedMemoryMapper() override;
159
160private:
161 struct Reservation {
162 void *LocalAddr;
163 size_t Size;
164 int SharedMemoryId;
165 };
166
167 ExecutionSession &ES;
168 SharedMemoryMapBindings B;
169
170 std::mutex Mutex;
171
172 std::map<ExecutorAddr, Reservation> Reservations;
173
174 size_t PageSize;
175};
176
177} // namespace orc
178} // end namespace llvm
179
180#endif // LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
bbsections prepare
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
#define G(x, y, z)
Definition MD5.cpp:55
Provides a library for accessing information about this process and other processes on the operating ...
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, const llvm::StringTable &StandardNames, VectorLibrary VecLib)
Initialize the set of available library functions based on the specified target triple.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
A pair of memory protections and allocation policies.
An ExecutionSession represents a running JIT program.
Definition Core.h:1111
Represents an address in the executor process.
static Expected< std::unique_ptr< InProcessMemoryMapper > > Create()
unsigned int getPageSize() override
Manages mapping, content transfer and protections for JIT memory.
unique_function< void(Error)> OnReleasedFunction
virtual char * prepare(jitlink::LinkGraph &G, ExecutorAddr Addr, size_t ContentSize)=0
Provides working memory The LinkGraph parameter is included to allow implementations to allocate work...
virtual unsigned int getPageSize()=0
virtual void release(ArrayRef< ExecutorAddr > Reservations, OnReleasedFunction OnRelease)=0
Release address space acquired through reserve()
virtual void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized)=0
Ensures executor memory is synchronized with working copy memory, sends functions to be called after ...
virtual void deinitialize(ArrayRef< ExecutorAddr > Allocations, OnDeinitializedFunction OnDeInitialized)=0
Runs previously specified deinitialization actions Executor addresses returned by initialize should b...
virtual void reserve(size_t NumBytes, OnReservedFunction OnReserved)=0
Reserves address space in executor process.
unique_function< void(Expected< ExecutorAddr >)> OnInitializedFunction
unique_function< void(Expected< ExecutorAddrRange >)> OnReservedFunction
unique_function< void(Error)> OnDeinitializedFunction
SharedMemoryMapper(ExecutionSession &ES, SharedMemoryMapBindings B, size_t PageSize)
unsigned int getPageSize() override
static Expected< std::unique_ptr< SharedMemoryMapper > > Create(ExecutionSession &ES, SharedMemoryMapBindings B)
unique_function is a type-erasing functor similar to std::function.
std::vector< AllocActionCallPair > AllocActions
A vector of allocation actions to be run for this allocation.
uint64_t ExecutorAddrDiff
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
Definition Mutex.h:66
This is an optimization pass for GlobalISel generic memory operations.
Summary of memprof metadata on allocations.
Represents a single allocation containing multiple segments and initialization and deinitialization a...
std::vector< SegInfo > Segments
The resolved controller-side handle to an executor-side shared-memory mapper: the address of the mapp...