• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Build definitions and rules for XNNPACK."""
2
3load(":emscripten.bzl", "xnnpack_emscripten_benchmark_linkopts", "xnnpack_emscripten_deps", "xnnpack_emscripten_minimal_linkopts", "xnnpack_emscripten_test_linkopts")
4
5def xnnpack_visibility():
6    """Visibility of :XNNPACK target.
7
8    All other targets have private visibility, and can not have external
9    dependencies.
10    """
11    return ["//visibility:public"]
12
13def xnnpack_min_size_copts():
14    """Compiler flags for size-optimized builds."""
15    return ["-Os"]
16
17def xnnpack_gcc_std_copts():
18    """GCC-like compiler flags to specify language standard for C sources."""
19    return ["-std=c99"]
20
21def xnnpack_msvc_std_copts():
22    """MSVC compiler flags to specify language standard for C sources."""
23    return ["/Drestrict="]
24
25def xnnpack_std_cxxopts():
26    """Compiler flags to specify language standard for C++ sources."""
27    return ["-std=gnu++11"]
28
29def xnnpack_optional_ruy_copts():
30    """Compiler flags to optionally enable Ruy benchmarks."""
31    return []
32
33def xnnpack_optional_gemmlowp_copts():
34    """Compiler flags to optionally enable Gemmlowp benchmarks."""
35    return []
36
37def xnnpack_optional_tflite_copts():
38    """Compiler flags to optionally enable TensorFlow Lite benchmarks."""
39    return []
40
41def xnnpack_optional_armcl_copts():
42    """Compiler flags to optionally enable ARM ComputeLibrary benchmarks."""
43    return []
44
45def xnnpack_optional_dnnl_copts():
46    """Compiler flags to optionally enable Intel DNNL benchmarks."""
47    return []
48
49def xnnpack_optional_ruy_deps():
50    """Optional Ruy dependencies."""
51    return []
52
53def xnnpack_optional_gemmlowp_deps():
54    """Optional Gemmlowp dependencies."""
55    return []
56
57def xnnpack_optional_tflite_deps():
58    """Optional TensorFlow Lite dependencies."""
59    return []
60
61def xnnpack_optional_armcl_deps():
62    """Optional ARM ComputeLibrary dependencies."""
63    return []
64
65def xnnpack_optional_dnnl_deps():
66    """Optional Intel DNNL dependencies."""
67    return []
68
69def xnnpack_cc_library(
70        name,
71        srcs = [],
72        x86_srcs = [],
73        aarch32_srcs = [],
74        aarch64_srcs = [],
75        riscv_srcs = [],
76        wasm_srcs = [],
77        wasmsimd_srcs = [],
78        wasmrelaxedsimd_srcs = [],
79        copts = [],
80        gcc_copts = [],
81        msvc_copts = [],
82        mingw_copts = [],
83        msys_copts = [],
84        gcc_x86_copts = [],
85        msvc_x86_32_copts = [],
86        msvc_x86_64_copts = [],
87        apple_aarch32_copts = [],
88        aarch32_copts = [],
89        aarch64_copts = [],
90        riscv_copts = [],
91        wasm_copts = [],
92        wasmsimd_copts = [],
93        wasmrelaxedsimd_copts = [],
94        optimized_copts = ["-O2"],
95        hdrs = [],
96        defines = [],
97        includes = [],
98        deps = [],
99        visibility = [],
100        testonly = False):
101    """C/C++/assembly library with architecture-specific configuration.
102
103    Define a static library with architecture- and instruction-specific
104    source files and/or compiler flags.
105
106    Args:
107      name: The name of the library target to define.
108      srcs: The list of architecture-independent source files.
109      x86_srcs: The list of x86-specific source files.
110      aarch32_srcs: The list of AArch32-specific source files.
111      aarch64_srcs: The list of AArch64-specific source files.
112      riscv_srcs: The list of RISC-V-specific source files.
113      wasm_srcs: The list of WebAssembly 1.0-specific source files.
114      wasmsimd_srcs: The list of WebAssembly SIMD-specific source files.
115      wasmrelaxedsimd_srcs: The list of WebAssembly Relaxed SIMD-specific
116                            source files.
117      copts: The list of compiler flags to use in all builds. -I flags for
118             include/ and src/ directories of XNNPACK are always prepended
119             before these user-specified flags.
120      gcc_copts: The list of compiler flags to use with GCC-like compilers.
121      msvc_copts: The list of compiler flags to use with MSVC compiler.
122      mingw_copts: The list of compiler flags to use with MinGW GCC compilers.
123      msys_copts: The list of compiler flags to use with MSYS (Cygwin) GCC
124                  compilers.
125      gcc_x86_copts: The list of GCC-like compiler flags to use in x86 (32-bit
126                     and 64-bit) builds.
127      msvc_x86_32_copts: The list of MSVC compiler flags to use in x86 (32-bit)
128                         builds.
129      msvc_x86_64_copts: The list of MSVC compiler flags to use in x86 (64-bit)
130                         builds.
131      apple_aarch32_copts: The list of compiler flags to use in AArch32 builds
132                           with Apple Clang.
133      aarch32_copts: The list of compiler flags to use in AArch32 builds.
134      aarch64_copts: The list of compiler flags to use in AArch64 builds.
135      riscv_copts: The list of compiler flags to use in RISC-V builds.
136      wasm_copts: The list of compiler flags to use in WebAssembly 1.0 builds.
137      wasmsimd_copts: The list of compiler flags to use in WebAssembly SIMD
138                      builds.
139      wasmrelaxedsimd_copts: The list of compiler flags to use in WebAssembly
140                             Relaxed SIMD builds.
141      optimized_copts: The list of compiler flags to use in optimized builds.
142                       Defaults to -O2.
143      hdrs: The list of header files published by this library to be textually
144            included by sources in dependent rules.
145      defines: List of predefines macros to be added to the compile line.
146      includes: List of include dirs to be added to the compile line.
147      deps: The list of other libraries to be linked.
148      visibility: The list of packages that can depend on this target.
149    """
150    native.cc_library(
151        name = name,
152        srcs = srcs + select({
153            ":linux_k8": x86_srcs,
154            ":linux_arm": aarch32_srcs,
155            ":linux_armeabi": aarch32_srcs,
156            ":linux_armhf": aarch32_srcs,
157            ":linux_armv7a": aarch32_srcs,
158            ":linux_arm64": aarch64_srcs,
159            ":macos_x86_64": x86_srcs,
160            ":macos_arm64": aarch64_srcs,
161            ":windows_x86_64_clang": x86_srcs,
162            ":windows_x86_64_mingw": x86_srcs,
163            ":windows_x86_64_msys": x86_srcs,
164            ":windows_x86_64": x86_srcs,
165            ":android_armv7": aarch32_srcs,
166            ":android_arm64": aarch64_srcs,
167            ":android_x86": x86_srcs,
168            ":android_x86_64": x86_srcs,
169            ":ios_armv7": aarch32_srcs,
170            ":ios_arm64": aarch64_srcs,
171            ":ios_arm64e": aarch64_srcs,
172            ":ios_sim_arm64": aarch64_srcs,
173            ":ios_x86": x86_srcs,
174            ":ios_x86_64": x86_srcs,
175            ":watchos_armv7k": aarch32_srcs,
176            ":watchos_arm64_32": aarch64_srcs,
177            ":watchos_x86": x86_srcs,
178            ":watchos_x86_64": x86_srcs,
179            ":tvos_arm64": aarch64_srcs,
180            ":tvos_x86_64": x86_srcs,
181            ":emscripten_wasm": wasm_srcs,
182            ":emscripten_wasmsimd": wasmsimd_srcs,
183            ":emscripten_wasmrelaxedsimd": wasmrelaxedsimd_srcs,
184            "//conditions:default": [],
185        }),
186        copts = [
187            "-Iinclude",
188            "-Isrc",
189        ] + copts + select({
190            ":linux_k8": gcc_x86_copts,
191            ":linux_arm": aarch32_copts,
192            ":linux_armeabi": aarch32_copts,
193            ":linux_armhf": aarch32_copts,
194            ":linux_armv7a": aarch32_copts,
195            ":linux_arm64": aarch64_copts,
196            ":macos_x86_64": gcc_x86_copts,
197            ":macos_arm64": aarch64_copts,
198            ":windows_x86_64_clang": ["/clang:" + opt for opt in gcc_x86_copts],
199            ":windows_x86_64_mingw": mingw_copts + gcc_x86_copts,
200            ":windows_x86_64_msys": msys_copts + gcc_x86_copts,
201            ":windows_x86_64": msvc_x86_64_copts,
202            ":android_armv7": aarch32_copts,
203            ":android_arm64": aarch64_copts,
204            ":android_x86": gcc_x86_copts,
205            ":android_x86_64": gcc_x86_copts,
206            ":ios_armv7": apple_aarch32_copts,
207            ":ios_arm64": aarch64_copts,
208            ":ios_arm64e": aarch64_copts,
209            ":ios_sim_arm64": aarch64_copts,
210            ":ios_x86": gcc_x86_copts,
211            ":ios_x86_64": gcc_x86_copts,
212            ":watchos_armv7k": apple_aarch32_copts,
213            ":watchos_arm64_32": aarch64_copts,
214            ":watchos_x86": gcc_x86_copts,
215            ":watchos_x86_64": gcc_x86_copts,
216            ":tvos_arm64": aarch64_copts,
217            ":tvos_x86_64": gcc_x86_copts,
218            ":emscripten_wasm": wasm_copts,
219            ":emscripten_wasmsimd": wasmsimd_copts,
220            ":emscripten_wasmrelaxedsimd": wasmrelaxedsimd_copts,
221            "//conditions:default": [],
222        }) + select({
223            ":windows_x86_64_clang": ["/clang:" + opt for opt in gcc_copts],
224            ":windows_x86_64_mingw": gcc_copts,
225            ":windows_x86_64_msys": gcc_copts,
226            ":windows_x86_64": msvc_copts,
227            "//conditions:default": gcc_copts,
228        }) + select({
229            ":optimized_build": optimized_copts,
230            "//conditions:default": [],
231        }),
232        defines = defines,
233        deps = deps,
234        includes = ["include", "src"] + includes,
235        linkstatic = True,
236        linkopts = select({
237            ":linux_k8": ["-lpthread"],
238            ":linux_arm": ["-lpthread"],
239            ":linux_armeabi": ["-lpthread"],
240            ":linux_armhf": ["-lpthread"],
241            ":linux_armv7a": ["-lpthread"],
242            ":linux_arm64": ["-lpthread"],
243            ":android": ["-lm"],
244            "//conditions:default": [],
245        }),
246        textual_hdrs = hdrs,
247        visibility = visibility,
248        testonly = testonly,
249    )
250
251def xnnpack_aggregate_library(
252        name,
253        generic_deps = [],
254        x86_deps = [],
255        aarch32_ios_deps = [],
256        aarch32_nonios_deps = [],
257        aarch64_deps = [],
258        riscv_deps = [],
259        wasm_deps = [],
260        wasmsimd_deps = [],
261        wasmrelaxedsimd_deps = []):
262    """Static library that aggregates architecture-specific dependencies.
263
264    Args:
265      name: The name of the library target to define.
266      generic_deps: The list of libraries to link on all architectures.
267      x86_deps: The list of libraries to link in x86 and x86-64 builds.
268      aarch32_ios_deps: The list of libraries to link in AArch32 iOS (incl
269                        WatchOS) builds.
270      aarch32_nonios_deps: The list of libraries to link in AArch32 non-iOS
271                           builds.
272      aarch64_deps: The list of libraries to link in AArch64 builds.
273      riscv_deps: The list of libraries to link in RISC-V builds.
274      wasm_deps: The list of libraries to link in WebAssembly 1.0 builds.
275      wasmsimd_deps: The list of libraries to link in WebAssembly SIMD builds.
276      wasmrelaxedsimd_deps: The list of libraries to link in WebAssembly
277                            Relaxed SIMD builds.
278    """
279
280    native.cc_library(
281        name = name,
282        linkstatic = True,
283        deps = generic_deps + select({
284            ":linux_k8": x86_deps,
285            ":linux_arm": aarch32_nonios_deps,
286            ":linux_armeabi": aarch32_nonios_deps,
287            ":linux_armhf": aarch32_nonios_deps,
288            ":linux_armv7a": aarch32_nonios_deps,
289            ":linux_arm64": aarch64_deps,
290            ":macos_x86_64": x86_deps,
291            ":macos_arm64": aarch64_deps,
292            ":windows_x86_64_clang": x86_deps,
293            ":windows_x86_64_mingw": x86_deps,
294            ":windows_x86_64_msys": x86_deps,
295            ":windows_x86_64": x86_deps,
296            ":android_armv7": aarch32_nonios_deps,
297            ":android_arm64": aarch64_deps,
298            ":android_x86": x86_deps,
299            ":android_x86_64": x86_deps,
300            ":ios_armv7": aarch32_ios_deps,
301            ":ios_arm64": aarch64_deps,
302            ":ios_arm64e": aarch64_deps,
303            ":ios_sim_arm64": aarch64_deps,
304            ":ios_x86": x86_deps,
305            ":ios_x86_64": x86_deps,
306            ":watchos_armv7k": aarch32_ios_deps,
307            ":watchos_arm64_32": aarch64_deps,
308            ":watchos_x86": x86_deps,
309            ":watchos_x86_64": x86_deps,
310            ":tvos_arm64": aarch64_deps,
311            ":tvos_x86_64": x86_deps,
312            ":emscripten_wasm": wasm_deps,
313            ":emscripten_wasmsimd": wasmsimd_deps,
314            ":emscripten_wasmrelaxedsimd": wasmrelaxedsimd_deps,
315        }),
316    )
317
318def xnnpack_unit_test(name, srcs, copts = [], mingw_copts = [], msys_copts = [], deps = [], tags = [], automatic = True, timeout = "short", shard_count = 1):
319    """Unit test binary based on Google Test.
320
321    Args:
322      name: The name of the test target to define.
323      srcs: The list of source and header files.
324      copts: The list of additional compiler flags for the target. -I flags
325             for include/ and src/ directories of XNNPACK are always prepended
326             before these user-specified flags.
327      mingw_copts: The list of compiler flags to use with MinGW GCC compilers.
328      msys_copts: The list of compiler flags to use with MSYS (Cygwin) GCC compilers.
329      deps: The list of additional libraries to be linked. Google Test library
330            (with main() function) is always added as a dependency and does not
331            need to be explicitly specified.
332      tags: List of arbitrary text tags.
333      automatic: Whether to create the test or testable binary.
334      timeout: How long the test is expected to run before returning.
335      shard_count: Specifies the number of parallel shards to use to run the test.
336    """
337
338    if automatic:
339        native.cc_test(
340            name = name,
341            srcs = srcs,
342            copts = xnnpack_std_cxxopts() + [
343                "-Iinclude",
344                "-Isrc",
345            ] + select({
346                ":windows_x86_64_mingw": mingw_copts,
347                ":windows_x86_64_msys": msys_copts,
348                "//conditions:default": [],
349            }) + select({
350                ":windows_x86_64_clang": ["/clang:-Wno-unused-function"],
351                ":windows_x86_64_mingw": ["-Wno-unused-function"],
352                ":windows_x86_64_msys": ["-Wno-unused-function"],
353                ":windows_x86_64": [],
354                "//conditions:default": ["-Wno-unused-function"],
355            }) + copts,
356            linkopts = select({
357                ":emscripten": xnnpack_emscripten_test_linkopts(),
358                "//conditions:default": [],
359            }),
360            linkstatic = True,
361            deps = [
362                "@com_google_googletest//:gtest_main",
363            ] + deps + select({
364                ":emscripten": xnnpack_emscripten_deps(),
365                "//conditions:default": [],
366            }),
367            tags = tags,
368            timeout = timeout,
369            shard_count = shard_count,
370        )
371    else:
372        native.cc_binary(
373            name = name,
374            srcs = srcs,
375            copts = xnnpack_std_cxxopts() + [
376                "-Iinclude",
377                "-Isrc",
378            ] + select({
379                ":windows_x86_64_mingw": mingw_copts,
380                ":windows_x86_64_msys": msys_copts,
381                "//conditions:default": [],
382            }) + select({
383                ":windows_x86_64_clang": ["/clang:-Wno-unused-function"],
384                ":windows_x86_64_mingw": ["-Wno-unused-function"],
385                ":windows_x86_64_msys": ["-Wno-unused-function"],
386                ":windows_x86_64": [],
387                "//conditions:default": ["-Wno-unused-function"],
388            }) + copts,
389            linkopts = select({
390                ":emscripten": xnnpack_emscripten_test_linkopts(),
391                "//conditions:default": [],
392            }),
393            linkstatic = True,
394            deps = [
395                "@com_google_googletest//:gtest_main",
396            ] + deps + select({
397                ":emscripten": xnnpack_emscripten_deps(),
398                "//conditions:default": [],
399            }),
400            testonly = True,
401            tags = tags,
402        )
403
404def xnnpack_binary(name, srcs, copts = [], deps = []):
405    """Minimal binary
406
407    Args:
408      name: The name of the binary target to define.
409      srcs: The list of source and header files.
410      copts: The list of additional compiler flags for the target. -I flags
411             for include/ and src/ directories of XNNPACK are always prepended
412             before these user-specified flags.
413      deps: The list of libraries to be linked.
414    """
415    native.cc_binary(
416        name = name,
417        srcs = srcs,
418        copts = [
419            "-Iinclude",
420            "-Isrc",
421        ] + copts,
422        linkopts = select({
423            ":emscripten": xnnpack_emscripten_minimal_linkopts(),
424            "//conditions:default": [],
425        }),
426        linkstatic = True,
427        deps = deps,
428    )
429
430def xnnpack_benchmark(name, srcs, copts = [], deps = [], tags = []):
431    """Microbenchmark binary based on Google Benchmark
432
433    Args:
434      name: The name of the binary target to define.
435      srcs: The list of source and header files.
436      copts: The list of additional compiler flags for the target. -I flags
437             for include/ and src/ directories of XNNPACK are always prepended
438             before these user-specified flags.
439      deps: The list of additional libraries to be linked. Google Benchmark
440            library is always added as a dependency and does not need to be
441            explicitly specified.
442    """
443    native.cc_binary(
444        name = name,
445        srcs = srcs,
446        copts = xnnpack_std_cxxopts() + [
447            "-Iinclude",
448            "-Isrc",
449        ] + select({
450            ":windows_x86_64_clang": ["/clang:-Wno-unused-function"],
451            ":windows_x86_64_mingw": ["-Wno-unused-function"],
452            ":windows_x86_64_msys": ["-Wno-unused-function"],
453            ":windows_x86_64": [],
454            "//conditions:default": ["-Wno-unused-function"],
455        }) + copts,
456        linkopts = select({
457            ":emscripten": xnnpack_emscripten_benchmark_linkopts(),
458            ":windows_x86_64_mingw": ["-lshlwapi"],
459            ":windows_x86_64_msys": ["-lshlwapi"],
460            "//conditions:default": [],
461        }),
462        linkstatic = True,
463        deps = [
464            "@com_google_benchmark//:benchmark",
465        ] + deps + select({
466            ":emscripten": xnnpack_emscripten_deps(),
467            "//conditions:default": [],
468        }),
469	tags = tags,
470    )
471