Elffuss BraidHub

kikocis/kleidiai public

main · 7.8 KiBRawPermalink
1#
2# SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
3#
4# SPDX-License-Identifier: Apache-2.0
5#
6
7"""Build definitions for KleidiAI"""
8
9load(
10 "@bazel_tools//tools/build_defs/repo:utils.bzl",
11 "update_attrs",
12 "workspace_and_buildfile",
13)
14load("@rules_cc//cc:defs.bzl", "cc_library")
15
16# Extra warnings for GCC/CLANG C/C++
17def kai_gcc_warn_copts():
18 return [
19 "-Wall",
20 "-Wdisabled-optimization",
21 "-Wextra",
22 "-Wformat-security",
23 "-Wformat=2",
24 "-Winit-self",
25 "-Wstrict-overflow=2",
26 "-Wswitch-default",
27 "-Wno-vla",
28 "-Wcast-qual",
29 ]
30
31# GCC/CLANG C only warning options
32def kai_gcc_warn_conlyopts():
33 return [
34 "-Wmissing-prototypes",
35 "-Wstrict-prototypes",
36 "-Wpedantic",
37 ]
38
39# GCC/CLANG C++ only warning options
40def kai_gcc_warn_cxxopts():
41 return [
42 "-Wctor-dtor-privacy",
43 "-Woverloaded-virtual",
44 "-Wsign-promo",
45 "-Wmissing-declarations",
46 ]
47
48# GCC/CLANG compiler options
49def kai_gcc_std_copts():
50 return ["-std=c99"] + kai_gcc_warn_copts() + kai_gcc_warn_conlyopts()
51
52# GCC/CLANG compiler options
53def kai_gcc_std_cxxopts():
54 return ["-std=c++17"] + kai_gcc_warn_copts() + kai_gcc_warn_cxxopts()
55
56def kai_cpu_select(cpu_uarch):
57 if len(cpu_uarch) == 0:
58 return "armv8-a"
59 else:
60 return "armv8.2-a+" + "+".join(cpu_uarch)
61
62def kai_cpu_i8mm():
63 return ["i8mm"]
64
65def kai_cpu_dotprod():
66 return ["dotprod"]
67
68def kai_cpu_bf16():
69 return ["bf16"]
70
71def kai_cpu_fp16():
72 return ["fp16"]
73
74def kai_cpu_neon():
75 return []
76
77def kai_cpu_sve():
78 return ["sve"]
79
80def kai_cpu_sve2():
81 return ["sve2"]
82
83def kai_cpu_sme():
84 return ["sme"]
85
86def kai_cpu_sme2():
87 return ["sme2"]
88
89# MSVC compiler options
90def kai_msvc_std_copts():
91 return ["/Wall"]
92
93def kai_msvc_std_cxxopts():
94 return ["/Wall"]
95
96def kai_copts(ua_variant):
97 return select({
98 "//:windows": kai_msvc_std_copts(),
99 # Assume default to use GCC/CLANG compilers. This is a fallback case to make it
100 # easier for KleidiAI library users
101 "//conditions:default": kai_gcc_std_copts() + ["-march=" + kai_cpu_select(ua_variant)],
102 })
103
104def kai_cxxopts(ua_variant):
105 return select({
106 "//:windows": kai_msvc_std_cxxopts(),
107 # Assume default to use GCC/CLANG compilers. This is a fallback case to make it
108 # easier for KleidiAI library users
109 "//conditions:default": kai_gcc_std_cxxopts() + ["-march=" + kai_cpu_select(ua_variant)],
110 })
111
112def _kai_list_check(predicate, sub_list, super_list):
113 """ Allow to check of any or all elements of first list are in second one
114
115 Args:
116 predicate (function): predicate to check. For example 'all' or 'any'
117 sub_list (list): first list
118 super_list (list): second list
119 """
120 return predicate([item in super_list for item in sub_list])
121
122def _kai_c_cxx_common(name, copts_def_func, **kwargs):
123 """Common C/C++ native cc_library wrapper with custom parameters and defaults
124
125 Args:
126 name (string): name of target library
127 copts_def_func (function): function to get C or C++ respective defaults
128 **kwargs (dict): other arguments like srcs, hdrs, deps
129 """
130
131 # Convert CPU uarch to list of features
132 cpu_uarch = kwargs.get("cpu_uarch", kai_cpu_neon())
133 extra_copts = []
134
135 # Indicate if SME flags should be replaced since a toolchain may not support it
136 replace_sme_flags = _kai_list_check(any, kai_cpu_sme() + kai_cpu_sme2(), cpu_uarch)
137
138 if replace_sme_flags:
139 if _kai_list_check(all, kai_cpu_sme(), cpu_uarch):
140 for uarch in kai_cpu_sme():
141 cpu_uarch.remove(uarch)
142
143 if _kai_list_check(all, kai_cpu_sme2(), cpu_uarch):
144 for uarch in kai_cpu_sme2():
145 cpu_uarch.remove(uarch)
146
147 # Replace SME/SME2 with SVE+SVE2, but disable compiler vectorization
148 cpu_uarch.extend(kai_cpu_sve())
149 cpu_uarch.extend(kai_cpu_sve2())
150
151 extra_copts.append("-fno-tree-vectorize")
152 extra_copts.append("-fno-tree-slp-vectorize")
153
154 kwargs["copts"] = kwargs.get("copts", []) + copts_def_func(cpu_uarch) + extra_copts
155 kwargs["deps"] = ["//:common"] + kwargs.get("deps", [])
156 kwargs["linkstatic"] = kwargs.get("linkstatic", True)
157
158 # Remove custom cpu_uarch paramter before passing it to cc_library
159 if "cpu_uarch" in kwargs:
160 kwargs.pop("cpu_uarch")
161
162 # Add kernels source files
163 if "kernels" in kwargs:
164 kwargs["srcs"] = kwargs.get("srcs", []) + [ukernel + ".c" for ukernel in kwargs["kernels"]]
165 kwargs["textual_hdrs"] = kwargs.get("textual_hdrs", []) + [ukernel + ".h" for ukernel in kwargs["kernels"]]
166 kwargs.pop("kernels")
167
168 # Add assembly kernels source files
169 if "kernels_asm" in kwargs:
170 kwargs["srcs"] = kwargs.get("srcs", []) + [ukernel + "_asm.S" for ukernel in kwargs["kernels_asm"]] + [ukernel + ".c" for ukernel in kwargs["kernels_asm"]]
171 kwargs["textual_hdrs"] = kwargs.get("textual_hdrs", []) + [ukernel + ".h" for ukernel in kwargs["kernels_asm"]]
172 kwargs.pop("kernels_asm")
173
174 # Add kernels source files without per-kernel headers
175 if "kernels_nohdr" in kwargs:
176 kernels_nohdr = kwargs["kernels_nohdr"]
177 kwargs["srcs"] = (
178 kwargs.get("srcs", []) +
179 [ukernel + ".c" for ukernel in kernels_nohdr]
180 )
181 kwargs.pop("kernels_nohdr")
182
183 # Add assembly kernels source files without per-kernel headers
184 if "kernels_asm_nohdr" in kwargs:
185 kernels_asm_nohdr = kwargs["kernels_asm_nohdr"]
186 kwargs["srcs"] = (
187 kwargs.get("srcs", []) +
188 [ukernel + "_asm.S" for ukernel in kernels_asm_nohdr] +
189 [ukernel + ".c" for ukernel in kernels_asm_nohdr]
190 )
191 kwargs.pop("kernels_asm_nohdr")
192
193 # Add assembly-only kernels source files
194 if "kernels_asm_only" in kwargs:
195 kernels_asm_only = kwargs["kernels_asm_only"]
196 kwargs["srcs"] = (
197 kwargs.get("srcs", []) +
198 [ukernel + "_asm.S" for ukernel in kernels_asm_only]
199 )
200 kwargs.pop("kernels_asm_only")
201
202 cc_library(
203 name = name,
204 **kwargs
205 )
206
207def kai_c_library(name, **kwargs):
208 """C native cc_library wrapper with custom parameters and defaults
209
210 Args:
211 name (string): name of target library
212 **kwargs (dict): other arguments like srcs, hdrs, deps
213 """
214 _kai_c_cxx_common(name, kai_copts, **kwargs)
215
216def kai_cxx_library(name, **kwargs):
217 """C++ native cc_library wrapper with custom parameters and defaults
218
219 Args:
220 name (string): name of target library
221 **kwargs (dict): other arguments like srcs, hdrs, deps
222 """
223 _kai_c_cxx_common(name, kai_cxxopts, **kwargs)
224
225def _kai_local_archive_impl(ctx):
226 """Implementation of the kai_local_archive rule."""
227 ctx.extract(
228 ctx.attr.archive,
229 stripPrefix = ctx.attr.strip_prefix,
230 )
231 workspace_and_buildfile(ctx)
232
233 return update_attrs(ctx.attr, _kai_local_archive_attrs.keys(), {})
234
235_kai_local_archive_attrs = {
236 "archive": attr.label(mandatory = True, allow_single_file = True, doc = "Path to local archive relative to workspace"),
237 "strip_prefix": attr.string(doc = "Strip prefix from archive internal content"),
238 "build_file": attr.label(allow_single_file = True, doc = "Name of BUILD file for extracted repository"),
239 "build_file_content": attr.string(doc = "Content of BUILD file for extracted repository"),
240 "workspace_file": attr.label(doc = "Name of WORKSPACE file for extracted repository"),
241 "workspace_file_content": attr.string(doc = "Content of WORKSPACE file for extracted repository"),
242}
243
244kai_local_archive = repository_rule(
245 implementation = _kai_local_archive_impl,
246 attrs = _kai_local_archive_attrs,
247 local = True,
248 doc = "Rule to use repository from compressed local archive",
249)