Jonathan M Davis wrote:
On Tuesday, July 20, 2010 17:24:33 bearophile wrote:
Jonathan M Davis:
But theoretically, it
could be used for optimizations just like inlining (particularly with
pure functions), and that being the case, it probably will at some
point.
Pure functions go well with CTFE because they don't use global state, but
you can run code at compile time only if all the arguments are known at
compile time :-)
If some of their arguments are known at compile time and some of them are
not known, and you want to optimize better, then you need partial
compilation, that is a bit hard to implement, probably makes the compiler
slow and complex.
So for a practical form of manual partial compilation practical time ago I
have proposed something similar to GCC __builtin_constant_p
(http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Other-Builtins.html#index-g_t
_005f_005fbuiltin_005fconstant_005fp-2392 ), that is syntax sugar that you
can use inside functions to turn them into templates (I don't know if you
can also have a single entry point to keep the possibility of taking their
address). You can use a static if with such __trait(isCTAgument, foo), to
tell if each function argument as foo is known at compile time, and do
something with it if it is. This is like defining those arguments as CT
template arguments, that later the optimizer can optimize better (and
perform the partial compilation).
Bye,
bearophile
Partial compilation would indeed be a big step forward, but it would certainly
be a lot of work. Just being able to have functions run with CTFE which could be
fully run with CTFE would lead to some nice optimizations. Certainly, that would
need to be achieved before we could look at having partial compilation. Still,
partial compilation would certainly be cool if we ever reached that point.
- Jonathan M Davis
While running the semantic on each function body, the compiler could
fairly easily check to see if the function is CTFEable. (The main
complication is that it has to guess about how many iterations are
performed in loops). Then, when a CTFEable function is called with
compile-time constants as arguments, it could run CTFE on it, even if it
is not mandatory.
Incidentally, having the function being marked as 'pure' doesn't really
help at all for determining if it is CTFEable.