[PATCH] D56534: [Verifier] Add verification of unaligned atomic load/store

James Y Knight via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 16 18:53:13 PST 2019


jyknight added a comment.

In D56534#1360892 <https://reviews.llvm.org/D56534#1360892>, @efriedma wrote:

> In general, any lock-based atomic can't overlap with any lock-free atomic operation: the lock-free operation will ignore the lock, so the behavior is unpredictable.


The contract for the atomic library is that it _MUST_ do a lock-free operation on an object on which _any_ compiler in use on the system may have emitted a lock-free operation for. That's why libatomic is shipped as a separate shared library with GCC.

E.g., if any compiler is able to emit a a lock-free cmpxchg for a particular object, then the __atomic_compare_exchange call must also use cmpxchg, given that same pointer.

As a concrete example: if a compiler sees an 8-byte object, which is only guaranteed to be 4-byte-aligned, it may not be able to emit an inline lock-free atomic instruction, and instead call the library routine. However, it may so happen that the object is in fact be 8-byte aligned. If that's true, the library routine MUST use the lock-free instruction, rather than a lock.

This ensures correctness even when other code in the program was able to infer a greater alignment for the object. It also ensures correctness when objects (or shared objects) from multiple compiler versions, or with different CPU targets, are combined.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D56534/new/

https://reviews.llvm.org/D56534





More information about the llvm-commits mailing list