Migrate Project Build system to LLVM BitCode

Hi All,

I am migrating a build system of an existing project from ‘Object files’ based executable generation to ‘LLVM Bitcode’ files based exe generation and applying OPT pass to LLVM Bitcode. I found out the following 4 step procedure. Please let me know if this is the right procedure or is there any other easy way of doing it. I need to modify ‘Makefile’ accordingly. I read on some forums about GoldPlugin but I don’t know exactly if it would be useful here:

Original Makefile- Object Files based Build/Link Steps:

clang -g -O2 –o .libs/mergedexe file1.o file2.o file3.o -pthread -Wl,–export-dynamic .libs/lib1.a -lssl –ldl –pthread .libs/lib2.so

Proposed Makefile- LLVM Bitcode based Build/Link Steps:

  1. llvm-link -o .libs/mergedbc.bc file1.bc file2.bc file3.bc

  2. opt –o optmerged.bc -load /path/to/Mypass.so -mypass < mergedbc.bc

  3. llc -o .libs/mergedbc.s .libs/mergedbc.bc

  4. gcc -g -O2 -o .libs/mergedexe .libs/mergedbc.s -pthread -Wl,–export-dynamic .libs/lib1.a -lssl -lcrypto –ldl –pthread .libs/lib2.so

Thanks.

Ahmad

Hi Ahmad,

Hi All,

I am migrating a build system of an existing project from ‘Object files’ based
executable generation to ‘LLVM Bitcode’ files based exe generation and applying
OPT pass to LLVM Bitcode. I found out the following 4 step procedure. Please let
me know if this is the right procedure or is there any other easy way of doing
it. I need to modify ‘Makefile’ accordingly. I read on some forums about
GoldPlugin but I don’t know exactly if it would be useful here:

*Original Makefile- Object Files based Build/Link Steps: *

clang -g -O2 –o .libs/mergedexe file1.o file2.o file3.o -pthread
-Wl,--export-dynamic .libs/lib1.a -lssl –ldl –pthread .libs/lib2.so

*Proposed Makefile- LLVM Bitcode based Build/Link Steps:*

1.llvm-link -o .libs/mergedbc.bc file1.bc file2.bc file3.bc

2.opt –o optmerged.bc -load /path/to/Mypass.so -mypass < mergedbc.bc

3.llc -o .libs/mergedbc.s .libs/mergedbc.bc

4.gcc -g -O2 -o .libs/mergedexe .libs/mergedbc.s -pthread
-Wl,--export-dynamic .libs/lib1.a -lssl -lcrypto –ldl –pthread .libs/lib2.so

if you pass -O4 rather than -O2 to clang I think it will in essence do this all
for you already. It might even do the link time optimization for you at -O2
even, I'm not sure.

Ciao, Duncan.

Hi Duncan,

4.gcc -g -O2 -o .libs/mergedexe .libs/mergedbc.s -pthread
-Wl,--export-dynamic .libs/lib1.a -lssl -lcrypto -ldl -pthread .libs/lib2.so

if you pass -O4 rather than -O2 to clang I think it will in essence do this all
for you already. It might even do the link time optimization for you at -O2
even, I'm not sure.

No, if I use clang for producing executable then I get series of error both with O2 and O4:

error: input can't have .file dwarf directives when -g is used to generate dwarf debug info for assembly code
        .file 38 "/usr/include/x86_64-linux-gnu/sys/un.h"

If I remove '-g' then I get
/usr/bin/ld: /usr/local/bin/../lib/LLVMgold.so: error loading plugin
/usr/bin/ld: /usr/local/bin/../lib/LLVMgold.so: error in plugin cleanup (ignored)
clang: error: linker command failed with exit code 1

It works fine if I use 'gcc' in step 4 for produce executable.

Cheers,
Ahmad

Ciao, Duncan.

Hi All,

I am migrating a build system of an existing project from ‘Object files’ based executable generation to ‘LLVM Bitcode’ files based exe generation and applying OPT pass to LLVM Bitcode. I found out the following 4 step procedure. Please let me know if this is the right procedure or is there any other easy way of doing it. I need to modify ‘Makefile’ accordingly. I read on some forums about GoldPlugin but I don’t know exactly if it would be useful here:

Original Makefile- Object Files based Build/Link Steps:

clang -g -O2 –o .libs/mergedexe file1.o file2.o file3.o -pthread -Wl,–export-dynamic .libs/lib1.a -lssl –ldl –pthread .libs/lib2.so

Proposed Makefile- LLVM Bitcode based Build/Link Steps:

  1. llvm-link -o .libs/mergedbc.bc file1.bc file2.bc file3.bc

  2. opt –o optmerged.bc -load /path/to/Mypass.so -mypass < mergedbc.bc

  3. llc -o .libs/mergedbc.s .libs/mergedbc.bc

  4. gcc -g -O2 -o .libs/mergedexe .libs/mergedbc.s -pthread -Wl,–export-dynamic .libs/lib1.a -lssl -lcrypto –ldl –pthread .libs/lib2.so

Thanks.

Ahmad

If you need to make it work using -O4, you will need to install GoldPlugin, since it needs to link the bitcode files. Otherwise you will get those ld linker errors.

And even if you use GoldPlugin, you will have to run your passes separately or incorporate your passes into the LTO module. It is probably not worth it since this Makefile seems simple.

-Swarup.

Hi Swarp,