[LLVMdev] Tool for run-time code generation?

Martin C. Martin martin at martincmartin.com
Sun Jul 18 09:42:25 PDT 2010

On 7/17/2010 12:38 PM, Nick Lewycky wrote:
> Martin C. Martin wrote:
>> On 7/16/2010 10:30 PM, Nick Lewycky wrote:
>>> Vlad wrote:
>>> Instead, break the chunks of C you would generate into functions and
>>> compile those ahead-of-time. At run time you use llvm only (no clang) to
>>> generate a series of function calls into those functions.
>> Compelling. I hadn't considered that.
>> In our application, we have a tree of primitive operations, where each
>> one calls into its children and returns to its parent. There are various
>> types of nodes, and we don't know the topology or types of nodes until
>> runtime (i.e. Clang/LLVM invocation time). Each operation is pretty
>> simple, so we'd like to inline the children's operations into the parent
>> where a C compiler would do it.
>> Could your technique be extended to that? e.g. precompile to LLVM IR
>> with calls to a non-existent "node_do_foo()" call, and then replace it
>> with the specific "childtype_do_foo()" call when we know the type of the
>> child?
> Will you know the prototype of the function call in advance? If so, you
> can do something really simple where you write the C functions with a
> function pointer parameter. Then at run-time, use
> llvm::CloneAndPruneFunctionInto to produce the specialized function by
> giving it a valuemap that maps the Argument* for the fptr to the
> concrete Function* you want it to call.

Great!  I wasn't aware of that, so that's really helpful.

> If you don't know the type of the call you intend to place, the question
> becomes "why not?" What arguments were you planning to pass it, if you
> don't know how many arguments it takes in advance? I don't see any
> reason to doubt that it's possible to do, but I would need more details
> before I could suggest an implementation.

We're processing large amounts of data, so imagine something like a SQL 
implementation.  In one query I might want to JOIN on an int field.  In 
another query, I'm JOINing on a pair of fields of type unsigned & 
double.  For those two queries, I'd generate:

rightChildType_seekTo(rigtChild, left.getIntColumn3());


rightChildType_seekTo(rightChild, left.getUIntColumn9(), 

Is that possible in LLVM?


More information about the llvm-dev mailing list