Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read an LLVM bitcode file into an llvm::Module?

I'm writing a compiler with LLVM. Each source file is compiled into an LLVM bitcode file. Eventually the linker links and optimizes all the bitcode files into one final binary.

I need a way to read the bitcode files in the compiler in order to access the type information. The LLVM documentation shows a class called BitcodeReader, but that appears to be internal to LLVM.

Is there any publicly accessible way to read a bitcode file into an llvm::Module?

like image 630
Zifre Avatar asked Aug 29 '10 20:08

Zifre


People also ask

What is bitcode file?

What is commonly known as the LLVM bitcode file format (also, sometimes anachronistically known as bytecode) is actually two things: a bitstream container format and an encoding of LLVM IR into the container format. The bitstream format is an abstract encoding of structured data, very similar to XML in some ways.

Is LLVM Bitcode portable?

So in that sense, it doesn't solve the aforementioned issues: The LLVM IR is not portable to platform with a different ABI. The only reason this is more portable is that the clients provide a layer which matches the PNaCl ABI to the actual ABI.

What is LLVM byte code?

The LLVM bytecode representation is used to store the intermediate representation on disk in compacted form. The LLVM bytecode format may change in the future, but LLVM will always be backwards compatible with older formats. This document will only describe the most current version of the bytecode format.


1 Answers

I looked through the source to the llvm-dis tool and found the function I was looking for:

Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
                         std::string *ErrMsg = 0);

from llvm/Bitcode/ReaderWriter.h.

like image 145
Zifre Avatar answered Sep 20 '22 15:09

Zifre