1# Ensure the ${model} is available at ${final_path}.
2#
3function(tfgetmodel model final_path)
4  if (IS_ABSOLUTE ${model})
5    set(${final_path} ${model} PARENT_SCOPE)
6  else()
7    set(${final_path}
8      ${CMAKE_CURRENT_SOURCE_DIR}/${model} PARENT_SCOPE)
9  endif()
10endfunction()
11
12# Run the tensorflow compiler (saved_model_cli) on the saved model in the
13# ${model} directory, looking for the ${tag_set} tag set, and the SignatureDef
14# ${signature_def_key}.
15# Produce a pair of files called ${fname}.h and  ${fname}.o in the
16# ${CMAKE_CURRENT_BINARY_DIR}. The generated header will define a C++ class
17# called ${cpp_class} - which may be a namespace-qualified class name.
18function(tfcompile model tag_set signature_def_key fname cpp_class)
19  tfgetmodel(${model} LLVM_ML_MODELS_ABSOLUTE)
20  message("Using model at " ${LLVM_ML_MODELS_ABSOLUTE})
21  set(prefix ${CMAKE_CURRENT_BINARY_DIR}/${fname})
22  set(obj_file ${prefix}.o)
23  set(hdr_file ${prefix}.h)
24  add_custom_command(OUTPUT ${obj_file} ${hdr_file}
25    COMMAND "XLA_FLAGS=\"--xla_cpu_multi_thread_eigen=false\"" ${TENSORFLOW_AOT_COMPILER} aot_compile_cpu
26          --dir ${LLVM_ML_MODELS_ABSOLUTE}
27          --tag_set ${tag_set}
28          --signature_def_key ${signature_def_key}
29          --output_prefix ${prefix}
30          --cpp_class ${cpp_class}
31          --target_triple ${LLVM_HOST_TRIPLE}
32  )
33
34  # Aggregate the objects so that results of different tfcompile calls may be
35  # grouped into one target.
36  set(GENERATED_OBJS ${GENERATED_OBJS} ${obj_file} PARENT_SCOPE)
37  set_source_files_properties(${obj_file} PROPERTIES
38    GENERATED 1 EXTERNAL_OBJECT 1)
39
40  set(GENERATED_HEADERS ${GENERATED_HEADERS} ${hdr_file} PARENT_SCOPE)
41  set_source_files_properties(${hdr_file} PROPERTIES
42    GENERATED 1)
43
44endfunction()
45