Skip to content

Commit

Permalink
[Target] Allow 'true' and 'false' strings in conversions to integer (a…
Browse files Browse the repository at this point in the history
…pache#8254)

* [Target] Allow 'true' and 'false' strings in conversions to integer

This will allow Bool parameters to take true/false values instead
of 0 and 1 only.

* Convert the string to lowercase.

* Reserve memory for lowercase string

* Add include <cctype>
  • Loading branch information
Krzysztof Parzyszek authored and trevor-m committed Jun 17, 2021
1 parent 9262699 commit a316906
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/target/target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <tvm/tir/expr.h>

#include <algorithm>
#include <cctype>
#include <stack>

#include "../runtime/object_internal.h"
Expand Down Expand Up @@ -210,7 +211,17 @@ ObjectRef TargetInternal::ParseType(const std::string& str,
// Parsing integer
int v;
if (!(is >> v)) {
throw Error(": Cannot parse into type \"Integer\" from string: " + str);
std::string lower(str.size(), '\x0');
std::transform(str.begin(), str.end(), lower.begin(),
[](unsigned char c) { return std::tolower(c); });
// Bool is a subclass of IntImm, so allow textual boolean values.
if (lower == "true") {
v = 1;
} else if (lower == "false") {
v = 0;
} else {
throw Error(": Cannot parse into type \"Integer\" from string: " + str);
}
}
return Integer(v);
} else if (info.type_index == String::ContainerType::_GetOrAllocRuntimeTypeIndex()) {
Expand Down
11 changes: 11 additions & 0 deletions tests/python/unittest/test_target_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,16 @@ def test_check_and_update_host_consist_3():
assert target.host == host


def test_target_attr_bool_value():
target0 = Target("llvm --link-params=True")
assert target0.attrs["link-params"] == 1
target1 = Target("llvm --link-params=true")
assert target1.attrs["link-params"] == 1
target2 = Target("llvm --link-params=False")
assert target2.attrs["link-params"] == 0
target3 = Target("llvm --link-params=false")
assert target3.attrs["link-params"] == 0


if __name__ == "__main__":
sys.exit(pytest.main([__file__] + sys.argv[1:]))

0 comments on commit a316906

Please sign in to comment.