|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | +import collections |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import logging |
| 8 | + |
| 9 | +BANNER = "Auto-generated by generate-wrappers.py script. Do not modify" |
| 10 | +WRAPPER_SRC_NAMES = { |
| 11 | + "PROD_SCALAR_PORTABLE_MICROKERNEL_SRCS": None, |
| 12 | + "PROD_SCALAR_AARCH32_MICROKERNEL_SRCS" : "defined(__arm__)", |
| 13 | + "PROD_NEON_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)", |
| 14 | + "PROD_NEONFP16_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)", |
| 15 | + "PROD_NEON_AARCH64_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)", |
| 16 | + "PROD_NEONFMA_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)", |
| 17 | + "PROD_AARCH64_NEON_MICROKERNEL_SRCS": "defined(__aarch64__)", |
| 18 | + "PROD_NEONV8_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)", |
| 19 | + "PROD_AARCH64_NEONFP16ARITH_MICROKERNEL_SRCS": "defined(__aarch64__)", |
| 20 | + "PROD_NEONDOT_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)", |
| 21 | + "PROD_SSE_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 22 | + "PROD_SSE2_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 23 | + "PROD_SSSE3_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 24 | + "PROD_SSE41_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 25 | + "PROD_AVX_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 26 | + "PROD_F16C_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 27 | + "PROD_XOP_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 28 | + "PROD_FMA3_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 29 | + "PROD_AVX2_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 30 | + "PROD_AVX512F_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 31 | + "PROD_AVX512SKX_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 32 | + "PROD_AVX512VBMI_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 33 | + "AARCH32_ASM_MICROKERNEL_SRCS": "defined(__arm__)", |
| 34 | + "AARCH64_ASM_MICROKERNEL_SRCS": "defined(__aarch64__)", |
| 35 | + |
| 36 | + # add additoonal: |
| 37 | + "PROD_NEONFP16ARITH_AARCH64_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)", |
| 38 | + "ALL_ARMSIMD32_MICROKERNEL_SRCS": "defined(__arm__)", |
| 39 | + "ALL_AVX_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 40 | + "ALL_AVX2_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 41 | + "ALL_AVX512F_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 42 | + |
| 43 | + 'ALL_AVX512SKX_MICROKERNEL_SRCS': "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 44 | + 'ALL_AVX512VBMI_MICROKERNEL_SRCS': "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 45 | + 'ALL_F16C_MICROKERNEL_SRCS': "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 46 | + 'ALL_FMA3_MICROKERNEL_SRCS': "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 47 | + 'ALL_FP16ARITH_MICROKERNEL_SRCS': "defined(__arm__) || defined(__aarch64__)", |
| 48 | + 'ALL_NEON_MICROKERNEL_SRCS': "defined(__arm__) || defined(__aarch64__)", |
| 49 | + 'ALL_NEON_AARCH64_MICROKERNEL_SRCS': "defined(__aarch64__)", |
| 50 | + 'ALL_NEONBF16_MICROKERNEL_SRCS': "defined(__arm__) || defined(__aarch64__)", |
| 51 | + 'ALL_NEONDOT_MICROKERNEL_SRCS': "defined(__arm__) || defined(__aarch64__)", |
| 52 | + 'ALL_NEONFMA_MICROKERNEL_SRCS': "defined(__arm__) || defined(__aarch64__)", |
| 53 | + 'ALL_NEONFMA_AARCH64_MICROKERNEL_SRCS': "defined(__aarch64__)", |
| 54 | + 'ALL_NEONFP16_MICROKERNEL_SRCS':"defined(__arm__) || defined(__aarch64__)", |
| 55 | + 'ALL_NEONFP16ARITH_MICROKERNEL_SRCS': "defined(__arm__) || defined(__aarch64__)", |
| 56 | + 'ALL_NEONFP16ARITH_AARCH64_MICROKERNEL_SRCS': "defined(__aarch64__)", |
| 57 | + 'ALL_NEONV8_MICROKERNEL_SRCS': "defined(__aarch64__)", |
| 58 | + 'ALL_SCALAR_MICROKERNEL_SRCS': "defined(__arm__)", |
| 59 | + 'ALL_SSE_MICROKERNEL_SRCS': "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 60 | + 'ALL_SSE2_MICROKERNEL_SRCS': "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 61 | + 'ALL_SSE41_MICROKERNEL_SRCS': "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 62 | + 'ALL_SSSE3_MICROKERNEL_SRCS': "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 63 | + 'ALL_XOP_MICROKERNEL_SRCS': "defined(__i386__) || defined(__i686__) || defined(__x86_64__)", |
| 64 | + 'AARCH32_ASM_MICROKERNEL_SRCS': "defined(__arm__)", |
| 65 | + "PROD_FP16ARITH_MICROKERNEL_SRCS": "defined(__aarch64__)", |
| 66 | + "PROD_NEONFP16ARITH_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)", |
| 67 | + "PROD_SCALAR_MICROKERNEL_SRCS": "defined(__arm__)", |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +SRC_NAMES = set([ |
| 72 | + "OPERATOR_SRCS", |
| 73 | + "SUBGRAPH_SRCS", |
| 74 | + "LOGGING_SRCS", |
| 75 | + "XNNPACK_SRCS", |
| 76 | + "HOT_SRCS", |
| 77 | + "TABLE_SRCS", |
| 78 | + "JIT_SRCS", |
| 79 | + "JIT_AARCH32_SRCS", |
| 80 | + "JIT_AARCH64_SRCS", |
| 81 | + "PROD_SCALAR_PORTABLE_MICROKERNEL_SRCS", |
| 82 | + "PROD_SSE_MICROKERNEL_SRCS", |
| 83 | + "PROD_SSE2_MICROKERNEL_SRCS", |
| 84 | + "PROD_SSSE3_MICROKERNEL_SRCS", |
| 85 | + "PROD_SSE41_MICROKERNEL_SRCS", |
| 86 | + "PROD_AVX_MICROKERNEL_SRCS", |
| 87 | + "PROD_F16C_MICROKERNEL_SRCS", |
| 88 | + "PROD_XOP_MICROKERNEL_SRCS", |
| 89 | + "PROD_FMA3_MICROKERNEL_SRCS", |
| 90 | + "PROD_AVX2_MICROKERNEL_SRCS", |
| 91 | + "PROD_AVX512F_MICROKERNEL_SRCS", |
| 92 | + "PROD_AVX512SKX_MICROKERNEL_SRCS", |
| 93 | + "PROD_SCALAR_MICROKERNEL_SRCS", |
| 94 | + "PROD_SCALAR_AARCH32_MICROKERNEL_SRCS", |
| 95 | + "PROD_SCALAR_RISCV_MICROKERNEL_SRCS", |
| 96 | + "PROD_ARMSIMD32_MICROKERNEL_SRCS", |
| 97 | + "PROD_FP16ARITH_MICROKERNEL_SRCS", |
| 98 | + "PROD_NEON_MICROKERNEL_SRCS", |
| 99 | + "PROD_NEONFP16_MICROKERNEL_SRCS", |
| 100 | + "PROD_NEONFMA_MICROKERNEL_SRCS", |
| 101 | + "PROD_NEON_AARCH64_MICROKERNEL_SRCS", |
| 102 | + "PROD_NEONV8_MICROKERNEL_SRCS", |
| 103 | + "PROD_NEONFP16ARITH_AARCH64_MICROKERNEL_SRCS", |
| 104 | + "PROD_NEONDOT_MICROKERNEL_SRCS", |
| 105 | + "PROD_SSE2_MICROKERNEL_SRCS", |
| 106 | + "PROD_SSSE3_MICROKERNEL_SRCS", |
| 107 | + "PROD_SSE41_MICROKERNEL_SRCS", |
| 108 | + "PROD_AVX_MICROKERNEL_SRCS", |
| 109 | + "PROD_F16C_MICROKERNEL_SRCS", |
| 110 | + "PROD_AVX512VBMI_MICROKERNEL_SRCS", |
| 111 | + "PROD_NEONFP16ARITH_MICROKERNEL_SRCS", |
| 112 | + |
| 113 | + # new adding libs: |
| 114 | + 'ALL_ARMSIMD32_MICROKERNEL_SRCS', |
| 115 | + 'ALL_AVX_MICROKERNEL_SRCS', |
| 116 | + 'ALL_AVX2_MICROKERNEL_SRCS', |
| 117 | + 'ALL_AVX512F_MICROKERNEL_SRCS', |
| 118 | + 'ALL_AVX512SKX_MICROKERNEL_SRCS', |
| 119 | + 'ALL_AVX512VBMI_MICROKERNEL_SRCS', |
| 120 | + 'ALL_F16C_MICROKERNEL_SRCS', |
| 121 | + 'ALL_FMA3_MICROKERNEL_SRCS', |
| 122 | + 'ALL_FP16ARITH_MICROKERNEL_SRCS', |
| 123 | + 'ALL_HEXAGON_MICROKERNEL_SRCS', |
| 124 | + 'ALL_NEON_MICROKERNEL_SRCS', |
| 125 | + 'ALL_NEON_AARCH64_MICROKERNEL_SRCS', |
| 126 | + 'ALL_NEONBF16_MICROKERNEL_SRCS', |
| 127 | + 'ALL_NEONBF16_AARCH64_MICROKERNEL_SRCS', |
| 128 | + 'ALL_NEONDOT_MICROKERNEL_SRCS', |
| 129 | + 'ALL_NEONFMA_MICROKERNEL_SRCS', |
| 130 | + 'ALL_NEONFMA_AARCH64_MICROKERNEL_SRCS', |
| 131 | + 'ALL_NEONFP16_MICROKERNEL_SRCS', |
| 132 | + 'ALL_NEONFP16ARITH_MICROKERNEL_SRCS', |
| 133 | + 'ALL_NEONFP16ARITH_AARCH64_MICROKERNEL_SRCS', |
| 134 | + 'ALL_NEONV8_MICROKERNEL_SRCS', |
| 135 | + 'ALL_SCALAR_MICROKERNEL_SRCS', |
| 136 | + 'ALL_SSE_MICROKERNEL_SRCS', |
| 137 | + 'ALL_SSE2_MICROKERNEL_SRCS', |
| 138 | + 'ALL_SSE41_MICROKERNEL_SRCS', |
| 139 | + 'ALL_SSSE3_MICROKERNEL_SRCS', |
| 140 | + 'ALL_WASM_MICROKERNEL_SRCS', |
| 141 | + 'ALL_WASMRELAXEDSIMD_MICROKERNEL_SRCS', |
| 142 | + 'ALL_WASMSIMD_MICROKERNEL_SRCS', |
| 143 | + 'ALL_XOP_MICROKERNEL_SRCS', |
| 144 | + 'AARCH32_ASM_MICROKERNEL_SRCS', |
| 145 | + 'AARCH64_ASM_MICROKERNEL_SRCS', |
| 146 | +]) |
| 147 | + |
| 148 | +def handle_singleline_parse(line): |
| 149 | + start_index = line.find("(") |
| 150 | + end_index = line.find(")") |
| 151 | + line = line[start_index+1:end_index] |
| 152 | + key_val = line.split(" ") |
| 153 | + return key_val[0], key_val[1][4:] |
| 154 | + |
| 155 | +def update_sources(xnnpack_path, cmakefile = "XNNPACK/CMakeLists.txt"): |
| 156 | + sources = collections.defaultdict(list) |
| 157 | + count = 0 |
| 158 | + with open(os.path.join(xnnpack_path, cmakefile)) as cmake: |
| 159 | + lines = cmake.readlines() |
| 160 | + i = 0 |
| 161 | + while i < len(lines): |
| 162 | + line = lines[i] |
| 163 | + |
| 164 | + if lines[i].startswith("SET") and "src/" in lines[i]: |
| 165 | + name, val = handle_singleline_parse(line) |
| 166 | + sources[name].append(val) |
| 167 | + i+=1 |
| 168 | + continue |
| 169 | + |
| 170 | + if line.startswith("SET") and line.split('(')[1].strip(' \t\n\r') in set(WRAPPER_SRC_NAMES.keys()) | set(SRC_NAMES): |
| 171 | + name = line.split('(')[1].strip(' \t\n\r') |
| 172 | + i += 1 |
| 173 | + while i < len(lines) and len(lines[i]) > 0 and ')' not in lines[i]: |
| 174 | + # remove "src/" at the beginning, remove whitespaces and newline |
| 175 | + value = lines[i].strip(' \t\n\r') |
| 176 | + sources[name].append(value[4:]) |
| 177 | + i += 1 |
| 178 | + if i < len(lines) and len(lines[i]) > 4: |
| 179 | + # remove "src/" at the beginning, possibly ')' at the end |
| 180 | + value = lines[i].strip(' \t\n\r)') |
| 181 | + sources[name].append(value[4:]) |
| 182 | + else: |
| 183 | + i += 1 |
| 184 | + return sources |
| 185 | + |
| 186 | +def gen_wrappers(xnnpack_path): |
| 187 | + xnnpack_sources = collections.defaultdict(list) |
| 188 | + sources = update_sources(xnnpack_path) |
| 189 | + |
| 190 | + microkernels_sources = update_sources(xnnpack_path, "XNNPACK/cmake/microkernels.cmake") |
| 191 | + for key in microkernels_sources: |
| 192 | + sources[key] = microkernels_sources[key] |
| 193 | + |
| 194 | + for name in WRAPPER_SRC_NAMES: |
| 195 | + xnnpack_sources[WRAPPER_SRC_NAMES[name]].extend(sources[name]) |
| 196 | + |
| 197 | + for condition, filenames in xnnpack_sources.items(): |
| 198 | + print(condition) |
| 199 | + for filename in filenames: |
| 200 | + filepath = os.path.join(xnnpack_path, "xnnpack_wrappers", filename) |
| 201 | + |
| 202 | + if not os.path.isdir(os.path.dirname(filepath)): |
| 203 | + os.makedirs(os.path.dirname(filepath)) |
| 204 | + with open(filepath, "w") as wrapper: |
| 205 | + print("/* {} */".format(BANNER), file=wrapper) |
| 206 | + print(file=wrapper) |
| 207 | + |
| 208 | + # Architecture- or platform-dependent preprocessor flags can be |
| 209 | + # defined here. Note: platform_preprocessor_flags can't be used |
| 210 | + # because they are ignored by arc focus & buck project. |
| 211 | + |
| 212 | + if condition is None: |
| 213 | + print("#include <%s>" % filename, file=wrapper) |
| 214 | + else: |
| 215 | + # Include source file only if condition is satisfied |
| 216 | + print("#if %s" % condition, file=wrapper) |
| 217 | + print("#include <%s>" % filename, file=wrapper) |
| 218 | + print("#endif /* %s */" % condition, file=wrapper) |
| 219 | + |
| 220 | + # update xnnpack_wrapper_defs.bzl file under the same folder |
| 221 | + with open(os.path.join(os.path.dirname(__file__), "xnnpack_wrapper_defs.bzl"), 'w') as wrapper_defs: |
| 222 | + print('"""', file=wrapper_defs) |
| 223 | + print(BANNER, file=wrapper_defs) |
| 224 | + print('"""', file=wrapper_defs) |
| 225 | + for name in WRAPPER_SRC_NAMES: |
| 226 | + print('\n' + name + ' = [', file=wrapper_defs) |
| 227 | + for file_name in sources[name]: |
| 228 | + print(' "xnnpack_wrappers/{}",'.format(file_name), file=wrapper_defs) |
| 229 | + print(']', file=wrapper_defs) |
| 230 | + |
| 231 | + # update xnnpack_src_defs.bzl file under the same folder |
| 232 | + with open(os.path.join(os.path.dirname(__file__), "xnnpack_src_defs.bzl"), 'w') as src_defs: |
| 233 | + print('"""', file=src_defs) |
| 234 | + print(BANNER, file=src_defs) |
| 235 | + print('"""', file=src_defs) |
| 236 | + for name in SRC_NAMES: |
| 237 | + print('\n' + name + ' = [', file=src_defs) |
| 238 | + for file_name in sources[name]: |
| 239 | + print(' "XNNPACK/src/{}",'.format(file_name), file=src_defs) |
| 240 | + print(']', file=src_defs) |
| 241 | + |
| 242 | + |
| 243 | +def main(argv): |
| 244 | + if argv is None or len(argv) == 0: |
| 245 | + gen_wrappers(".") |
| 246 | + else: |
| 247 | + gen_wrappers(argv[0]) |
| 248 | + |
| 249 | +# The first argument is the place where the "xnnpack_wrappers" folder will be created. |
| 250 | +# Run it without arguments will generate "xnnpack_wrappers" in the current path. |
| 251 | +# The two .bzl files will always be generated in the current path. |
| 252 | +if __name__ == "__main__": |
| 253 | + main(sys.argv[1:]) |
0 commit comments