|
1 | 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
2 | 2 | # All rights reserved. |
| 3 | +# Copyright 2026 Arm Limited and/or its affiliates. |
3 | 4 | # |
4 | 5 | # This source code is licensed under the BSD-style license found in the |
5 | 6 | # LICENSE file in the root directory of this source tree. |
@@ -150,6 +151,81 @@ function(gen_selected_max_kernel_num) |
150 | 151 | ) |
151 | 152 | endfunction() |
152 | 153 |
|
| 154 | +# Generate a prim ops registration library. By default the library includes only |
| 155 | +# operators from SELECTED_OPS_YAML. INCLUDE_ALL_OPS disables prim-op selective |
| 156 | +# build and registers every prim op. |
| 157 | +function(gen_selected_prim_ops_lib) |
| 158 | + # Parse arguments. |
| 159 | + set(options INCLUDE_ALL_OPS) |
| 160 | + set(one_value_args LIB_NAME SELECTED_OPS_YAML) |
| 161 | + set(multi_value_args DEPS) |
| 162 | + cmake_parse_arguments( |
| 163 | + GEN "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN} |
| 164 | + ) |
| 165 | + |
| 166 | + message(STATUS "Generating selected prim ops lib:") |
| 167 | + message(STATUS " LIB_NAME: ${GEN_LIB_NAME}") |
| 168 | + message(STATUS " SELECTED_OPS_YAML: ${GEN_SELECTED_OPS_YAML}") |
| 169 | + message(STATUS " INCLUDE_ALL_OPS: ${GEN_INCLUDE_ALL_OPS}") |
| 170 | + message(STATUS " DEPS: ${GEN_DEPS}") |
| 171 | + |
| 172 | + if(NOT GEN_LIB_NAME) |
| 173 | + message(FATAL_ERROR "gen_selected_prim_ops_lib: LIB_NAME is required") |
| 174 | + endif() |
| 175 | + if(NOT GEN_INCLUDE_ALL_OPS AND NOT GEN_SELECTED_OPS_YAML) |
| 176 | + message( |
| 177 | + FATAL_ERROR |
| 178 | + "gen_selected_prim_ops_lib: SELECTED_OPS_YAML is required unless INCLUDE_ALL_OPS is set" |
| 179 | + ) |
| 180 | + endif() |
| 181 | + |
| 182 | + set(_out_dir ${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME}) |
| 183 | + file(MAKE_DIRECTORY ${_out_dir}) |
| 184 | + |
| 185 | + # Generate selected operator list if needed. |
| 186 | + set(_sources ${EXECUTORCH_ROOT}/kernels/prim_ops/register_prim_ops.cpp) |
| 187 | + if(NOT GEN_INCLUDE_ALL_OPS) |
| 188 | + set(_selected_prim_ops_header ${_out_dir}/selected_prim_ops.h) |
| 189 | + set(_gen_selected_prim_ops_script |
| 190 | + ${EXECUTORCH_ROOT}/codegen/tools/gen_selected_prim_ops.py |
| 191 | + ) |
| 192 | + set(_gen_selected_prim_ops_command |
| 193 | + "${PYTHON_EXECUTABLE}" -m codegen.tools.gen_selected_prim_ops |
| 194 | + --op-selection-yaml-path=${GEN_SELECTED_OPS_YAML} |
| 195 | + --output-dir=${_out_dir} |
| 196 | + ) |
| 197 | + add_custom_command( |
| 198 | + COMMENT "Generating selected_prim_ops.h for ${GEN_LIB_NAME}" |
| 199 | + OUTPUT ${_selected_prim_ops_header} |
| 200 | + COMMAND ${_gen_selected_prim_ops_command} |
| 201 | + DEPENDS ${GEN_SELECTED_OPS_YAML} ${_gen_selected_prim_ops_script} |
| 202 | + WORKING_DIRECTORY ${EXECUTORCH_ROOT} |
| 203 | + ) |
| 204 | + list(APPEND _sources ${_selected_prim_ops_header}) |
| 205 | + endif() |
| 206 | + |
| 207 | + # Add prim ops registration library. |
| 208 | + add_library(${GEN_LIB_NAME} ${_sources}) |
| 209 | + target_include_directories( |
| 210 | + ${GEN_LIB_NAME} PRIVATE ${EXECUTORCH_ROOT}/kernels/prim_ops ${_out_dir} |
| 211 | + ) |
| 212 | + if(NOT GEN_INCLUDE_ALL_OPS) |
| 213 | + target_compile_definitions( |
| 214 | + ${GEN_LIB_NAME} PRIVATE ET_PRIM_OPS_SELECTIVE_BUILD |
| 215 | + EXECUTORCH_ENABLE_PRIM_OPS_SELECTIVE_BUILD |
| 216 | + ) |
| 217 | + endif() |
| 218 | + target_link_libraries(${GEN_LIB_NAME} PRIVATE ${GEN_DEPS}) |
| 219 | + executorch_target_link_options_shared_lib(${GEN_LIB_NAME}) |
| 220 | + |
| 221 | + # Add prim ops kernel library. |
| 222 | + add_library( |
| 223 | + ${GEN_LIB_NAME}_impl ${EXECUTORCH_ROOT}/kernels/prim_ops/et_copy_index.cpp |
| 224 | + ${EXECUTORCH_ROOT}/kernels/prim_ops/et_view.cpp |
| 225 | + ) |
| 226 | + target_link_libraries(${GEN_LIB_NAME}_impl PRIVATE ${GEN_DEPS}) |
| 227 | +endfunction() |
| 228 | + |
153 | 229 | # Codegen for registering kernels. Kernels are defined in functions_yaml and |
154 | 230 | # custom_ops_yaml. |
155 | 231 | # |
|
0 commit comments