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).

llvm-svn: 191804
This commit is contained in:
Filip Pizlo 2013-10-02 00:59:25 +00:00
parent f2dd4ef157
commit 7aa695e026
16 changed files with 97 additions and 70 deletions

View File

@ -171,13 +171,14 @@ void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global);
/*===-- Operations on memory managers -------------------------------------===*/
typedef uint8_t *(*LLVMMemoryManagerAllocateCodeSectionCallback)(void *Opaque,
uintptr_t Size, unsigned Alignment,
unsigned SectionID);
typedef uint8_t *(*LLVMMemoryManagerAllocateDataSectionCallback)(void *Opaque,
uintptr_t Size, unsigned Alignment,
unsigned SectionID, LLVMBool IsReadOnly);
typedef LLVMBool (*LLVMMemoryManagerFinalizeMemoryCallback)(void *Opaque, char **ErrMsg);
typedef uint8_t *(*LLVMMemoryManagerAllocateCodeSectionCallback)(
void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID,
const char *SectionName);
typedef uint8_t *(*LLVMMemoryManagerAllocateDataSectionCallback)(
void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID,
const char *SectionName, LLVMBool IsReadOnly);
typedef LLVMBool (*LLVMMemoryManagerFinalizeMemoryCallback)(
void *Opaque, char **ErrMsg);
typedef void (*LLVMMemoryManagerDestroyCallback)(void *Opaque);
/**

View File

@ -38,14 +38,16 @@ public:
/// executable code. The SectionID is a unique identifier assigned by the JIT
/// engine, and optionally recorded by the memory manager to access a loaded
/// section.
virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID) = 0;
virtual uint8_t *allocateCodeSection(
uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName) = 0;
/// Allocate a memory block of (at least) the given size suitable for data.
/// The SectionID is a unique identifier assigned by the JIT engine, and
/// optionally recorded by the memory manager to access a loaded section.
virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, bool IsReadOnly) = 0;
virtual uint8_t *allocateDataSection(
uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName, bool IsReadOnly) = 0;
/// Register the EH frames with the runtime so that c++ exceptions work.
virtual void registerEHFrames(StringRef SectionData);

View File

@ -49,7 +49,8 @@ public:
/// The value of \p Alignment must be a power of two. If \p Alignment is zero
/// a default alignment of 16 will be used.
virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID);
unsigned SectionID,
StringRef SectionName);
/// \brief Allocates a memory block of (at least) the given size suitable for
/// executable code.
@ -58,6 +59,7 @@ public:
/// a default alignment of 16 will be used.
virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID,
StringRef SectionName,
bool isReadOnly);
/// \brief Update section-specific memory permissions and other attributes.

View File

@ -351,12 +351,13 @@ public:
void *Opaque);
virtual ~SimpleBindingMemoryManager();
virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID);
virtual uint8_t *allocateCodeSection(
uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName);
virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID,
bool isReadOnly);
virtual uint8_t *allocateDataSection(
uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName, bool isReadOnly);
virtual bool finalizeMemory(std::string *ErrMsg);
@ -384,13 +385,17 @@ SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
}
uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
uintptr_t Size, unsigned Alignment, unsigned SectionID) {
return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID);
uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName) {
return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
SectionName.str().c_str());
}
uint8_t *SimpleBindingMemoryManager::allocateDataSection(
uintptr_t Size, unsigned Alignment, unsigned SectionID, bool isReadOnly) {
uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName, bool isReadOnly) {
return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
SectionName.str().c_str(),
isReadOnly);
}

View File

@ -464,7 +464,7 @@ namespace {
/// allocateCodeSection - Allocate memory for a code section.
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID) {
unsigned SectionID, StringRef SectionName) {
// Grow the required block size to account for the block header
Size += sizeof(*CurBlock);
@ -510,7 +510,8 @@ namespace {
/// allocateDataSection - Allocate memory for a data section.
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, bool IsReadOnly) {
unsigned SectionID, StringRef SectionName,
bool IsReadOnly) {
return (uint8_t*)DataAllocator.Allocate(Size, Alignment);
}

View File

@ -35,14 +35,15 @@ public:
// Functions deferred to client memory manager
virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID) {
return ClientMM->allocateCodeSection(Size, Alignment, SectionID);
unsigned SectionID, StringRef SectionName) {
return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
}
virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, bool IsReadOnly) {
unsigned SectionID, StringRef SectionName,
bool IsReadOnly) {
return ClientMM->allocateDataSection(Size, Alignment,
SectionID, IsReadOnly);
SectionID, SectionName, IsReadOnly);
}
virtual void registerEHFrames(StringRef SectionData) {

View File

@ -19,9 +19,10 @@
namespace llvm {
uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
bool IsReadOnly) {
unsigned Alignment,
unsigned SectionID,
StringRef SectionName,
bool IsReadOnly) {
if (IsReadOnly)
return allocateSection(RODataMem, Size, Alignment);
return allocateSection(RWDataMem, Size, Alignment);
@ -29,7 +30,8 @@ uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
unsigned Alignment,
unsigned SectionID) {
unsigned SectionID,
StringRef SectionName) {
return allocateSection(CodeMem, Size, Alignment);
}

View File

@ -182,8 +182,8 @@ void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
SymbolTableMap &SymbolTable) {
// Allocate memory for the section
unsigned SectionID = Sections.size();
uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
SectionID, false);
uint8_t *Addr = MemMgr->allocateDataSection(
TotalSize, sizeof(void*), SectionID, StringRef(), false);
if (!Addr)
report_fatal_error("Unable to allocate memory for common symbols!");
uint64_t Offset = 0;
@ -278,8 +278,9 @@ unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
if (IsRequired) {
Allocate = DataSize + StubBufSize;
Addr = IsCode
? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
: MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly);
? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID, Name)
: MemMgr->allocateDataSection(Allocate, Alignment, SectionID, Name,
IsReadOnly);
if (!Addr)
report_fatal_error("Unable to allocate section memory!");

View File

@ -1337,8 +1337,8 @@ void RuntimeDyldELF::finalizeLoad() {
// Allocate memory for the section
unsigned SectionID = Sections.size();
size_t TotalSize = numGOTEntries * getGOTEntrySize();
uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
SectionID, false);
uint8_t *Addr = MemMgr->allocateDataSection(
TotalSize, getGOTEntrySize(), SectionID, ".got", false);
if (!Addr)
report_fatal_error("Unable to allocate memory for GOT!");
Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));

View File

@ -27,7 +27,8 @@ RecordingMemoryManager::~RecordingMemoryManager() {
}
uint8_t *RecordingMemoryManager::
allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID) {
allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName) {
// The recording memory manager is just a local copy of the remote target.
// The alignment requirement is just stored here for later use. Regular
// heap storage is sufficient here, but we're using mapped memory to work
@ -39,7 +40,8 @@ allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID) {
uint8_t *RecordingMemoryManager::
allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, bool IsReadOnly) {
unsigned SectionID, StringRef SectionName,
bool IsReadOnly) {
// The recording memory manager is just a local copy of the remote target.
// The alignment requirement is just stored here for later use. Regular
// heap storage is sufficient here, but we're using mapped memory to work

View File

@ -50,10 +50,11 @@ public:
const_code_iterator code_end() const { return AllocatedCodeMem.end(); }
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID);
unsigned SectionID, StringRef SectionName);
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, bool IsReadOnly);
unsigned SectionID, StringRef SectionName,
bool IsReadOnly);
void *getPointerToNamedFunction(const std::string &Name,
bool AbortOnFailure = true);

View File

@ -60,9 +60,10 @@ public:
SmallVector<sys::MemoryBlock, 16> DataMemory;
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID);
unsigned SectionID, StringRef SectionName);
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, bool IsReadOnly);
unsigned SectionID, StringRef SectionName,
bool IsReadOnly);
virtual void *getPointerToNamedFunction(const std::string &Name,
bool AbortOnFailure = true) {
@ -80,7 +81,8 @@ public:
uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
unsigned Alignment,
unsigned SectionID) {
unsigned SectionID,
StringRef SectionName) {
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
FunctionMemory.push_back(MB);
return (uint8_t*)MB.base();
@ -89,6 +91,7 @@ uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
StringRef SectionName,
bool IsReadOnly) {
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
DataMemory.push_back(MB);

View File

@ -281,11 +281,11 @@ TEST(JITMemoryManagerTest, TestManyStubs) {
TEST(JITMemoryManagerTest, AllocateSection) {
OwningPtr<JITMemoryManager> MemMgr(
JITMemoryManager::CreateDefaultMemManager());
uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1);
uint8_t *data1 = MemMgr->allocateDataSection(256, 16, 2, true);
uint8_t *code2 = MemMgr->allocateCodeSection(257, 32, 3);
uint8_t *data2 = MemMgr->allocateDataSection(256, 64, 4, false);
uint8_t *code3 = MemMgr->allocateCodeSection(258, 64, 5);
uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1, StringRef());
uint8_t *data1 = MemMgr->allocateDataSection(256, 16, 2, StringRef(), true);
uint8_t *code2 = MemMgr->allocateCodeSection(257, 32, 3, StringRef());
uint8_t *data2 = MemMgr->allocateDataSection(256, 64, 4, StringRef(), false);
uint8_t *code3 = MemMgr->allocateCodeSection(258, 64, 5, StringRef());
EXPECT_NE((uint8_t*)0, code1);
EXPECT_NE((uint8_t*)0, code2);

View File

@ -135,13 +135,17 @@ public:
EndFunctionBodyCall(F, FunctionStart, FunctionEnd));
Base->endFunctionBody(F, FunctionStart, FunctionEnd);
}
virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, bool IsReadOnly) {
return Base->allocateDataSection(Size, Alignment, SectionID, IsReadOnly);
virtual uint8_t *allocateDataSection(
uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName, bool IsReadOnly) {
return Base->allocateDataSection(
Size, Alignment, SectionID, SectionName, IsReadOnly);
}
virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID) {
return Base->allocateCodeSection(Size, Alignment, SectionID);
virtual uint8_t *allocateCodeSection(
uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName) {
return Base->allocateCodeSection(
Size, Alignment, SectionID, SectionName);
}
virtual bool finalizeMemory(std::string *ErrMsg) { return false; }
virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {

View File

@ -28,18 +28,20 @@ static bool didCallAllocateCodeSection;
static uint8_t *roundTripAllocateCodeSection(void *object, uintptr_t size,
unsigned alignment,
unsigned sectionID) {
unsigned sectionID,
const char *sectionName) {
didCallAllocateCodeSection = true;
return static_cast<SectionMemoryManager*>(object)->allocateCodeSection(
size, alignment, sectionID);
size, alignment, sectionID, sectionName);
}
static uint8_t *roundTripAllocateDataSection(void *object, uintptr_t size,
unsigned alignment,
unsigned sectionID,
const char *sectionName,
LLVMBool isReadOnly) {
return static_cast<SectionMemoryManager*>(object)->allocateDataSection(
size, alignment, sectionID, isReadOnly);
size, alignment, sectionID, sectionName, isReadOnly);
}
static LLVMBool roundTripFinalizeMemory(void *object, char **errMsg) {

View File

@ -19,10 +19,10 @@ namespace {
TEST(MCJITMemoryManagerTest, BasicAllocations) {
OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1);
uint8_t *data1 = MemMgr->allocateDataSection(256, 0, 2, true);
uint8_t *code2 = MemMgr->allocateCodeSection(256, 0, 3);
uint8_t *data2 = MemMgr->allocateDataSection(256, 0, 4, false);
uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1, "");
uint8_t *data1 = MemMgr->allocateDataSection(256, 0, 2, "", true);
uint8_t *code2 = MemMgr->allocateCodeSection(256, 0, 3, "");
uint8_t *data2 = MemMgr->allocateDataSection(256, 0, 4, "", false);
EXPECT_NE((uint8_t*)0, code1);
EXPECT_NE((uint8_t*)0, code2);
@ -52,10 +52,10 @@ TEST(MCJITMemoryManagerTest, BasicAllocations) {
TEST(MCJITMemoryManagerTest, LargeAllocations) {
OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
uint8_t *code1 = MemMgr->allocateCodeSection(0x100000, 0, 1);
uint8_t *data1 = MemMgr->allocateDataSection(0x100000, 0, 2, true);
uint8_t *code2 = MemMgr->allocateCodeSection(0x100000, 0, 3);
uint8_t *data2 = MemMgr->allocateDataSection(0x100000, 0, 4, false);
uint8_t *code1 = MemMgr->allocateCodeSection(0x100000, 0, 1, "");
uint8_t *data1 = MemMgr->allocateDataSection(0x100000, 0, 2, "", true);
uint8_t *code2 = MemMgr->allocateCodeSection(0x100000, 0, 3, "");
uint8_t *data2 = MemMgr->allocateDataSection(0x100000, 0, 4, "", false);
EXPECT_NE((uint8_t*)0, code1);
EXPECT_NE((uint8_t*)0, code2);
@ -91,8 +91,8 @@ TEST(MCJITMemoryManagerTest, ManyAllocations) {
for (unsigned i = 0; i < 10000; ++i) {
const bool isReadOnly = i % 2 == 0;
code[i] = MemMgr->allocateCodeSection(32, 0, 1);
data[i] = MemMgr->allocateDataSection(32, 0, 2, isReadOnly);
code[i] = MemMgr->allocateCodeSection(32, 0, 1, "");
data[i] = MemMgr->allocateDataSection(32, 0, 2, "", isReadOnly);
for (unsigned j = 0; j < 32; j++) {
code[i][j] = 1 + (i % 254);
@ -130,8 +130,8 @@ TEST(MCJITMemoryManagerTest, ManyVariedAllocations) {
bool isReadOnly = i % 3 == 0;
unsigned Align = 8 << (i % 4);
code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i);
data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000,
code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i, "");
data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000, "",
isReadOnly);
for (unsigned j = 0; j < CodeSize; j++) {