C ++ string for LLVM IR

I would like to take a string representation of a C ++ lambda function like this:

string fun = "[](int x) { return x + 5;}";
string llvm_ir = clang.get_llvm_ir(fun);  // does something like this exist?

      

and convert it to LLVM IR using Clang from C ++. Is there a way to do this directly using the internal Clang API?

+3


source to share


1 answer


As far as I know, there is no stable, officially supported API for this. Clang C API provides interface level information (source code). Clang toolkit doesn't do that either.

You have good options. The easiest way is to just call the front-end Clang as a subprocess clang -cc1 -emit-llvm ...<other options>

. This will create an LLVM IR file, which you can then read. This is indeed a fairly common practice in compilers - the Clang driver itself does this - it calls the interface and many other tools (such as the linker), depending on the specific compilation task.



Alternatively, if you think you must have a programmatic API to do this, you can dig up the code in the Clang frontend (the call -cc1

mentioned above) to see how it performs it and collect bits and pieces of code. Expect to write a huge amount of scaffolding because these APIs are not meant to be used externally.

To recap, it is possible to use internal APIs, but there is no easy or recommended way to follow this path.

+4


source







All Articles