On 5/27/2026 3:10 AM, Eikansh Gupta wrote:
> Fold relaxed __atomic_fetch_*_N and __atomic_*_fetch_N calls with
> identity operands to the matching __atomic_load_N builtin during
> generic GIMPLE builtin folding.  This covers add/sub/or/xor by zero
> and and by all-ones, avoiding unnecessary atomic RMW expansion when
> the operation only returns the loaded value.
>
>       PR tree-optimization/107462
>
> gcc/ChangeLog:
>       * gimple-fold.cc: Include memmodel.h.
>       (atomic_load_fn_for_fetch_op): New function.
>       (gimple_fold_builtin_atomic_fetch_op): New function.
>       (gimple_fold_builtin): Handle no-op relaxed atomic fetch operations.
>
> gcc/testsuite/ChangeLog:
>       * gcc.dg/tree-ssa/pr107462.c: New test.
>       * gcc.dg/tree-ssa/pr107462-1.c: New test.
>
> Signed-off-by: Eikansh Gupta <[email protected]>
I love the idea and I think the implementation is sound, but I'd be very 
leery of making this transformation.  These are RMW operations.  My 
understanding is it is never safe to drop the W phase of an atomic, even 
relaxed atomics.   This kind of transformation would change the 
modification order for the object which I think is forbidden.

So I think we'd need someone well versed in memory models to chime in 
and explain why this is safe according to the C/C++ standards.

Jeff

Reply via email to