Elffuss BraidHub

kikocis/kleidiai public

main · 4.1 KiBRawPermalink
1#!/usr/bin/env python3
2#
3# SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7"""
8Validate that implemented micro-kernels are listed in docs/microkernel_tables.md.
9"""
10from __future__ import annotations
11
12import argparse
13import os
14import re
15import sys
16from typing import Optional
17from typing import Sequence
18from typing import Set
19
20import utils.git as git_utils
21
22IMPLEMENTATION_EXTS = {".c", ".S"}
23KERNEL_NAME_RE = re.compile(r"`(kai_[A-Za-z0-9_]+)`")
24TABLE_SEPARATOR_RE = re.compile(r"^\s*\|(?:\s*:?-{3,}:?\s*\|)+\s*$")
25
26
27def resolve_path(repo_root: str, rel_path: str) -> str:
28 path = os.path.join(repo_root, rel_path)
29 return os.path.abspath(path)
30
31
32# Extract micro-kernel name from filename
33def get_kernel_name(filename: str) -> Optional[str]:
34 stem, _ = os.path.splitext(filename)
35 if not stem.startswith("kai_"):
36 return None
37 if stem.endswith("_asm"):
38 stem = stem[: -len("_asm")]
39 return stem
40
41
42def gather_implemented_kernels(ukernels_dir: str) -> Set[str]:
43 kernels: Set[str] = set()
44 for dirpath, _, filenames in os.walk(ukernels_dir):
45 for filename in filenames:
46 _, ext = os.path.splitext(filename)
47 if ext not in IMPLEMENTATION_EXTS:
48 continue
49 kernel_name = get_kernel_name(filename)
50 if kernel_name is None:
51 continue
52 kernels.add(kernel_name)
53 return kernels
54
55
56def split_table_row(line: str) -> list[str]:
57 content = line.strip()
58 if content.startswith("|"):
59 content = content[1:]
60 if content.endswith("|"):
61 content = content[:-1]
62 return [cell.strip() for cell in content.split("|")]
63
64
65def gather_documented_kernels(table_file: str) -> Set[str]:
66 try:
67 with open(table_file, "r", encoding="utf-8") as fh:
68 lines = fh.read().splitlines()
69 except OSError as err:
70 raise SystemExit(f"Cannot read table file '{table_file}': {err}") from err
71
72 documented: Set[str] = set()
73 index = 0
74 while index + 1 < len(lines):
75 # Find the start of the next table in the table file
76 header_line = lines[index]
77 separator_line = lines[index + 1]
78
79 if not header_line.lstrip().startswith("|") or not TABLE_SEPARATOR_RE.match(
80 separator_line
81 ):
82 index += 1
83 continue
84
85 # Find the index of the column that lists the micro-kernel names
86 headers = split_table_row(header_line)
87 microkernel_column = None
88 for column_index, header in enumerate(headers):
89 if header == "Micro-kernel":
90 microkernel_column = column_index
91 break
92
93 index += 2
94 while index < len(lines):
95 # Iterate over the entries in the table and add the micro-kernel names to the set that is used
96 # to keep track of the documented micro-kernels
97 row = lines[index]
98 if not row.lstrip().startswith("|"):
99 break
100 cells = split_table_row(row)
101 if (
102 microkernel_column is not None
103 and microkernel_column < len(cells)
104 and cells[microkernel_column]
105 ):
106 documented.update(KERNEL_NAME_RE.findall(cells[microkernel_column]))
107 index += 1
108
109 return documented
110
111
112def main() -> int:
113 root_dir = git_utils.repo_root()
114 ukernels_dir = resolve_path(root_dir, "kai/ukernels")
115 table_file = resolve_path(root_dir, "docs/microkernel_tables.md")
116
117 assert os.path.isdir(
118 ukernels_dir
119 ), f"Micro-kernel directory not found: {ukernels_dir}"
120 assert os.path.isfile(table_file), f"Table file not found: {table_file}"
121
122 implemented = gather_implemented_kernels(ukernels_dir)
123 documented = gather_documented_kernels(table_file)
124
125 missing = sorted(implemented - documented)
126 if missing:
127 print(f"Missing micro-kernels in {table_file}:")
128 for kernel_name in missing:
129 print(kernel_name)
130 return 1
131
132 return 0
133
134
135if __name__ == "__main__":
136 sys.exit(main())