Lun Dong | 6b5469b | 2022-09-03 11:19:49 -0700 | [diff] [blame] | 1 | # springbok_static_module() |
| 2 | # |
| 3 | # A modified version of iree_static_linker_test to apply common iree-compile flags |
| 4 | # Parameters: |
| 5 | # NAME: Name of target. |
| 6 | # SRC: Source file to compile into a bytecode module. Support relative path. |
| 7 | # C_IDENTIFIER: Identifier to use for generate c embed code. |
| 8 | # FLAGS: Flags to pass to the translation tool (list of strings). |
| 9 | # RVV_OFF: Indicate RVV is OFF (default: ON) |
| 10 | # EMITC: Uses EmitC to output C code instead of VM bytecode. |
| 11 | # |
| 12 | # Examples: |
| 13 | # springbok_static_module( |
| 14 | # NAME |
| 15 | # daredevel_bytecode_module_static |
| 16 | # SRC |
| 17 | # "daredevil_quant.tflite" |
| 18 | # C_IDENTIFIER |
| 19 | # "daredevil_bytecode_module_static" |
| 20 | # FLAGS |
| 21 | # "-iree-input-type=tosa" |
| 22 | # ) |
| 23 | # |
| 24 | # springbok_static_module( |
| 25 | # NAME |
| 26 | # simple_float_mul_c_module_static |
| 27 | # SRC |
| 28 | # "simple_float_mul.mlir" |
| 29 | # C_IDENTIFIER |
| 30 | # "simple_float_mul" |
| 31 | # FLAGS |
| 32 | # "-iree-input-type=mhlo" |
| 33 | # RVV_OFF |
| 34 | # EMITC |
| 35 | # ) |
| 36 | # |
| 37 | function(springbok_static_module) |
| 38 | cmake_parse_arguments( |
| 39 | _RULE |
| 40 | "RVV_OFF;EMITC" |
| 41 | "NAME;SRC;C_IDENTIFIER" |
| 42 | "FLAGS" |
| 43 | ${ARGN} |
| 44 | ) |
| 45 | |
| 46 | set(_MLIR_SRC "${_RULE_SRC}") |
| 47 | string(FIND "${_RULE_SRC}" ".tflite" _IS_TFLITE REVERSE) |
| 48 | if(${_IS_TFLITE} GREATER 0) |
| 49 | iree_get_executable_path(IREE_IMPORT_TFLITE_TOOL "iree-import-tflite") |
| 50 | set(_MLIR_SRC "${CMAKE_CURRENT_BINARY_DIR}/${_RULE_NAME}.mlir") |
| 51 | get_filename_component(_SRC_PATH "${_RULE_SRC}" REALPATH) |
| 52 | set(_ARGS "${_SRC_PATH}") |
Cindy Liu | fc8e73f | 2022-09-14 11:05:24 -0700 | [diff] [blame] | 53 | list(APPEND _ARGS "--output-format=mlir-ir") |
Lun Dong | 6b5469b | 2022-09-03 11:19:49 -0700 | [diff] [blame] | 54 | list(APPEND _ARGS "-o") |
| 55 | list(APPEND _ARGS "${_RULE_NAME}.mlir") |
| 56 | # Only add the custom_command here. The output is passed to |
| 57 | # iree_bytecode_module as the source. |
| 58 | add_custom_command( |
| 59 | OUTPUT |
| 60 | "${_RULE_NAME}.mlir" |
| 61 | COMMAND |
| 62 | ${IREE_IMPORT_TFLITE_TOOL} |
| 63 | ${_ARGS} |
| 64 | DEPENDS |
| 65 | ${IREE_IMPORT_TFLITE_TOOL} |
| 66 | ) |
| 67 | endif() |
| 68 | |
| 69 | iree_get_executable_path(_COMPILER_TOOL "iree-compile") |
| 70 | iree_package_name(_PACKAGE_NAME) |
| 71 | iree_package_ns(_PACKAGE_NS) |
| 72 | |
| 73 | set(_CPU_FEATURES "+m,+f,+zvl512b,+zve32x") |
| 74 | if (${_RULE_RVV_OFF}) |
| 75 | set(_CPU_FEATURES "+m,+f") |
| 76 | endif() |
| 77 | |
| 78 | # Set common iree-compile flags |
| 79 | set(_COMPILER_ARGS ${_RULE_FLAGS}) |
| 80 | list(APPEND _COMPILER_ARGS "--iree-hal-target-backends=llvm-cpu") |
| 81 | list(APPEND _COMPILER_ARGS "-iree-llvm-debug-symbols=false") |
| 82 | list(APPEND _COMPILER_ARGS "-iree-vm-bytecode-module-strip-source-map=true") |
| 83 | list(APPEND _COMPILER_ARGS "-iree-vm-emit-polyglot-zip=false") |
| 84 | list(APPEND _COMPILER_ARGS "-iree-llvm-target-triple=riscv32-pc-linux-elf") |
| 85 | list(APPEND _COMPILER_ARGS "-iree-llvm-target-cpu=generic-rv32") |
| 86 | list(APPEND _COMPILER_ARGS "-iree-llvm-target-cpu-features=${_CPU_FEATURES}") |
| 87 | list(APPEND _COMPILER_ARGS "-iree-llvm-target-abi=ilp32") |
| 88 | list(APPEND _COMPILER_ARGS "-iree-llvm-link-embedded=false") |
| 89 | |
| 90 | if(_RULE_EMITC) |
Lun Dong | 406ad51 | 2022-09-06 23:28:12 -0700 | [diff] [blame] | 91 | # TODO(b/245584726): iree_c_module causes long build time for some models |
Lun Dong | 6b5469b | 2022-09-03 11:19:49 -0700 | [diff] [blame] | 92 | set(_O_FILE_NAME "${_RULE_NAME}_c.o") |
| 93 | set(_H_FILE_NAME "${_RULE_NAME}_emitc.h") |
| 94 | set(_MODULE_NAME "${_RULE_NAME}_emitc") |
Lun Dong | 406ad51 | 2022-09-06 23:28:12 -0700 | [diff] [blame] | 95 | |
| 96 | get_filename_component(_MLIR_SRC "${_MLIR_SRC}" REALPATH) |
| 97 | list(APPEND _COMPILER_ARGS "-output-format=vm-c") |
| 98 | list(APPEND _COMPILER_ARGS "-iree-llvm-link-static") |
| 99 | list(APPEND _COMPILER_ARGS "-iree-llvm-static-library-output-path=${_O_FILE_NAME}") |
| 100 | list(APPEND _COMPILER_ARGS "${_MLIR_SRC}") |
| 101 | list(APPEND _COMPILER_ARGS "-o") |
| 102 | list(APPEND _COMPILER_ARGS "${_H_FILE_NAME}") |
| 103 | |
| 104 | set(_OUTPUT_FILES "${_H_FILE_NAME}") |
| 105 | string(REPLACE ".o" ".h" _STATIC_HDR_PATH "${_O_FILE_NAME}") |
| 106 | list(APPEND _OUTPUT_FILES "${_O_FILE_NAME}" "${_STATIC_HDR_PATH}") |
| 107 | |
| 108 | add_custom_command( |
| 109 | OUTPUT ${_OUTPUT_FILES} |
| 110 | COMMAND ${_COMPILER_TOOL} ${_COMPILER_ARGS} |
| 111 | DEPENDS ${_COMPILER_TOOL} ${_MLIR_SRC} |
Lun Dong | 6b5469b | 2022-09-03 11:19:49 -0700 | [diff] [blame] | 112 | ) |
Lun Dong | 406ad51 | 2022-09-06 23:28:12 -0700 | [diff] [blame] | 113 | |
| 114 | set(_EMITC_LIB_NAME "${_PACKAGE_NAME}_${_MODULE_NAME}") |
| 115 | add_library(${_EMITC_LIB_NAME} |
| 116 | STATIC |
| 117 | ${_H_FILE_NAME} |
| 118 | ) |
| 119 | target_compile_definitions(${_EMITC_LIB_NAME} PUBLIC EMITC_IMPLEMENTATION=\"${_H_FILE_NAME}\") |
| 120 | SET_TARGET_PROPERTIES( |
| 121 | ${_EMITC_LIB_NAME} |
| 122 | PROPERTIES |
| 123 | LINKER_LANGUAGE C |
| 124 | ) |
| 125 | add_library(${_PACKAGE_NS}::${_MODULE_NAME} ALIAS ${_EMITC_LIB_NAME}) |
| 126 | |
Lun Dong | 6b5469b | 2022-09-03 11:19:49 -0700 | [diff] [blame] | 127 | else() # bytecode module path |
| 128 | # Generate the embed data with the bytecode module. |
| 129 | set(_O_FILE_NAME "${_RULE_NAME}.o") |
| 130 | set(_H_FILE_NAME "${_RULE_NAME}.h") |
| 131 | set(_MODULE_NAME "${_RULE_NAME}") |
| 132 | if(NOT _RULE_C_IDENTIFIER) |
| 133 | set(_RULE_C_IDENTIFIER "${_PACKAGE_NAME}_${_RULE_NAME}") |
| 134 | endif() |
| 135 | |
| 136 | iree_bytecode_module( |
| 137 | NAME |
| 138 | ${_MODULE_NAME} |
| 139 | SRC |
| 140 | "${_MLIR_SRC}" |
| 141 | FLAGS |
| 142 | ${_COMPILER_ARGS} |
| 143 | STATIC_LIB_PATH |
| 144 | "${_O_FILE_NAME}" |
| 145 | C_IDENTIFIER |
| 146 | "${_RULE_C_IDENTIFIER}" |
| 147 | PUBLIC |
| 148 | ) |
| 149 | endif(_RULE_EMITC) |
| 150 | |
| 151 | set(_NAME "${_RULE_NAME}_lib") |
| 152 | set(_LIB_NAME "${_PACKAGE_NAME}_${_NAME}") |
| 153 | add_library(${_LIB_NAME} |
| 154 | STATIC |
| 155 | ${_O_FILE_NAME} |
| 156 | ) |
| 157 | SET_TARGET_PROPERTIES( |
| 158 | ${_LIB_NAME} |
| 159 | PROPERTIES |
| 160 | LINKER_LANGUAGE C |
| 161 | ) |
| 162 | |
| 163 | # Set alias for this static library to be used later in the function. |
| 164 | add_library(${_PACKAGE_NS}::${_NAME} ALIAS ${_LIB_NAME}) |
| 165 | endfunction() |