Skip to content

Commit

Permalink
[Target] Only append default keys if target doesn't have any yet (#12474
Browse files Browse the repository at this point in the history
)

* [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.

* Add "cpu" to ARM CPU keys

* Add "cpu" to the keys in the mprofile target parser

* Restore the mprofile cpptest, since the "cpu" key is back

* So the -device attribute is actually needed...
  • Loading branch information
Krzysztof Parzyszek authored Aug 18, 2022
1 parent d1e6f39 commit 6def53a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
32 changes: 16 additions & 16 deletions python/tvm/target/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "-device=arm_cpu"] + pre_defined_opt
opts = _merge_opts(opts, options)
return Target(" ".join(["llvm"] + opts))

Expand Down Expand Up @@ -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", "-device=arm_cpu"] + pre_defined_opt
opts = _merge_opts(opts, options)
return Target(" ".join(["llvm"] + opts))

Expand Down Expand Up @@ -762,22 +762,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", "-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": ["-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", "-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": ["-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", "-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+"],
}


Expand Down
10 changes: 6 additions & 4 deletions src/target/parsers/mprofile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ static TargetFeatures GetFeatures(TargetJSON target) {
}

static Array<String> MergeKeys(Optional<Array<String>> existing_keys) {
const String kExtraKey = "arm_cpu";
const Array<String> kExtraKeys = {"arm_cpu", "cpu"};

if (!existing_keys) {
return {kExtraKey};
return kExtraKeys;
}

Array<String> 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;
}
Expand Down
11 changes: 7 additions & 4 deletions src/target/target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ ObjectPtr<Object> TargetInternal::FromConfig(Map<String, ObjectRef> config) {
// parse "keys"
{
std::vector<String> 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<ArrayNode>()) {
for (const ObjectRef& e : *cfg_keys) {
Expand All @@ -909,9 +910,11 @@ ObjectPtr<Object> TargetInternal::FromConfig(Map<String, ObjectRef> config) {
keys.push_back(GetRef<String>(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);
Expand Down
3 changes: 2 additions & 1 deletion tests/cpp/target/parsers/mprofile_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ TEST(MProfileParser, ParseTarget) {
TargetJSON target = ParseTarget({});
TargetFeatures features = Downcast<TargetFeatures>(target.at("features"));
Array<String> keys = Downcast<Array<String>>(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<Bool>(features.at("has_mve")), false);
ASSERT_EQ(Downcast<Bool>(features.at("has_dsp")), false);
Expand Down
3 changes: 1 addition & 2 deletions tests/cpp/target_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ TEST(TargetCreationFail, TargetKindNotFound) {
TEST(TargetCreation, TargetParser) {
Target test_target("TestTargetParser -mcpu=woof");
ASSERT_EQ(test_target->GetAttr<String>("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) {
Expand Down

0 comments on commit 6def53a

Please sign in to comment.