[LLVMdev] Reading LLVM bitcode into existing module
Tim Northover
t.p.northover at gmail.com
Sun Jul 20 23:46:49 PDT 2014
Hi David,
On 21 July 2014 02:22, David Jones <djones at xtreme-eda.com> wrote:
> Question: is this true? I am assuming that LLVM works like a C++ compiler:
> before you can call a function from anywhere in a compilation unit, you need
> its prototype in scope.
Pretty much. There is only one scope for functions in LLVM IR
(module-global) but they do have to be declared.
> Initially, I did this by calling Function::Create for each declaration I
> wanted to make. However, this is starting to "not scale".
You don't say quite why it's not scaling, but the functions
Module::getOrInsertFunction make some of the details easier. It's how
I'd always create a function declaration.
> To this end, I would like to read LLVM bitcode into an existing module. The
> bitcode would contain declarations for all of my library functions, plus
> definitions for anything I want to try to inline and optimize.
The easiest way to do this is probably to use the llvm::Linker class.
That utility class basically just merges one module's definitions into
another, so you just load your library bitcode and link it into the
module you actually care about.
Alternatively you could just call setModuleIdentifier to rename the
loaded library module. I suppose that would be simpler unless you
could see yourself splitting the library functions into multiple files
to help organisation.
Once your library is in the same module as your functions, you can
probably simplify the management issue too: declarations will already
exist so you can just look them up by name with
Module::getOrInsertFunction.
You could actually import *just* the declarations like that if you
wanted to experiment with just how much benefit came from the inlining
at some later date. That is, load a module which looked like just:
declare float @sinf(float)
declare double @sin(double)
> There also seems to be a mechanism for adding "library dependencies" to a Module
Hmm. Not heard of that one. It's the kind of thing multiple languages
would find useful so it wouldn't surprise me if it did exist, but I've
not encountered it anywhere.
Cheers.
Tim.
More information about the llvm-dev
mailing list