[LLVMdev] reading a module from a memory string (BitCode)

Chris Lattner sabre at nondot.org
Sat May 12 16:42:49 PDT 2007

On Sat, 12 May 2007, Basile STARYNKEVITCH wrote:
> with the latest LLVM (almost 2.0 CVS) what is the right way to read a module
> from a byte array fetched from a database?

The bitcode reader will read from any MemoryBuffer object.  There are a 
variety of static methods on MemoryBuffer to create them from files, 
stdio, and memory.  If your buffer is in memory, just create a 
memorybuffer and pass in the range of bytes already in memory.  Once the 
appropriate MemoryBuffer is created, you load it like any other bitcode 

> I thought that I could subclass llbm::module to add my own fields
> (typically, a MySQL id number) and then parse it as bitcode, but I am stuck,
> since apparently the only way to parse bitcode is to use a BitcodeReader
> then calling materializeModule gives a fresh llvm Module (not my subclass)

Right, don't do that :).  There are three easy ways to do this sort of 
thing: 1) add intrinsics to capture the information you want, 2) encode 
the information as global variable initializers (as we do with debug info) 
3) store it out-of-band, as reid suggests.

> As a general question, are Llvm classes supposed to be usually subclassed to
> add application data (like my modtime and id), or not...


> Does any one have some example of reading Bitcode encoded modules? Can I
> assume that such modules are not tied to a particular (LLVM target)
> architecture, in other words can I store into my database modules build on
> AMD64 and reload them on x86 (32 bits)?

LLVM IR is, in general, not portable if it comes from a non-portable 
source language like C.



More information about the llvm-dev mailing list