[cfe-dev] Getting LLVM IR from clang

Alex Denisov via cfe-dev cfe-dev at lists.llvm.org
Thu Nov 29 00:48:58 PST 2018


Hi David,

There are at least two robust ways to get the Bitcode/IR:

1. LTO. You can enable link-time optimization by adding -flto compiler flags, in the end all object files (*.o) will in fact be bitcode files. I.e.:

    > file gmock.dir/gmock-gtest-all.cc.o
    gmock.dir/gmock-gtest-all.cc.o: LLVM bitcode, wrapper x86_6

2. Embedded bitcode. Clang 3.9 and higher has an option -fembed-bitcode. When enabled, the resulting executable will contain additional section containing bitcode.
You can use this great tool[1] to extract the bitcode from an executable.

Both approaches produce bitcode (binary format), if you need IR (human-readable format), then you can post-process the bitcode by running llvm-dis against each bitcode file.

I hope it helps.

[1] https://github.com/JDevlieghere/LibEBC

> On 28. Nov 2018, at 23:22, David Greene via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> How do I get the LLVM IR produced by clang?  I know that -emit-llvm
> produces IR but it seems to be after processing by LLVM (things are
> optimized out, for example).  Is there an "official" way to get the IR
> coming right out of clang's codegen?  I can do a hack with -mllvm
> -print-before-all but that's icky.
> 
> Thanks!
> 
>                          -David
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list