xref: /aosp_15_r20/external/executorch/kernels/optimized/lib_defs.bzl (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1load("@fbsource//tools/build_defs:default_platform_defs.bzl", "DEVSERVER_PLATFORM_REGEX")
2load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
3load("@fbsource//xplat/executorch/backends/xnnpack/third-party:third_party_libs.bzl", "third_party_dep")
4load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
5load(
6    "@fbsource//xplat/executorch/kernels/portable:op_registration_util.bzl",
7    "get_compiler_optimization_flags",
8)
9
10# Because vec exists as a collection of header files, compile and preprocessor
11# flags applied to the vec target do not have any effect, since no compilation
12# actually occurs for the target.
13#
14# Targets using the vec library must therefore call the get_vec_*_flags
15# functions in order to declare the required compiler flags needed in order to
16# access CPU vector intrinsics.
17
18def get_vec_preprocessor_flags():
19    if not runtime.is_oss:
20        # various ovr_configs are not available in oss
21        preprocessor_flags = select({
22            "ovr_config//os:linux-x86_64": [
23                "-DET_BUILD_ARM_VEC256_WITH_SLEEF",
24            ] if not runtime.is_oss else [],
25            "ovr_config//os:iphoneos-arm64": [
26                "-DET_BUILD_ARM_VEC256_WITH_SLEEF",
27            ] if not runtime.is_oss else [],
28            "ovr_config//os:macos-arm64": [
29                "-DET_BUILD_ARM_VEC256_WITH_SLEEF",
30            ] if not runtime.is_oss else [],
31            "ovr_config//os:android-arm64": [
32                "-DET_BUILD_ARM_VEC256_WITH_SLEEF",
33            ] if not runtime.is_oss else [],
34            "DEFAULT": [],
35        })
36        return preprocessor_flags
37    return []
38
39def get_vec_deps():
40    if not runtime.is_oss:
41        # various ovr_configs are not available in oss
42        deps = select({
43            "ovr_config//os:iphoneos-arm64": [
44                "fbsource//third-party/sleef:sleef_arm",
45            ] if not runtime.is_oss else [],
46            "ovr_config//os:macos-arm64": [
47                "fbsource//third-party/sleef:sleef_arm",
48            ] if not runtime.is_oss else [],
49            "ovr_config//os:android-arm64": [
50                "fbsource//third-party/sleef:sleef_arm",
51            ] if not runtime.is_oss else [],
52            "DEFAULT": [],
53        })
54        return deps
55    return []
56
57def get_vec_cxx_preprocessor_flags():
58    preprocessor_flags = [
59        (
60            DEVSERVER_PLATFORM_REGEX,
61            [
62                "-DCPU_CAPABILITY_AVX2",
63            ],
64        ),
65    ]
66    return preprocessor_flags
67
68def get_vec_fbcode_preprocessor_flags():
69    preprocessor_flags = [
70        "-DCPU_CAPABILITY_AVX2",
71    ]
72    return preprocessor_flags
73
74def get_apple_framework_deps_kwargs(is_fbcode):
75    # various ovr_configs are not available in oss
76    if not runtime.is_oss and not is_fbcode:
77        # Jump through few hoops since 'frameworks' is not a valid kwarg
78        # for some buck rules
79        frameworks = {'frameworks': select({
80            "DEFAULT": [],
81            "ovr_config//os:iphoneos": ["$SDKROOT/System/Library/Frameworks/Accelerate.framework"],
82            "ovr_config//os:macos-arm64": ["$SDKROOT/System/Library/Frameworks/Accelerate.framework"],
83            "ovr_config//os:macos-x86_64": ["$SDKROOT/System/Library/Frameworks/Accelerate.framework"],
84        })}
85        return frameworks
86    return {'fbobjc_frameworks': ["Accelerate"]}
87
88def get_preprocessor_flags():
89    # various ovr_configs are not available in oss
90    preprocessor_flags = select({
91      ":linux-x86_64": [
92          "-DET_BUILD_WITH_BLAS",
93      ] if not runtime.is_oss else [],
94      "DEFAULT": [],
95    })
96
97    if not runtime.is_oss:
98        # various ovr_configs are not available in oss
99        additional_preprocessor_flags = select({
100            "ovr_config//os:iphoneos": [
101                "-DET_BUILD_WITH_BLAS",
102                "-DET_BUILD_FOR_APPLE",
103            ] if not runtime.is_oss else [],
104            "ovr_config//os:macos-arm64": [
105                "-DET_BUILD_WITH_BLAS",
106                "-DET_BUILD_FOR_APPLE",
107            ] if not runtime.is_oss else [],
108            "ovr_config//os:macos-x86_64": [
109                "-DET_BUILD_WITH_BLAS",
110                "-DET_BUILD_FOR_APPLE",
111            ] if not runtime.is_oss else [],
112            "DEFAULT": [],
113        })
114        preprocessor_flags = preprocessor_flags + additional_preprocessor_flags
115    return preprocessor_flags
116
117
118# Currently, having a dependency on fbsource//third-party/sleef:sleef may cause
119# duplicate symbol errors when linking fbcode targets in opt mode that also
120# depend on ATen. This is because ATen accesses sleef via the third-party folder
121# in caffe2 (caffe2/third-party//sleef:sleef).
122# TODO(ssjia): Enable -DCPU_CAPABILITY_AVX2 in fbcode, which requires sleef.
123def define_libs(is_fbcode=False):
124    runtime.cxx_library(
125        name = "libvec",
126        srcs = [],
127        exported_headers = native.glob([
128            "vec/**/*.h",
129        ]),
130        header_namespace = "executorch/kernels/optimized",
131        visibility = [
132            "//executorch/...",
133            "@EXECUTORCH_CLIENTS",
134        ],
135        cxx_platform_deps = select({
136            "DEFAULT": [
137                (
138                    DEVSERVER_PLATFORM_REGEX,
139                    [
140                        "fbsource//third-party/sleef:sleef",
141                    ],
142                ),
143            ],
144            "ovr_config//cpu:arm64": [
145                (
146                    DEVSERVER_PLATFORM_REGEX,
147                    [
148                        "fbsource//third-party/sleef:sleef_arm",
149                    ],
150                ),
151            ],
152        }),
153        fbandroid_platform_deps = [
154            (
155                "^android-arm64.*$",
156                [
157                    "fbsource//third-party/sleef:sleef_arm",
158                ],
159            ),
160        ],
161    )
162
163    runtime.cxx_library(
164        name = "libutils",
165        srcs = [],
166        exported_headers = native.glob([
167            "utils/**/*.h",
168        ]),
169        header_namespace = "executorch/kernels/optimized",
170        visibility = [
171            "//executorch/...",
172            "@EXECUTORCH_CLIENTS",
173        ],
174        exported_deps = [
175            # Needed to access the ET_INLINE macro
176            "//executorch/runtime/platform:compiler",
177        ],
178    )
179
180    # OSS doesn't have ovr_config//os:linux-x86_64
181    fb_native.config_setting(
182        name = "linux-x86_64",
183        constraint_values = [
184            "ovr_config//os/constraints:linux",
185            "ovr_config//cpu/constraints:x86_64",
186        ],
187    )
188
189    LIBBLAS_DEPS = [third_party_dep("cpuinfo")]
190
191    for libblas_name, mkl_dep in [("libblas", "fbsource//third-party/mkl:mkl_lp64_omp"), ("libblas_mkl_noomp", "fbsource//third-party/mkl:mkl")]:
192        runtime.cxx_library(
193            name = libblas_name,
194            srcs = native.glob([
195                "blas/**/*.cpp",
196            ]),
197            exported_headers = native.glob([
198                "blas/**/*.h",
199            ]),
200            compiler_flags = get_compiler_optimization_flags(),
201            header_namespace = "executorch/kernels/optimized",
202            visibility = [
203                "//executorch/...",
204                "@EXECUTORCH_CLIENTS",
205            ],
206            preprocessor_flags = get_preprocessor_flags(),
207            fbandroid_platform_preprocessor_flags = [
208                (
209                    "^android-arm64.*$",
210                    [
211                        "-DET_BUILD_WITH_BLAS",
212                    ],
213                ),
214            ],
215            fbandroid_platform_deps = [
216                (
217                    "^android-arm64.*$",
218                    [
219                        "fbsource//arvr/third-party/eigen:eigen3_blas",
220                    ],
221                ),
222            ],
223            fbobjc_compiler_flags = [
224                "-march=armv8+bf16",
225            ],
226            fbobjc_exported_preprocessor_flags = [
227                "-DET_BUILD_WITH_BLAS",
228                "-DET_BUILD_FOR_APPLE",
229            ],
230            deps = select({
231                ":linux-x86_64": [mkl_dep] if not runtime.is_oss else [],
232                "DEFAULT": [],
233            }) + LIBBLAS_DEPS,
234            exported_deps = [
235                "//executorch/extension/parallel:thread_parallel",
236                "//executorch/kernels/optimized:libutils",
237                "//executorch/runtime/core/exec_aten:lib",
238            ],
239            **get_apple_framework_deps_kwargs(is_fbcode),
240        )
241