Parse LLVM IR using LLVM C API


I’m writing a compiler and I am thinking of mixing LLVM IR inside the source code I’m compiling. So user can write normal code and write some functions using LLVM IR.

Is it possible to use LLVM C API to parse those sections of the source code and convert them into LLVMValueRefs pointing to parsed functions?


I assume you mean “use your own code to parse some text representation of LLVM IR and then use the LLVM API to create the appropriate data stuctures”.

Certainly you can do that with C++ code. With the C API, I don’t know.

But it’s probably easier to use something like the parseAssemblyInto() function in include/llvm/AsmParser/Parser.h. You pass it a memory buffer containing IR text to be parsed, and a Module (possibly empty, possibly one your compiler has already added things to) to insert the parsed items into.

That’s in C++, of course, but you can make a wrapper and export your own pure C interface to it if you want.

Note that the text representation of LLVM IR is currently intended for debugging and is not guaranteed to be stable. If you’re using the C API, then I’d guess that you want your compiler to work with multiple versions of LLVM, but it’s likely that any code that embeds the text form of LLVM IR will be tied to a specific compiler version (the auto-updater should work with the bitcode representation, but is best-effort when it comes to the text version).

If you have some parts of your language’s runtime library that you want to write directly in LLVM IR and don’t mind having to modify them them when you update LLVM, then this might be a good idea, but exposing it to users is likely to cause problems.


We have this feature for LDC the LLVM D Compiler1 (although we use the C++ API).
This is the relevant code: