1212include (${EXECUTORCH_ROOT} /tools/cmake/Utils.cmake)
1313
1414function (gen_selected_ops)
15-   set (arg_names LIB_NAME OPS_SCHEMA_YAML ROOT_OPS INCLUDE_ALL_OPS)
15+   set (arg_names LIB_NAME OPS_SCHEMA_YAML ROOT_OPS INCLUDE_ALL_OPS DTYPE_SELECT )
1616  cmake_parse_arguments (GEN ""  ""  "${arg_names} "  ${ARGN} )
1717
1818  message (STATUS  "Generating operator lib:" )
1919  message (STATUS  "  LIB_NAME: ${GEN_LIB_NAME} " )
2020  message (STATUS  "  OPS_SCHEMA_YAML: ${GEN_OPS_SCHEMA_YAML} " )
2121  message (STATUS  "  ROOT_OPS: ${GEN_ROOT_OPS} " )
2222  message (STATUS  "  INCLUDE_ALL_OPS: ${GEN_INCLUDE_ALL_OPS} " )
23+   message (STATUS  "  DTYPE_SELECT: ${GEN_DTYPE_SELECT} " )
2324
2425  set (_oplist_yaml
2526      ${CMAKE_CURRENT_BINARY_DIR} /${GEN_LIB_NAME} /selected_operators.yaml
2627  )
28+ 
2729  file (MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} /${GEN_LIB_NAME} )
2830
2931  file (GLOB_RECURSE _codegen_tools_srcs "${EXECUTORCH_ROOT} /codegen/tools/*.py" )
@@ -53,6 +55,23 @@ function(gen_selected_ops)
5355    WORKING_DIRECTORY  ${EXECUTORCH_ROOT} 
5456  )
5557
58+   if (GEN_DTYPE_SELECT)
59+     set (_opvariant_h
60+       ${CMAKE_CURRENT_BINARY_DIR} /${GEN_LIB_NAME} /selected_op_variants.h
61+     )
62+     set (_gen_opvariant_command "${PYTHON_EXECUTABLE} "  -m codegen.tools.gen_selected_op_variants
63+                           --yaml-file=${_oplist_yaml} 
64+                           --output -dir=${CMAKE_CURRENT_BINARY_DIR} /${GEN_LIB_NAME} /
65+     )
66+     message ("Command - ${_gen_opvariant_command} " )
67+     add_custom_command (
68+       COMMENT  "Generating selected_op_variants.h for ${GEN_LIB_NAME} " 
69+       OUTPUT  ${_opvariant_h} 
70+       COMMAND  ${_gen_opvariant_command} 
71+       DEPENDS  ${_optlist_yaml}  ${_codegen_tools_srcs} 
72+       WORKING_DIRECTORY  ${EXECUTORCH_ROOT} 
73+     )
74+   endif ()
5675endfunction ()
5776
5877# Codegen for registering kernels. Kernels are defined in functions_yaml and 
@@ -62,14 +81,15 @@ endfunction()
6281# functions_yaml CUSTOM_OPS_YAML custom_ops_yaml ) 
6382function (generate_bindings_for_kernels)
6483  set (options  ADD_EXCEPTION_BOUNDARY)
65-   set (arg_names LIB_NAME FUNCTIONS_YAML CUSTOM_OPS_YAML)
84+   set (arg_names LIB_NAME FUNCTIONS_YAML CUSTOM_OPS_YAML DTYPE_SELECT )
6685  cmake_parse_arguments (GEN "${options} "  "${arg_names} "  ""  ${ARGN} )
6786
6887  message (STATUS  "Generating kernel bindings:" )
6988  message (STATUS  "  LIB_NAME: ${GEN_LIB_NAME} " )
7089  message (STATUS  "  FUNCTIONS_YAML: ${GEN_FUNCTIONS_YAML} " )
7190  message (STATUS  "  CUSTOM_OPS_YAML: ${GEN_CUSTOM_OPS_YAML} " )
7291  message (STATUS  "  ADD_EXCEPTION_BOUNDARY: ${GEN_ADD_EXCEPTION_BOUNDARY} " )
92+   message (STATUS  "  DTYPE_SELECT: ${GEN_DTYPE_SELECT} " )
7393
7494  # Command to generate selected_operators.yaml from custom_ops.yaml. 
7595  file (GLOB_RECURSE _codegen_templates "${EXECUTORCH_ROOT} /codegen/templates/*" )
@@ -78,6 +98,13 @@ function(generate_bindings_for_kernels)
7898  # By default selective build output is selected_operators.yaml 
7999  set (_oplist_yaml ${_out_dir} /selected_operators.yaml)
80100
101+   # If dtype selective build is enable, force header file to be preserved 
102+   if (GEN_DTYPE_SELECT)
103+     set (_opvariant_h ${_out_dir} /selected_op_variants.h)
104+   else ()
105+     set (_opvariant_h "" )
106+   endif ()
107+ 
81108  # Command to codegen C++ wrappers to register custom ops to both PyTorch and 
82109  # Executorch runtime. 
83110  execute_process (
@@ -108,6 +135,10 @@ function(generate_bindings_for_kernels)
108135      ${_out_dir} /Functions.h ${_out_dir} /NativeFunctions.h
109136  )
110137
138+   if (GEN_DTYPE_SELECT)
139+     list (APPEND  _gen_command_sources ${_out_dir} /selected_op_variants.h)
140+   endif ()
141+ 
111142  if (GEN_FUNCTIONS_YAML)
112143    list (APPEND  _gen_command --functions-yaml-path =${GEN_FUNCTIONS_YAML} )
113144  endif ()
@@ -122,8 +153,9 @@ function(generate_bindings_for_kernels)
122153    COMMENT  "Generating code for kernel registration" 
123154    OUTPUT  ${_gen_command_sources} 
124155    COMMAND  ${_gen_command} 
125-     DEPENDS  ${_oplist_yaml}  ${GEN_CUSTOM_OPS_YAML}  ${GEN_FUNCTIONS_YAML} 
126-             ${_codegen_templates}  ${_torchgen_srcs} 
156+     DEPENDS  ${_oplist_yaml}  ${_opvariants_h}  ${GEN_CUSTOM_OPS_YAML} 
157+             ${GEN_FUNCTIONS_YAML}  ${_codegen_templates} 
158+ 	    ${_torchgen_srcs} 
127159    WORKING_DIRECTORY  ${EXECUTORCH_ROOT} 
128160  )
129161  # Make generated file list available in parent scope 
@@ -165,29 +197,43 @@ endfunction()
165197
166198# Generate a runtime lib for registering operators in Executorch 
167199function (gen_operators_lib)
168-   set (multi_arg_names LIB_NAME KERNEL_LIBS DEPS)
200+   set (multi_arg_names LIB_NAME KERNEL_LIBS DEPS DTYPE_SELECT )
169201  cmake_parse_arguments (GEN ""  ""  "${multi_arg_names} "  ${ARGN} )
170202
171203  message (STATUS  "Generating operator lib:" )
172204  message (STATUS  "  LIB_NAME: ${GEN_LIB_NAME} " )
173205  message (STATUS  "  KERNEL_LIBS: ${GEN_KERNEL_LIBS} " )
174206  message (STATUS  "  DEPS: ${GEN_DEPS} " )
207+   message (STATUS  "  DTYPE_SELECT: ${GEN_DTYPE_SELECT} " )
175208
176209  set (_out_dir ${CMAKE_CURRENT_BINARY_DIR} /${GEN_LIB_NAME} )
177210
178211  add_library (${GEN_LIB_NAME} )
179-   target_sources (
180-     ${GEN_LIB_NAME} 
181-     PRIVATE  ${_out_dir} /RegisterCodegenUnboxedKernelsEverything.cpp
182-             ${_out_dir} /Functions.h ${_out_dir} /NativeFunctions.h
183-   )
212+   if (GEN_DTYPE_SELECT)
213+     target_sources (
214+       ${GEN_LIB_NAME} 
215+       PRIVATE  ${_out_dir} /RegisterCodegenUnboxedKernelsEverything.cpp
216+               ${_out_dir} /Functions.h ${_out_dir} /NativeFunctions.h
217+               ${_out_dir} /selected_op_variants.h
218+     )
219+   else ()
220+     target_sources (
221+       ${GEN_LIB_NAME} 
222+       PRIVATE  ${_out_dir} /RegisterCodegenUnboxedKernelsEverything.cpp
223+               ${_out_dir} /Functions.h ${_out_dir} /NativeFunctions.h
224+     )
225+   endif ()
226+ 
184227  target_link_libraries (${GEN_LIB_NAME}  PRIVATE  ${GEN_DEPS} )
185228  if (GEN_KERNEL_LIBS)
186229    target_link_libraries (${GEN_LIB_NAME}  PUBLIC  ${GEN_KERNEL_LIBS} )
187230  endif ()
188231
189232  target_link_options_shared_lib(${GEN_LIB_NAME} )
190233  set (_generated_headers ${_out_dir} /Functions.h ${_out_dir} /NativeFunctions.h)
234+   if (GEN_DTYPE_SELECT)
235+     list (APPEND  _generated_headers ${_out_dir} /selected_op_variants.h)
236+   endif ()
191237  set_target_properties (
192238    ${GEN_LIB_NAME}  PROPERTIES PUBLIC_HEADER  "${_generated_headers} " 
193239  )
0 commit comments