diff --git a/compiler/one-cmds/one-codegen b/compiler/one-cmds/one-codegen index 8551056f6a8..84e7b31d906 100644 --- a/compiler/one-cmds/one-codegen +++ b/compiler/one-cmds/one-codegen @@ -35,6 +35,9 @@ import onelib.utils as oneutils # TODO Find better way to suppress trackback on error sys.tracebacklimit = 0 +COMMAND_KEYS = ['__command', 'command'] +BACKEND_KEY = 'BACKEND' + def _get_parser(backends_list): codegen_usage = 'one-codegen [-h] [-v] [-C CONFIG] [-b BACKEND | -T TARGET] [--] [COMMANDS FOR BACKEND]' @@ -81,7 +84,8 @@ def _verify_arg(parser, args, cfg_args, cfg_target_args, backend_args, unknown_a # overwrite the value if it exists as command line option has higher priority. if oneutils.is_valid_attr(args, 'target'): target_to_run = args.target - given_backend = backends.get_backend_from_target_conf(target_to_run) + given_backend = backends.get_value_from_target_conf( + target_to_run, BACKEND_KEY) if not given_backend: parser.error(f'Not found {target_to_run} target.') else: @@ -213,8 +217,11 @@ def main(): assert (oneutils.is_valid_attr(cfg_args, 'command')) setattr(cfg_args, args.backend, cfg_args.command) else: + given_backend = None # get backend information - given_backend = backends.get_backend_from_target_conf(target_to_run) + if target_to_run: + given_backend = backends.get_value_from_target_conf( + target_to_run, BACKEND_KEY) # check if command schema for the backend exists # 1. if it exists, run the command according to the schema. # 2. if it doesn't exist, insert "--target ${TARGET}" at the beginning of the given command. @@ -251,7 +258,9 @@ def main(): # [15], [16] else: assert oneutils.is_valid_attr(args, 'target') - given_backends = [backends.get_backend_from_target_conf(target_to_run)] + given_backends = [ + backends.get_value_from_target_conf(target_to_run, BACKEND_KEY) + ] # make commands # 1. if command schema exists diff --git a/compiler/one-cmds/one-profile b/compiler/one-cmds/one-profile index 2477a350bf2..585517f7c4c 100644 --- a/compiler/one-cmds/one-profile +++ b/compiler/one-cmds/one-profile @@ -35,6 +35,9 @@ import onelib.utils as oneutils # TODO Find better way to suppress trackback on error sys.tracebacklimit = 0 +COMMAND_KEYS = ['__command', 'command'] +BACKEND_KEY = 'BACKEND' + def _get_backends_list(): """ @@ -120,7 +123,8 @@ def _verify_arg(parser, args, cfg_args, cfg_target_args, backend_args, unknown_a # overwrite the value if it exists as command line option has higher priority. if oneutils.is_valid_attr(args, 'target'): target_to_run = args.target - given_backend = backends.get_backend_from_target_conf(target_to_run) + given_backend = backends.get_value_from_target_conf( + target_to_run, BACKEND_KEY) if not given_backend: parser.error(f'Not found {target_to_run} target.') else: @@ -248,8 +252,11 @@ def main(): assert (oneutils.is_valid_attr(cfg_args, 'command')) setattr(cfg_args, args.backend, cfg_args.command) else: + given_backend = None # get backend information - given_backend = backends.get_backend_from_target_conf(target_to_run) + if target_to_run: + given_backend = backends.get_value_from_target_conf( + target_to_run, BACKEND_KEY) # check if command schema exists # 1. if it exists, run the command according to the schema. # 2. if it doesn't exist, insert "--target ${TARGET}" at the beginning of the given command. @@ -286,7 +293,9 @@ def main(): # [15], [16] else: assert oneutils.is_valid_attr(args, 'target') - given_backends = [backends.get_backend_from_target_conf(target_to_run)] + given_backends = [ + backends.get_value_from_target_conf(target_to_run, BACKEND_KEY) + ] # make commands # 1. if command schema exists diff --git a/compiler/one-cmds/onelib/backends.py b/compiler/one-cmds/onelib/backends.py index 4403a07cbb6..f7336bde233 100644 --- a/compiler/one-cmds/onelib/backends.py +++ b/compiler/one-cmds/onelib/backends.py @@ -28,6 +28,7 @@ ├── include ├── lib ├── optimization +├── target └── test The list where `one-XXXX` finds its backends @@ -36,6 +37,21 @@ NOTE If there are backends of the same name in different places, the closer to the top in the list, the higher the priority. + +[About TARGET and BACKEND] + "Target" refers to an instance from the core of the system and + "Backend" refers to an architecture. Say there is a NPU that has + multiple cores. Its cores may have different global buffer + size, DSPM size and clock rate, etc, which are described in + each configuration file of "Target". Even though they + are different target, they may follow same architecture, which means + they have same "Backend". + +[Path for TARGET configuration] + - /usr/share/one/target/${TARGET}.ini + +[Path for BACKEND tools] + - /usr/share/one/backends/${BACKEND} """ @@ -62,11 +78,11 @@ def get_list(cmdname): return backends_list -def get_backend_from_target_conf(target: str): +def get_value_from_target_conf(target: str, key: str): dir_path = os.path.dirname(os.path.realpath(__file__)) target_conf_path = dir_path + f'/../../target/{target}.ini' if not os.path.isfile(target_conf_path): - return None + raise FileNotFoundError(f"Not found given target configuration: {target}") # target config doesn't have section. # but, configparser needs configs to have one or more sections. @@ -77,11 +93,16 @@ def get_backend_from_target_conf(target: str): parser.read_string(config_str) assert parser.has_section(DUMMY_SECTION) - BACKEND_KEY = 'BACKEND' - if BACKEND_KEY in parser[DUMMY_SECTION]: - return parser[DUMMY_SECTION][BACKEND_KEY] + # Check if target file is valid + TARGET_KEY = 'TARGET' + assert TARGET_KEY in parser[DUMMY_SECTION] + if target != parser[DUMMY_SECTION][TARGET_KEY]: + raise RuntimeError("Invalid target file.") - return None + if key in parser[DUMMY_SECTION]: + return parser[DUMMY_SECTION][key] + + raise RuntimeError(f"Not found '{key}' key in target configuration.") def search_driver(driver): diff --git a/compiler/one-cmds/tests/onecc_060.ini b/compiler/one-cmds/tests/onecc_060.ini index 6d3a9ac3849..23f2a32b75a 100644 --- a/compiler/one-cmds/tests/onecc_060.ini +++ b/compiler/one-cmds/tests/onecc_060.ini @@ -1,2 +1,2 @@ -TARGET=rose +TARGET=onecc_060 BACKEND=dummy diff --git a/compiler/one-cmds/tests/onecc_neg_039.cfg b/compiler/one-cmds/tests/onecc_neg_039.cfg new file mode 100644 index 00000000000..18275d484f8 --- /dev/null +++ b/compiler/one-cmds/tests/onecc_neg_039.cfg @@ -0,0 +1,9 @@ +[onecc] +one-codegen=True + +[backend] +target=onecc_neg_039 + +[one-codegen] +backend=dummy +command=-o onecc_neg_039.tvn onecc_neg_039.circle diff --git a/compiler/one-cmds/tests/onecc_neg_039.ini b/compiler/one-cmds/tests/onecc_neg_039.ini new file mode 100644 index 00000000000..e2bc54b04e1 --- /dev/null +++ b/compiler/one-cmds/tests/onecc_neg_039.ini @@ -0,0 +1,2 @@ +TARGET=rose # invalid name +BACKEND=dummy diff --git a/compiler/one-cmds/tests/onecc_neg_039.test b/compiler/one-cmds/tests/onecc_neg_039.test new file mode 100644 index 00000000000..75a365dea99 --- /dev/null +++ b/compiler/one-cmds/tests/onecc_neg_039.test @@ -0,0 +1,84 @@ +#!/bin/bash + +# Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Invalid target file + +: ' +This test assumes below directories. + +[one hierarchy] + one + ├── backends + ├── bin + ├── doc + ├── include + ├── lib + ├── optimization + ├── target + └── test # pwd +' + +TARGET_ALREADY_EXIST=true + +filename_ext="$(basename -- $0)" +filename="${filename_ext%.*}" + +configfile="onecc_neg_039.cfg" +outputfile="onecc_neg_039.tvn" +targetfile="onecc_neg_039.ini" + +clean_envir() +{ + rm -rf ../bin/dummy-compile + rm -rf ../target/${targetfile} + if [ "$TARGET_ALREADY_EXIST" = false ]; then + rm -rf ../target/ + fi +} + +trap_err_onexit() +{ + if grep -q "Invalid target file" "${filename}.log"; then + echo "${filename_ext} SUCCESS" + clean_envir + exit 0 + fi + + echo "${filename_ext} FAILED" + clean_envir + exit 255 +} + +trap trap_err_onexit ERR + +rm -f ${filename}.log +rm -rf ${outputfile} + +if [ ! -d "../target/" ]; then + mkdir -p ../target/ + TARGET_ALREADY_EXIST=false +fi + +# copy dummy tools to bin folder +cp dummy-compile ../bin/dummy-compile +cp ${targetfile} ../target/ + +# run test +onecc -C ${configfile} > ${filename}.log 2>&1 + +echo "${filename_ext} FAILED" +clean_envir +exit 255