by millstone · 13y ago · 0 comments
Here is a C program:
#include <stdio.h>
int main(void) { while (1) malloc(64); }
With clang, when compiled with -O0, this consumes all the memory on my system. When compiled with -O2, it runs forever in constant memory, because clang has optimized out the call to malloc. If I replace the call with, say, 'malloc((size_t)(-1) >> 1)', it even changes the output of the program.Here is another example:
int factorial(long long x) { return x * factorial(x-1); }
With gcc, when compiled with -O2, the factorial function can correctly compute the factorial of a very large number. When compiled with -O0, it cannot.I would call both of these optimizations, even though someone may rely on either for correctness. So I don't think tail calls are unique in this respect.
make the syntax for guaranteed-eliminated-tail-call different than a regular call
Agreed.
In a language which prefers diagnosing programmer error over punting both should be errors.
That someone is playing with fire. It's the kind of optimization that might be removed in a hotfix because it turns out to break something else.
It's even worse than relying on things like byte order -- and if you rely on them, you are definitely outside the realm of "C language", and into the realm of "Specifically LLVM v.37x patchlevel 2, on an SSE4 architecture"