Elffuss BraidHub

kikocis/kleidiai public

main · 5.6 KiBRenderedRawPermalink
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
9KleidiAI source code must follow the project coding convention described in this
10section. The convention is intentionally small and relies on the repository's
11tooling as the baseline enforcement mechanism.
12
13## clang-format and clang-tidy
14
15Follow the formatting and static-analysis rules configured in `.clang-format`
16and `.clang-tidy`.
17
18The clang-format configuration is based on Google style with project-specific
19adjustments. Deviation from the base format should be minimal and justified.
20
21The clang-tidy configuration enables the checks that are relevant to KleidiAI
22and disables unsuitable checks explicitly.
23
24## Comments and documentation
25
26Use 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
32Write comments in descriptive third person when describing what code does.
33Imperative comments are acceptable when describing a future action, for example
34in a `TODO`.
35
36Example:
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.
44void softmax(float* dst, const float* src, size_t length) {
45 // Finds max.
46 // Regularizes.
47 // Normalizes.
48}
49```
50
51## Integer data types
52
53Use `size_t` for sizes and fixed-width integer types, e.g. `int32_t`,
54for integer values.
55
56In C++ code, do not qualify `size_t` with the `std` namespace.
57
58## Data pointers
59
60Use `void*` and `const void*` for data pointers in micro-kernel APIs. This
61keeps the public API consistent for cases when C and C++ do not provide native
62types for the stored format.
63
64## Code structure
65
66Use blank lines to separate blocks of distinct functionality. This is not
67enforced automatically, but it should be used where it makes the structure of a
68function easier to read.
69
70Use `const` for variables and parameters that are not modified.
71
72For classes where member-name shadowing is an issue, use a leading `m_` for
73member variables.
74
75Example:
76
77```cpp
78struct Foo {
79public:
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
88private:
89 int m_x;
90 int m_y;
91};
92```
93
94## Assertions and assumptions
95
96Use `KAI_ASSUME` for expected preconditions and `KAI_ASSERT` for invariants that
97must hold when those preconditions are true. Function parameter requirements
98should normally be expressed as assumptions rather than runtime `if` checks.
99
100Example:
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.
108void 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
135Do not assert or assume values for unused parameters. Mark unused parameters
136with `KAI_UNUSED` instead. Requiring specific values for parameters that are not
137used places an unnecessary burden on integrators.
138
139Example:
140
141```c
142void 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
168Test code must use `KAI_ASSUME_ALWAYS(expr)` and `KAI_ASSERT_ALWAYS(expr)` where
169the check must not be optimized away in release builds.
170
171## Assembly code
172
173Pure 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
179Avoid using inline assembly, as compiler support is not standardized across the
180supported compilers.
181
182## Naming and file layout
183
184Follow the established naming, directory, CMake, and Bazel conventions used by
185the surrounding code. Micro-kernel naming is described in
186[docs/microkernel_names.md](microkernel_names.md).
187
188## Build scripts
189
190New source files must be added to all relevant build scripts. CMake source lists
191are named `KLEIDIAI_FILES_<TECH>[_<FEAT>]*[_ASM]`. Bazel source lists are named
192`<TECH>[_<FEAT>]*_KERNELS[_ASM]`. Keep file lists sorted when adding files.
193
194Kernels that use inline assembly belong in the non-`_ASM` list. Kernels that do
195not use inline assembly normally belong in an `_ASM` list, which is preferred
196for compiler support.
197
198## Test code
199
200New unit tests must use the NextGen test framework which is described in
201[docs/microkernel_testing.md](docs/microkernel_testing.md).
202
203Cache expensive reference-data generation where appropriate to keep the CI
204pipeline execution time low.