[LLVMdev] Reading LLVM bitcode into existing module
Jim Idle
jimi at temporal-wave.com
Mon Jul 21 00:29:37 PDT 2014
On Mon, Jul 21, 2014 at 9:22 AM, David Jones <djones at xtreme-eda.com> wrote:
> I am writing a compiler using LLVM 3.2 to generate native code (currently
> x86-64) from IR. The native code will be linked by the system linker (not a
> JIT).
>
> The compiler generates calls to a run-time library to perform many
> operations. Therefore, each Module that I generate needs to be have
> declarations for all of these run-time functions added to it.
>
> 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.
>
Yes.
>
> Initially, I did this by calling Function::Create for each declaration I
> wanted to make. However, this is starting to "not scale".
>
I also started out on this path, but once there are a lot of runtime
functions it creates a lot of code to gen them, which is what I presume you
mean by "not scale".
>
> I also want to experiment with defining some of these library functions
> using LLVM IR directly. I can then have LLVM inline and optimize calls to
> these functions. Given that many of the arguments to the functions are
> constants, there is plenty of opportunity for loop unrolling and
> optimization.
>
> 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.
>
Being unable to find any articles that suggested one method or another, I
do this, which works well for what I am doing:
I have a runtime library written in C++ and compiled with clang.
I have a project module with a single .cpp file, that includes all the
relevant headers to pick up code inlined the .h files, and which uses
enough runtime methods for the inline code to be generated etc. In practice
this is not that much code.
I compile that .cpp file to a .bc file runtimeinterface.bc
When it comes to code gen time in my compiler, I first load
runtimeinterface.bc and compile it:
this->codeModule = llvm::ParseIRFile(libFile, ed, this->Context);
I then use setModuleIdentifier() to change the module ID appropriately
I then locate the 'junk' added by clang, such as the GLOBAL__I constructor
stuff and my dummy runtime interface function and perform
removeFromParent().
I now have declarations for all the runtime methods, which I need not hard
code in to the compiler as well as having the IR for the internal link once
inlinable functions in my runtime .h files. Use module->getFunction() to
find the function definitions and the approriate other getXXX calls in the
Module class. Use assert so that you can detect in debug builds when
someone changed the runtime underneath you (you cannot find the function or
definition any more)
I then start code generation in to this module as if it were an empty one
as is the usual case.
I have to admit that I just dreamed this method up after casting around
with Google quite a bit and I can see a few possible issue with this such
as being possibly dependent on the version of clang that is used. This
works for me because I am in control of the end use environment however and
can ensure that the versions of the components being used are the correct
ones.
If anyone with greater experience in LLVM than I have sees other issues,
then please pipe up!
Cheers,
Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140721/995f7fbd/attachment.html>
More information about the llvm-dev
mailing list