[LLVMdev] experimenting with partial evaluation

Pierre C lists at peufeu.com
Fri Jun 11 07:19:13 PDT 2010

Hello !

I wanted to experiment with partial evaluation and llvm seems to be the
right tool for this, but since I'm new to it of course I'm a bit lost !

I'll try to explain what I want to do in the simplest possible way :

I have a C program.
In this program, there is a function f( a,b ).
I have a value A for a.
I want to specialise f() so I get a function fA( b ) which is the same as
f( a=A, b ).
It looks like a futamura projection (think about the classical adder

So, in my program, at runtime, I'd like to get a llvm Function* on the
function I want to specialize, so I can write code that hacks on the
arguments and bitcode of f and JITs a new version of it.

I have found examples about how to generate llvm instructions at runtime
and JIT that into a function, but that's not what I'm interested in : I
want to modify the code of an existing function, not generate a function
   from nothing.

I succeeded in compiling to bitcode, loading the program's bitcode,
extracting a Function *, JITting it and executing it, but then whatever is
in my function can't call the rest of the program, because when I load the
bitcode, what is in it has no relation to the rest of my program, even
though it is the same code, there are actually 2 copies of it which are
not linked.

And I'm a llvm newbie so I have a few questions :

- In a running program, can I get pointers to the llvm Module that
contains the code currently being executed ?
     Using this I could do a module->getFunction() to get the Function,  
it, tweak it, JIT it and I'd be happy.

- When I JIT something how can I link it to my running program so it can
call functions and access globals ?

- How should I compile my program so I can do all that ? (I guess I
shouldn't compile it to x86-64 native but bitcode instead, but what
options ?..)

Maybe I'm asking the wrong questions, feel free to object ;)

Thanks for your time.

More information about the llvm-dev mailing list