llvm.org GIT mirror llvm / 8c651fb
[JIT] Towards a working small memory model. This commit introduces an option, --preallocate, so that we can get memory upfront and use it in small memory model tests (in order to get reliable results). Differential Revision: http://reviews.llvm.org/D13630 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@250956 91177308-0d34-0410-b5e6-96231b3b80d8 Davide Italiano 3 years ago
1 changed file(s) with 55 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
9393 cl::ZeroOrMore);
9494
9595 static cl::opt
96 PreallocMemory("preallocate",
97 cl::desc("Allocate memory upfront rather than on-demand"),
98 cl::init(0));
99
100 static cl::opt
96101 TargetAddrStart("target-addr-start",
97102 cl::desc("For -verify only: start of phony target address "
98103 "range."),
172177 size_t Size) override {}
173178 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
174179 size_t Size) override {}
180
181 void preallocateSlab(uint64_t Size) {
182 std::string Err;
183 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, &Err);
184 if (!MB.base())
185 report_fatal_error("Can't allocate enough memory: " + Err);
186
187 PreallocSlab = MB;
188 UsePreallocation = true;
189 SlabSize = Size;
190 }
191
192 uint8_t *allocateFromSlab(uintptr_t Size, unsigned Alignment, bool isCode) {
193 Size = RoundUpToAlignment(Size, Alignment);
194 if (CurrentSlabOffset + Size > SlabSize)
195 report_fatal_error("Can't allocate enough memory. Tune --preallocate");
196
197 uintptr_t OldSlabOffset = CurrentSlabOffset;
198 sys::MemoryBlock MB((void *)OldSlabOffset, Size);
199 if (isCode)
200 FunctionMemory.push_back(MB);
201 else
202 DataMemory.push_back(MB);
203 CurrentSlabOffset += Size;
204 return (uint8_t*)OldSlabOffset;
205 }
206
175207 private:
176208 std::map DummyExterns;
209 sys::MemoryBlock PreallocSlab;
210 bool UsePreallocation = false;
211 uintptr_t SlabSize = 0;
212 uintptr_t CurrentSlabOffset = 0;
177213 };
178214
179215 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
180216 unsigned Alignment,
181217 unsigned SectionID,
182218 StringRef SectionName) {
219 if (UsePreallocation)
220 return allocateFromSlab(Size, Alignment, true /* isCode */);
221
183222 std::string Err;
184223 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, &Err);
185224 if (!MB.base())
193232 unsigned SectionID,
194233 StringRef SectionName,
195234 bool IsReadOnly) {
235 if (UsePreallocation)
236 return allocateFromSlab(Size, Alignment, false /* isCode */);
237
196238 std::string Err;
197239 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, &Err);
198240 if (!MB.base())
331373 return 0;
332374 }
333375
376 static void doPreallocation(TrivialMemoryManager &MemMgr) {
377 // Allocate a slab of memory upfront, if required. This is used if
378 // we want to test small code models.
379 if (static_cast(PreallocMemory) < 0)
380 report_fatal_error("Pre-allocated bytes of memory must be a positive integer.");
381
382 // FIXME: Limit the amount of memory that can be preallocated?
383 if (PreallocMemory != 0)
384 MemMgr.preallocateSlab(PreallocMemory);
385 }
386
334387 static int executeInput() {
335388 // Load any dylibs requested on the command line.
336389 loadDylibs();
337390
338391 // Instantiate a dynamic linker.
339392 TrivialMemoryManager MemMgr;
393 doPreallocation(MemMgr);
340394 RuntimeDyld Dyld(MemMgr, MemMgr);
341395
342396 // FIXME: Preserve buffers until resolveRelocations time to work around a bug
612666
613667 // Instantiate a dynamic linker.
614668 TrivialMemoryManager MemMgr;
669 doPreallocation(MemMgr);
615670 RuntimeDyld Dyld(MemMgr, MemMgr);
616671 Dyld.setProcessAllSections(true);
617672 RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),