Skip to content

Commit 037f131

Browse files
committed
Separate datatypes parsing to its own function
1 parent 2c61a16 commit 037f131

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

include/tvm/runtime/packed_func.h

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,20 @@ namespace runtime {
6969
TVM_DLL std::string GetCustomTypeName(uint8_t type_code);
7070

7171
/*!
72-
* \brief Runtime utility for getting custom type code from name
73-
* \param type_name Custom type name
74-
* \return Custom type code
75-
*/
76-
TVM_DLL uint8_t GetCustomTypeCode(const std::string& type_name);
77-
78-
/*!
79-
* \brief Runtime utility for checking whether custom type is registered
80-
* \param type_code Custom type code
81-
* \return Bool representing whether type is registered
82-
*/
72+
* \brief Runtime utility for checking whether custom type is registered
73+
* \param type_code Custom type code
74+
* \return Bool representing whether type is registered
75+
*/
8376
TVM_DLL bool GetCustomTypeRegistered(uint8_t type_code);
8477

78+
/*!
79+
* \brief Runtime utility for parsing string of the form "custom[<typename>]"
80+
* \param s String to parse
81+
* \param scan pointer to parsing pointer, which is scanning across s
82+
* \return type code of custom type parsed
83+
*/
84+
TVM_DLL uint8_t ParseCustomDatatype(const std::string& s, const char** scan);
85+
8586
// forward declarations
8687
class TVMArgs;
8788
class TVMArgValue;
@@ -1025,22 +1026,7 @@ inline TVMType String2TVMType(std::string s) {
10251026
t.lanes = 1;
10261027
return t;
10271028
} else if (s.substr(0, 6) == "custom") {
1028-
// TODO(gus) this should be separated out into its own parsing function and cleaned up, or
1029-
// replaced by a regex.
1030-
scan = s.c_str() + 6;
1031-
if (*scan != '[')
1032-
LOG(FATAL) << "expected opening brace after 'custom' type in" << s;
1033-
++scan;
1034-
size_t custom_name_len = 0;
1035-
while (scan + custom_name_len <= s.c_str() + s.length() &&
1036-
*(scan + custom_name_len) != ']')
1037-
++custom_name_len;
1038-
if (*(scan + custom_name_len) != ']')
1039-
LOG(FATAL) << "expected closing brace after 'custom' type in" << s;
1040-
scan += custom_name_len + 1;
1041-
1042-
auto type_name = s.substr(7, custom_name_len);
1043-
t.code = GetCustomTypeCode(type_name);
1029+
t.code = ParseCustomDatatype(s, &scan);
10441030
} else {
10451031
scan = s.c_str();
10461032
LOG(FATAL) << "unknown type " << s;

src/runtime/c_runtime_api.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,34 @@ bool GetCustomTypeRegistered(uint8_t type_code) {
6363
return (*f)(type_code).operator bool();
6464
}
6565

66+
uint8_t ParseCustomDatatype(const std::string& s, const char** scan) {
67+
CHECK(s.substr(0, 6) == "custom") << "Not a valid custom datatype string";
68+
69+
auto tmp = s.c_str();
70+
71+
CHECK(s.c_str() == tmp);
72+
*scan = s.c_str() + 6;
73+
CHECK(s.c_str() == tmp);
74+
if (**scan != '[') LOG(FATAL) << "expected opening brace after 'custom' type in" << s;
75+
CHECK(s.c_str() == tmp);
76+
*scan += 1;
77+
CHECK(s.c_str() == tmp);
78+
size_t custom_name_len = 0;
79+
CHECK(s.c_str() == tmp);
80+
while (*scan + custom_name_len <= s.c_str() + s.length() && *(*scan + custom_name_len) != ']')
81+
++custom_name_len;
82+
CHECK(s.c_str() == tmp);
83+
if (*(*scan + custom_name_len) != ']')
84+
LOG(FATAL) << "expected closing brace after 'custom' type in" << s;
85+
CHECK(s.c_str() == tmp);
86+
*scan += custom_name_len + 1;
87+
CHECK(s.c_str() == tmp);
88+
89+
auto type_name = s.substr(7, custom_name_len);
90+
CHECK(s.c_str() == tmp);
91+
return GetCustomTypeCode(type_name);
92+
}
93+
6694
class DeviceAPIManager {
6795
public:
6896
static const int kMaxDeviceAPI = 32;

0 commit comments

Comments
 (0)