9 ms·
When compilers surprise you
- mgaunard 10mo ago[flagged]
- bayesnet 10mo agoTo provide the solution to the second part of the question, there is no closed-form solution. Since floating point math is not associative, there’s no O(1) optimization that can be applied that preserves the exact output of the O(n) loop.
- zipy124 10mo agoTechnically there is a closed form solution as long as the answer is less than 2^24 for a float32 or 2^53 for a float64, since below those all integers can be represented fully by a floating point number, and integer addition even with floating point numbers is identical if the result is below those caps. I doubt a compiler would catch that one, but it technically could do the optimisation and have the exact same bit answer. If result was intialised to a non-integer number this would not be true however of course.
- bayesnet 10mo agoA very good point! I didn’t think of that.
- dist-epoch 10mo agoThis is why you have options like -ffast-math, to allow more aggressive but not 100% identical outcome optimizations.
- Dylan16807 10mo agoYou can split the problem into chunks, where each chunk has the same exponents all the way through. It doesn't get you O(1), but it gets you O(log(n)).
- ramraj07 10mo agoIm curious what exactly you ask here. I consider myself to be a decent engineer (for practical purposes) but without a CS degree, and I might likely have not passed that question. I know compilers can do some crazy optimizations but wouldn't have guessed it'll transform something from O(n) to O(1). Having said that, I dont still feel this has too much relevance to my actual job for the most part. Such performance knowledge seems to be very abstracted away from actual programming by database systems, or managed offerings like spark and snowflake, that unless you intend to work on these systems this knowledge isn't that useful (being aware they happen can be though, for sure).
- scuff3d 10mo agoHe thinks it makes him look clever, or more likely subtlety wants people to think "wow, this guy thinks something is obvious when Matt Godbolt found it surprising". This kind of question is entirely useless in an interview. It's just a random bit of trivia that either a potential hire happen to have come across, or happens to remember from math class.
- yeasku 10mo agoTrying to look smart by dissing Matt is not a good idea.
- f1shy 10mo agoAKA you get exactly the opposite…
- mattgodbolt 10mo agoI dunno he can honestly be quite a jerk sometimes
- yeasku 10mo agoBro...
- nickysielicki 10mo ago
- zipy124 10mo agoTo those who don't know about compiler optimisation, the replacement with a closed form is rather suprising I'd say, especially if someone with Matt Godbolt's experience of all people is saying it is surprising. Also this series is targeted towards more of a beginner audience to compilers, thus its likely to be suprising to the audience, even if not to you.
- mattgrice 10mo agoGauss supposedly did it when he was 7. The hardest part for the compiler is figuring out that you have a loop that computes that sum and does nothing else important.
- saagarjha 10mo agoUnfortunately I don’t have a hiring pipeline filled with Gausses
- CorrectHorseBat 10mo agoIt's something we saw in highschool, I would expect anyone with a CS degree to recognize this optimization. I barely know anything about compiler optimization, so I have no clue whether a compiler applying this optimization is surprising or something trivial.
- saagarjha 10mo agoImplementing this in a compiler is nontrivial.
- CorrectHorseBat 10mo agoYes, that was clear to me from the article and the discussion. My point is that to someone who knows about Gauss' formula but doesn't know anything about compilers might not understand what the fuss is about.
- yeasku 10mo agoFor Matt, the creator of compiler explorer, those are surprises. For you are essentials. You and the juniors you hire must have a deeper knoledge than him.
- porise 10mo agoYou don't have to be an expert in compiler design to make godbolt in fairness, although he does know a lot. I spend a lot of time looking at generated assembly and there are some more impressive ones.
- yeasku 10mo agoAs i said you must have a deeper knoledge than him. It would be great if you shared it with the world like Matt does instead of being smug about it.
- deleted 10mo ago[deleted]
- xandrius 10mo agoNothing is surprising once you know the answer. It takes some mental gymnastics to put yourself in someone else's shoes before they discovered it and thus making it less "basic".
- hypeatei 10mo agoWhat type of positions are you interviewing for? Software development is a big tent and I don't think this would be pertinent in a web dev interview, for example.
- cratermoon 10mo agohttps://xkcd.com/1053/ https://xkcd.com/1053/
- deleted 10mo ago[deleted]
- phh 10mo agoSince GCC is lacking such an essential optimization, you should consider have one of your junior interviewee contribute this basic optimization mainline.
- f1shy 10mo agoI’m pretty sure making an algorithm that converts loops to close forms (I’m sure it detects much more than just a summation) is a little bit complicated. Maybe you have much more experience than Mr Godbolt in compiliers.
- nebezb 10mo agohttps://www.npopov.com/2023/10/03/LLVM-Scalar-evolution.html https://www.npopov.com/2023/10/03/LLVM-Scalar-evolution.html “basic and essential” are interesting ways to describe the field of compiler optimization research. Are you suggesting that the discovery and implementation of SCEV in LLVM is basic and essential? Or that summing integers in a range is basic and essential?
- mgaunard 10mo agoI spoke in the context of coding those optimizations yourself.
- f1shy 10mo agoYeah. Pretty basic. Just 14k LOC https://github.com/llvm/llvm-project/blob/release/21.x/llvm/lib/Analysis/ScalarEvolution.cpp https://github.com/llvm/llvm-project/blob/release/21.x/llvm/...
- jjmarr 10mo agoI would've assumed it was hardcoded. Not a generic solution for any loop involving a recurring variable.
- rramadass 10mo agoEveryone knows the Gauss Summation formula for sum of n integers i.e. n*(n+1)/2 but it is just nice to see it in GCC vs. Clang.
- dejj 10mo agoIt’s neat. I wonder if someone attempted detecting a graph coloring problem to replace it with a constant.
- emih 10mo agoGraph coloring is NP-hard so it would be very difficult to replace it with an O(1) algorithm. If you mean graph coloring restricted to planar graphs, yes it can always be done with at most 4 colors. But it could still be less, so the answer is not always the same. (I know it was probably not a very serious comment but I just wanted to infodump about graph theory.)
- andrepd 10mo agoI'm actually surprised that gcc doesn't do this! If there's one thing compilers do well is pattern match on code patterns and replace with more efficient ones; just try pasting things from Hacker's Delight and watch it always canonicalise it to the equivalent, fastest machine code.
- nikic 10mo agoThis particular case isn't really due to pattern matching -- it's a result of a generic optimization that evaluates the exit value of an add recurrence using binomial coefficients (even if the recurrence is non-affine). This means it will work even if the contents of the loop get more exotic (e.g. if you perform the sum over x * x * x * x * x instead of x).
- f1shy 10mo agoDoing something like that with a pattern is obvious, but also useless, as it will catch very limited cases. The example presented, is known there is a closed form (it’s believed Gauss even discovered it being 6 yo). I’m sure this optimization will catch many other things, so is not trivial at all.
- morkalork 10mo agoFirst time I encountered that book was seeing it on the desk of a compiler engineer.
- g0wda 10mo agoIf you now have a function where you call this one with an integer literal, you will end up with a fully inlined integer answer!
- loeg 10mo agoCould do that whether SCEV’d or not with C++20 consteval, lol.
- JonChesterfield 10mo agoThat one is called scalar evolution, llvm abbreviates it as SCEV. The implementation is relatively complicated.
- gslin 10mo agoMore similar optimizations: https://matklad.github.io/2025/12/09/do-not-optimize-away.html https://matklad.github.io/2025/12/09/do-not-optimize-away.ht...
- Lvl999Noob 10mo agoCouldn't the compiler optimise this still? Make two versions of the function, one with constant folding and one without. Then at runtime, check the value of the parameter and call the corresponding version.
- saagarjha 10mo agoYes, a sufficiently smart compiler can always tell you’re doing a benchmark and delete it. It’s just unlikely.
- wging 10mo agoThe beginning of that article is slightly wrong: the compiler should compute N(N-1)/2 (and does), because the original code adds up all the numbers from 0 to N excluding N. The usual formulation in math includes the upper bound: the sum of integers from 1 to N, including N, is N(N+1)/2, so you have to replace N by (N-1) if you want a formula for the sum where the last number is N-1.
- phplovesong 10mo agoThis exact content was posted a few months ago. Is this AI or just a copy paste job?
- mattgodbolt 10mo agoThis exact content was only posted today? :)
- ForceBru 10mo agoYou're probably thinking of another post (https://xania.org/202512/11-pop-goes-the-weasel-er-count https://xania.org/202512/11-pop-goes-the-weasel-er-count) where an entire loop was optimized to a single instruction
- dist-epoch 10mo ago> I love that despite working with compilers for more than twenty years, they can still surprise and delight me. This kind of optimization, complete loop removal and computing the final value for simple math loops, is at least 10 years old.
- nebezb 10mo agoLearning something old can be surprising. Enjoying that learning can be delightful. Seems like the author is both surprised and delighted with an optimization they learned of today. Surely you’ve been in the same situation before.
- f1shy 10mo ago10 years is not a lot. Is almost “yesterday” things being done in a field 10 years old, can still surprise experts in the field. With 30+ years experience I still find relatively new things, that are maybe 15 yo. In topics like compiler optimization, is not like there are many books which describe this kind of algorithms.
- bumholes 10mo agoThe code that does this is here, if anyone is curious: https://github.com/llvm/llvm-project/blob/release/21.x/llvm/lib/Analysis/ScalarEvolution.cpp https://github.com/llvm/llvm-project/blob/release/21.x/llvm/... https://github.com/llvm/llvm-project/blob/release/21.x/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp https://github.com/llvm/llvm-project/blob/release/21.x/llvm/...
- vodou 10mo agoAlmost 16000 lines in a single source code file. I find this both admirable and unsettling.
- zahlman 10mo agoI do too, but I'm pretty sure I've seen worse.
- loeg 10mo agoDoes it really matter where the lines are? 16,000 lines is still 16,000 lines.
- vodou 10mo agoEven though I do find your indifference refreshing I must say: it does matter for quite a few people.
- MobiusHorizons 10mo agoIf you don’t rely on IDE features or completion plugins in an editor like vim, it can be easier to navigate tightly coupled complexity if it is all in one file. You can’t really scan it or jump to the right spot as easily as smaller files, but in vim searching for the exact symbol under the cursor is a single character shortcut, and that only works if the symbol is in the current buffer. This type of development works best for academic style code with a small number (usually one or two) experts that are familiar with the implementation, but in that context it’s remarkably effective. Not great for merge conflicts in frequently updated code though.
- vatsachak 10mo agoCompilers can add way more closed forms. Would it be worth it? https://en.wikipedia.org/wiki/Wilf%E2%80%93Zeilberger_pair https://en.wikipedia.org/wiki/Wilf%E2%80%93Zeilberger_pair
- maximgeorge 10mo ago[flagged]
- vardump 10mo agoOnly thing that surprised me was that GCC didn't manage to optimize it. I expected it to be able to do so.
- j16sdiz 10mo agoThe first thing I had in mind was: the final answer needed to be /2. keeping the number before dividing not overflowing needs some tedious work
- trehalose 10mo agoIt's not very tedious. Instead of dividing the product by 2, you can just divide whichever of x or x+1 is even by 2 before multiplying.
- Neywiny 10mo agoI'm once again surprised at GCC being slower than clang. I would have thought that GCC, which had a 20? year head start would've made faster code. And yet, occasionally I look into the assembly and go "what are you doing?" And the same flags + source into clang is better optimized or uses better instructions or whatever. One time it was bit extraction using shifts. Clang did it in 2 steps: shift left, shift right. GCC did it in 3 I think? I think it maybe shifted right first or maybe did a logical instead of arithmetic and then sign extended. Point is, it was just slower.
- stmw 10mo agoCompiler know-how and resources available during compilations made very signicant progress between gcc and LLVM/clang era. gcc was and is an incredible achievement, but it is traditionally considered difficult to implement many modern compiler techqniques in it. It's at least unpleasant, let's put it this way.
- uecker 10mo agoNot sure whether this is generally true. GCC appears to have similar optimizations and I personally find LLVM's code much more intimidating. But it is certainly true that LLVM seems to see more investment. I assume the license may also play a role. For comparison, here is some related code: https://github.com/gcc-mirror/gcc/blob/master/gcc/tree-chrec.cc https://github.com/gcc-mirror/gcc/blob/master/gcc/tree-chrec... https://github.com/llvm/llvm-project/blob/release/21.x/llvm/lib/Analysis/ScalarEvolution.cpp https://github.com/llvm/llvm-project/blob/release/21.x/llvm/...
- saagarjha 10mo agoGCC has almost the same modern compiler techniques implemented.
- fweimer 10mo agoDid it involve bitfields? GCC is notoriously bad at optimizing them. There are some target-specific optimizations, but pretty much nothing in the middle-end.
- tester756 10mo agoA lot of hardcoding, making expression consistent, e.g transforming a+3 into 3+a for easier pattern matching
- MobiusHorizons 10mo agoI will admit I was initially surprised Matt was not already familiar with this behavior given his reputation. I remember discovering it while playing with llvm intermediate representation 10 years ago in college. I would never have considered myself very knowledgeable about modern compilers, and have never done any serious performance work. In that case it had solved a recursion to a simple multiplication, which completely surprised me. The fact that Matt did not know this makes me think this pass may only work on relatively trivial problems that he would never have written in the first place, and therefore never have witnessed the optimization.
- pwdisswordfishy 10mo agoHe was: he brought up the very same example in a talk in 2017. https://www.youtube.com/watch?v=bSkpMdDe4g4&t=2640 https://www.youtube.com/watch?v=bSkpMdDe4g4&t=2640
- MobiusHorizons 10mo agoAh that makes much more sense. I guess he means the optimization is surprising when you first discover it, which it certainly was for me!
- WalterBright 10mo agoThese sorts of things are fun and interesting. Compiler optimizations fall into two categories: 1. organized data flow analysis 2. recognizing a pattern and replacing it with a faster version The first is very effective over a wide range of programs and styles, and is the bulk of the actual transformations. The second is a never-ending accumulation of patterns, where one reaches diminishing returns fairly quickly. The example in the linked article is very clever and fun, but not really of much value (I've never written a loop like that in 45 years). As mentioned elsewhere "Everyone knows the Gauss Summation formula for sum of n integers i.e. n(n+1)/2" and since everyone knows it why not just write that instead of the loop! Of course one could say that for any pattern, like replacing i*2 with i<<1, but those pattern replacements are very valuable because they are generated by high level generic coding. And you could say I'm just being grumpy about this because my optimizer does not do this particular optimization. Fair enough!
- Validark 10mo agoIt might have more value than you think. If you look up SCEV in LLVM you'll see it's primarily used for analysis and it enables other optimizations outside of math loops that, by themselves, probably don't show up very often.
- WalterBright 10mo agoYou might be right.
- gizmo686 10mo agoIt's not clear to me what optimizations the compiler actually did here. Years ago, I worked on a niche compiler, and was routinely surprised by what the optimizer was able to figure out; despite having personally written most of the optimization transformations myself.
- steveklabnik 10mo agoI can't actually speak to the specifics here but usually this is "idiom recognition", that is, it just notices that the pattern is there and transforms it directly.
- Animats 10mo agoThat's neat. A hard problem in optimization today is trying to fit code into the things complex SSE-type instructions can do. Someone recently posted an example where they'd coded a loop to count the number of one bits in a word, and the compiler generated a "popcount" instruction. That's impressive.
- mattgodbolt 10mo agoIt may be a different post, but I covered this earlier this month in the same series of blog posts/YouTube videos.
- Validark 10mo agoWhat's actually way cooler about this is that it's generic. Anybody could pattern match the "sum of a finite integer sequence" but the fact that it's general purpose is really awesome.
- cjdell 10mo agoThis is really bluring the line between implementation and specification. You may think you're writing the implementation but it is really a proxy for the specification. In other words, the compiler creating an illusion of an imperative machine.