pw_alignment#
Pigweed AI summary: The "pw_alignment" module provides an interface for ensuring natural alignment of objects, which is useful for preventing the compiler from emitting libcalls to builtin atomics functions and instead using native atomic instructions. The module includes a wrapper class called "pw::NaturallyAligned" for enforcing natural alignment without any changes to how the object is used, and an additional class called "pw::AlignedAtomic" for simplifying things when used with atomics. The latter class forces the object to be aligned to its size
This module defines an interface for ensuring natural alignment of objects.
Avoiding Atomic Libcalls#
Pigweed AI summary: The article discusses the benefits of avoiding libcalls to builtin atomic functions and instead using native atomic instructions, especially for users of std::atomic. It explains that the compiler can replace a libcall with native instructions if it can determine that the size of the object is the same as its alignment. However, there can be situations where the compiler cannot assert this. To enforce natural alignment without changing how the object is used, the article provides a wrapper class called pw::NaturallyAligned and an additional class called
The main motivation is to provide a portable way for
preventing the compiler from emitting libcalls to builtin atomics
functions and instead use native atomic instructions. This is especially
useful for any pigweed user that uses std::atomic
.
Take for example std::atomic<std::optional<bool>>. Accessing the underlying object would normally involve a call to a builtin __atomic_* function provided by a builtins library. However, if the compiler can determine that the size of the object is the same as its alignment, then it can replace a libcall to __atomic_* with native instructions.
There can be certain situations where a compiler might not be able to assert this. Depending on the implementation of std::optional<bool>, this object could have a size of 2 bytes but an alignment of 1 byte which wouldn’t satisfy the constraint.
Basic usage#
Pigweed AI summary: The article discusses the usage of pw_alignment, which provides a wrapper class for enforcing natural alignment without any changes to how the object is used. An additional class is provided for simplifying things when commonly used with atomics. The article also provides examples of how to use these classes to ensure proper alignment when passing objects to __atomic_exchange.
pw_alignment provides a wrapper class pw::NaturallyAligned for enforcing natural alignment without any changes to how the object is used. Since this is commonly used with atomics, an aditional class pw::AlignedAtomic is provided for simplifying things.
// Passing a `std::optional<bool>` to `__atomic_exchange` might not replace the call
// with native instructions if the compiler cannot determine all instances of this object
// will happen to be aligned.
std::atomic<std::optional<bool>> maybe_nat_aligned_obj;
// `pw::NaturallyAligned<...>` forces the object to be aligned to its size, so passing
// it to `__atomic_exchange` will result in a replacement with native instructions.
std::atomic<pw::NaturallyAligned<std::optional<bool>>> nat_aligned_obj;
// Shorter spelling for the same as above.
std::AlignedAtomic<std::optional<bool>> also_nat_aligned_obj;