llvm.org GIT mirror llvm / 6eb43d2
This threads SectionName through the allocateCodeSection/allocateDataSection APIs, both in C++ and C land. It's useful for the memory managers that are allocating a section to know what the name of the section is. At a minimum, this is useful for low-level debugging - it's customary for JITs to be able to tell you what memory they allocated, and as part of any such dump, they should be able to tell you some meta-data about what each allocation is for. This allows clients that supply their own memory managers to do this. Additionally, we also envision the SectionName being useful for passing meta-data from within LLVM to an LLVM client. This changes both the C and C++ APIs, and all of the clients of those APIs within LLVM. I'm assuming that it's safe to change the C++ API because that API is allowed to change. I'm assuming that it's safe to change the C API because we haven't shipped the API in a release yet (LLVM 3.3 doesn't include the MCJIT memory management C API). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191804 91177308-0d34-0410-b5e6-96231b3b80d8 Filip Pizlo 6 years ago
16 changed file(s) with 99 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
3737 /// executable code. The SectionID is a unique identifier assigned by the JIT
3838 /// engine, and optionally recorded by the memory manager to access a loaded
3939 /// section.
40 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
41 unsigned SectionID) = 0;
42
40 virtual uint8_t *allocateCodeSection(
41 uintptr_t Size, unsigned Alignment, unsigned SectionID,
42 StringRef SectionName) = 0;
43
4344 /// Allocate a memory block of (at least) the given size suitable for data.
4445 /// The SectionID is a unique identifier assigned by the JIT engine, and
4546 /// optionally recorded by the memory manager to access a loaded section.
46 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
47 unsigned SectionID, bool IsReadOnly) = 0;
47 virtual uint8_t *allocateDataSection(
48 uintptr_t Size, unsigned Alignment, unsigned SectionID,
49 StringRef SectionName, bool IsReadOnly) = 0;
4850
4951 /// Register the EH frames with the runtime so that c++ exceptions work.
5052 virtual void registerEHFrames(StringRef SectionData);
4848 /// The value of \p Alignment must be a power of two. If \p Alignment is zero
4949 /// a default alignment of 16 will be used.
5050 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
51 unsigned SectionID);
51 unsigned SectionID,
52 StringRef SectionName);
5253
5354 /// \brief Allocates a memory block of (at least) the given size suitable for
5455 /// executable code.
5758 /// a default alignment of 16 will be used.
5859 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
5960 unsigned SectionID,
61 StringRef SectionName,
6062 bool isReadOnly);
6163
6264 /// \brief Update section-specific memory permissions and other attributes.
170170
171171 /*===-- Operations on memory managers -------------------------------------===*/
172172
173 typedef uint8_t *(*LLVMMemoryManagerAllocateCodeSectionCallback)(void *Opaque,
174 uintptr_t Size, unsigned Alignment,
175 unsigned SectionID);
176 typedef uint8_t *(*LLVMMemoryManagerAllocateDataSectionCallback)(void *Opaque,
177 uintptr_t Size, unsigned Alignment,
178 unsigned SectionID, LLVMBool IsReadOnly);
179 typedef LLVMBool (*LLVMMemoryManagerFinalizeMemoryCallback)(void *Opaque, char **ErrMsg);
173 typedef uint8_t *(*LLVMMemoryManagerAllocateCodeSectionCallback)(
174 void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID,
175 const char *SectionName);
176 typedef uint8_t *(*LLVMMemoryManagerAllocateDataSectionCallback)(
177 void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID,
178 const char *SectionName, LLVMBool IsReadOnly);
179 typedef LLVMBool (*LLVMMemoryManagerFinalizeMemoryCallback)(
180 void *Opaque, char **ErrMsg);
180181 typedef void (*LLVMMemoryManagerDestroyCallback)(void *Opaque);
181182
182183 /**
350350 void *Opaque);
351351 virtual ~SimpleBindingMemoryManager();
352352
353 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
354 unsigned SectionID);
355
356 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
357 unsigned SectionID,
358 bool isReadOnly);
353 virtual uint8_t *allocateCodeSection(
354 uintptr_t Size, unsigned Alignment, unsigned SectionID,
355 StringRef SectionName);
356
357 virtual uint8_t *allocateDataSection(
358 uintptr_t Size, unsigned Alignment, unsigned SectionID,
359 StringRef SectionName, bool isReadOnly);
359360
360361 virtual bool finalizeMemory(std::string *ErrMsg);
361362
383384 }
384385
385386 uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
386 uintptr_t Size, unsigned Alignment, unsigned SectionID) {
387 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID);
387 uintptr_t Size, unsigned Alignment, unsigned SectionID,
388 StringRef SectionName) {
389 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
390 SectionName.str().c_str());
388391 }
389392
390393 uint8_t *SimpleBindingMemoryManager::allocateDataSection(
391 uintptr_t Size, unsigned Alignment, unsigned SectionID, bool isReadOnly) {
394 uintptr_t Size, unsigned Alignment, unsigned SectionID,
395 StringRef SectionName, bool isReadOnly) {
392396 return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
397 SectionName.str().c_str(),
393398 isReadOnly);
394399 }
395400
463463
464464 /// allocateCodeSection - Allocate memory for a code section.
465465 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
466 unsigned SectionID) {
466 unsigned SectionID, StringRef SectionName) {
467467 // Grow the required block size to account for the block header
468468 Size += sizeof(*CurBlock);
469469
509509
510510 /// allocateDataSection - Allocate memory for a data section.
511511 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
512 unsigned SectionID, bool IsReadOnly) {
512 unsigned SectionID, StringRef SectionName,
513 bool IsReadOnly) {
513514 return (uint8_t*)DataAllocator.Allocate(Size, Alignment);
514515 }
515516
3434
3535 // Functions deferred to client memory manager
3636 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
37 unsigned SectionID) {
38 return ClientMM->allocateCodeSection(Size, Alignment, SectionID);
37 unsigned SectionID, StringRef SectionName) {
38 return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
3939 }
4040
4141 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
42 unsigned SectionID, bool IsReadOnly) {
42 unsigned SectionID, StringRef SectionName,
43 bool IsReadOnly) {
4344 return ClientMM->allocateDataSection(Size, Alignment,
44 SectionID, IsReadOnly);
45 SectionID, SectionName, IsReadOnly);
4546 }
4647
4748 virtual void registerEHFrames(StringRef SectionData) {
1818 namespace llvm {
1919
2020 uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
21 unsigned Alignment,
22 unsigned SectionID,
23 bool IsReadOnly) {
21 unsigned Alignment,
22 unsigned SectionID,
23 StringRef SectionName,
24 bool IsReadOnly) {
2425 if (IsReadOnly)
2526 return allocateSection(RODataMem, Size, Alignment);
2627 return allocateSection(RWDataMem, Size, Alignment);
2829
2930 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
3031 unsigned Alignment,
31 unsigned SectionID) {
32 unsigned SectionID,
33 StringRef SectionName) {
3234 return allocateSection(CodeMem, Size, Alignment);
3335 }
3436
181181 SymbolTableMap &SymbolTable) {
182182 // Allocate memory for the section
183183 unsigned SectionID = Sections.size();
184 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
185 SectionID, false);
184 uint8_t *Addr = MemMgr->allocateDataSection(
185 TotalSize, sizeof(void*), SectionID, StringRef(), false);
186186 if (!Addr)
187187 report_fatal_error("Unable to allocate memory for common symbols!");
188188 uint64_t Offset = 0;
277277 if (IsRequired) {
278278 Allocate = DataSize + StubBufSize;
279279 Addr = IsCode
280 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
281 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly);
280 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID, Name)
281 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, Name,
282 IsReadOnly);
282283 if (!Addr)
283284 report_fatal_error("Unable to allocate section memory!");
284285
13361336 // Allocate memory for the section
13371337 unsigned SectionID = Sections.size();
13381338 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1339 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1340 SectionID, false);
1339 uint8_t *Addr = MemMgr->allocateDataSection(
1340 TotalSize, getGOTEntrySize(), SectionID, ".got", false);
13411341 if (!Addr)
13421342 report_fatal_error("Unable to allocate memory for GOT!");
13431343 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
2626 }
2727
2828 uint8_t *RecordingMemoryManager::
29 allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID) {
29 allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
30 StringRef SectionName) {
3031 // The recording memory manager is just a local copy of the remote target.
3132 // The alignment requirement is just stored here for later use. Regular
3233 // heap storage is sufficient here, but we're using mapped memory to work
3839
3940 uint8_t *RecordingMemoryManager::
4041 allocateDataSection(uintptr_t Size, unsigned Alignment,
41 unsigned SectionID, bool IsReadOnly) {
42 unsigned SectionID, StringRef SectionName,
43 bool IsReadOnly) {
4244 // The recording memory manager is just a local copy of the remote target.
4345 // The alignment requirement is just stored here for later use. Regular
4446 // heap storage is sufficient here, but we're using mapped memory to work
4949 const_code_iterator code_end() const { return AllocatedCodeMem.end(); }
5050
5151 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
52 unsigned SectionID);
52 unsigned SectionID, StringRef SectionName);
5353
5454 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
55 unsigned SectionID, bool IsReadOnly);
55 unsigned SectionID, StringRef SectionName,
56 bool IsReadOnly);
5657
5758 void *getPointerToNamedFunction(const std::string &Name,
5859 bool AbortOnFailure = true);
5959 SmallVector DataMemory;
6060
6161 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
62 unsigned SectionID);
62 unsigned SectionID, StringRef SectionName);
6363 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
64 unsigned SectionID, bool IsReadOnly);
64 unsigned SectionID, StringRef SectionName,
65 bool IsReadOnly);
6566
6667 virtual void *getPointerToNamedFunction(const std::string &Name,
6768 bool AbortOnFailure = true) {
7980
8081 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
8182 unsigned Alignment,
82 unsigned SectionID) {
83 unsigned SectionID,
84 StringRef SectionName) {
8385 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
8486 FunctionMemory.push_back(MB);
8587 return (uint8_t*)MB.base();
8890 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
8991 unsigned Alignment,
9092 unsigned SectionID,
93 StringRef SectionName,
9194 bool IsReadOnly) {
9295 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
9396 DataMemory.push_back(MB);
280280 TEST(JITMemoryManagerTest, AllocateSection) {
281281 OwningPtr MemMgr(
282282 JITMemoryManager::CreateDefaultMemManager());
283 uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1);
284 uint8_t *data1 = MemMgr->allocateDataSection(256, 16, 2, true);
285 uint8_t *code2 = MemMgr->allocateCodeSection(257, 32, 3);
286 uint8_t *data2 = MemMgr->allocateDataSection(256, 64, 4, false);
287 uint8_t *code3 = MemMgr->allocateCodeSection(258, 64, 5);
283 uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1, StringRef());
284 uint8_t *data1 = MemMgr->allocateDataSection(256, 16, 2, StringRef(), true);
285 uint8_t *code2 = MemMgr->allocateCodeSection(257, 32, 3, StringRef());
286 uint8_t *data2 = MemMgr->allocateDataSection(256, 64, 4, StringRef(), false);
287 uint8_t *code3 = MemMgr->allocateCodeSection(258, 64, 5, StringRef());
288288
289289 EXPECT_NE((uint8_t*)0, code1);
290290 EXPECT_NE((uint8_t*)0, code2);
134134 EndFunctionBodyCall(F, FunctionStart, FunctionEnd));
135135 Base->endFunctionBody(F, FunctionStart, FunctionEnd);
136136 }
137 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
138 unsigned SectionID, bool IsReadOnly) {
139 return Base->allocateDataSection(Size, Alignment, SectionID, IsReadOnly);
140 }
141 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
142 unsigned SectionID) {
143 return Base->allocateCodeSection(Size, Alignment, SectionID);
137 virtual uint8_t *allocateDataSection(
138 uintptr_t Size, unsigned Alignment, unsigned SectionID,
139 StringRef SectionName, bool IsReadOnly) {
140 return Base->allocateDataSection(
141 Size, Alignment, SectionID, SectionName, IsReadOnly);
142 }
143 virtual uint8_t *allocateCodeSection(
144 uintptr_t Size, unsigned Alignment, unsigned SectionID,
145 StringRef SectionName) {
146 return Base->allocateCodeSection(
147 Size, Alignment, SectionID, SectionName);
144148 }
145149 virtual bool finalizeMemory(std::string *ErrMsg) { return false; }
146150 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
2727
2828 static uint8_t *roundTripAllocateCodeSection(void *object, uintptr_t size,
2929 unsigned alignment,
30 unsigned sectionID) {
30 unsigned sectionID,
31 const char *sectionName) {
3132 didCallAllocateCodeSection = true;
3233 return static_cast(object)->allocateCodeSection(
33 size, alignment, sectionID);
34 size, alignment, sectionID, sectionName);
3435 }
3536
3637 static uint8_t *roundTripAllocateDataSection(void *object, uintptr_t size,
3738 unsigned alignment,
3839 unsigned sectionID,
40 const char *sectionName,
3941 LLVMBool isReadOnly) {
4042 return static_cast(object)->allocateDataSection(
41 size, alignment, sectionID, isReadOnly);
43 size, alignment, sectionID, sectionName, isReadOnly);
4244 }
4345
4446 static LLVMBool roundTripFinalizeMemory(void *object, char **errMsg) {
1818 TEST(MCJITMemoryManagerTest, BasicAllocations) {
1919 OwningPtr MemMgr(new SectionMemoryManager());
2020
21 uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1);
22 uint8_t *data1 = MemMgr->allocateDataSection(256, 0, 2, true);
23 uint8_t *code2 = MemMgr->allocateCodeSection(256, 0, 3);
24 uint8_t *data2 = MemMgr->allocateDataSection(256, 0, 4, false);
21 uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1, "");
22 uint8_t *data1 = MemMgr->allocateDataSection(256, 0, 2, "", true);
23 uint8_t *code2 = MemMgr->allocateCodeSection(256, 0, 3, "");
24 uint8_t *data2 = MemMgr->allocateDataSection(256, 0, 4, "", false);
2525
2626 EXPECT_NE((uint8_t*)0, code1);
2727 EXPECT_NE((uint8_t*)0, code2);
5151 TEST(MCJITMemoryManagerTest, LargeAllocations) {
5252 OwningPtr MemMgr(new SectionMemoryManager());
5353
54 uint8_t *code1 = MemMgr->allocateCodeSection(0x100000, 0, 1);
55 uint8_t *data1 = MemMgr->allocateDataSection(0x100000, 0, 2, true);
56 uint8_t *code2 = MemMgr->allocateCodeSection(0x100000, 0, 3);
57 uint8_t *data2 = MemMgr->allocateDataSection(0x100000, 0, 4, false);
54 uint8_t *code1 = MemMgr->allocateCodeSection(0x100000, 0, 1, "");
55 uint8_t *data1 = MemMgr->allocateDataSection(0x100000, 0, 2, "", true);
56 uint8_t *code2 = MemMgr->allocateCodeSection(0x100000, 0, 3, "");
57 uint8_t *data2 = MemMgr->allocateDataSection(0x100000, 0, 4, "", false);
5858
5959 EXPECT_NE((uint8_t*)0, code1);
6060 EXPECT_NE((uint8_t*)0, code2);
9090 for (unsigned i = 0; i < 10000; ++i) {
9191 const bool isReadOnly = i % 2 == 0;
9292
93 code[i] = MemMgr->allocateCodeSection(32, 0, 1);
94 data[i] = MemMgr->allocateDataSection(32, 0, 2, isReadOnly);
93 code[i] = MemMgr->allocateCodeSection(32, 0, 1, "");
94 data[i] = MemMgr->allocateDataSection(32, 0, 2, "", isReadOnly);
9595
9696 for (unsigned j = 0; j < 32; j++) {
9797 code[i][j] = 1 + (i % 254);
129129 bool isReadOnly = i % 3 == 0;
130130 unsigned Align = 8 << (i % 4);
131131
132 code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i);
133 data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000,
132 code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i, "");
133 data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000, "",
134134 isReadOnly);
135135
136136 for (unsigned j = 0; j < CodeSize; j++) {