When compilers surprise you

39 points by eranb


chinmay

when i read the first half my brain immediately went "i swear i've seen compilers just do n(n+1)/2 before", then i saw the second half and clang did :)

BinaryIgor

Fascinating - given the nature of computing, there probably is a close to infinity of arbitrary and case-specific optimization one can implement.

I wonder what gems one can find in interpreter-compiler hybrids of a JVM variety :)

smlckz

From LLVM documentation about indvars pass:

Any use outside of the loop of an expression derived from the indvar is changed to compute the derived value outside of the loop, eliminating the dependence on the exit value of the induction variable. If the only purpose of the loop is to compute the exit value of some derived expression, this transformation will make the loop dead.


Sigh, compilers really need to understand before performing transformations like these.. I'll take this chance to ask: could we have benefited from expressing the intent directly*, than using "idioms" and letting the compiler guess the intent from "pattern matching" over such idioms; and, are undefined behaviour worth leaving undefined: now that we take matters of "safety" more seriously†, what benefits does undefined behaviour still have? This manner of tacit ((potential of) mis)understanding arising from (in)advertend use of undefined behaviour leaves one apprehensive.


* At a ''comfortable'' level of abstraction that would be appropriate for a particular language; so preferably not expanding/reifying down, and more so much not using inline assembly or intrinsics etc. directly. Some way we could provide more information, available to us, to the compiler, so that it could make more informed decision at optimization..

† Or it seems so: while it has been a matter of prudence and discipline, we can now afford better tools, constructs and mechanisms for ensuring some more safety; which doesn't mean we can bear so much with the difficulty of using these.. one can consider the (lack of) popularity of formal methods.