Hello everyone, I've been watching LLVM since about a year now, and I thought it was finally time I'd do something with it. So I've got my hands dirty with a cool project (who knows? maybe I'll end up releasing it), but since I'm fairly new to the whole thing, there are still a number of things I'm not too sure about. First, the way we create instructions confuses me a little. My best experience with factory-like patterns is the JavaScript DOM interface, where you'd do like document.createElement, mess with the new element, then put it back somewhere doing node.appendChild. However, seeing the results from llvm2cpp, it looks like simply creating an instruction is enough to put it somewhere (at the end of the specified basic block, I suppose) unless no parameter is specified. Skimming through the classes reference, I see no 'obvious' way to add an existing "orphan" instruction to a block. What would it be? Also, I'd like to integrate C++ method calls to my jitted code. From what I've seen in the Kaleidoscope tutorial it's easy to use external C functions but there's nothing about C++ functions or methods. Problem is, since their names are mangled, it won't be as easy as C for C functions. What I was really looking for was a way to create a Function object from a function pointer. I think I understand why it can't be that simple (LLVM really needs a function name, and function pointers don't have that) so I've made a funny hack with dladdr to get the name of a method at runtime, but just in case I really missed something I'll ask you guys: is there a "standard", LLVM-backed way to make a Function object from a function or method with C++ linkage? (I know I could also make an extern "C" function to wrap the call, but that's not quite as fun). Thanks for your help! Félix Cloutier
For the second question, I think you can just create a constant address (integer constant, then cast it to pointer-to-your-function type) and call that. Eugene 2010/6/24 Félix Cloutier <felixcca at yahoo.ca>:> Hello everyone, > > I've been watching LLVM since about a year now, and I thought it was finally time I'd do something with it. So I've got my hands dirty with a cool project (who knows? maybe I'll end up releasing it), but since I'm fairly new to the whole thing, there are still a number of things I'm not too sure about. > > First, the way we create instructions confuses me a little. My best experience with factory-like patterns is the JavaScript DOM interface, where you'd do like document.createElement, mess with the new element, then put it back somewhere doing node.appendChild. However, seeing the results from llvm2cpp, it looks like simply creating an instruction is enough to put it somewhere (at the end of the specified basic block, I suppose) unless no parameter is specified. Skimming through the classes reference, I see no 'obvious' way to add an existing "orphan" instruction to a block. What would it be? > > Also, I'd like to integrate C++ method calls to my jitted code. From what I've seen in the Kaleidoscope tutorial it's easy to use external C functions but there's nothing about C++ functions or methods. Problem is, since their names are mangled, it won't be as easy as C for C functions. What I was really looking for was a way to create a Function object from a function pointer. I think I understand why it can't be that simple (LLVM really needs a function name, and function pointers don't have that) so I've made a funny hack with dladdr to get the name of a method at runtime, but just in case I really missed something I'll ask you guys: is there a "standard", LLVM-backed way to make a Function object from a function or method with C++ linkage? (I know I could also make an extern "C" function to wrap the call, but that's not quite as fun). > > Thanks for your help! > > Félix Cloutier > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
2010/6/24 Félix Cloutier <felixcca at yahoo.ca>:> Hello everyone, > > I've been watching LLVM since about a year now, and I thought it was finally time I'd do something with it. So I've got my hands dirty with a cool project (who knows? maybe I'll end up releasing it), but since I'm fairly new to the whole thing, there are still a number of things I'm not too sure about. > > First, the way we create instructions confuses me a little. My best experience with factory-like patterns is the JavaScript DOM interface, where you'd do like document.createElement, mess with the new element, then put it back somewhere doing node.appendChild. However, seeing the results from llvm2cpp, it looks like simply creating an instruction is enough to put it somewhere (at the end of the specified basic block, I suppose) unless no parameter is specified. Skimming through the classes reference, I see no 'obvious' way to add an existing "orphan" instruction to a block. What would it be?Instruction::insertBefore? Or if you prefer, you can mess with the result of BasicBlock::getInstList() directly.> Also, I'd like to integrate C++ method calls to my jitted code. From what I've seen in the Kaleidoscope tutorial it's easy to use external C functions but there's nothing about C++ functions or methods. Problem is, since their names are mangled, it won't be as easy as C for C functions. What I was really looking for was a way to create a Function object from a function pointer. I think I understand why it can't be that simple (LLVM really needs a function name, and function pointers don't have that) so I've made a funny hack with dladdr to get the name of a method at runtime, but just in case I really missed something I'll ask you guys: is there a "standard", LLVM-backed way to make a Function object from a function or method with C++ linkage? (I know I could also make an extern "C" function to wrap the call, but that's not quite as fun).You can cast your pointer to an integer, create an LLVM integer constant, and create a constant cast from that to the relevant pointer type. Or, you can add an arbitrarily-named Function to the module, and map it to the appropriate address with ExecutionEngine::addGlobalMapping. Whichever is more convenient... -Eli
On Jun 24, 2010, at 12:16 PM, Félix Cloutier wrote:> First, the way we create instructions confuses me a little. My best experience with factory-like patterns is the JavaScript DOM interface, where you'd do like document.createElement, mess with the new element, then put it back somewhere doing node.appendChild. However, seeing the results from llvm2cpp, it looks like simply creating an instruction is enough to put it somewhere (at the end of the specified basic block, I suppose) unless no parameter is specified. Skimming through the classes reference, I see no 'obvious' way to add an existing "orphan" instruction to a block. What would it be?The IRBuilder class is more than just a factory; it's actually designed to write instructions sequentially into the current block. However, if you don't give it an insertion point, it will just create the instructions as orphans, and you can land them in basic blocks with the APIs that Eli mentioned. That said, I *strongly* encourage you to use IRBuilder to create instructions sequentially rather than trying to place them all yourself. John.
Felix, It really depends on what you are doing with the JIT. In my case, I have a scripting language that has a limited set of base data types. I wanted to be able to trivially interface to new C++ methods and classes, so I'm interfacing to C++ classes by creating external definitions much like the way Kaleidoscope accesses external C functions. I only have to deal with a few types so I can do the name mangling automatically pretty easily without having to do too much work. If you are just dealing with relatively simple functions it's not too hard. See section 5.1.2 of the document here: http://www.codesourcery.com/public/cxx-abi/abi.html for details on the mangling, if you are targeting the Itanium ABI which is the gcc/clang default ABI for X86-64. I have found the following suggestion (made by someone on the llvm IRC channel) to figure out how LLVM IR works extremely helpful. First, I write a small sample code in C or C++ which I then compile into LLVM IR using: /usr/local/bin/clang++ -S -O0 -emit-llvm source.cpp where source.cpp contains my sample C++ code or /usr/local/bin/clang -S -O0 -emit-llvm source.c for C code. I suggest building clang and LLVM from trunk if you are going to be doing much with C++ as 2.7's C++ support is not near as good as trunk's. Once you've decided exactly what LLVM IR you want. You can see how to use the IRBuilder to create it using the very handy: /usr/local/bin/llc -march=cpp source.s which creates a file called source.s.cpp which contains the C++ which will create the LLVM IR found in source.s. I hope that helps. - Curtis On Jun 24, 2010, at 5:32 PM, Eli Friedman wrote:> 2010/6/24 Félix Cloutier <felixcca at yahoo.ca>: >> Hello everyone, >> >> I've been watching LLVM since about a year now, and I thought it was finally time I'd do something with it. So I've got my hands dirty with a cool project (who knows? maybe I'll end up releasing it), but since I'm fairly new to the whole thing, there are still a number of things I'm not too sure about. >> >> First, the way we create instructions confuses me a little. My best experience with factory-like patterns is the JavaScript DOM interface, where you'd do like document.createElement, mess with the new element, then put it back somewhere doing node.appendChild. However, seeing the results from llvm2cpp, it looks like simply creating an instruction is enough to put it somewhere (at the end of the specified basic block, I suppose) unless no parameter is specified. Skimming through the classes reference, I see no 'obvious' way to add an existing "orphan" instruction to a block. What would it be? > > Instruction::insertBefore? Or if you prefer, you can mess with the > result of BasicBlock::getInstList() directly. > >> Also, I'd like to integrate C++ method calls to my jitted code. From what I've seen in the Kaleidoscope tutorial it's easy to use external C functions but there's nothing about C++ functions or methods. Problem is, since their names are mangled, it won't be as easy as C for C functions. What I was really looking for was a way to create a Function object from a function pointer. I think I understand why it can't be that simple (LLVM really needs a function name, and function pointers don't have that) so I've made a funny hack with dladdr to get the name of a method at runtime, but just in case I really missed something I'll ask you guys: is there a "standard", LLVM-backed way to make a Function object from a function or method with C++ linkage? (I know I could also make an extern "C" function to wrap the call, but that's not quite as fun). > > You can cast your pointer to an integer, create an LLVM integer > constant, and create a constant cast from that to the relevant pointer > type. Or, you can add an arbitrarily-named Function to the module, > and map it to the appropriate address with > ExecutionEngine::addGlobalMapping. Whichever is more convenient... > > -Eli > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100624/5f43d4c4/attachment.html>