by wheresvic4 on 7/11/21, 7:58 PM with 250 comments
by chrisseaton on 7/11/21, 8:59 PM
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
by dang on 7/11/21, 10:34 PM
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
by andi999 on 7/11/21, 10:20 PM
by tester756 on 7/11/21, 9:05 PM
>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
by DangitBobby on 7/11/21, 11:32 PM
by rurban on 7/11/21, 8:58 PM