Skip to content

Commit

Permalink
[metal] Fix creating device via registry path (iree-org#15142)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
antiagainst authored Oct 12, 2023
1 parent 374f854 commit ef25b0c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 9 additions & 3 deletions runtime/src/iree/base/string_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/iree/base/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions runtime/src/iree/hal/drivers/metal/metal_driver.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ef25b0c

Please sign in to comment.