[LLVMdev] Instantiating modules from .bc files

Danny llvm at pixelperplexity.net
Wed Dec 26 14:39:36 PST 2007

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;

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!

More information about the llvm-dev mailing list