Partial evaluation and LLVM (2) (Chris Lattner)

Message: 2
Date: Fri, 15 Jun 2007 12:16:32 -0700 (PDT)
From: Chris Lattner <>
Subject: Re: [LLVMdev] Partial evaluation and LLVM (2)
To: LLVM Developers Mailing List <>
Message-ID: <>
Content-Type: text/plain; charset="x-unknown"

Thanks, this works.


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?


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

extern int specializevar;

void genericfunc() {
   if (specializevar == 42)

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.


Thanks, it works with the example you gave, but how is this method supposed to work with a C++ method considering the object fields it uses as "free" variables?
These object fields are not global variables of the module?


Stephane Letz

I can't and won't. You have to model specializable values as globals or something else easily changable by the optimizer. If you want, you can write a custom optimization pass to substitute any value you want, but this is certainly the most straight-forward way to do it.