Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial evaluation/specialization with LLVM-gcc or gcc

I am interestent in (partial) compile-time evaluation for c/c++ (not with template parameters like in c++). Lets consider the following case (taken from [1]):

double mypower(double x, int n) {
  int i;
  double ret = x;
  for (i = 1; i < n; i++) {
    ret *= x;
  }
  return ret;
}

Then you call this function somewhere in the code with:

mypower(x,3); // y varies all the time, 

Then the compiler could optimize this (e.g. loop unrolling). Some often used function I use could really benefit from that optimization (tested by the creating specialized function manually). The presentation [1] descibes a process where the function is searched and is replaced by a specialised version of the function. This seems to work. But it does not seem to be very universal, code needs to be written for the functions which should be replaced.

The presentation seems to be from 2008, i could not find anything substantial more information than in this source. So has anything improved since then? I would prefer some kind of automatism, which does the same for all function possibly controlled by attribute syntax (e.g. __attribute__(peval)...). Further I would like the same to work for object-oriented code, creating specialized classes for different objects ([2] seems to suggest that this is not possible).

Additionally, I would like this specialization not only work for constants found in code. I am thinking about a program compiled to LLVM IR (bytecode) could do the following:

  1. Running the programm during an initialization phase in an interpreter, during that initialization phase the program could read some configuration from a file. After the initialization the interpreter is stopped.

  2. Some variables (including member variables) are fixed from that point on. Extract these variables (e.g. marked by attribute during compilation).

  3. Create specialized functions and classes. Clone these into the bytecode.

  4. Run the JIT to create native machine code.

This is a lot I ask for and only few computation intensive programs would benifit from such a optimization. But some people must be working on that. I probably just don't know the right search terms to feed google.

Note: Please don't suggest template classes with non-type parameters or manual specialization, I already do that. I just would prefer the compiler doing the work for me.

Links:

[1] Presentation how to partial evaluate in LLVM

[2] Forum message about partial evaluation

like image 852
Niggler88 Avatar asked Nov 14 '22 02:11

Niggler88


1 Answers

This is largely in the arena of interprocedural optimizations. llvm has a few of them, in particular IP constant propagation which would only help if you used mypower(x, 3) at all of the call sites in a translation unit. What you describe is possible, but hasn't really been done yet. Improving the IPCP pass is what you'd want to look into doing - by having it clone and specialize a function at a particular call site. This could, if you have enough translation units, cause some pretty large code bloat though which is why people haven't really looked into doing it.

This would probably have some more use at the LTO level when you can look at all of the calls in a program.

like image 76
echristo Avatar answered Dec 30 '22 11:12

echristo