| 1 | <!-- |
| 2 | SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | |
| 4 | SPDX-License-Identifier: Apache-2.0 |
| 5 | --> |
| 6 | |
| 7 | # Coding standard and conventions |
| 8 | |
| 9 | KleidiAI source code must follow the project coding convention described in this |
| 10 | section. The convention is intentionally small and relies on the repository's |
| 11 | tooling as the baseline enforcement mechanism. |
| 12 | |
| 13 | ## clang-format and clang-tidy |
| 14 | |
| 15 | Follow the formatting and static-analysis rules configured in `.clang-format` |
| 16 | and `.clang-tidy`. |
| 17 | |
| 18 | The clang-format configuration is based on Google style with project-specific |
| 19 | adjustments. Deviation from the base format should be minimal and justified. |
| 20 | |
| 21 | The clang-tidy configuration enables the checks that are relevant to KleidiAI |
| 22 | and disables unsuitable checks explicitly. |
| 23 | |
| 24 | ## Comments and documentation |
| 25 | |
| 26 | Use line comments for both code comments and API documentation: |
| 27 | |
| 28 | - Use `//` for ordinary code comments. |
| 29 | - Use `///` for documentation comments. |
| 30 | - Do not use block comments for normal source documentation. |
| 31 | |
| 32 | Write comments in descriptive third person when describing what code does. |
| 33 | Imperative comments are acceptable when describing a future action, for example |
| 34 | in a `TODO`. |
| 35 | |
| 36 | Example: |
| 37 | |
| 38 | ```cpp |
| 39 | /// Performs softmax activation function. |
| 40 | /// |
| 41 | /// @param[out] dst Output data buffer. |
| 42 | /// @param[in] src Input data buffer. |
| 43 | /// @param[in] length Number of elements. |
| 44 | void softmax(float* dst, const float* src, size_t length) { |
| 45 | // Finds max. |
| 46 | // Regularizes. |
| 47 | // Normalizes. |
| 48 | } |
| 49 | ``` |
| 50 | |
| 51 | ## Integer data types |
| 52 | |
| 53 | Use `size_t` for sizes and fixed-width integer types, e.g. `int32_t`, |
| 54 | for integer values. |
| 55 | |
| 56 | In C++ code, do not qualify `size_t` with the `std` namespace. |
| 57 | |
| 58 | ## Data pointers |
| 59 | |
| 60 | Use `void*` and `const void*` for data pointers in micro-kernel APIs. This |
| 61 | keeps the public API consistent for cases when C and C++ do not provide native |
| 62 | types for the stored format. |
| 63 | |
| 64 | ## Code structure |
| 65 | |
| 66 | Use blank lines to separate blocks of distinct functionality. This is not |
| 67 | enforced automatically, but it should be used where it makes the structure of a |
| 68 | function easier to read. |
| 69 | |
| 70 | Use `const` for variables and parameters that are not modified. |
| 71 | |
| 72 | For classes where member-name shadowing is an issue, use a leading `m_` for |
| 73 | member variables. |
| 74 | |
| 75 | Example: |
| 76 | |
| 77 | ```cpp |
| 78 | struct Foo { |
| 79 | public: |
| 80 | Foo(int x, int y) : m_x{x}, m_y{y} { |
| 81 | } |
| 82 | |
| 83 | void set(int x, int y) { |
| 84 | m_x = x; |
| 85 | m_y = y; |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | int m_x; |
| 90 | int m_y; |
| 91 | }; |
| 92 | ``` |
| 93 | |
| 94 | ## Assertions and assumptions |
| 95 | |
| 96 | Use `KAI_ASSUME` for expected preconditions and `KAI_ASSERT` for invariants that |
| 97 | must hold when those preconditions are true. Function parameter requirements |
| 98 | should normally be expressed as assumptions rather than runtime `if` checks. |
| 99 | |
| 100 | Example: |
| 101 | |
| 102 | ```cpp |
| 103 | /// Performs softmax activation function. |
| 104 | /// |
| 105 | /// @param[out] dst Output data buffer. |
| 106 | /// @param[in] src Input data buffer. |
| 107 | /// @param[in] length Number of elements. |
| 108 | void softmax(float* dst, const float* src, size_t length) { |
| 109 | KAI_ASSUME(dst != NULL); |
| 110 | KAI_ASSUME(src != NULL); |
| 111 | KAI_ASSUME(length > 0); |
| 112 | |
| 113 | float max = -INFINITY; |
| 114 | for (size_t i = 0; i < length; ++i) { |
| 115 | if (src[i] > max) { |
| 116 | max = src[i]; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | float sum = 0; |
| 121 | for (size_t i = 0; i < length; ++i) { |
| 122 | const float tmp = exp(src[i] - max); |
| 123 | dst[i] = tmp; |
| 124 | sum += tmp; |
| 125 | } |
| 126 | |
| 127 | KAI_ASSERT(sum > 0); |
| 128 | |
| 129 | for (size_t i = 0; i < length; ++i) { |
| 130 | dst[i] = dst[i] / sum; |
| 131 | } |
| 132 | } |
| 133 | ``` |
| 134 | |
| 135 | Do not assert or assume values for unused parameters. Mark unused parameters |
| 136 | with `KAI_UNUSED` instead. Requiring specific values for parameters that are not |
| 137 | used places an unnecessary burden on integrators. |
| 138 | |
| 139 | Example: |
| 140 | |
| 141 | ```c |
| 142 | void kai_run_...( |
| 143 | size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, |
| 144 | size_t rhs_stride_row, const void* rhs, const void* bias, const void* scale, |
| 145 | void* rhs_packed, size_t extra_bytes, const void* params) { |
| 146 | KAI_UNUSED(num_groups); |
| 147 | KAI_UNUSED(nr); |
| 148 | KAI_UNUSED(kr); |
| 149 | KAI_UNUSED(sr); |
| 150 | KAI_ASSUME(rhs != NULL); |
| 151 | KAI_ASSUME(bias != NULL); |
| 152 | KAI_UNUSED(scale); |
| 153 | KAI_ASSUME(rhs_packed != NULL); |
| 154 | KAI_UNUSED(extra_bytes); |
| 155 | KAI_UNUSED(params); |
| 156 | |
| 157 | KernelArgs args; |
| 158 | args.bias_ptr = bias; |
| 159 | args.height = k; |
| 160 | args.width = n; |
| 161 | args.in = rhs; |
| 162 | args.out = rhs_packed; |
| 163 | args.in_stride = rhs_stride_row; |
| 164 | kai_kernel_...(&args); |
| 165 | } |
| 166 | ``` |
| 167 | |
| 168 | Test code must use `KAI_ASSUME_ALWAYS(expr)` and `KAI_ASSERT_ALWAYS(expr)` where |
| 169 | the check must not be optimized away in release builds. |
| 170 | |
| 171 | ## Assembly code |
| 172 | |
| 173 | Pure assembly micro-kernels must: |
| 174 | |
| 175 | - Conform to [AAPCS64](https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst). |
| 176 | - Emit exactly one `ret`. |
| 177 | - Avoid calls with `bl`. |
| 178 | |
| 179 | Avoid using inline assembly, as compiler support is not standardized across the |
| 180 | supported compilers. |
| 181 | |
| 182 | ## Naming and file layout |
| 183 | |
| 184 | Follow the established naming, directory, CMake, and Bazel conventions used by |
| 185 | the surrounding code. Micro-kernel naming is described in |
| 186 | [docs/microkernel_names.md](microkernel_names.md). |
| 187 | |
| 188 | ## Build scripts |
| 189 | |
| 190 | New source files must be added to all relevant build scripts. CMake source lists |
| 191 | are named `KLEIDIAI_FILES_<TECH>[_<FEAT>]*[_ASM]`. Bazel source lists are named |
| 192 | `<TECH>[_<FEAT>]*_KERNELS[_ASM]`. Keep file lists sorted when adding files. |
| 193 | |
| 194 | Kernels that use inline assembly belong in the non-`_ASM` list. Kernels that do |
| 195 | not use inline assembly normally belong in an `_ASM` list, which is preferred |
| 196 | for compiler support. |
| 197 | |
| 198 | ## Test code |
| 199 | |
| 200 | New unit tests must use the NextGen test framework which is described in |
| 201 | [docs/microkernel_testing.md](docs/microkernel_testing.md). |
| 202 | |
| 203 | Cache expensive reference-data generation where appropriate to keep the CI |
| 204 | pipeline execution time low. |