LLVM  15.0.0git
MemoryFlags.h
Go to the documentation of this file.
1 //===-------- MemoryFlags.h - Memory allocation flags -----------*- 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 // Defines types and operations related to memory protection and allocation
10 // lifetimes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_JITLINK_MEMORYFLAGS_H
15 #define LLVM_EXECUTIONENGINE_JITLINK_MEMORYFLAGS_H
16 
17 #include "llvm/ADT/BitmaskEnum.h"
18 #include "llvm/ADT/DenseMapInfo.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Memory.h"
22 
23 namespace llvm {
24 namespace jitlink {
25 
26 /// Describes Read/Write/Exec permissions for memory.
27 enum class MemProt {
28  None = 0,
29  Read = 1U << 0,
30  Write = 1U << 1,
31  Exec = 1U << 2,
32  LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Exec)
33 };
34 
35 /// Print a MemProt as an RWX triple.
37 
38 /// Convert a MemProt value to a corresponding sys::Memory::ProtectionFlags
39 /// value.
41  std::underlying_type_t<sys::Memory::ProtectionFlags> PF = 0;
42  if ((MP & MemProt::Read) != MemProt::None)
44  if ((MP & MemProt::Write) != MemProt::None)
46  if ((MP & MemProt::Exec) != MemProt::None)
48  return static_cast<sys::Memory::ProtectionFlags>(PF);
49 }
50 
51 /// Convert a sys::Memory::ProtectionFlags value to a corresponding MemProt
52 /// value.
55  if (PF & sys::Memory::MF_READ)
56  MP |= MemProt::Read;
57  if (PF & sys::Memory::MF_WRITE)
58  MP |= MemProt::Write;
59  if (PF & sys::Memory::MF_EXEC)
60  MP |= MemProt::None;
61  return MP;
62 }
63 
64 /// Describes a memory deallocation policy for memory to be allocated by a
65 /// JITLinkMemoryManager.
66 ///
67 /// All memory allocated by a call to JITLinkMemoryManager::allocate should be
68 /// deallocated if a call is made to
69 /// JITLinkMemoryManager::InFlightAllocation::abandon. The policies below apply
70 /// to finalized allocations.
71 enum class MemDeallocPolicy {
72  /// Standard memory should be deallocated when the deallocate method is called
73  /// for the finalized allocation.
74  Standard,
75 
76  /// Finalize memory should be overwritten and then deallocated after all
77  /// finalization functions have been run.
78  Finalize
79 };
80 
81 /// Print a MemDeallocPolicy.
83 
84 /// A pair of memory protections and allocation policies.
85 ///
86 /// Optimized for use as a small map key.
87 class AllocGroup {
89 
90  using underlying_type = uint8_t;
91  static constexpr unsigned BitsForProt = 3;
92  static constexpr unsigned BitsForDeallocPolicy = 1;
93  static constexpr unsigned MaxIdentifiers =
94  1U << (BitsForProt + BitsForDeallocPolicy);
95 
96 public:
97  static constexpr unsigned NumGroups = MaxIdentifiers;
98 
99  /// Create a default AllocGroup. No memory protections, standard
100  /// deallocation policy.
101  AllocGroup() = default;
102 
103  /// Create an AllocGroup from a MemProt only -- uses
104  /// MemoryDeallocationPolicy::Standard.
105  AllocGroup(MemProt MP) : Id(static_cast<underlying_type>(MP)) {}
106 
107  /// Create an AllocGroup from a MemProt and a MemoryDeallocationPolicy.
109  : Id(static_cast<underlying_type>(MP) |
110  (static_cast<underlying_type>(MDP) << BitsForProt)) {}
111 
112  /// Returns the MemProt for this group.
113  MemProt getMemProt() const {
114  return static_cast<MemProt>(Id & ((1U << BitsForProt) - 1));
115  }
116 
117  /// Returns the MemoryDeallocationPolicy for this group.
119  return static_cast<MemDeallocPolicy>(Id >> BitsForProt);
120  }
121 
122  friend bool operator==(const AllocGroup &LHS, const AllocGroup &RHS) {
123  return LHS.Id == RHS.Id;
124  }
125 
126  friend bool operator!=(const AllocGroup &LHS, const AllocGroup &RHS) {
127  return !(LHS == RHS);
128  }
129 
130  friend bool operator<(const AllocGroup &LHS, const AllocGroup &RHS) {
131  return LHS.Id < RHS.Id;
132  }
133 
134 private:
135  AllocGroup(underlying_type RawId) : Id(RawId) {}
136  underlying_type Id = 0;
137 };
138 
139 /// A specialized small-map for AllocGroups.
140 ///
141 /// Iteration order is guaranteed to match key ordering.
142 template <typename T> class AllocGroupSmallMap {
143 private:
144  using ElemT = std::pair<AllocGroup, T>;
146 
147  static bool compareKey(const ElemT &E, const AllocGroup &G) {
148  return E.first < G;
149  }
150 
151 public:
152  using iterator = typename VectorTy::iterator;
153 
154  AllocGroupSmallMap() = default;
155  AllocGroupSmallMap(std::initializer_list<std::pair<AllocGroup, T>> Inits)
156  : Elems(Inits) {
157  llvm::sort(Elems, llvm::less_first());
158  }
159 
160  iterator begin() { return Elems.begin(); }
161  iterator end() { return Elems.end(); }
163  auto I = lower_bound(Elems, G, compareKey);
164  return (I->first == G) ? I : end();
165  }
166 
167  bool empty() const { return Elems.empty(); }
168  size_t size() const { return Elems.size(); }
169 
171  auto I = lower_bound(Elems, G, compareKey);
172  if (I == Elems.end() || I->first != G)
173  I = Elems.insert(I, std::make_pair(G, T()));
174  return I->second;
175  }
176 
177 private:
178  VectorTy Elems;
179 };
180 
181 /// Print an AllocGroup.
182 raw_ostream &operator<<(raw_ostream &OS, AllocGroup AG);
183 
184 } // end namespace jitlink
185 
186 template <> struct DenseMapInfo<jitlink::MemProt> {
187  static inline jitlink::MemProt getEmptyKey() {
188  return jitlink::MemProt(~uint8_t(0));
189  }
191  return jitlink::MemProt(~uint8_t(0) - 1);
192  }
193  static unsigned getHashValue(const jitlink::MemProt &Val) {
194  using UT = std::underlying_type_t<jitlink::MemProt>;
195  return DenseMapInfo<UT>::getHashValue(static_cast<UT>(Val));
196  }
197  static bool isEqual(const jitlink::MemProt &LHS,
198  const jitlink::MemProt &RHS) {
199  return LHS == RHS;
200  }
201 };
202 
203 template <> struct DenseMapInfo<jitlink::AllocGroup> {
205  return jitlink::AllocGroup(~uint8_t(0));
206  }
208  return jitlink::AllocGroup(~uint8_t(0) - 1);
209  }
210  static unsigned getHashValue(const jitlink::AllocGroup &Val) {
212  Val.Id);
213  }
214  static bool isEqual(const jitlink::AllocGroup &LHS,
215  const jitlink::AllocGroup &RHS) {
216  return LHS == RHS;
217  }
218 };
219 
220 } // end namespace llvm
221 
222 #endif // LLVM_EXECUTIONENGINE_JITLINK_MEMORYFLAGS_H
llvm::sys::Memory::MF_READ
@ MF_READ
Definition: Memory.h:55
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::sys::Memory::MF_WRITE
@ MF_WRITE
Definition: Memory.h:56
LLVM_MARK_AS_BITMASK_ENUM
#define LLVM_MARK_AS_BITMASK_ENUM(LargestValue)
LLVM_MARK_AS_BITMASK_ENUM lets you opt in an individual enum type so you can perform bitwise operatio...
Definition: BitmaskEnum.h:41
llvm::lower_bound
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1726
llvm::SmallVector< ElemT, 4 >
T
#define T
Definition: Mips16ISelLowering.cpp:341
STLExtras.h
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::DenseMapInfo
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: APInt.h:34
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::less_first
Function object to check whether the first component of a std::pair compares less than the first comp...
Definition: STLExtras.h:1344
llvm::sys::Memory::ProtectionFlags
ProtectionFlags
Definition: Memory.h:54
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::sys::Memory::MF_EXEC
@ MF_EXEC
Definition: Memory.h:57
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
I
#define I(x, y, z)
Definition: MD5.cpp:58
BitmaskEnum.h
Memory.h
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1552
llvm::SmallVectorImpl< ElemT >::iterator
typename SuperClass::iterator iterator
Definition: SmallVector.h:558
DenseMapInfo.h
raw_ostream.h
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:235
llvm::SmallVectorImpl::insert
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:792