Could LLVM remove unused relaxed atomic loads?

I’ve noticed that neither gcc nor clang optimize away unused relaxed atomic loads: Compiler Explorer. I thought this would be permissible for relaxed loads specifically, since they’re not supposed to synchronize anything, and so removing them shouldn’t affect program behavior. Am I missing something that would make this optimization not allowed, or has it simply not been implemented yet?

Note that LLVM does appear to remove the load when it can prove that the atomic is never written to (gcc still doesn’t): Compiler Explorer.

See Poor codegen with atomics · Issue #37064 · llvm/llvm-project · GitHub

1 Like