Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and using LLVM bitcode libraries

Tags:

llvm

clang

I have a C++ project that uses a C++ library that I also wrote. I'm using clang++ 3.3 to build everything. Each file in the library is compiled as

clang++ -c -O -emit-llvm somefile.cpp -o somefile.bc

I'm then using llvm-link to combine all the library *.bc files into a single bit code file like so

llvm-link -o MyLibrary.bc somefile.bc someotherfile.bc etc.bc

I'm conceptualizing this to be similar to creating an archive of object files, but I don't think that's true based on how things are acting.

I then compile the source files of my project using a similar command to the one above. I then use llvm-link (again) to combine these, along with the library bit code file into a single bit code file like this

llvm-link -o app.bc1 main.bc x.bc y.bc path/to/MyLibrary.bc

Next I compile app.bc1 into a native object file

llc -filetype=obj app.bc1 -o app.o

Finally I use clang++ again to link this native object file (and against the other native libraries I need, such as the C++ standard library, etc)

clang++ app.o -o app

However, what appears to be happening is that when I llvm-link the application's bit code the entire contents of MyLibrary.bc seems to be included in the result. Thus the final linking needs to resolve references made by library components that I'm not actually using.

What I would like to do is extract from MyLibrary.bc only the bit code files that my application needs. I see there is an llvm-ar program but in reading about it I don't get the impression that it would help here. I guessed I could combine the library with llvm-ar instead of llvm-link, but I can't figure it out. I'm hoping all I need is a little push :)

like image 566
Peter C. Chapin Avatar asked Jul 03 '13 23:07

Peter C. Chapin


People also ask

What is LLVM Bitcode?

What is commonly known as the LLVM bitcode file format (also, sometimes anachronistically known as bytecode) is actually two things: a bitstream container format and an encoding of LLVM IR into the container format. The bitstream format is an abstract encoding of structured data, very similar to XML in some ways.

Is LLVM better than GCC?

While LLVM and GCC both support a wide variety languages and libraries, they are licensed and developed differently. LLVM libraries are licensed more liberally and GCC has more restrictions for its reuse. When it comes to performance differences, GCC has been considered superior in the past.


1 Answers

EDIT: It is actually ar which makes it work.

Bit late but still might be relevant to someone, we use ar and ld.gold with LLVM plugin to link bitcode:

ar r --plugin /usr/lib64/llvm/LLVMgold.so library.a <files...>
ld.gold --plugin /usr/lib64/llvm/LLVMgold.so -plugin-opt emit-llvm  main.bc library.a

Of course the path to LLVMgold.so might be different. This way resulting .bc has just needed symbols.

like image 99
Vladimir Still Avatar answered Oct 12 '22 23:10

Vladimir Still