[LLVMdev] Partial evaluation and LLVM (2)

Chris Lattner sabre at nondot.org
Fri Jun 15 12:16:32 PDT 2007


On Fri, 15 Jun 2007, [ISO-8859-1] Stéphane Letz wrote:
> Thanks, this works.

Great.

> Would it be possible to specialize a function that has some "free"
> variables, more precisely a method in a class that use the value of
> some of the object instance fields? Or does the function to
> specialize has to be closed in some way?

Yes.

The solution I've found is to use llvm global variables.  Consider:


extern int specializevar;

void genericfunc() {
   if (specializevar == 42)
     blah();
}

The steps become:

1. Clone genericfunc (specializing based on arguments if you like).
2. Give specializevar an initializer for the value you want to specialize 
on.  Mark it constant.
3. Run instcombine and other xforms on genericfunc.  Instcombine will 
forward substitute loads from the global into the constant values.
4. JIT the result etc.
5. Mark specializevar non-constant, remove the initializer.

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/


More information about the llvm-dev mailing list