xref: /aosp_15_r20/external/pigweed/pw_alignment/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_alignment:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker============
4*61c4878aSAndroid Build Coastguard Workerpw_alignment
5*61c4878aSAndroid Build Coastguard Worker============
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_alignment
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker- **Transparent**: Enforce natural alignment without any changes to how your
10*61c4878aSAndroid Build Coastguard Worker  objects are used.
11*61c4878aSAndroid Build Coastguard Worker- **Efficient**: Prevent your compiler from emitting libcalls to builtin
12*61c4878aSAndroid Build Coastguard Worker  atomic functions and ensure it uses native atomic instructions instead.
13*61c4878aSAndroid Build Coastguard Worker
14*61c4878aSAndroid Build Coastguard Worker.. code-block:: c++
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Worker   #include "pw_alignment/alignment.h"
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker   // Passing a `std::optional<bool>` to `__atomic_exchange` might not replace
19*61c4878aSAndroid Build Coastguard Worker   // the call with native instructions if the compiler cannot determine all
20*61c4878aSAndroid Build Coastguard Worker   // instances of this object will happen to be aligned.
21*61c4878aSAndroid Build Coastguard Worker   std::atomic<std::optional<bool>> maybe_nat_aligned_obj;
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard Worker   // `pw::NaturallyAligned<...>` forces the object to be aligned to its size,
24*61c4878aSAndroid Build Coastguard Worker   // so passing it to `__atomic_exchange` will result in a replacement with
25*61c4878aSAndroid Build Coastguard Worker   // native instructions.
26*61c4878aSAndroid Build Coastguard Worker   std::atomic<pw::NaturallyAligned<std::optional<bool>>> nat_aligned_obj;
27*61c4878aSAndroid Build Coastguard Worker
28*61c4878aSAndroid Build Coastguard Worker   // Shorter spelling for the same as above.
29*61c4878aSAndroid Build Coastguard Worker   pw::AlignedAtomic<std::optional<bool>> also_nat_aligned_obj;
30*61c4878aSAndroid Build Coastguard Worker
31*61c4878aSAndroid Build Coastguard Worker``pw_alignment`` portably ensures that your compiler uses native atomic
32*61c4878aSAndroid Build Coastguard Workerinstructions rather than libcalls to builtin atomic functions. Take for example
33*61c4878aSAndroid Build Coastguard Worker``std::atomic<std::optional<bool>>``. Accessing the underlying object normally
34*61c4878aSAndroid Build Coastguard Workerinvolves a call to a builtin ``__atomic_*`` function. The problem is that the
35*61c4878aSAndroid Build Coastguard Workercompiler sometimes can't determine that the size of the object is the same
36*61c4878aSAndroid Build Coastguard Workeras the size of its alignment. ``pw_alignment`` ensures that an object aligns to
37*61c4878aSAndroid Build Coastguard Workerits size so that compilers can always make this optimization.
38*61c4878aSAndroid Build Coastguard Worker
39*61c4878aSAndroid Build Coastguard Worker.. warning::
40*61c4878aSAndroid Build Coastguard Worker   Using ``std::optional<bool>`` is only a workaround that may not work with all
41*61c4878aSAndroid Build Coastguard Worker   compilers. This specifically does not work when targetting ARM cortex M0.
42*61c4878aSAndroid Build Coastguard Worker   Additionally, ``std::optional<bool>`` is not the recommended way to represent
43*61c4878aSAndroid Build Coastguard Worker   a ternary variable.
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard Worker.. grid:: 2
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`rocket` Get Started
48*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_alignment-getstarted
49*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
50*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-primary
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard Worker      How to set up ``pw_alignment`` in your build system.
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` API Reference
55*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_alignment-reference
56*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
57*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
58*61c4878aSAndroid Build Coastguard Worker
59*61c4878aSAndroid Build Coastguard Worker      Reference information about the ``pw_alignment`` API.
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard Worker.. _module-pw_alignment-getstarted:
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard Worker-----------
64*61c4878aSAndroid Build Coastguard WorkerGet started
65*61c4878aSAndroid Build Coastguard Worker-----------
66*61c4878aSAndroid Build Coastguard Worker.. repository: https://bazel.build/concepts/build-ref#repositories
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard Worker.. tab-set::
69*61c4878aSAndroid Build Coastguard Worker
70*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: Bazel
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker      Add ``@pigweed//pw_alignment`` to the ``deps`` list in your Bazel target:
73*61c4878aSAndroid Build Coastguard Worker
74*61c4878aSAndroid Build Coastguard Worker      .. code-block::
75*61c4878aSAndroid Build Coastguard Worker
76*61c4878aSAndroid Build Coastguard Worker         cc_library("...") {
77*61c4878aSAndroid Build Coastguard Worker           # ...
78*61c4878aSAndroid Build Coastguard Worker           deps = [
79*61c4878aSAndroid Build Coastguard Worker             # ...
80*61c4878aSAndroid Build Coastguard Worker             "@pigweed//pw_alignment",
81*61c4878aSAndroid Build Coastguard Worker             # ...
82*61c4878aSAndroid Build Coastguard Worker           ]
83*61c4878aSAndroid Build Coastguard Worker         }
84*61c4878aSAndroid Build Coastguard Worker
85*61c4878aSAndroid Build Coastguard Worker      This assumes that your Bazel ``WORKSPACE`` has a `repository
86*61c4878aSAndroid Build Coastguard Worker      <https://bazel.build/concepts/build-ref#repositories>`_ named ``@pigweed``
87*61c4878aSAndroid Build Coastguard Worker      that points to the upstream Pigweed repository.
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: GN
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard Worker      Add ``$dir_pw_alignment`` to the ``deps`` list in your ``pw_executable()``
92*61c4878aSAndroid Build Coastguard Worker      build target:
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker      .. code-block::
95*61c4878aSAndroid Build Coastguard Worker
96*61c4878aSAndroid Build Coastguard Worker         pw_executable("...") {
97*61c4878aSAndroid Build Coastguard Worker           # ...
98*61c4878aSAndroid Build Coastguard Worker           deps = [
99*61c4878aSAndroid Build Coastguard Worker             # ...
100*61c4878aSAndroid Build Coastguard Worker             "$dir_pw_alignment",
101*61c4878aSAndroid Build Coastguard Worker             # ...
102*61c4878aSAndroid Build Coastguard Worker           ]
103*61c4878aSAndroid Build Coastguard Worker         }
104*61c4878aSAndroid Build Coastguard Worker
105*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: CMake
106*61c4878aSAndroid Build Coastguard Worker
107*61c4878aSAndroid Build Coastguard Worker      Add ``pw_alignment`` to your ``pw_add_library`` or similar CMake target:
108*61c4878aSAndroid Build Coastguard Worker
109*61c4878aSAndroid Build Coastguard Worker      .. code-block::
110*61c4878aSAndroid Build Coastguard Worker
111*61c4878aSAndroid Build Coastguard Worker         pw_add_library(my_library STATIC
112*61c4878aSAndroid Build Coastguard Worker           HEADERS
113*61c4878aSAndroid Build Coastguard Worker             ...
114*61c4878aSAndroid Build Coastguard Worker           PRIVATE_DEPS
115*61c4878aSAndroid Build Coastguard Worker             # ...
116*61c4878aSAndroid Build Coastguard Worker             pw_alignment
117*61c4878aSAndroid Build Coastguard Worker             # ...
118*61c4878aSAndroid Build Coastguard Worker         )
119*61c4878aSAndroid Build Coastguard Worker
120*61c4878aSAndroid Build Coastguard Worker.. _module-pw_alignment-reference:
121*61c4878aSAndroid Build Coastguard Worker
122*61c4878aSAndroid Build Coastguard Worker-------------
123*61c4878aSAndroid Build Coastguard WorkerAPI reference
124*61c4878aSAndroid Build Coastguard Worker-------------
125*61c4878aSAndroid Build Coastguard Worker.. doxygengroup:: pw_alignment
126*61c4878aSAndroid Build Coastguard Worker   :members:
127