Hey there, I'm relatively new to the llvm scene, and so far I'm liking it quite a bit. I'm a bit perplexed as to what llvm-gcc's role is in everything. I've used it to create .bc files for really simple functions and it seems to do quite well at that, but I've been trying to figure out how to take the output from llvm-gcc and actually use it in another program. I tried to get a module out of a file that I'd read in to it using this code: MemoryBuffer* memBuf = MemoryBuffer::getFile(filename, sizeof filename, &errStr); printf("errStr: %s\n", errStr.c_str()); BitcodeReader::BitcodeReader bcReader(memBuf); Module* Mod = bcReader.materializeModule(&errInfo); printf("errInfo: %s\n", errInfo.c_str()); verifyModule(*Mod, PrintMessageAction); The errStr and the errInfo strings were empty, but I got a crash on the verifyModule call. The filename was produced by running "llvm-gcc -c -emit-llvm tmp.cpp" on the file tmp.cpp, which just contains: #include <stdio.h> typedef struct _whatever { float fa; float fb; float fr; int i; }mystruct; extern "C" float whatever(mystruct*); float whatever(mystruct* str) { return str->fa + str->fb + str->fr + str->i; } int main(int argc, char**argv) { mystruct str = {45.0, 2.0, 0.0, 3}; printf("result = %f\n", whatever(&str)); return 0; } Did I miss something fundamental here? Also I've noticed that the BitcodeReader appears to be an internal module, but the BitstreamReader is public. Should I be using the BitstreamReader? If so how. Thank you! -danny
Hi Danny, On 2007-12-26, at 15:39, Danny wrote:> I've noticed that the BitcodeReader appears to be an internal > module, but the BitstreamReader is public. Should I be using the > BitstreamReader? If so how.The generic BitstreamReader class is public because it's used in other projects, including clang, to serialize data structures other than LLVM IR. The coding of the LLVM IR within the bitstream are private to the reader and writer modules. The functions exposed in ReaderWriter.h are the public interface to that functionality.> I tried to get a module out of a file that I'd read in to it using > this code: > > MemoryBuffer* memBuf = MemoryBuffer::getFile(filename, sizeof > filename, &errStr); > printf("errStr: %s\n", errStr.c_str()); > BitcodeReader::BitcodeReader bcReader(memBuf); > Module* Mod = bcReader.materializeModule(&errInfo); > printf("errInfo: %s\n", errInfo.c_str()); > > verifyModule(*Mod, PrintMessageAction); > > The errStr and the errInfo strings were empty, but I got a crash on > the verifyModule call.Try this: #include "llvm/Bitcode/ReaderWriter.h" ... std::string &Message; llvm::Module* Mod = llvm::ParseBitcodeFile(MemBuf, &Message); if (!Mod) { // Message contains an error message. std::cerr << "error reading bitcode: " << Message << "\n"; exit(1); // Don't segfault! } // Okay, Mod is valid. ... delete Mod; Also note that the primary error indicator is not the string, but the return pointer, so you should always check that (not the error string) and stop if it's null in order to avoid segfaulting. — Gordon -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20071226/0eb7bf1a/attachment.html>
That worked quite well. Thank you. One question as a follow up: is there a nice/standard way of including the pre-made bitcode chunks in with the binaries that are being created which read them? Bascially, I'd like to have the same functionality, but rather than having one or more .bc files running around which need to be read at runtime by an executable, moving that into the code itself so it's all contained in one. Thanks, danny On Dec 26, 2007, at 3:33 PM, Gordon Henriksen wrote:> Hi Danny, > > On 2007-12-26, at 15:39, Danny wrote: > >> I've noticed that the BitcodeReader appears to be an internal >> module, but the BitstreamReader is public. Should I be using the >> BitstreamReader? If so how. > > The generic BitstreamReader class is public because it's used in > other projects, including clang, to serialize data structures other > than LLVM IR. The coding of the LLVM IR within the bitstream are > private to the reader and writer modules. The functions exposed in > ReaderWriter.h are the public interface to that functionality. > >> I tried to get a module out of a file that I'd read in to it using >> this code: >> >> MemoryBuffer* memBuf = MemoryBuffer::getFile(filename, sizeof >> filename, &errStr); >> printf("errStr: %s\n", errStr.c_str()); >> BitcodeReader::BitcodeReader bcReader(memBuf); >> Module* Mod = bcReader.materializeModule(&errInfo); >> printf("errInfo: %s\n", errInfo.c_str()); >> >> verifyModule(*Mod, PrintMessageAction); >> >> The errStr and the errInfo strings were empty, but I got a crash >> on the verifyModule call. > > > Try this: > > #include "llvm/Bitcode/ReaderWriter.h" > > ... > > std::string &Message; > llvm::Module* Mod = llvm::ParseBitcodeFile(MemBuf, &Message); > if (!Mod) { > // Message contains an error message. > std::cerr << "error reading bitcode: " << Message << "\n"; > exit(1); // Don't segfault! > } > > // Okay, Mod is valid. > ... > > delete Mod; > > Also note that the primary error indicator is not the string, but > the return pointer, so you should always check that (not the error > string) and stop if it's null in order to avoid segfaulting. > > — Gordon > > _______________________________________________ > 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/20071226/c1a33f3e/attachment.html>
Apparently Analagous Threads
- [LLVMdev] Instantiating modules from .bc files
- [LLVMdev] Instantiating modules from .bc files
- [LLVMdev] How to get a return type of a function with LLVM-C API
- [LLVMdev] reading a module from a memory string (BitCode)
- [LLVMdev] [PATCH / PROPOSAL] bitcode encoding that is ~15% smaller for large bitcode files...