From 93ab17ec1baea6405f46225e39a3763717177315 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Wed, 17 Aug 2022 06:55:22 -0700 Subject: [PATCH 1/5] [Target] Only append default keys if target doesn't have any yet This allows target parsers to provide their own target keys. Without this change, the default keys would always be appended, which may or may not be desirable. --- src/target/target.cc | 11 +++++++---- tests/cpp/target_test.cc | 3 +-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/target/target.cc b/src/target/target.cc index 6124cedc7256..e3e9354a61bb 100644 --- a/src/target/target.cc +++ b/src/target/target.cc @@ -885,7 +885,8 @@ ObjectPtr TargetInternal::FromConfig(Map config) { // parse "keys" { std::vector keys; - if (config.count(kKeys)) { + bool has_user_keys = config.count(kKeys); + if (has_user_keys) { // user provided keys if (const auto* cfg_keys = config[kKeys].as()) { for (const ObjectRef& e : *cfg_keys) { @@ -909,9 +910,11 @@ ObjectPtr TargetInternal::FromConfig(Map config) { keys.push_back(GetRef(device)); } } - // add default keys - for (const auto& key : target->kind->default_keys) { - keys.push_back(key); + if (!has_user_keys) { + // add default keys + for (const auto& key : target->kind->default_keys) { + keys.push_back(key); + } } // de-duplicate keys target->keys = DeduplicateKeys(keys); diff --git a/tests/cpp/target_test.cc b/tests/cpp/target_test.cc index 4b4de2b5f44a..694a9226ed6f 100644 --- a/tests/cpp/target_test.cc +++ b/tests/cpp/target_test.cc @@ -170,9 +170,8 @@ TEST(TargetCreationFail, TargetKindNotFound) { TEST(TargetCreation, TargetParser) { Target test_target("TestTargetParser -mcpu=woof"); ASSERT_EQ(test_target->GetAttr("mcpu").value(), "super_woof"); - ASSERT_EQ(test_target->keys.size(), 2); + ASSERT_EQ(test_target->keys.size(), 1); ASSERT_EQ(test_target->keys[0], "super"); - ASSERT_EQ(test_target->keys[1], "cpu"); } TEST(TargetCreation, TargetFeatures) { From c03d0690ada7341f45ea4c58dedbac630daeba37 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Wed, 17 Aug 2022 11:34:33 -0700 Subject: [PATCH 2/5] Add "cpu" to ARM CPU keys --- python/tvm/target/target.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/python/tvm/target/target.py b/python/tvm/target/target.py index e53c7096279c..9c43f59fb865 100644 --- a/python/tvm/target/target.py +++ b/python/tvm/target/target.py @@ -531,7 +531,7 @@ def arm_cpu(model="unknown", options=None): } pre_defined_opt = trans_table.get(model, ["-model=%s" % model]) - opts = ["-device=arm_cpu"] + pre_defined_opt + opts = ["-keys=arm_cpu,cpu"] + pre_defined_opt opts = _merge_opts(opts, options) return Target(" ".join(["llvm"] + opts)) @@ -612,7 +612,7 @@ def riscv_cpu(model="sifive-u54", options=None): } pre_defined_opt = trans_table.get(model, ["-model=%s" % model]) - opts = ["-device=arm_cpu"] + pre_defined_opt + opts = ["-keys=arm_cpu,cpu"] + pre_defined_opt opts = _merge_opts(opts, options) return Target(" ".join(["llvm"] + opts)) @@ -756,22 +756,22 @@ def create_tvm_options(cpu_ver, config): # pylint: disable=unused-argument STM32_SUPPORTED_SERIES = { # High-Performance - "stm32H7xx": ["-device=arm_cpu", "-mcpu=cortex-m7", "-march=armv7e-m"], - "stm32F7xx": ["-device=arm_cpu", "-mcpu=cortex-m7"], - "stm32F4xx": ["-device=arm_cpu", "-mcpu=cortex-m4"], - "stm32F2xx": ["-device=arm_cpu", "-mcpu=cortex-m3"], + "stm32H7xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m7", "-march=armv7e-m"], + "stm32F7xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m7"], + "stm32F4xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m4"], + "stm32F2xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m3"], # Mainstream - "stm32G0xx": ["-device=arm_cpu", "-mcpu=cortex-m0+"], - "stm32F0xx": ["-device=arm_cpu", "-mcpu=cortex-m0"], - "stm32F1xx": ["-device=arm_cpu", "-mcpu=cortex-m3"], - "stm32G4xx": ["-device=arm_cpu", "-mcpu=cortex-m4"], - "stm32F3xx": ["-device=arm_cpu", "-mcpu=cortex-m4"], + "stm32G0xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m0+"], + "stm32F0xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m0"], + "stm32F1xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m3"], + "stm32G4xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m4"], + "stm32F3xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m4"], # Low-power - "stm32U5xx": ["-device=arm_cpu", "-mcpu=cortex-m33"], - "stm32L5xx": ["-device=arm_cpu", "-mcpu=cortex-m33"], - "stm32L4xx": ["-device=arm_cpu", "-mcpu=cortex-m4"], - "stm32L1xx": ["-device=arm_cpu", "-mcpu=cortex-m3"], - "stm32L0xx": ["-device=arm_cpu", "-mcpu=cortex-m0+"], + "stm32U5xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m33"], + "stm32L5xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m33"], + "stm32L4xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m4"], + "stm32L1xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m3"], + "stm32L0xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m0+"], } From c9456f6f2c4061b7e28a44f6dce24990a8de9922 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Wed, 17 Aug 2022 12:53:24 -0700 Subject: [PATCH 3/5] Add "cpu" to the keys in the mprofile target parser --- src/target/parsers/mprofile.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/target/parsers/mprofile.cc b/src/target/parsers/mprofile.cc index 7fb61ff893b6..9bcf2c907832 100644 --- a/src/target/parsers/mprofile.cc +++ b/src/target/parsers/mprofile.cc @@ -105,15 +105,17 @@ static TargetFeatures GetFeatures(TargetJSON target) { } static Array MergeKeys(Optional> existing_keys) { - const String kExtraKey = "arm_cpu"; + const Array kExtraKeys = {"arm_cpu", "cpu"}; if (!existing_keys) { - return {kExtraKey}; + return kExtraKeys; } Array keys = existing_keys.value(); - if (std::find(keys.begin(), keys.end(), kExtraKey) == keys.end()) { - keys.push_back(kExtraKey); + for (String key : kExtraKeys) { + if (std::find(keys.begin(), keys.end(), key) == keys.end()) { + keys.push_back(key); + } } return keys; } From 53c0271ac275c62f6469d3ac0dbf72bc958dd766 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Wed, 17 Aug 2022 13:26:52 -0700 Subject: [PATCH 4/5] Restore the mprofile cpptest, since the "cpu" key is back --- tests/cpp/target/parsers/mprofile_test.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/cpp/target/parsers/mprofile_test.cc b/tests/cpp/target/parsers/mprofile_test.cc index 0dcc23141ddb..97fb227e4190 100644 --- a/tests/cpp/target/parsers/mprofile_test.cc +++ b/tests/cpp/target/parsers/mprofile_test.cc @@ -69,8 +69,9 @@ TEST(MProfileParser, ParseTarget) { TargetJSON target = ParseTarget({}); TargetFeatures features = Downcast(target.at("features")); Array keys = Downcast>(target.at("keys")); - ASSERT_EQ(keys.size(), 1); + ASSERT_EQ(keys.size(), 2); ASSERT_EQ(keys[0], "arm_cpu"); + ASSERT_EQ(keys[1], "cpu"); ASSERT_EQ(Downcast(features.at("has_mve")), false); ASSERT_EQ(Downcast(features.at("has_dsp")), false); From 6f5c1b176ce48a54faf8a8da58a76b27fe512183 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Wed, 17 Aug 2022 14:42:26 -0700 Subject: [PATCH 5/5] So the -device attribute is actually needed... --- python/tvm/target/target.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/python/tvm/target/target.py b/python/tvm/target/target.py index 9c43f59fb865..0a9fc3ff74f2 100644 --- a/python/tvm/target/target.py +++ b/python/tvm/target/target.py @@ -531,7 +531,7 @@ def arm_cpu(model="unknown", options=None): } pre_defined_opt = trans_table.get(model, ["-model=%s" % model]) - opts = ["-keys=arm_cpu,cpu"] + pre_defined_opt + opts = ["-keys=arm_cpu,cpu", "-device=arm_cpu"] + pre_defined_opt opts = _merge_opts(opts, options) return Target(" ".join(["llvm"] + opts)) @@ -612,7 +612,7 @@ def riscv_cpu(model="sifive-u54", options=None): } pre_defined_opt = trans_table.get(model, ["-model=%s" % model]) - opts = ["-keys=arm_cpu,cpu"] + pre_defined_opt + opts = ["-keys=arm_cpu,cpu", "-device=arm_cpu"] + pre_defined_opt opts = _merge_opts(opts, options) return Target(" ".join(["llvm"] + opts)) @@ -756,22 +756,22 @@ def create_tvm_options(cpu_ver, config): # pylint: disable=unused-argument STM32_SUPPORTED_SERIES = { # High-Performance - "stm32H7xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m7", "-march=armv7e-m"], - "stm32F7xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m7"], - "stm32F4xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m4"], - "stm32F2xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m3"], + "stm32H7xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m7", "-march=armv7e-m"], + "stm32F7xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m7"], + "stm32F4xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m4"], + "stm32F2xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m3"], # Mainstream - "stm32G0xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m0+"], - "stm32F0xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m0"], - "stm32F1xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m3"], - "stm32G4xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m4"], - "stm32F3xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m4"], + "stm32G0xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m0+"], + "stm32F0xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m0"], + "stm32F1xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m3"], + "stm32G4xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m4"], + "stm32F3xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m4"], # Low-power - "stm32U5xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m33"], - "stm32L5xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m33"], - "stm32L4xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m4"], - "stm32L1xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m3"], - "stm32L0xx": ["-keys=arm_cpu,cpu", "-mcpu=cortex-m0+"], + "stm32U5xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m33"], + "stm32L5xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m33"], + "stm32L4xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m4"], + "stm32L1xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m3"], + "stm32L0xx": ["-keys=arm_cpu,cpu", "-device=arm_cpu", "-mcpu=cortex-m0+"], }