blob: c48a8502152a5450ee7a70e1bc1864ee269f181f [file] [log] [blame]
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00001# Copyright (c) 2016-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002#
3# SPDX-License-Identifier: MIT
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to
7# deal in the Software without restriction, including without limitation the
8# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9# sell copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
Motti Gondabi6f3a9f52021-11-09 15:47:17 +020022
Anthony Barbier6ff3b192017-09-04 18:44:23 +010023import collections
24import os.path
25import re
26import subprocess
Georgios Pinitasea857272021-01-22 05:47:37 +000027import zlib
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010028import json
Adnan AlSinan39aebd12021-08-06 12:44:51 +010029import codecs
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31VERSION = "v0.0-unreleased"
Gunes Bayir456fb2b2021-11-04 16:14:37 +000032LIBRARY_VERSION_MAJOR = 25
Sang-Hoon Park6d0b3842020-08-14 14:48:08 +010033LIBRARY_VERSION_MINOR = 0
Georgios Pinitas35fcc432020-03-26 18:47:46 +000034LIBRARY_VERSION_PATCH = 0
35SONAME_VERSION = str(LIBRARY_VERSION_MAJOR) + "." + str(LIBRARY_VERSION_MINOR) + "." + str(LIBRARY_VERSION_PATCH)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37Import('env')
38Import('vars')
Anthony Barbier01bbd5f2018-11-01 15:10:51 +000039Import('install_lib')
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
Michalis Spyrou748a7c82019-10-07 13:00:44 +010041def build_bootcode_objs(sources):
Michalis Spyrou748a7c82019-10-07 13:00:44 +010042 arm_compute_env.Append(ASFLAGS = "-I bootcode/")
43 obj = arm_compute_env.Object(sources)
44 obj = install_lib(obj)
45 Default(obj)
46 return obj
47
Motti Gondabi6f3a9f52021-11-09 15:47:17 +020048# @brief Generates SVE/SVE2 shared object files for a specific V8 architechture.
49#
50# @param sources The target source files
51# @param arch_info A Tuple represents the architecture info
52# such as the compiler flags and defines.
53#
54# @return A list of objects for the corresponding architecture.
Motti Gondabif76a5022021-12-21 13:19:29 +020055#
Motti Gondabi6f3a9f52021-11-09 15:47:17 +020056def build_multi_isa_objs(sources, arch_v8_info):
Michalis Spyrou20fca522021-06-07 14:23:57 +010057
Motti Gondabif76a5022021-12-21 13:19:29 +020058 # Get the march
Motti Gondabi6f3a9f52021-11-09 15:47:17 +020059 arch_v8 = arch_v8_info[0]
60
61 # Create a temp environment
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010062 tmp_env = arm_compute_env.Clone()
Motti Gondabi6f3a9f52021-11-09 15:47:17 +020063
64 if 'cxxflags' in arch_v8_info[1] and len(arch_v8_info[1]['cxxflags']) > 0:
65 tmp_env.Append(CXXFLAGS = arch_v8_info[1]['cxxflags'])
66 if 'cppdefines' in arch_v8_info[1] and len(arch_v8_info[1]['cppdefines']) > 0:
67 tmp_env.Append(CPPDEFINES = arch_v8_info[1]['cppdefines'])
68
69 if 'sve' in arch_v8:
Motti Gondabi6f3a9f52021-11-09 15:47:17 +020070 tmp_env.Append(CPPDEFINES = ['ENABLE_SVE', 'ARM_COMPUTE_ENABLE_SVE'])
71 if 'sve2' in arch_v8:
72 tmp_env.Append(CPPDEFINES = ['ARM_COMPUTE_ENABLE_SVE2'])
73 else:
Motti Gondabi6f3a9f52021-11-09 15:47:17 +020074 tmp_env.Append(CPPDEFINES = ['ENABLE_NEON', 'ARM_COMPUTE_ENABLE_NEON'])
75
76 # we must differentiate the file object names
77 # as we accumulate the set.
Motti Gondabif76a5022021-12-21 13:19:29 +020078 objs = []
Motti Gondabi6f3a9f52021-11-09 15:47:17 +020079 for src in sources:
Motti Gondabif76a5022021-12-21 13:19:29 +020080 objs += tmp_env.SharedObject('{}-{}.o'.format(src.replace('.cpp', ''), arch_v8), src)
Motti Gondabi6f3a9f52021-11-09 15:47:17 +020081
Motti Gondabif76a5022021-12-21 13:19:29 +020082 tmp_env.Default(objs)
83 return objs
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010084
Michalis Spyrou20fca522021-06-07 14:23:57 +010085
Georgios Pinitasb6af4822021-09-14 12:33:34 +010086def build_objs(sources):
Michalis Spyrou20fca522021-06-07 14:23:57 +010087 obj = arm_compute_env.SharedObject(sources)
Michalis Spyrou20fca522021-06-07 14:23:57 +010088 Default(obj)
89 return obj
90
Georgios Pinitasb6af4822021-09-14 12:33:34 +010091
Georgios Pinitas4d9687e2020-10-21 18:33:36 +010092def build_library(name, build_env, sources, static=False, libs=[]):
Mohammed Suhail Munshide60ed92021-12-21 11:07:27 +000093 cloned_build_env = build_env.Clone()
Motti Gondabif76a5022021-12-21 13:19:29 +020094 if env['os'] == 'android' and static == False:
Mohammed Suhail Munshide60ed92021-12-21 11:07:27 +000095 cloned_build_env["LINKFLAGS"].remove('-pie')
96 cloned_build_env["LINKFLAGS"].remove('-static-libstdc++')
97
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 if static:
Mohammed Suhail Munshide60ed92021-12-21 11:07:27 +000099 obj = cloned_build_env.StaticLibrary(name, source=sources, LIBS = arm_compute_env["LIBS"] + libs)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 else:
101 if env['set_soname']:
Mohammed Suhail Munshide60ed92021-12-21 11:07:27 +0000102 obj = cloned_build_env.SharedLibrary(name, source=sources, SHLIBVERSION = SONAME_VERSION, LIBS = arm_compute_env["LIBS"] + libs)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 else:
Mohammed Suhail Munshide60ed92021-12-21 11:07:27 +0000104 obj = cloned_build_env.SharedLibrary(name, source=sources, LIBS = arm_compute_env["LIBS"] + libs)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
Anthony Barbier01bbd5f2018-11-01 15:10:51 +0000106 obj = install_lib(obj)
Motti Gondabif76a5022021-12-21 13:19:29 +0200107 build_env.Default(obj)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 return obj
109
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100110
Georgios Pinitasf605bd22020-11-26 11:55:09 +0000111def remove_incode_comments(code):
112 def replace_with_empty(match):
113 s = match.group(0)
114 if s.startswith('/'):
115 return " "
116 else:
117 return s
118
119 comment_regex = re.compile(r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', re.DOTALL | re.MULTILINE)
120 return re.sub(comment_regex, replace_with_empty, code)
121
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100122
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123def resolve_includes(target, source, env):
124 # File collection
125 FileEntry = collections.namedtuple('FileEntry', 'target_name file_contents')
126
127 # Include pattern
128 pattern = re.compile("#include \"(.*)\"")
129
130 # Get file contents
131 files = []
132 for i in range(len(source)):
133 src = source[i]
134 dst = target[i]
Georgios Pinitasf605bd22020-11-26 11:55:09 +0000135 contents = src.get_contents().decode('utf-8')
136 contents = remove_incode_comments(contents).splitlines()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 entry = FileEntry(target_name=dst, file_contents=contents)
138 files.append((os.path.basename(src.get_path()),entry))
139
140 # Create dictionary of tupled list
141 files_dict = dict(files)
142
143 # Check for includes (can only be files in the same folder)
144 final_files = []
145 for file in files:
146 done = False
147 tmp_file = file[1].file_contents
148 while not done:
149 file_count = 0
150 updated_file = []
151 for line in tmp_file:
152 found = pattern.search(line)
153 if found:
154 include_file = found.group(1)
155 data = files_dict[include_file].file_contents
156 updated_file.extend(data)
157 else:
158 updated_file.append(line)
159 file_count += 1
160
161 # Check if all include are replaced.
162 if file_count == len(tmp_file):
163 done = True
164
165 # Update temp file
166 tmp_file = updated_file
167
168 # Append and prepend string literal identifiers and add expanded file to final list
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 entry = FileEntry(target_name=file[1].target_name, file_contents=tmp_file)
170 final_files.append((file[0], entry))
171
172 # Write output files
173 for file in final_files:
174 with open(file[1].target_name.get_path(), 'w+') as out_file:
Georgios Pinitasea857272021-01-22 05:47:37 +0000175 file_to_write = "\n".join( file[1].file_contents )
176 if env['compress_kernels']:
Adnan AlSinan39aebd12021-08-06 12:44:51 +0100177 file_to_write = zlib.compress(file_to_write.encode('utf-8'), 9)
178 file_to_write = codecs.encode(file_to_write, "base64").decode('utf-8').replace("\n", "")
Georgios Pinitasea857272021-01-22 05:47:37 +0000179 file_to_write = "R\"(" + file_to_write + ")\""
180 out_file.write(file_to_write)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100182
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183def create_version_file(target, source, env):
184# Generate string with build options library version to embed in the library:
185 try:
186 git_hash = subprocess.check_output(["git", "rev-parse", "HEAD"])
187 except (OSError, subprocess.CalledProcessError):
188 git_hash="unknown"
189
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 build_info = "\"arm_compute_version=%s Build options: %s Git hash=%s\"" % (VERSION, vars.args, git_hash.strip())
191 with open(target[0].get_path(), "w") as fd:
192 fd.write(build_info)
193
Michalis Spyrou20fca522021-06-07 14:23:57 +0100194
Freddie Liardet487d3902021-09-21 12:36:43 +0100195def get_attrs_list(env, data_types, data_layouts):
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100196 attrs = []
Michalis Spyrou20fca522021-06-07 14:23:57 +0100197
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100198 # Manage data-types
Freddie Liardet487d3902021-09-21 12:36:43 +0100199 if 'all' in data_types:
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100200 attrs += ['fp16', 'fp32', 'integer', 'qasymm8', 'qasymm8_signed', 'qsymm16']
201 else:
Freddie Liardet487d3902021-09-21 12:36:43 +0100202 if 'fp16' in data_types: attrs += ['fp16']
203 if 'fp32' in data_types: attrs += ['fp32']
204 if 'integer' in data_types: attrs += ['integer']
205 if 'qasymm8' in data_types: attrs += ['qasymm8']
206 if 'qasymm8_signed' in data_types: attrs += ['qasymm8_signed']
207 if 'qsymm16' in data_types: attrs += ['qsymm16']
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100208 # Manage data-layouts
Freddie Liardet487d3902021-09-21 12:36:43 +0100209 if 'all' in data_layouts:
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100210 attrs += ['nhwc', 'nchw']
211 else:
Freddie Liardet487d3902021-09-21 12:36:43 +0100212 if 'nhwc' in data_layouts: attrs += ['nhwc']
213 if 'nchw' in data_layouts: attrs += ['nchw']
Michalis Spyrou20fca522021-06-07 14:23:57 +0100214
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100215 # Manage execution state
Freddie Liardet487d3902021-09-21 12:36:43 +0100216 attrs += ['estate32' if (env['estate'] == 'auto' and 'v7a' in env['arch']) or '32' in env['estate'] else 'estate64']
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100217 return attrs
Michalis Spyrou20fca522021-06-07 14:23:57 +0100218
Michalis Spyrou20fca522021-06-07 14:23:57 +0100219
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100220def get_operator_backend_files(filelist, operators, backend='', techs=[], attrs=[]):
221 files = { "common" : [] }
Michalis Spyrou20fca522021-06-07 14:23:57 +0100222
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100223 # Early return if filelist is empty
224 if backend not in filelist:
225 return files
Michalis Spyrou20fca522021-06-07 14:23:57 +0100226
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100227 # Iterate over operators and create the file lists to compiler
228 for operator in operators:
229 if operator in filelist[backend]['operators']:
230 files['common'] += filelist[backend]['operators'][operator]["files"]["common"]
231 for tech in techs:
232 if tech in filelist[backend]['operators'][operator]["files"]:
233 # Add tech as a key to dictionary if not there
234 if tech not in files:
235 files[tech] = []
Michalis Spyrou20fca522021-06-07 14:23:57 +0100236
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100237 # Add tech files to the tech file list
238 tech_files = filelist[backend]['operators'][operator]["files"][tech]
239 files[tech] += tech_files.get('common', [])
240 for attr in attrs:
241 files[tech] += tech_files.get(attr, [])
242
243 # Remove duplicates if they exist
244 return {k: list(set(v)) for k,v in files.items()}
245
246def collect_operators(filelist, operators, backend=''):
247 ops = set()
248 for operator in operators:
249 if operator in filelist[backend]['operators']:
250 ops.add(operator)
251 if 'deps' in filelist[backend]['operators'][operator]:
252 ops.update(filelist[backend]['operators'][operator]['deps'])
253 else:
254 print("Operator {0} is unsupported on {1} backend!".format(operator, backend))
255
256 return ops
257
258
259def resolve_operator_dependencies(filelist, operators, backend=''):
260 resolved_operators = collect_operators(filelist, operators, backend)
261
262 are_ops_resolved = False
263 while not are_ops_resolved:
264 resolution_pass = collect_operators(filelist, resolved_operators, backend)
265 if len(resolution_pass) != len(resolved_operators):
266 resolved_operators.update(resolution_pass)
267 else:
268 are_ops_resolved = True
269
270 return resolved_operators
271
Freddie Liardet487d3902021-09-21 12:36:43 +0100272def read_build_config_json(build_config):
273 build_config_contents = {}
274 custom_operators = []
275 custom_types = []
276 custom_layouts = []
277 if os.path.isfile(build_config):
278 with open(build_config) as f:
279 try:
280 build_config_contents = json.load(f)
281 except:
282 print("Warning: Build configuration file is of invalid JSON format!")
283 else:
284 try:
285 build_config_contents = json.loads(build_config)
286 except:
287 print("Warning: Build configuration string is of invalid JSON format!")
288 if build_config_contents:
289 custom_operators = build_config_contents.get("operators", [])
290 custom_types = build_config_contents.get("data_types", [])
291 custom_layouts = build_config_contents.get("data_layouts", [])
292 return custom_operators, custom_types, custom_layouts
Michalis Spyrou20fca522021-06-07 14:23:57 +0100293
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100294arm_compute_env = env.Clone()
Anthony Barbier0e72c692018-08-24 11:22:08 +0100295version_file = arm_compute_env.Command("src/core/arm_compute_version.embed", "", action=create_version_file)
296arm_compute_env.AlwaysBuild(version_file)
Anthony Barbier6a3daf12018-02-19 17:24:27 +0000297
Georgios Pinitas45514032020-12-30 00:03:09 +0000298default_cpp_compiler = 'g++' if env['os'] not in ['android', 'macos'] else 'clang++'
Georgios Pinitas4d9687e2020-10-21 18:33:36 +0100299cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
300
Anthony Barbier6a3daf12018-02-19 17:24:27 +0000301# Generate embed files
Anthony Barbier0e72c692018-08-24 11:22:08 +0100302generate_embed = [ version_file ]
Anthony Barbier6a3daf12018-02-19 17:24:27 +0000303if env['opencl'] and env['embed_kernels']:
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100304
305 # Header files
306 cl_helper_files = [ 'src/core/CL/cl_kernels/activation_float_helpers.h',
307 'src/core/CL/cl_kernels/activation_quant_helpers.h',
308 'src/core/CL/cl_kernels/gemm_helpers.h',
309 'src/core/CL/cl_kernels/helpers_asymm.h',
310 'src/core/CL/cl_kernels/helpers.h',
311 'src/core/CL/cl_kernels/load_store_utility.h',
312 'src/core/CL/cl_kernels/repeat.h',
313 'src/core/CL/cl_kernels/tile_helpers.h',
314 'src/core/CL/cl_kernels/types.h',
SiCongLi1af54162021-10-06 15:25:57 +0100315 'src/core/CL/cl_kernels/warp_helpers.h',
316 'src/core/CL/cl_kernels/common/experimental/gemm_fused_post_ops/act_eltwise_op_act/fp_post_ops_act_eltwise_op_act.h',
317 'src/core/CL/cl_kernels/common/experimental/gemm_fused_post_ops/fp_mixed_precision_helpers.h',
318 'src/core/CL/cl_kernels/common/experimental/gemm_fused_post_ops/fp_elementwise_op_helpers.h',
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100319 ]
Anthony Barbier6a3daf12018-02-19 17:24:27 +0000320
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100321 # Common kernels
322 cl_files_common = ['src/core/CL/cl_kernels/common/activation_layer.cl',
323 'src/core/CL/cl_kernels/common/activation_layer_quant.cl',
324 'src/core/CL/cl_kernels/common/arg_min_max.cl',
325 'src/core/CL/cl_kernels/common/batchnormalization_layer.cl',
326 'src/core/CL/cl_kernels/common/bounding_box_transform.cl',
327 'src/core/CL/cl_kernels/common/bounding_box_transform_quantized.cl',
328 'src/core/CL/cl_kernels/common/bitwise_op.cl',
329 'src/core/CL/cl_kernels/common/cast.cl',
330 'src/core/CL/cl_kernels/common/comparisons.cl',
331 'src/core/CL/cl_kernels/common/concatenate.cl',
332 'src/core/CL/cl_kernels/common/col2im.cl',
333 'src/core/CL/cl_kernels/common/convert_fc_weights.cl',
334 'src/core/CL/cl_kernels/common/copy_tensor.cl',
335 'src/core/CL/cl_kernels/common/crop_tensor.cl',
336 'src/core/CL/cl_kernels/common/deconvolution_layer.cl',
337 'src/core/CL/cl_kernels/common/dequantization_layer.cl',
338 'src/core/CL/cl_kernels/common/elementwise_operation.cl',
339 'src/core/CL/cl_kernels/common/elementwise_operation_quantized.cl',
340 'src/core/CL/cl_kernels/common/elementwise_unary.cl',
341 'src/core/CL/cl_kernels/common/fft_digit_reverse.cl',
342 'src/core/CL/cl_kernels/common/fft.cl',
343 'src/core/CL/cl_kernels/common/fft_scale.cl',
344 'src/core/CL/cl_kernels/common/fill_border.cl',
345 'src/core/CL/cl_kernels/common/floor.cl',
346 'src/core/CL/cl_kernels/common/gather.cl',
347 'src/core/CL/cl_kernels/common/gemm.cl',
ramelg019cca5922021-11-11 10:05:00 +0000348 'src/core/CL/cl_kernels/common/gemm_utils.cl',
SiCongLiafa19722021-10-24 19:12:33 +0100349 'src/core/CL/cl_kernels/common/experimental/gemm_fused_post_ops/act_eltwise_op_act/gemm_mm_native.cl',
SiCongLi1af54162021-10-06 15:25:57 +0100350 'src/core/CL/cl_kernels/common/experimental/gemm_fused_post_ops/act_eltwise_op_act/gemm_mm_reshaped.cl',
SiCongLiafa19722021-10-24 19:12:33 +0100351 'src/core/CL/cl_kernels/common/experimental/gemm_fused_post_ops/act_eltwise_op_act/gemm_mm_reshaped_only_rhs.cl',
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100352 'src/core/CL/cl_kernels/common/gemv.cl',
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100353 'src/core/CL/cl_kernels/common/gemmlowp.cl',
354 'src/core/CL/cl_kernels/common/generate_proposals.cl',
355 'src/core/CL/cl_kernels/common/generate_proposals_quantized.cl',
356 'src/core/CL/cl_kernels/common/instance_normalization.cl',
357 'src/core/CL/cl_kernels/common/l2_normalize.cl',
358 'src/core/CL/cl_kernels/common/mean_stddev_normalization.cl',
359 'src/core/CL/cl_kernels/common/unpooling_layer.cl',
360 'src/core/CL/cl_kernels/common/memset.cl',
361 'src/core/CL/cl_kernels/common/nonmax.cl',
362 'src/core/CL/cl_kernels/common/minmax_layer.cl',
363 'src/core/CL/cl_kernels/common/pad_layer.cl',
364 'src/core/CL/cl_kernels/common/permute.cl',
365 'src/core/CL/cl_kernels/common/pixelwise_mul_float.cl',
366 'src/core/CL/cl_kernels/common/pixelwise_mul_int.cl',
367 'src/core/CL/cl_kernels/common/qlstm_layer_normalization.cl',
368 'src/core/CL/cl_kernels/common/quantization_layer.cl',
369 'src/core/CL/cl_kernels/common/range.cl',
370 'src/core/CL/cl_kernels/common/reduction_operation.cl',
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100371 'src/core/CL/cl_kernels/common/reshape_layer.cl',
372 'src/core/CL/cl_kernels/common/convolution_layer.cl',
373 'src/core/CL/cl_kernels/common/reverse.cl',
374 'src/core/CL/cl_kernels/common/roi_align_layer.cl',
375 'src/core/CL/cl_kernels/common/roi_align_layer_quantized.cl',
376 'src/core/CL/cl_kernels/common/roi_pooling_layer.cl',
377 'src/core/CL/cl_kernels/common/select.cl',
378 'src/core/CL/cl_kernels/common/softmax_layer.cl',
379 'src/core/CL/cl_kernels/common/softmax_layer_quantized.cl',
380 'src/core/CL/cl_kernels/common/stack_layer.cl',
381 'src/core/CL/cl_kernels/common/slice_ops.cl',
382 'src/core/CL/cl_kernels/common/tile.cl',
383 'src/core/CL/cl_kernels/common/transpose.cl'
384 ]
385
386 # NCHW kernels
387 cl_files_nchw = ['src/core/CL/cl_kernels/nchw/batch_to_space.cl',
388 'src/core/CL/cl_kernels/nchw/batchnormalization_layer.cl',
389 'src/core/CL/cl_kernels/nchw/channel_shuffle.cl',
390 'src/core/CL/cl_kernels/nchw/depth_to_space.cl',
Adnan AlSinan30124352021-12-02 19:12:20 +0000391 'src/core/CL/cl_kernels/nchw/direct_convolution.cl',
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100392 'src/core/CL/cl_kernels/nchw/dequantization_layer.cl',
393 'src/core/CL/cl_kernels/nchw/im2col.cl',
394 'src/core/CL/cl_kernels/nchw/normalization_layer.cl',
395 'src/core/CL/cl_kernels/nchw/normalize_planar_yuv_layer.cl',
396 'src/core/CL/cl_kernels/nchw/normalize_planar_yuv_layer_quantized.cl',
397 'src/core/CL/cl_kernels/nchw/pooling_layer.cl',
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100398 'src/core/CL/cl_kernels/nchw/prior_box_layer.cl',
399 'src/core/CL/cl_kernels/nchw/remap.cl',
400 'src/core/CL/cl_kernels/nchw/reorg_layer.cl',
401 'src/core/CL/cl_kernels/nchw/scale.cl',
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100402 'src/core/CL/cl_kernels/nchw/space_to_batch.cl',
403 'src/core/CL/cl_kernels/nchw/space_to_depth.cl',
404 'src/core/CL/cl_kernels/nchw/upsample_layer.cl',
405 'src/core/CL/cl_kernels/nchw/winograd_filter_transform.cl',
406 'src/core/CL/cl_kernels/nchw/winograd_input_transform.cl',
407 'src/core/CL/cl_kernels/nchw/winograd_output_transform.cl'
408 ]
409
410 # NHWC kernels
411 cl_files_nhwc = ['src/core/CL/cl_kernels/nhwc/batch_to_space.cl',
412 'src/core/CL/cl_kernels/nhwc/batchnormalization_layer.cl',
413 'src/core/CL/cl_kernels/nhwc/channel_shuffle.cl',
414 'src/core/CL/cl_kernels/nhwc/direct_convolution.cl',
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100415 'src/core/CL/cl_kernels/nhwc/direct_convolution3d.cl',
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100416 'src/core/CL/cl_kernels/nhwc/depth_to_space.cl',
417 'src/core/CL/cl_kernels/nhwc/dequantization_layer.cl',
418 'src/core/CL/cl_kernels/nhwc/dwc_native_fp_nhwc.cl',
419 'src/core/CL/cl_kernels/nhwc/dwc_native_quantized_nhwc.cl',
420 'src/core/CL/cl_kernels/nhwc/im2col.cl',
421 'src/core/CL/cl_kernels/nhwc/normalization_layer.cl',
422 'src/core/CL/cl_kernels/nhwc/normalize_planar_yuv_layer.cl',
423 'src/core/CL/cl_kernels/nhwc/normalize_planar_yuv_layer_quantized.cl',
424 'src/core/CL/cl_kernels/nhwc/pooling_layer.cl',
425 'src/core/CL/cl_kernels/nhwc/pooling_layer_quantized.cl',
426 'src/core/CL/cl_kernels/nhwc/remap.cl',
427 'src/core/CL/cl_kernels/nhwc/reorg_layer.cl',
428 'src/core/CL/cl_kernels/nhwc/scale.cl',
Adnan AlSinan7075fe22021-07-05 13:12:52 +0100429 'src/core/CL/cl_kernels/nhwc/space_to_batch.cl',
430 'src/core/CL/cl_kernels/nhwc/space_to_depth.cl',
431 'src/core/CL/cl_kernels/nhwc/upsample_layer.cl',
432 'src/core/CL/cl_kernels/nhwc/winograd_filter_transform.cl',
433 'src/core/CL/cl_kernels/nhwc/winograd_input_transform.cl',
434 'src/core/CL/cl_kernels/nhwc/winograd_output_transform.cl'
435 ]
436
437 cl_files = cl_helper_files + cl_files_common + cl_files_nchw + cl_files_nhwc
438
439 embed_files = [ f+"embed" for f in cl_files ]
Anthony Barbier6a3daf12018-02-19 17:24:27 +0000440 arm_compute_env.Append(CPPPATH =[Dir("./src/core/CL/").path] )
441
442 generate_embed.append(arm_compute_env.Command(embed_files, cl_files, action=resolve_includes))
443
Anthony Barbier6a3daf12018-02-19 17:24:27 +0000444Default(generate_embed)
445if env["build"] == "embed_only":
446 Return()
447
Georgios Pinitas35fcc432020-03-26 18:47:46 +0000448# Append version defines for semantic versioning
449arm_compute_env.Append(CPPDEFINES = [('ARM_COMPUTE_VERSION_MAJOR', LIBRARY_VERSION_MAJOR),
450 ('ARM_COMPUTE_VERSION_MINOR', LIBRARY_VERSION_MINOR),
451 ('ARM_COMPUTE_VERSION_PATCH', LIBRARY_VERSION_PATCH)])
452
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000453# Don't allow undefined references in the libraries:
Georgios Pinitas45514032020-12-30 00:03:09 +0000454undefined_flag = '-Wl,-undefined,error' if 'macos' in arm_compute_env["os"] else '-Wl,--no-undefined'
455arm_compute_env.Append(LINKFLAGS=[undefined_flag])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100456arm_compute_env.Append(CPPPATH =[Dir("./src/core/").path] )
457
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100458arm_compute_env.Append(LIBS = ['dl'])
459
Motti Gondabi6f3a9f52021-11-09 15:47:17 +0200460# Load build definitions file
461with (open(Dir('#').path + '/filedefs.json')) as fd:
462 filedefs = json.load(fd)
463
464
Georgios Pinitasbdcdc392021-04-22 16:42:03 +0100465with (open(Dir('#').path + '/filelist.json')) as fp:
466 filelist = json.load(fp)
467
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100468# Common backend files
469lib_files = filelist['common']
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100470
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100471# Logging files
472if env["logging"]:
473 lib_files += filelist['logging']
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000474
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000475# C API files
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100476lib_files += filelist['c_api']['common']
477lib_files += filelist['c_api']['operators']
Georgios Pinitasbdcdc392021-04-22 16:42:03 +0100478
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100479# Scheduler infrastructure
480lib_files += filelist['scheduler']['single']
481if env['cppthreads']:
482 lib_files += filelist['scheduler']['threads']
483if env['openmp']:
484 lib_files += filelist['scheduler']['omp']
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000485
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100486# Graph files
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100487graph_files = Glob('src/graph/*.cpp')
488graph_files += Glob('src/graph/*/*.cpp')
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000489
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100490# Specify user-defined priority operators
Freddie Liardet487d3902021-09-21 12:36:43 +0100491custom_operators = []
492custom_types = []
493custom_layouts = []
494
495use_custom_ops = env['high_priority'] or env['build_config'];
496
497if env['high_priority']:
498 custom_operators = filelist['high_priority']
499 custom_types = ['all']
500 custom_layouts = ['all']
501
502if env['build_config']:
503 custom_operators, custom_types, custom_layouts = read_build_config_json(env['build_config'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100504
505if env['opencl']:
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100506 lib_files += filelist['c_api']['gpu']
507 lib_files += filelist['gpu']['common']
Michele Di Giorgio760c7832021-08-05 10:54:13 +0100508
Freddie Liardet487d3902021-09-21 12:36:43 +0100509 cl_operators = custom_operators if use_custom_ops else filelist['gpu']['operators'].keys()
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100510 cl_ops_to_build = resolve_operator_dependencies(filelist, cl_operators, 'gpu')
511 lib_files += get_operator_backend_files(filelist, cl_ops_to_build, 'gpu')['common']
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000512
Georgios Pinitas13ef1762021-07-14 17:14:43 +0100513 graph_files += Glob('src/graph/backends/CL/*.cpp')
514
Motti Gondabi6f3a9f52021-11-09 15:47:17 +0200515multi_isa_objs_list = []
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100516lib_files_sve = []
Motti Gondabi6f3a9f52021-11-09 15:47:17 +0200517
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100518if env['neon']:
Georgios Pinitas30271c72019-06-24 14:56:34 +0100519 # build winograd/depthwise sources for either v7a / v8a
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100520 arm_compute_env.Append(CPPPATH = ["src/core/NEON/kernels/convolution/common/",
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +0100521 "src/core/NEON/kernels/convolution/winograd/",
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100522 "src/core/NEON/kernels/convolution/depthwise/",
523 "src/core/NEON/kernels/assembly/",
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100524 "arm_compute/core/NEON/kernels/assembly/",
Motti Gondabi6f3a9f52021-11-09 15:47:17 +0200525 "src/cpu/kernels/assembly/"])
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000526
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100527 lib_files += filelist['cpu']['common']
Georgios Pinitasff4fca02020-10-02 21:00:00 +0100528
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100529 # Setup SIMD file list to include
530 simd = []
Motti Gondabi6f3a9f52021-11-09 15:47:17 +0200531 if 'sve' in env['arch'] or env['multi_isa']: simd += ['sve']
532 if 'sve' not in env['arch'] or env['multi_isa']: simd += ['neon']
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100533
534 # Get attributes
Freddie Liardet487d3902021-09-21 12:36:43 +0100535 if(use_custom_ops):
536 attrs = get_attrs_list(env, custom_types, custom_layouts)
537 else:
538 attrs = get_attrs_list(env, env['data_type_support'], env['data_layout_support'])
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100539
540 # Setup data-type and data-layout files to include
Freddie Liardet487d3902021-09-21 12:36:43 +0100541 cpu_operators = custom_operators if use_custom_ops else filelist['cpu']['operators'].keys()
542 cpu_ops_to_build = resolve_operator_dependencies(filelist, cpu_operators, 'cpu')
543
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100544 cpu_files = get_operator_backend_files(filelist, cpu_ops_to_build, 'cpu', simd, attrs)
Motti Gondabi6f3a9f52021-11-09 15:47:17 +0200545
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100546 lib_files += cpu_files.get('common', [])
547 lib_files += cpu_files.get('neon', [])
548 lib_files_sve += cpu_files.get('sve', [])
Georgios Pinitas13ef1762021-07-14 17:14:43 +0100549
550 graph_files += Glob('src/graph/backends/NEON/*.cpp')
Georgios Pinitas70eb53b2021-01-06 19:42:21 +0000551
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100552# Restrict from building graph API if a reduced operator list has been provided
Freddie Liardet487d3902021-09-21 12:36:43 +0100553if use_custom_ops:
554 print("WARNING: Graph library requires all operators to be built")
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100555 graph_files = []
556
557# Build bootcode in case of bare-metal
Michalis Spyrou748a7c82019-10-07 13:00:44 +0100558bootcode_o = []
559if env['os'] == 'bare_metal':
560 bootcode_files = Glob('bootcode/*.s')
561 bootcode_o = build_bootcode_objs(bootcode_files)
562Export('bootcode_o')
563
Motti Gondabif76a5022021-12-21 13:19:29 +0200564# STATIC library build.
565#
566# Multi-ISA: We split the library into 2 static libs: arm_compute-static.a and arm_compute_multi_isa-static.a
567# to avoid 'argument list too long' error which being thrown due to OS string length limitation at link time.
568#
569# For single arch build: No change and we produce a single arm_compute-static.a
Motti Gondabi6f3a9f52021-11-09 15:47:17 +0200570if (env['multi_isa']):
Motti Gondabif76a5022021-12-21 13:19:29 +0200571 # Get v8 variants
Motti Gondabi6f3a9f52021-11-09 15:47:17 +0200572 arch_v8s = filedefs['cpu']['arch']
573 for arch_v8_info in arch_v8s.items():
574 multi_isa_objs_list += build_multi_isa_objs(lib_files_sve, arch_v8_info)
575
Motti Gondabif76a5022021-12-21 13:19:29 +0200576 arm_compute_a = build_library('arm_compute-static', arm_compute_env, lib_files, static=True)
577 arm_compute_multi_isa_a = build_library('arm_compute_multi_isa-static', arm_compute_env, multi_isa_objs_list, static=True)
578 Export('arm_compute_multi_isa_a')
Georgios Pinitasbdcdc392021-04-22 16:42:03 +0100579else:
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100580 arm_compute_a = build_library('arm_compute-static', arm_compute_env, lib_files + lib_files_sve, static=True)
Motti Gondabif76a5022021-12-21 13:19:29 +0200581 arm_compute_multi_isa_a = None
582
Michalis Spyrou62c2ad62021-06-21 17:40:09 +0100583Export('arm_compute_a')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100584
Motti Gondabif76a5022021-12-21 13:19:29 +0200585# SHARED library build.
586#
587# Multi-ISA: we leverage the prior static build such that the resulting
588# "libarm_compute.so" is combined from "arm_compute_multi_isa-static.a" and "lib_files" objects.
589#
590# For single arch build: No change.
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100591if env['os'] != 'bare_metal' and not env['standalone']:
Motti Gondabi6f3a9f52021-11-09 15:47:17 +0200592 if (env['multi_isa']):
Motti Gondabif76a5022021-12-21 13:19:29 +0200593 arm_compute_so = build_library('arm_compute', arm_compute_env, lib_files, static=False, libs=[arm_compute_multi_isa_a])
Georgios Pinitasbdcdc392021-04-22 16:42:03 +0100594 else:
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100595 arm_compute_so = build_library('arm_compute', arm_compute_env, lib_files + lib_files_sve, static=False)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100596
Michalis Spyrou62c2ad62021-06-21 17:40:09 +0100597 Export('arm_compute_so')
598
Michalis Spyrou62c2ad62021-06-21 17:40:09 +0100599# Generate dummy core lib for backwards compatibility
Pablo Marquez Tello48f26152021-11-18 10:15:23 +0000600if env['os'] == 'macos':
601 # macos static library archiver fails if given an empty list of files
602 arm_compute_core_a = build_library('arm_compute_core-static', arm_compute_env, [lib_files], static=True)
603else:
604 arm_compute_core_a = build_library('arm_compute_core-static', arm_compute_env, [], static=True)
605
Michalis Spyrou62c2ad62021-06-21 17:40:09 +0100606Export('arm_compute_core_a')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100607
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100608if env['os'] != 'bare_metal' and not env['standalone']:
Michalis Spyrou62c2ad62021-06-21 17:40:09 +0100609 arm_compute_core_a_so = build_library('arm_compute_core', arm_compute_env, [], static=False)
610 Export('arm_compute_core_a_so')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100611
Sang-Hoon Park18fbb922021-01-14 14:50:25 +0000612arm_compute_graph_env = arm_compute_env.Clone()
613
Georgios Pinitasb6af4822021-09-14 12:33:34 +0100614# Build graph libraries
Sang-Hoon Park18fbb922021-01-14 14:50:25 +0000615arm_compute_graph_env.Append(CXXFLAGS = ['-Wno-redundant-move', '-Wno-pessimizing-move'])
616
Motti Gondabif76a5022021-12-21 13:19:29 +0200617arm_compute_graph_a = build_library('arm_compute_graph-static', arm_compute_graph_env, graph_files, static=True, libs = [ arm_compute_a, arm_compute_multi_isa_a ])
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100618Export('arm_compute_graph_a')
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000619
620if env['os'] != 'bare_metal' and not env['standalone']:
Michalis Spyrou62c2ad62021-06-21 17:40:09 +0100621 arm_compute_graph_so = build_library('arm_compute_graph', arm_compute_graph_env, graph_files, static=False, libs = [ "arm_compute" ])
Georgios Pinitas9873ea32017-12-05 15:28:55 +0000622 Depends(arm_compute_graph_so, arm_compute_so)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100623 Export('arm_compute_graph_so')
624
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100625if env['standalone']:
Motti Gondabif76a5022021-12-21 13:19:29 +0200626 alias = arm_compute_env.Alias("arm_compute", [arm_compute_a, arm_compute_multi_isa_a])
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100627else:
628 alias = arm_compute_env.Alias("arm_compute", [arm_compute_a, arm_compute_so])
629
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100630Default(alias)
631
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100632if env['standalone']:
Michalis Spyrou62c2ad62021-06-21 17:40:09 +0100633 Depends([alias], generate_embed)
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100634else:
Michalis Spyrou62c2ad62021-06-21 17:40:09 +0100635 Depends([alias], generate_embed)