131 points · by dejawu · 5y ago · 88 comments
Secondly, as others have already explained, endless loops are UB and optimized away, but, thirdly, even if they weren't, C++ integers are more like Z/nZ than Z and checking if Collatz holds in Z/nZ is rather trivial.
This is an widely-held misconception, but C++ signed integers are _nothing_ like Z/nZ. Unsigned integers are (except for division), and machine arithmetic often is, but in the C++ abstract machine, signed overflow is undefined behavior.
That soundbite also repeats/paraphrases what thread author Joe Groff said "LLVM considers infinite recursion to be UB,"
But... the key is infinite loops without side effects can be optimized away. Endless loops even without a provable termination (The Halting Problem) are not UB.
But in this case, the value 'x' has a side effect of determining what bool of true/false value to return. That while() loop does not look like it's free of side effects.
That leaves only the loop itself, which can run infinitely long (which would be UB, and the compiler is free to assume that won't happen), or it won't, in which case it will always return true. Thus, like them or not, the rules of C++ allow this particular outcome.
I can't say I like this kind of optimisation. Sure, you can construct neat circus tricks with it like this, but other than that it seems pretty pointless for real-world software, and something that could easily turn a simple mistake into a debugging disaster.
(b) The Collatz Conjecture isn't one of the Millennium problems;
(c) It's using bounded numbers in the range where the Collatz Conjecture is known to be true;
(d) It seems reasonable to say "If this routine exits then the value will be "true";
(e) None of what I say here is deep or new, so when people get excited about this sort of thing it makes me wonder what I've missed.
So ... what have I missed?
(b) the Millennium problems are not the only million-dollar prizes in mathematics
(c) part of the joke
(d) part of the joke (and part of the C++ standard)
Searching 'collatz conjecture prize' quite literally puts a prize of a bout 120MM Yen (~ $1MM) as the top result.
But also a layer deeper, collatz() takes a uint128 as input and the Collatz conjecture has already been proven to be true for all numbers that would fit into a uint128. So LLVM arrives to the correct answer (`return true`) because of the wrong reason, mathematically speaking.
So the compiler doesn't actually need to figure out if the loop is infinite or not.
Specifically, Collatz says "if x is even then x /= 2" but the code checks "x is odd".
It's not but there are more bounties out there than just the Millenium list. For the Collatz Conjecture specifically there's a 120M Japanese Yen prize which at this moment is a bit more than 1 million USD.
https://www.prnewswire.com/news-releases/bakuage-offers-priz...
Thanks.
The input is an unsigned __int128, and I believe Collatz has only been verified up to 2^68.
I get that it is supposed to be a joke, but it kind of falls flat because it misses the point entirely.
For instance not all languages that target LLVM will get this result. That is interesting.
I'd also note that this particular footgun only exists in C++, the C standard allows loops that never terminate and never do IO or volatile memory access.
In a valid C++ program, every thread eventually does one of the following:
* terminate
* makes a call to an I/O library function
* performs an access through a volatile glvalue
* performs an atomic operation or a synchronization operation
It's fairly obvious how a loop that has none of these side-effects is thus a target to get turned into one that's guaranteed to terminate.
Maybe they secretly hope that some tool, someday, will spit out a correct proof? :)
[1] https://github.com/sosy-lab/sv-benchmarks/blob/svcomp17/c/te...
Not sure why he used the word "recursion" since collatz() is not calling itself.
In reality, the reason is that out of the two outcomes: true or infinite loop, the infinite loop violates the standard, and therefore, the result must be true. Another way to see it is that the only way for the code to be valid is if the programmer knows that the Collatz conjecture is true, and the compiler trusts the programmer, saying that the compiler knows the answer to the Collatz conjecture is a kind of circular reasoning.
There are other issues here: the numbers are bounded (a 128 bits integer), but the conjecture is not. And no million dollar prize have been assigned to this problem (it is not one of the millenium problems). Also, an infinite loop is not a result, it is actually the textbook example of an undecidable problem.
> 6.9.2.2 Forward progress [intro.progress]
> The implementation may assume that any thread will eventually do one of the following:
> — terminate,
> — make a call to a library I/O function,
> — perform an access through a volatile glvalue, or
> — perform a synchronization operation or an atomic operation.
That loop doesn't ever do any of the second through fourth things, and if it didn't return true, then it would never terminate either, so Clang is allowed to assume that it will eventually return true. And since the loop doesn't have any other effects visible outside the function, it can be optimized away entirely.
unsigned __int128