[llvm-dev] JIT compiler and calls to existing functions
Caldarale, Charles R via llvm-dev
llvm-dev at lists.llvm.org
Tue Mar 29 19:27:17 PDT 2016
> From: Philip Reames [mailto:listmail at philipreames.com]
> Subject: Re: [llvm-dev] JIT compiler and calls to existing functions
> There is no documentation I know of. Rough sketch:
> 1) Create a subclass of SectionMemoryManager
> 2) Create an instance of this class and add it the EngineBuilder via setMCJITMemoryManager
> (Make sure everything runs without changes)
> 3) Override the getSymbolAddress method, have your implementation call the base classes impl
> (Make sure everything runs without changes)
> 4) Add handling to map name->address for any custom functions you want.
Here's what we use for the functions that have statically-known addresses:
First, make LLVM's symbol library available:
#include <llvm/Support/DynamicLibrary.h> // for linking in C code
Make sure the symbols from the executable are available:
sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
For each function whose name and code pointer are known, create its llvm::Function definition, and enter the name and code pointer into the symbol table if not already there:
fType = FunctionType::get(retType, margs, false);
pFunc = Function::Create(fType, Function::ExternalLinkage, Twine(name), pMod);
setFuncAttr(pFunc, margs); // customize as needed
if (sys::DynamicLibrary::SearchForAddressOfSymbol(name) == NULL) {
sys::DynamicLibrary::AddSymbol(name, (void*)ptr);
}
In our class derived from SectionMemoryManager, we have the following:
uint64_t getSymbolAddress(const std::string& Name) override {
const char* NameStr = Name.c_str();
void* ptr;
if (NameStr[0] == '_' &&
(ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr + 1)) != NULL) {
return uint64_t(ptr);
}
/* If Name did not require demangling, or we failed to find the demangled name,
try again without demangling. */
return uint64_t(sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr));
}
- Chuck
More information about the llvm-dev
mailing list