0

I'm new to LLVM, clang etc. My primary need is to convert C code to LLVM IR (.ll) then parse this to a assembly-like language. For this I was till now working with small length c codes (single file) and to get these converted to LLVM IR I used -

$ clang -emit-llvm -S multiply.c -o multiply.ll

To do this conversion with a large C project, I tried gllvm - https://github.com/SRI-CSL/gllvm and it was really easy & convenient but I am facing this issue with it - https://github.com/PLSysSec/sys/issues/16
which basically a bug in the llvm-hs repo essentially asking me to pull the commit which has the fix and build it. I'm trying to avoid doing this (is this a better way ?)

So I moved onto another solution of using the gold linker - http://gbalats.github.io/2015/12/10/compiling-autotooled-projects-to-LLVM-bitcode.html

I am now stuck on using this gold plugin to get .bc or .ll files - I want to use this gold plugin to convert a large C project into .ll or .bc files - but the steps only show how to use the linker to get optimised executable. Are there any command line options to only convert it till .bc files ?

Any help would be appreciated.

fennec
  • 13
  • 3
  • In your question it's not clear what are you trying to achieve. Are you compiling several small C files into LLVM-IR and then you want to link them all into a single IR file ? If that's the case, you could link them using the llvm-link binary. This is great if you control the build scripts being used. If you don't, you could compile with -fembed-bitcode, generate the final binary files as-usual, and extract the embedded bitcode (in the .llvmbc section in ELF binaries), and link all the bitcodes again with llvm-link. – jmmartinez Apr 11 '22 at 07:59
  • thanks for your response @jmmartinez I am trying to compile a large C project into .ll files or .bc files - I will also modify the question and make it more legible – fennec Apr 11 '22 at 15:42
  • If I understand correctly you want to generate a single whole-program LLVM-IR for each binary (executable or dynamic-library) in the project. Then, you can compile with `-flto -fuse-ld=lld -Wl,-save-temps`: 1) this will enable LTO for LLVM (so each generated object file contains in fact bitcode); 2) when linking, you'll be using the LLVM linker lld that can corretly handle LLVM-IR and 3) the `-Wl,-save-temps` flag will make lld keep the temporary bitcode files used while linking (they have some suffixes such as .preopt.bc, .internalize.bc and .precodegen.bc). You can then use those. – jmmartinez Apr 12 '22 at 07:58
  • This may be related to what you are looking for: https://stackoverflow.com/questions/46957981/emitting-a-single-ir-bitcode-file-with-llvm-lld-using-cmake – jmmartinez Apr 12 '22 at 08:04
  • I'm not sure how to use this with a large project, For eg openssl, which has a build script (./configure) or is it better to go with tools like gllvm https://github.com/SRI-CSL/gllvm for autotooled projects, given that I have limited time ? and it would take longer to modify large build scripts – fennec Apr 13 '22 at 16:16
  • Oh ! Yes. If you have limited time, I think you'll be better off by picking the fix from gllvm. – jmmartinez Apr 14 '22 at 08:04

1 Answers1

0

The llvm-link tool combines multiple .bc files into a single .bc. It does not support native object files at all, nor does it work well with normal linker flags (it does not pretend to be ld).

If you do want changes to the llvm IR due to your native libraries on the system, the gold plugin to llvm also supports a emit-llvm flag. Run a clang or gcc link with -Wl,-plugin-opt=emit-llvm when linking with gold.

Nick Lewycky
  • 1,182
  • 6
  • 14