Finally moving VM type registration to iree_vm_instance_t. (#12650)
This allows for thread-safe type registration scoped to instances and
unregistration of types as required by types in dynamically loaded
modules that may wink out of existence at some point. The main trick
here was changing the type ID from an ordinal in the type table to just
the pointer of the type descriptor. This requires an extra 4 bytes per
ref on 64-bit systems but who cares - now there's no round-tripping
through the type table for common operations.
As part of simplifying the way types are referenced VM type descriptors
are now hidden behind iree_vm_ref_type_t. This makes refs much easier to
work with as there's only one way to reference types and it always
bottoms out on the registered descriptor
handle. It also allows us to remove some type descriptor indirection
we'd previously required in order to get reference counter offsets as we
can share the same packed type identifier in type defs, refs, or lists.
Thanks to @simon-camp for the required EmitC changes!
---------
Co-authored-by: Simon Camphausen <simon.camphausen@iml.fraunhofer.de>
diff --git a/tools/android/run_module_app/src/main.cc b/tools/android/run_module_app/src/main.cc
index 739c3fe..4d9b804 100644
--- a/tools/android/run_module_app/src/main.cc
+++ b/tools/android/run_module_app/src/main.cc
@@ -92,7 +92,8 @@
Status RunModule(const IreeModuleInvocation& invocation) {
iree_vm_instance_t* instance = nullptr;
IREE_RETURN_IF_ERROR(
- iree_vm_instance_create(iree_allocator_system(), &instance),
+ iree_vm_instance_create(IREE_VM_TYPE_CAPACITY_DEFAULT,
+ iree_allocator_system(), &instance),
"creating instance");
IREE_RETURN_IF_ERROR(iree_hal_module_register_all_types(instance),
"registering HAL types");
@@ -145,8 +146,9 @@
iree_allocator_system(), &inputs));
vm::ref<iree_vm_list_t> outputs;
- IREE_RETURN_IF_ERROR(iree_vm_list_create(/*element_type=*/nullptr, 16,
- iree_allocator_system(), &outputs));
+ IREE_RETURN_IF_ERROR(iree_vm_list_create(iree_vm_make_undefined_type_def(),
+ 16, iree_allocator_system(),
+ &outputs));
LOGI("Execute @%s", function_name.c_str());
IREE_RETURN_IF_ERROR(
diff --git a/tools/iree-benchmark-module-main.cc b/tools/iree-benchmark-module-main.cc
index f04f4d1..b242b64 100644
--- a/tools/iree-benchmark-module-main.cc
+++ b/tools/iree-benchmark-module-main.cc
@@ -167,7 +167,7 @@
IREE_TRACE_FRAME_MARK();
vm::ref<iree_vm_list_t> outputs;
- IREE_CHECK_OK(iree_vm_list_create(/*element_type=*/nullptr, 16,
+ IREE_CHECK_OK(iree_vm_list_create(iree_vm_make_undefined_type_def(), 16,
iree_allocator_system(), &outputs));
// Benchmarking loop.
@@ -289,7 +289,7 @@
// Setup empty outputs.
vm::ref<iree_vm_list_t> outputs;
- IREE_CHECK_OK(iree_vm_list_create(/*element_type=*/nullptr, 16,
+ IREE_CHECK_OK(iree_vm_list_create(iree_vm_make_undefined_type_def(), 16,
host_allocator, &outputs));
invocation_outputs.push_back(std::move(outputs));
}
@@ -362,13 +362,13 @@
IREE_TRACE_FRAME_MARK();
vm::ref<iree_vm_list_t> inputs;
- IREE_CHECK_OK(iree_vm_list_create(/*element_type=*/nullptr, 16,
+ IREE_CHECK_OK(iree_vm_list_create(iree_vm_make_undefined_type_def(), 16,
iree_allocator_system(), &inputs));
iree_vm_value_t batch_size = iree_vm_value_make_i32(FLAG_batch_size);
IREE_CHECK_OK(iree_vm_list_push_value(inputs.get(), &batch_size));
vm::ref<iree_vm_list_t> outputs;
- IREE_CHECK_OK(iree_vm_list_create(/*element_type=*/nullptr, 16,
+ IREE_CHECK_OK(iree_vm_list_create(iree_vm_make_undefined_type_def(), 16,
iree_allocator_system(), &outputs));
// Benchmarking loop.
diff --git a/tools/iree-benchmark-trace-main.c b/tools/iree-benchmark-trace-main.c
index e07aedf..7951249 100644
--- a/tools/iree-benchmark-trace-main.c
+++ b/tools/iree-benchmark-trace-main.c
@@ -277,7 +277,8 @@
// Setup shared instance used for each benchmark.
iree_vm_instance_t* instance = NULL;
- IREE_CHECK_OK(iree_vm_instance_create(host_allocator, &instance));
+ IREE_CHECK_OK(iree_vm_instance_create(IREE_VM_TYPE_CAPACITY_DEFAULT,
+ host_allocator, &instance));
// Parse all of stdin right away. The traces we run may source things from it
// and because we're running in a benchmark loop it'll quickly drain. To
diff --git a/tools/iree-e2e-matmul-test.c b/tools/iree-e2e-matmul-test.c
index 68892af..a0fe5b1 100644
--- a/tools/iree-e2e-matmul-test.c
+++ b/tools/iree-e2e-matmul-test.c
@@ -302,7 +302,7 @@
iree_vm_type_def_t elem_type = iree_vm_list_element_type(src);
iree_host_size_t size = iree_vm_list_size(src);
iree_allocator_t allocator = iree_hal_allocator_host_allocator(hal_allocator);
- IREE_RETURN_IF_ERROR(iree_vm_list_create(&elem_type, size, allocator, dst));
+ IREE_RETURN_IF_ERROR(iree_vm_list_create(elem_type, size, allocator, dst));
IREE_RETURN_IF_ERROR(iree_vm_list_resize(*dst, size));
for (iree_host_size_t i = 0; i < size; ++i) {
iree_hal_buffer_view_t* src_elem = NULL;
@@ -312,7 +312,7 @@
src_elem, &dst_elem));
iree_vm_ref_t dst_elem_ref = {0};
IREE_RETURN_IF_ERROR(iree_vm_ref_wrap_assign(
- dst_elem, iree_hal_buffer_view_type_id(), &dst_elem_ref));
+ dst_elem, iree_hal_buffer_view_type(), &dst_elem_ref));
IREE_RETURN_IF_ERROR(iree_vm_list_set_ref_move(*dst, i, &dst_elem_ref));
}
return iree_ok_status();
@@ -987,7 +987,7 @@
iree_host_size_t size = iree_vm_list_size(src_list);
iree_allocator_t allocator = iree_hal_allocator_host_allocator(hal_allocator);
IREE_RETURN_IF_ERROR(
- iree_vm_list_create(&elem_type, size, allocator, dst_list));
+ iree_vm_list_create(elem_type, size, allocator, dst_list));
IREE_RETURN_IF_ERROR(iree_vm_list_resize(*dst_list, size));
for (iree_host_size_t i = 0; i < size; ++i) {
iree_hal_buffer_view_t* src = NULL;
@@ -997,7 +997,7 @@
device, hal_allocator, src, mask[i], &dst));
iree_vm_ref_t dst_ref = {0};
IREE_RETURN_IF_ERROR(
- iree_vm_ref_wrap_assign(dst, iree_hal_buffer_view_type_id(), &dst_ref));
+ iree_vm_ref_wrap_assign(dst, iree_hal_buffer_view_type(), &dst_ref));
IREE_RETURN_IF_ERROR(iree_vm_list_set_ref_move(*dst_list, i, &dst_ref));
}
return iree_ok_status();
@@ -1039,7 +1039,7 @@
// Invoke the function to produce the actual result.
iree_vm_list_t* device_outputs = NULL;
if (iree_status_is_ok(status)) {
- status = iree_vm_list_create(/*element_type=*/NULL,
+ status = iree_vm_list_create(iree_vm_make_undefined_type_def(),
/*initial_capacity=*/8, replay->host_allocator,
&device_outputs);
}
@@ -1356,8 +1356,8 @@
}
iree_vm_instance_t* instance = NULL;
- iree_status_t status =
- iree_vm_instance_create(iree_allocator_system(), &instance);
+ iree_status_t status = iree_vm_instance_create(
+ IREE_VM_TYPE_CAPACITY_DEFAULT, iree_allocator_system(), &instance);
if (iree_status_is_ok(status)) {
status = run_trace_files(argc - 1, argv + 1, instance);
}
diff --git a/tools/iree-run-mlir-main.cc b/tools/iree-run-mlir-main.cc
index 0cc309c..bb1b4b5 100644
--- a/tools/iree-run-mlir-main.cc
+++ b/tools/iree-run-mlir-main.cc
@@ -371,8 +371,8 @@
// Prepare outputs list to accept the results from the invocation.
vm::ref<iree_vm_list_t> outputs;
- IREE_RETURN_IF_ERROR(iree_vm_list_create(/*element_type=*/nullptr, 16,
- host_allocator, &outputs));
+ IREE_RETURN_IF_ERROR(iree_vm_list_create(iree_vm_make_undefined_type_def(),
+ 16, host_allocator, &outputs));
// Synchronously invoke the function.
IREE_RETURN_IF_ERROR(iree_vm_invoke(
diff --git a/tools/iree-run-module-main.cc b/tools/iree-run-module-main.cc
index 7f5a439..aaf3837 100644
--- a/tools/iree-run-module-main.cc
+++ b/tools/iree-run-module-main.cc
@@ -136,8 +136,8 @@
&finish_fence));
vm::ref<iree_vm_list_t> outputs;
- IREE_RETURN_IF_ERROR(iree_vm_list_create(/*element_type=*/nullptr, 16,
- host_allocator, &outputs));
+ IREE_RETURN_IF_ERROR(iree_vm_list_create(iree_vm_make_undefined_type_def(),
+ 16, host_allocator, &outputs));
printf("EXEC @%s\n", function_name.c_str());
IREE_RETURN_IF_ERROR(
diff --git a/tools/iree-run-trace-main.c b/tools/iree-run-trace-main.c
index 502c7a2..fc9563b 100644
--- a/tools/iree-run-trace-main.c
+++ b/tools/iree-run-trace-main.c
@@ -363,8 +363,8 @@
}
iree_vm_instance_t* instance = NULL;
- iree_status_t status =
- iree_vm_instance_create(iree_allocator_system(), &instance);
+ iree_status_t status = iree_vm_instance_create(
+ IREE_VM_TYPE_CAPACITY_DEFAULT, iree_allocator_system(), &instance);
if (iree_status_is_ok(status)) {
status = iree_run_trace_files(argc - 1, argv + 1, instance);
}