When Compilers Surprise You

(xania.org)

75 points | by brewmarche 3 hours ago

9 comments

  • bumholes 2 minutes ago
    The code that does this is here, if anyone is curious:

    https://github.com/llvm/llvm-project/blob/release/21.x/llvm/...

  • JonChesterfield 50 minutes ago
    That one is called scalar evolution, llvm abbreviates it as SCEV. The implementation is relatively complicated.
  • gslin 50 minutes ago
  • dejj 1 hour ago
    It’s neat. I wonder if someone attempted detecting a graph coloring problem to replace it with a constant.
    • emih 35 minutes ago
      Graph 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.)

  • mgaunard 2 hours ago
    Those are just basic and essential optimizations, nothing too surprising here.

    The sum of integers is actually a question I ask developers in interviews (works well from juniors to seniors), with the extra problem of what happens if we were to use floating-point instead of integers.

    • phh 6 minutes ago
      Since GCC is lacking such an essential optimization, you should consider have one of your junior interviewee contribute this basic optimization mainline.
    • f1shy 3 minutes ago
      I’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.

    • zipy124 1 hour ago
      To 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.

    • yeasku 1 hour ago
      For 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 1 hour ago
        You 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 1 hour ago
          As 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.

    • ramraj07 1 hour ago
      Im 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 1 hour ago
        He 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.

        • nickysielicki 0 minutes ago
          Whether they get the question exactly right and can pinpoint the specific compiler passes or algebraic properties responsible for reductions like this is totally irrelevant and not what you’re actually looking for or asking about. It’s a very good jumping point for a conversation about optimization and testing whether they’re the type of developer who has ever looked at the assembly produced in their hotpath or not.

          Anyone who dumbly suggests that loops in source code will result in loops in assembly doesn’t have a clue. Anyone who throws their hands up and says, “I have no idea, but I wonder if there’s some loop invariant or algebraic trick that can be used to optimize this, let’s think about it out loud for a bit” has taken a compiler class and gets full marks. Anyone who says, “I dunno, let’s see what godbolt does” gets an explicit, “hire this one” in the feedback to the hiring manager.

        • yeasku 35 minutes ago
          Trying to look smart by dissing Matt is not a good idea.
    • bayesnet 1 hour ago
      To 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 1 hour ago
        Technically 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 1 hour ago
          A very good point! I didn’t think of that.
      • dist-epoch 32 minutes ago
        This is why you have options like -ffast-math, to allow more aggressive but not 100% identical outcome optimizations.
    • hypeatei 1 hour ago
      What 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.
    • xandrius 1 hour ago
      Nothing 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".
    • cratermoon 50 minutes ago
  • g0wda 1 hour ago
    If you now have a function where you call this one with an integer literal, you will end up with a fully inlined integer answer!
  • dist-epoch 34 minutes 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.

  • andrepd 1 hour ago
    I'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 59 minutes ago
      This 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).
  • phplovesong 39 minutes ago
    This exact content was posted a few months ago. Is this AI or just a copy paste job?