News for hackers. Stuff that matters.
Re:Compiler Explorer by kllrnohj on Wednesday August 05, 2020 @09:06PM (#24067258)
I'm actually quite surprised GCC's isn't also a bare ret. It of course is if you replace the atomic_int with a regular int, I don't know why that wouldn't be hitting the same optimizations. Yes it's atomic, but it's still an unused local that doesn't escape the function.
Re:Compiler Explorer by hackingthenews on Wednesday August 05, 2020 @11:02PM (#24067755)
Clang/llvm also does heap ellision (removing unnecessary mallocs) while gcc didn't last time I checked. I think the llvm devs allows themselves to have a more practical understanding of the cpp standard.
Re:Compiler Explorer by BeeOnRope on Thursday August 06, 2020 @01:27AM (#24068347)
Well we could check with godbolt:

https://godbolt.org/z/1reM4s

GCC seems to eliminate them since at least 4.7.1.

Re:Compiler Explorer by hackingthenews on Thursday August 06, 2020 @12:04PM (#24072227)
That's fair. Here is another example where it eliminates since 5.1.

https://godbolt.org/z/3oGc3P

Re:Compiler Explorer by d1zzy on Friday August 07, 2020 @03:21PM (#24085556)
Right, that's why atomic ints are not a replacement for volatile when doing inter-process communication.

If the memory access (load or store) itself is the desired behavior, you better make sure you add volatile, even to atomic<int> types. The atomic<> simply provides certain guarantees about atomicity of that access in regards to other accesses within the same process, not about access from different process. If the compiler analysis determines that the atomic<> store/load isn't necessary within the currently compiled program, than it may completely elide it.

I think GCC may be disabling certain optimizations on atomic<> access because many people mistakenly use atomic<> for interprocess communication and it would break that code?