From ef25b0c5d09d1a88755030c6aee21bde24fdc5b1 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 12 Oct 2023 19:53:42 -0400 Subject: [PATCH] [metal] Fix creating device via registry path (#15142) The registry ID is effectively a uint64_t value. Previously we were parsing it as hex bytes and casting the whole. It was wrong for little endian format. This commit switches to parse the number using `iree_string_view_atoi_uint64` directly. --- runtime/src/iree/base/string_view.c | 12 +++++++++--- runtime/src/iree/base/string_view.h | 3 +++ runtime/src/iree/hal/drivers/metal/metal_driver.m | 11 +++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/runtime/src/iree/base/string_view.c b/runtime/src/iree/base/string_view.c index d1983299d92b..8e5c95cf1417 100644 --- a/runtime/src/iree/base/string_view.c +++ b/runtime/src/iree/base/string_view.c @@ -375,8 +375,9 @@ IREE_API_EXPORT bool iree_string_view_atoi_int64(iree_string_view_t value, return parsed_value != 0 || errno == 0; } -IREE_API_EXPORT bool iree_string_view_atoi_uint64(iree_string_view_t value, - uint64_t* out_value) { +IREE_API_EXPORT bool iree_string_view_atoi_uint64_base(iree_string_view_t value, + int base, + uint64_t* out_value) { // Copy to scratch memory with a NUL terminator. char temp[32] = {0}; if (value.size >= IREE_ARRAYSIZE(temp)) return false; @@ -385,13 +386,18 @@ IREE_API_EXPORT bool iree_string_view_atoi_uint64(iree_string_view_t value, // Attempt to parse. errno = 0; char* end = NULL; - unsigned long long parsed_value = strtoull(temp, &end, 0); + unsigned long long parsed_value = strtoull(temp, &end, base); if (temp == end) return false; if (parsed_value == ULLONG_MAX && errno == ERANGE) return false; *out_value = (uint64_t)parsed_value; return parsed_value != 0 || errno == 0; } +IREE_API_EXPORT bool iree_string_view_atoi_uint64(iree_string_view_t value, + uint64_t* out_value) { + return iree_string_view_atoi_uint64_base(value, 0, out_value); +} + IREE_API_EXPORT bool iree_string_view_atof(iree_string_view_t value, float* out_value) { // Copy to scratch memory with a NUL terminator. diff --git a/runtime/src/iree/base/string_view.h b/runtime/src/iree/base/string_view.h index b342dabe454c..313e3db5c6a2 100644 --- a/runtime/src/iree/base/string_view.h +++ b/runtime/src/iree/base/string_view.h @@ -210,6 +210,9 @@ IREE_API_EXPORT bool iree_string_view_atoi_uint32(iree_string_view_t value, uint32_t* out_value); IREE_API_EXPORT bool iree_string_view_atoi_int64(iree_string_view_t value, int64_t* out_value); +IREE_API_EXPORT bool iree_string_view_atoi_uint64_base(iree_string_view_t value, + int base, + uint64_t* out_value); IREE_API_EXPORT bool iree_string_view_atoi_uint64(iree_string_view_t value, uint64_t* out_value); IREE_API_EXPORT bool iree_string_view_atof(iree_string_view_t value, diff --git a/runtime/src/iree/hal/drivers/metal/metal_driver.m b/runtime/src/iree/hal/drivers/metal/metal_driver.m index 993b5ac366c6..a83191702420 100644 --- a/runtime/src/iree/hal/drivers/metal/metal_driver.m +++ b/runtime/src/iree/hal/drivers/metal/metal_driver.m @@ -414,12 +414,11 @@ static iree_status_t iree_hal_metal_driver_create_device_by_path( } // Try parsing as a device ID. - uint8_t device_registry_id[8] = {0}; - if (iree_string_view_parse_hex_bytes(device_path, IREE_ARRAYSIZE(device_registry_id), - device_registry_id)) { - return iree_hal_metal_driver_create_device_by_registry_id( - base_driver, driver_name, *(uint64_t*)device_registry_id, param_count, params, - host_allocator, out_device); + uint64_t device_registry_id = 0; + if (iree_string_view_atoi_uint64_base(device_path, 16, &device_registry_id)) { + return iree_hal_metal_driver_create_device_by_registry_id(base_driver, driver_name, + device_registry_id, param_count, + params, host_allocator, out_device); } // Fallback and try to parse as a device index.