Serialization llvm IR


I am new to clang/llvm, and have read through some documents and articles. I understand that, if wrong please correct me, llvm is a toolchain where clang served as the frontend compiler to take a source file, generate and AST, and produces the llvm IR from the AST. The llvm will then take the llvm IR to deliver assembly and eventually the machine code.

The portable llvm IR provides a great chance to cache for better performance. However, I can’t find useful information on how to serialize the llvm IR, which is maintained in a llvm::Module in any chance. A related, and possible related llvm API is the clang::serialization::moduleFile, but I can’t find any example regarding how to get a ModuleFIle from an llvm::Module.

Another solution I am thinking about is to apply a different CodeGenAction, such as EmitBCAction. Still, I can’t find an example of this action works (how it produce the bitcode and where can I get it), and a way to read the bitcode and reconstruct a llvm::Module.

I appreciate any clarification and help. Thank you so much!

Are you trying to understand how to output LLVM IR/LLVM bitcode?

Or do you want to understand how serialization is implemented?

Thanks for the response! To put it short, I am trying to understand how to serialize the llvm::Module. I want to find a way to output it as a byte string so that I can deserialize it later to reconstruct the llvm::Module and feed it to a compiler instance for machine code.

In the end, I figure out that I can actually use llvm::WriteBitcodeToFile() to make a llvm::Module serialized.