Is there a way to make Clang output LLVM IR alongside executable?

Viewed 1680

I know that you can make clang output LLVM IR using -emit-llvm option, however this makes it the sole output.

I was wondering if there is some combination of compiler options that would make clang function exactly as before, but also produce .ll files as a byproduct?

The problem I'm facing right now is a project with a very complicated cmake-based build, for which I can only change clang compile options. I want to generate llvm IR files for it, but unfortunately, if I just pass -emit-llvm, CMake fails, since it's compiler tests/sanity checks don't pass (since .ll file is generated instead of the valid executable).

Is there some way to make clang generate both exe/object and .ll files? Or somehow workaround this issue in other ways?

1 Answers

There are at least two ways to achieve that:

  1. -flto: instead of each object file you will get an LLVM Bitcode file (except of files that compiled from assembly, they will still be object files).
  2. -fembed-bitcode: clang will add another section into the final executable, which contains all the LLVM bitcode files (again, except of the assembly files, they will still be object files). Then, you can use LibEBC to extract all those files.

No matter which approach you take, you would have to use llvm-dis tool to convert LLVM Bitcode files into LLVM IR files.

I hope it helps.

Related