[LLVMdev] Proper way to use "host application" function from JIT code

Andrea Bigagli andrea.bigagli at me.com
Sun Apr 15 08:21:31 PDT 2012


Hi,
I found myself doing very similar things to what's discussed in http://lists.cs.uiuc.edu/pipermail/cfe-dev/2010-July/009836.html.

More precisely, I've modified the clang-interpreter example so that I can specify a .cpp source file at runtime which is JIT-compiled and some functions inside it get called by the interpreter executable itself.
In this context, "host-application" is actually the clang-interpreter executable, while the external .cpp file is what I refer to as the "plugin.cpp".

Proceeding with my experiments, I wanted to try to "export" a function from the "host-application" to be called by the "plugin" and following the discussion at the above linked address, it seems like I should have to go through the 

llvm::Function *KnownFunction = cast<llvm::Function>(Mod->getOrInsertFunction(...)

route, but it turns out I was able to simply define a function in the host-application, and then extern declare it in my plugin.cpp and then be able to call it from one of the functions inside the plugin itself.

In other words, I didn't have to do any "manual" function instantiation in the jitted code.
Has something changed (that wasn't possible at the time of the referenced discussion), or am I doing something that's working by chance but conceptually wrong?

On a side note, I found out that in this case of a host-application exported function I don't even have to declare it as extern "C", as the reference in the plugin is correctly mangled, although the host-application is compiled with gcc and so I think this works almost by chance.

On the other side, all functions defined in the plugin that I'm calling from the host-application seem to have to be declared extern "C", otherwise all calls to llvm::Module::getFunction fail.
Is this the only way to do that or is there some supported way to find mangled function names in the plugin, other than dumping its contents and discovering what's the real name in the IR?

Thanks,
Andrea.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120415/b04becdf/attachment.html>


More information about the llvm-dev mailing list