ConstExprPreter – Clang Constexpr Interpreter
lists.llvm.org
lists.llvm.org
The natural progression of JITs is to start with using llvm for the jit, only to realize that it's too slow and big for anything but AoT compilations since llvm is tuned for AoT with clang (and other AoT compilers). So the awkward next step is to add a custom jit to clang for zippy cobstexpr eval because llvm itself doesn't work super well for JITs due to it being tuned for clang's use case. Ironic.
> This project serves mostly as a prototype in order to determine what kind of bytecode and compiler is required to efficiently evaluate code and emit useful diagnostic messages, paving the way for a future fast, potentially JIT-ed, interpreter.
Unless the most trivial possible JIT is used (one that doesn't optimize at all)?
Obviously when this happens it's a bug; the real question is "if such a bug occurs (i.e. the semantics of the program is affected by the use of constexpr) will it confuse the developer?"
Lisps (which have always intermixed interpreted and compiled code as long as lisp compilers have existed) have often struggled with this problem even though they have in many ways a simpler problem to address.
And since I'm writing about C++: I really love c++ but I hate the keyword "constexpr" as it's actually "const_statement". the distinction between statements and expressions, and the concomitant problem that expressions aren't first class, is a c-inherited botch on the language IMHO.
Edit: s/similar/simpler/
C++ defines constant evaluation to be the same as normal evaluation, although C++20 is adding a way to tell when you're evaluating at compile time so you can avoid things like inline asm at compile time.
constexpr doesn't even mean const_statement. All it means is don't emit an error if constant evaluation calls this function.
That is what bothers me about constexpr. It conflates compile-time evaluation with non-mutability. I want a keyword that guarantees an evaluation happens at compile-time, but doesn't by itself imply "const".
You can get this behavior by initializing a variable with the value of another variable whose value is a constexpr. This is a case in which the order of initialization of global variables is not ambiguous!
constexpr int foo_init_value = 2 + 4;
int foo = foo_init_value;
Actually not a bad idiom.But like you I do wish you could apply constexpr-like behavior to expressions rather than just statements:
What you're If you could really apply really applied to expressions you could write something like
int foo = _compute_at_compile_time(2 + 4);
Of course the compiler can already do this degenerate example for you.If so, why invent another bytecode to do the evaluation, with the risk of introducing inconsistencies? Couldn’t they add a flag “must be fully constant folded” to the compiler intermediate code, and, if (likely) necessary, optimize the LLVM constant folding optimization passes?
As a bonus, that could help other languages with a LLVM backend that want to do similar stuff.
Also, people have done interesting things in constexpr that currently suffer from slow compile times: https://github.com/hanickadot/compile-time-regular-expressio...
Repeat compilation (conceptually, the implementation might be more efficient), replacing anything ”constexpr” by what you got earlier, and you’re done.
Or am I overlooking something?
Maybe native code-generation isn't so bad, after all. Maybe we can just redefine "constexpr" to nothing, then compilers can just dump it all in a file, compile it, run it and there we go, the blazing fast integrated code generation pipeline we all seem to want and need.
The semantics of constexpr do not allow for undefined behavior, so this would unfortunately not be feasible.
But the problem is, as far as I can see, that the whole "Interpreter" project is started based on the assumptions, not on the realistic measurement of real life timings for real life inputs.
At least in the text no proof is presented that the work is really justifiable.