[LLVMdev] Convert C code with external library access to llvm

Dan Liew dan at su-root.co.uk
Wed Sep 3 01:52:21 PDT 2014


Hi Prakash,

There are multiple answers to this question which depend on what you
mean by intend to do with IR and what you mean by "link" in your
context. Here are at least two things I can think of

1) You want to run "insertselect.c" using LLVM's MCJIT infrastructure
inside your application.

In this case you wouldn't need to "link" the LLVM IR to the library.
You would just

- Load the LLVM IR module into memory
- Make sure your library (i.e. libsqlite3.so) is dynamically loaded
(I'm pretty sure LLVM has infrastructure for this). This would be
needed so the MCJIT can resolve calls to the sqlite library.
- Use the MCJIT to "JIT" the loaded LLVM IR module so you can run it

I'm sorry if this is vague, I'm not familiar with the MCJIT.

2) You want to analyse your "insertselect.c" program in some way at
the LLVM IR level and need access to the implementations and globals
of library functions as LLVM IR.

In this case you effectively want a single LLVM IR module that has
everything in it (i.e. "insertselect.c" and all the used functions and
globals from the sqlite library).

So you'll need to compile sqlite as LLVM IR.

There is also a hacky way of doing this using a tool called
whole-program-llvm [1] which you could use to build a static library
(for sqlite) containing LLVM IR modules. Once you have that you could
extract all the modules from the archive and then run

$ llvm-link inserselect.bc sqlite_moduel0.bc sqlite_moduel1.bc .... -o messy.bc

then you'll want to run the internalize and globaldce passes strip all
the dead code because llvm-link just merges the modules (I'm assuming
you have a single entry point called main)

$ opt -internalize-public-api-list=main -internalize -globaldce
messy.bc -o final.bc

There is probably a nicer way of doing all this using the LLVM gold
plugin and using ``-use-gold-plugin -Wl,-plugin-opt=also-emit-llvm``
with clang but I've never tried.


[1] https://github.com/travitch/whole-program-llvm

Hope that helps.

Dan Liew
PhD Student - Imperial College London



More information about the llvm-dev mailing list