[llvm-dev] Compiling LLVM to LLVM IR

Alex Denisov via llvm-dev llvm-dev at lists.llvm.org
Sat Jul 29 02:50:50 PDT 2017


Hi Mackenzie,

The trick is to use LTO.
Here is a set of commands you need to run in order to prepare build system:

$ mkdir build
$ cd build
$ export CFLAGS="-flto -g "
$ export CXXFLAGS="-flto -g "
$ export CPPFLAGS="-flto -g "
$ cmake ../llvm

Now you can build any target you want, for instance:

$ make LLVMSupport -j8

When it's done you will have bunch of .o files, which are actually LLVM bitcode files:

$ find ./ -name "*.o" | xargs file
.//CMakeFiles/test.o:                                                            LLVM bitcode, wrapper x86_64
.//lib/Support/CMakeFiles/LLVMSupport.dir/Allocator.cpp.o:                       LLVM bitcode, wrapper x86_64
.//lib/Support/CMakeFiles/LLVMSupport.dir/APFloat.cpp.o:                         LLVM bitcode, wrapper x86_64
.//lib/Support/CMakeFiles/LLVMSupport.dir/APInt.cpp.o:                           LLVM bitcode, wrapper x86_64
.//lib/Support/CMakeFiles/LLVMSupport.dir/APSInt.cpp.o:                          LLVM bitcode, wrapper x86_64
.//lib/Support/CMakeFiles/LLVMSupport.dir/ARMAttributeParser.cpp.o:              LLVM bitcode, wrapper x86_64
....

If you need a human readable IR, then you can convert them into .ll using llvm-dis:

$ llvm-dis .//lib/Support/CMakeFiles/LLVMSupport.dir/Allocator.cpp.o
$ file .//lib/Support/CMakeFiles/LLVMSupport.dir/Allocator.cpp.o.ll
.//lib/Support/CMakeFiles/LLVMSupport.dir/Allocator.cpp.o.ll: ASCII text, with very long lines

I hope that helps.

Cheers,
Alex.

> On 29. Jul 2017, at 10:32, Mackenzie Moore via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> Hello everyone,
> 
> I'm trying to compile LLVM and Clang into LLVM IR with debug info. I know that clang++ -g2 -S -emit-llvm <filename> does this, but I'm unfamiliar with CMake.
> 
> I tried changing CMAKE_CXX_FLAGS in CMakeCache.txt to "clang++ -g2 -S -emit-llvm," "-g2 -S -emit-llvm," and "-emit-llvm," but everything I tried resulted in a failed build, and/or .o files that didn't look like LLVM IR files when I opened them up. Where is the right place to pass these flags in?
> 
> Thanks,
> Mackenzie
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

--
AlexDenisov
Software Engineer, https://lowlevelbits.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 496 bytes
Desc: Message signed with OpenPGP
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170729/698899ec/attachment.sig>


More information about the llvm-dev mailing list