LLVM: lib/ExecutionEngine/Orc/MapperJITLinkMemoryManager.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

10

13

15

16namespace llvm {

17namespace orc {

18

21public:

24 std::vectorMemoryMapper::AllocInfo::SegInfo Segs)

25 : Parent(Parent), G(G), AllocAddr(AllocAddr), Segs(std::move(Segs)) {}

26

30

33

34 Parent.Mapper->initialize(AI, [OnFinalize = std::move(OnFinalize)](

37 OnFinalize(Result.takeError());

38 return;

39 }

40

42 });

43 }

44

46 Parent.Mapper->deinitialize({AllocAddr}, std::move(OnFinalize));

47 }

48

49private:

53 std::vectorMemoryMapper::AllocInfo::SegInfo Segs;

54};

55

57 size_t ReservationGranularity, std::unique_ptr Mapper)

58 : ReservationUnits(ReservationGranularity), AvailableMemory(AMAllocator),

59 Mapper(std::move(Mapper)) {}

60

64

65

67 if (!SegsSizes) {

68 OnAllocated(SegsSizes.takeError());

69 return;

70 }

71

72 auto TotalSize = SegsSizes->total();

73

74 auto CompleteAllocation = [this, &G, BL = std::move(BL),

75 OnAllocated = std::move(OnAllocated)](

78 Mutex.unlock();

79 return OnAllocated(Result.takeError());

80 }

81

82 auto NextSegAddr = Result->Start;

83

84 std::vectorMemoryMapper::AllocInfo::SegInfo SegInfos;

85

86 for (auto &KV : BL.segments()) {

87 auto &AG = KV.first;

88 auto &Seg = KV.second;

89

90 auto TotalSize = Seg.ContentSize + Seg.ZeroFillSize;

91

92 Seg.Addr = NextSegAddr;

93 Seg.WorkingMem = Mapper->prepare(G, NextSegAddr, TotalSize);

94

95 NextSegAddr += alignTo(TotalSize, Mapper->getPageSize());

96

98 SI.Offset = Seg.Addr - Result->Start;

99 SI.ContentSize = Seg.ContentSize;

100 SI.ZeroFillSize = Seg.ZeroFillSize;

101 SI.AG = AG;

102 SI.WorkingMem = Seg.WorkingMem;

103

104 SegInfos.push_back(SI);

105 }

106

107 UsedMemory.insert({Result->Start, NextSegAddr - Result->Start});

108

109 if (NextSegAddr < Result->End) {

110

111 AvailableMemory.insert(NextSegAddr, Result->End - 1, true);

112 }

113 Mutex.unlock();

114

115 if (auto Err = BL.apply()) {

116 OnAllocated(std::move(Err));

117 return;

118 }

119

120 OnAllocated(std::make_unique(*this, G, Result->Start,

121 std::move(SegInfos)));

122 };

123

124 Mutex.lock();

125

126

128

129 for (AvailableMemoryMap::iterator It = AvailableMemory.begin();

130 It != AvailableMemory.end(); It++) {

131 if (It.stop() - It.start() + 1 >= TotalSize) {

133 It.erase();

134 break;

135 }

136 }

137

138 if (SelectedRange.empty()) {

139 auto TotalAllocation = alignTo(TotalSize, ReservationUnits);

140 Mapper->reserve(TotalAllocation, std::move(CompleteAllocation));

141 } else {

142 CompleteAllocation(SelectedRange);

143 }

144}

145

148 std::vector Bases;

149 Bases.reserve(Allocs.size());

150 for (auto &FA : Allocs) {

152 Bases.push_back(Addr);

153 }

154

155 Mapper->deinitialize(Bases, [this, Allocs = std::move(Allocs),

156 OnDeallocated = std::move(OnDeallocated)](

158

159

160

161

162 if (Err) {

163 for (auto &FA : Allocs)

164 FA.release();

165 OnDeallocated(std::move(Err));

166 return;

167 }

168

169 {

170 std::lock_guardstd::mutex Lock(Mutex);

171

172 for (auto &FA : Allocs) {

175

176 UsedMemory.erase(Addr);

177 AvailableMemory.insert(Addr, Addr + Size - 1, true);

178

179 FA.release();

180 }

181 }

182

184 });

185}

186

187}

188}

Provides a library for accessing information about this process and other processes on the operating ...

Lightweight error class with error context and mandatory checking.

static ErrorSuccess success()

Create a success value.

Tagged union holding either a T or a Error.

BasicLayout simplifies the implementation of JITLinkMemoryManagers.

LLVM_ABI Error apply()

Apply the layout to the graph.

LLVM_ABI Expected< ContiguousPageBasedLayoutSizes > getContiguousPageBasedLayoutSizes(uint64_t PageSize)

Returns the total number of required to allocate all segments (with each segment padded out to page s...

iterator_range< SegmentMap::iterator > segments()

Returns an iterator over the segments of the layout.

Represents a finalized allocation.

Represents an allocation which has not been finalized yet.

unique_function< void(Error)> OnAbandonedFunction

unique_function< void(Expected< FinalizedAlloc >)> OnFinalizedFunction

unique_function< void(AllocResult)> OnAllocatedFunction

Called when allocation has been completed.

unique_function< void(Error)> OnDeallocatedFunction

Called when deallocation has completed.

Represents an address in the executor process.

InFlightAlloc(MapperJITLinkMemoryManager &Parent, LinkGraph &G, ExecutorAddr AllocAddr, std::vector< MemoryMapper::AllocInfo::SegInfo > Segs)

Definition MapperJITLinkMemoryManager.cpp:22

void finalize(OnFinalizedFunction OnFinalize) override

Called to transfer working memory to the target and apply finalization.

Definition MapperJITLinkMemoryManager.cpp:27

void abandon(OnAbandonedFunction OnFinalize) override

Called prior to finalization if the allocation should be abandoned.

Definition MapperJITLinkMemoryManager.cpp:45

void deallocate(std::vector< FinalizedAlloc > Allocs, OnDeallocatedFunction OnDeallocated) override

Deallocate a list of allocation objects.

Definition MapperJITLinkMemoryManager.cpp:146

MapperJITLinkMemoryManager(size_t ReservationGranularity, std::unique_ptr< MemoryMapper > Mapper)

Definition MapperJITLinkMemoryManager.cpp:56

void allocate(const jitlink::JITLinkDylib *JD, jitlink::LinkGraph &G, OnAllocatedFunction OnAllocated) override

Start the allocation process.

Definition MapperJITLinkMemoryManager.cpp:61

uint64_t ExecutorAddrDiff

This is an optimization pass for GlobalISel generic memory operations.

uint64_t alignTo(uint64_t Size, Align A)

Returns a multiple of A needed to store Size bytes.

OutputIt move(R &&Range, OutputIt Out)

Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.

Implement std::hash so that hash_code can be used in STL containers.

void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)

Implement std::swap in terms of BitVector swap.

Represents an address range in the exceutor process.

Represents a single allocation containing multiple segments and initialization and deinitialization a...

std::vector< SegInfo > Segments

shared::AllocActions Actions