from Hacker News

No sane compiler would optimize atomics (2015)

by wheresvic4 on 7/11/21, 7:58 PM with 250 comments

  • by chrisseaton on 7/11/21, 8:59 PM

    A similar thing is that I often come across people who are well-informed but still surprised that compilers will combine two observable operations into one, and complain that some other thread somehow 'should' be able to observe the intermediate state. But I don't understand how they think they would be able to tell the difference between an interleaving that never happens to happen, and one that will never happen.
  • by a1369209993 on 7/12/21, 1:08 AM

      > int rlo() {
      >   // Dead store eliminated.
      >   y.store(0, std::memory_order_release);
      >   // Redundant load eliminated.
      >   x = 1;
      >   return 0; // Stored value propagated here.
      >   }
    
    This is actually wrong, for subtler reasons than one might initially think[0]. Consider the case where rlo is called with x!=0,1 and y!=0. Another thread that observes y=0 is guaranteed not to see the originial value of x - it might see 0, it might see 1, but something has to have been written to x by the time it observes y=0. The above optimization allows even a single-processor execution to context-switch at "// Redundant load eliminated" and observe y=0, x=??? from another thread.

    Oddly, the correct code seems to be:

      x = 1;
      y.store(0, std::memory_order_release);
      return 0;
    
    On the face of it, the load acquire seems like it would prohibit that, but in that's only a problem if it (the acquire) observes some release with which x = 1 can't be reordered. But it's hard coded to always observe the y = 0 release, so that can't happen. (I'm less sure that this is correct than that the previous version is wrong, admittedly.)

    0: In particular, it's not just a matter of "x = 0 has to happen because store-release".

  • by overgard on 7/11/21, 10:56 PM

    I'd love to have a compiler that just tells me what optimizations could be made, but let me either do it myself (if syntactically available in the language), or explicitly mark the optimization as ok through a #pragma or something like that. I just think having to read the disassembly to figure out what happened isn't a great user experience.
  • by dang on 7/11/21, 10:34 PM

    One past thread:

    No Sane Compiler Would Optimize Atomics - https://news.ycombinator.com/item?id=11694325 - May 2016 (56 comments)

  • by elliotpage on 7/11/21, 10:37 PM

    2105? I love posts from the future
  • by andi999 on 7/11/21, 10:20 PM

    Why is it surprising that atomic can be optimized?
  • by tester756 on 7/11/21, 9:05 PM

    >For Compiler Writers

    >Get back to work, there’s so much more to optimize… and so much code to break! Help users write good code: the compiler should provide diagnostics when it detects anti-patterns or misuses of atomics.

    Is it worth to put effort into compiler optimizations instead of putting more effort into providing "diagnostics" and informations for programmer?

    in context of this: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.29....

  • by MauranKilom on 7/11/21, 10:55 PM

    Could you fix the title to not be almost a century in the future please? :)
  • by DangitBobby on 7/11/21, 11:32 PM

    Feels like a really weird context for click-bait. I guess I shouldn't be surprised.
  • by rurban on 7/11/21, 8:58 PM

    Likewise no sane compiler would optimize memset. Unluckily we don't have sane compilers