Allow defining `IREE_HOST_SIZE_T` to other types. (#14040)
I tried redefining these:
```c
// iree/base/config.h
#if !defined(IREE_HOST_SIZE_T)
#define IREE_HOST_SIZE_T size_t
#define PRIhsz "zu"
#endif // !IREE_HOST_SIZE_T
```
to `uint32_t` and `PRIu32` while debugging some issues with Emscripten
builds, but many parts of the project failed to build.
`IREE_HOST_SIZE_T` should really be set to `size_t` in most/all cases,
but the project should still build if someone wants to redefine it.
Some cases were obviously using the wrong format specifier (e.g. `zu`
instead of `PRIhsz`), but other were more subtle (e.g. interop between
`std::string` with `iree_string_view_t` or using `SIZE_MAX` instead of
`IREE_HOST_SIZE_MAX`). I tried to update as many places as I could
safely.
diff --git a/runtime/bindings/python/hal.cc b/runtime/bindings/python/hal.cc
index accae92..eb65c5b 100644
--- a/runtime/bindings/python/hal.cc
+++ b/runtime/bindings/python/hal.cc
@@ -268,7 +268,8 @@
py::object HalDriver::Create(const std::string& device_uri,
py::dict& driver_cache) {
iree_string_view_t driver_name, device_path, params_str;
- iree_string_view_t device_uri_sv{device_uri.data(), device_uri.size()};
+ iree_string_view_t device_uri_sv{
+ device_uri.data(), static_cast<iree_host_size_t>(device_uri.size())};
iree_uri_split(device_uri_sv, &driver_name, &device_path, ¶ms_str);
// Check cache.
@@ -393,7 +394,8 @@
HalDevice HalDriver::CreateDeviceByURI(std::string& device_uri,
const py::kwargs& kwargs) {
iree_hal_device_t* device;
- iree_string_view_t device_uri_sv{device_uri.data(), device_uri.size()};
+ iree_string_view_t device_uri_sv{
+ device_uri.data(), static_cast<iree_host_size_t>(device_uri.size())};
CheckApiStatus(
iree_hal_driver_create_device_by_uri(raw_ptr(), device_uri_sv,
iree_allocator_system(), &device),
diff --git a/runtime/bindings/python/py_module.cc b/runtime/bindings/python/py_module.cc
index 1f7ccd3..f3b8b34 100644
--- a/runtime/bindings/python/py_module.cc
+++ b/runtime/bindings/python/py_module.cc
@@ -58,7 +58,8 @@
static iree_string_view_t ModuleName(void* vself) {
auto self = AsSelf(vself);
- return {self->module_name_.data(), self->module_name_.size()};
+ return {self->module_name_.data(),
+ static_cast<iree_host_size_t>(self->module_name_.size())};
}
static iree_vm_module_signature_t ModuleSignature(void* vself) {
@@ -93,11 +94,12 @@
out_function->ordinal = ordinal;
}
if (IREE_LIKELY(out_name)) {
- *out_name = {f->name.data(), f->name.size()};
+ *out_name = {f->name.data(),
+ static_cast<iree_host_size_t>(f->name.size())};
}
if (IREE_LIKELY(out_signature)) {
- out_signature->calling_convention = {f->cconv.data(),
- f->cconv.size()};
+ out_signature->calling_convention = {
+ f->cconv.data(), static_cast<iree_host_size_t>(f->cconv.size())};
}
return iree_ok_status();
}
@@ -239,9 +241,11 @@
std::move(name), std::move(cconv), std::move(callable));
exports_.push_back({});
iree_vm_native_export_descriptor_t& d = exports_.back();
- d.local_name = {py_function->name.data(), py_function->name.size()};
- d.calling_convention = {py_function->cconv.data(),
- py_function->cconv.size()};
+ d.local_name = {py_function->name.data(),
+ static_cast<iree_host_size_t>(py_function->name.size())};
+ d.calling_convention = {
+ py_function->cconv.data(),
+ static_cast<iree_host_size_t>(py_function->cconv.size())};
d.attr_count = 0;
d.attrs = nullptr;
std::string& alloced_name = py_function->name;
@@ -261,7 +265,8 @@
AssertMutable();
initialized_ = true;
memset(&descriptor_, 0, sizeof(descriptor_));
- descriptor_.name = {module_name_.data(), module_name_.size()};
+ descriptor_.name = {module_name_.data(),
+ static_cast<iree_host_size_t>(module_name_.size())};
descriptor_.version = version_;
descriptor_.attr_count = attrs_.size();
descriptor_.attrs = attrs_.empty() ? nullptr : attrs_.data();
@@ -298,7 +303,8 @@
iree_status_t ParseCconv() {
iree_vm_function_signature_t signature;
memset(&signature, 0, sizeof(signature));
- signature.calling_convention = {cconv.data(), cconv.size()};
+ signature.calling_convention = {
+ cconv.data(), static_cast<iree_host_size_t>(cconv.size())};
IREE_RETURN_IF_ERROR(iree_vm_function_call_get_cconv_fragments(
&signature, &cconv_arguments, &cconv_results));
@@ -324,10 +330,11 @@
&packed_arguments_required_size));
if (IREE_UNLIKELY(packed_arguments_required_size !=
call.arguments.data_length)) {
- return iree_make_status(
- IREE_STATUS_INVALID_ARGUMENT,
- "mismatched packed argument size: actual=%zu, required=%zu",
- call.arguments.data_length, packed_arguments_required_size);
+ return iree_make_status(IREE_STATUS_INVALID_ARGUMENT,
+ "mismatched packed argument size: actual=%" PRIhsz
+ ", required=%" PRIhsz,
+ call.arguments.data_length,
+ packed_arguments_required_size);
}
// Unpack arguments.
diff --git a/runtime/bindings/python/vm.cc b/runtime/bindings/python/vm.cc
index 9a3a4fd..c39a374 100644
--- a/runtime/bindings/python/vm.cc
+++ b/runtime/bindings/python/vm.cc
@@ -205,7 +205,8 @@
const std::string& name, iree_vm_function_linkage_t linkage) {
iree_vm_function_t f;
auto status = iree_vm_module_lookup_function_by_name(
- raw_ptr(), linkage, {name.data(), name.size()}, &f);
+ raw_ptr(), linkage,
+ {name.data(), static_cast<iree_host_size_t>(name.size())}, &f);
if (iree_status_is_not_found(status)) {
iree_status_ignore(status);
return std::nullopt;
@@ -388,7 +389,7 @@
}
// Extract dims from the buffer view.
- size_t rank = 0;
+ iree_host_size_t rank = 0;
std::vector<iree_hal_dim_t> dims(6);
iree_status_t status = iree_hal_buffer_view_shape(
buffer_view, dims.capacity(), dims.data(), &rank);