Skip to main content

iree_embedded/
instance.rs

1//! The VM instance: process-wide IREE state with HAL types registered.
2
3use crate::{Arena, Result, check};
4use iree_embedded_sys as sys;
5
6/// The IREE VM instance: the top-level runtime object shared across sessions.
7pub struct Instance {
8    raw: *mut sys::iree_vm_instance_t,
9}
10
11impl Instance {
12    /// Create a VM instance, allocating from `arena`.
13    pub fn new(arena: &Arena) -> Result<Self> {
14        let mut raw = core::ptr::null_mut();
15        // SAFETY: out-pointer is valid; allocator outlives the instance.
16        unsafe {
17            check(sys::iree_vm_instance_create(
18                sys::IREE_VM_TYPE_CAPACITY_DEFAULT as sys::iree_host_size_t,
19                arena.as_iree_allocator(),
20                &mut raw,
21            ))?;
22            // HAL custom types must be registered before HAL modules are used.
23            check(sys::iree_hal_module_register_all_types(raw))?;
24        }
25        Ok(Instance { raw })
26    }
27
28    pub(crate) fn raw(&self) -> *mut sys::iree_vm_instance_t {
29        self.raw
30    }
31}
32
33impl Drop for Instance {
34    fn drop(&mut self) {
35        // SAFETY: raw was created by iree_vm_instance_create and not released.
36        unsafe { sys::iree_vm_instance_release(self.raw) };
37    }
38}