executorch_sys/cxx_bridge/module.rs
1// Clippy doesnt detect the 'Safety' comments in the cxx bridge.
2#![allow(clippy::missing_safety_doc)]
3// The ET_-prefixed C bridge type names are not UpperCamelCase.
4#![allow(non_camel_case_types)]
5
6use cxx::{ExternType, type_id};
7
8#[cxx::bridge]
9pub(crate) mod ffi {
10
11 unsafe extern "C++" {
12 include!("executorch-sys/cpp/executorch_rs/cxx_bridge.hpp");
13
14 /// Cpp executorch error type.
15 type ET_Error = crate::ET_Error;
16
17 /// Types of validation that a `ET_Program` can do before parsing the data.
18 type ET_ProgramVerification = crate::ET_ProgramVerification;
19
20 /// Describes a method in an ExecuTorch program.
21 ///
22 /// The program used to create a ET_MethodMeta object must outlive the ET_MethodMeta.
23 /// It is separate from ET_Method so that this information can be accessed without
24 /// paying the initialization cost of loading the full ET_Method.
25 type ET_MethodMeta = crate::ET_MethodMeta;
26
27 /// A facade class for loading programs and executing methods within them.
28 #[namespace = "executorch::extension"]
29 type Module;
30
31 /// Enum to define loading behavior.
32 type ET_ModuleLoadMode = crate::ET_ModuleLoadMode;
33
34 /// A specification of `ArrayRef<EValue>`.
35 type ET_ArrayRefEValue = crate::ET_ArrayRefEValue;
36
37 /// A vector of `EValue`.
38 type ET_VecEValue = crate::ET_VecEValue;
39
40 /// EventTracer is a class that users can inherit and implement to log/serialize/stream etc.
41 #[namespace = "executorch::runtime"]
42 type EventTracer;
43
44 /// An allocator used to allocate objects for the runtime.
45 type ET_MemoryAllocator = crate::ET_MemoryAllocator;
46
47 /// Redefinition of the [`ET_HierarchicalAllocator`](crate::ET_HierarchicalAllocator).
48 type ET_HierarchicalAllocator = crate::ET_HierarchicalAllocator;
49
50 /// Maps backend IDs to their load-time options.
51 type ET_LoadBackendOptionsMap = crate::ET_LoadBackendOptionsMap;
52
53 /// Constructs an instance by loading a program from a file with specified
54 /// memory locking behavior.
55 ///
56 /// # Arguments
57 ///
58 /// - `file_path`: The path to the ExecuTorch program file to load.
59 /// - `data_files`: The path to one or more .ptd file/s.
60 /// - `load_mode`: The loading mode to use.
61 /// - `event_tracer`: An EventTracer used for tracking and logging events, or null if not needed.
62 /// - `share_memory_arenas`: When true, all methods loaded by this Module share a single set of
63 /// memory-planned buffers.
64 #[namespace = "executorch_rs"]
65 fn Module_new(
66 file_path: &CxxString,
67 data_files: &[&str],
68 load_mode: ET_ModuleLoadMode,
69 event_tracer: UniquePtr<EventTracer>,
70 memory_allocator: UniquePtr<ET_MemoryAllocator>,
71 temp_allocator: UniquePtr<ET_MemoryAllocator>,
72 share_memory_arenas: bool,
73 ) -> UniquePtr<Module>;
74
75 /// Load the program if needed, optionally with per-delegate load-time options.
76 ///
77 /// # Arguments
78 ///
79 /// - `backend_options`: Per-delegate load-time options, or null. When non-null the Module
80 /// deep-copies it into internal storage, so the caller may drop the map immediately after
81 /// this returns.
82 /// - `verification`: The type of verification to do before returning success.
83 ///
84 /// # Returns
85 ///
86 /// An ET_Error to indicate success or failure of the loading process.
87 ///
88 /// # Safety
89 ///
90 /// `backend_options` must be null or point to a valid `ET_LoadBackendOptionsMap`.
91 #[namespace = "executorch_rs"]
92 unsafe fn Module_load(
93 self_: Pin<&mut Module>,
94 backend_options: *const ET_LoadBackendOptionsMap,
95 verification: ET_ProgramVerification,
96 ) -> ET_Error;
97
98 /// Returns the deep-copied LoadBackendOptionsMap most recently installed
99 /// via `load(LoadBackendOptionsMap, ...)`.
100 ///
101 /// If `load(LoadBackendOptionsMap, ...)` has never been called, returns a
102 /// default-constructed (empty, `size() == 0`) map.
103 ///
104 /// # Returns
105 ///
106 /// Const reference to the Module-owned LoadBackendOptionsMap.
107 #[namespace = "executorch_rs"]
108 fn Module_backend_options(self_: &Module) -> &ET_LoadBackendOptionsMap;
109
110 /// Checks if the program is loaded.
111 #[namespace = "executorch_rs"]
112 fn Module_is_loaded(self_: &Module) -> bool;
113
114 /// Get the number of methods available in the loaded program.
115 ///
116 /// # Safety
117 ///
118 /// The `method_num_out` is valid only if the function returns `ET_Error::Ok`.
119 #[namespace = "executorch_rs"]
120 unsafe fn Module_num_methods(
121 self_: Pin<&mut Module>,
122 method_num_out: *mut usize,
123 ) -> ET_Error;
124
125 /// Get a list of method names available in the loaded program.
126 ///
127 /// Loads the program and method if needed.
128 ///
129 /// # Arguments
130 ///
131 /// - `method_names_out`: A pointer to a (non initialized) vector that will be created and filled with
132 /// the method names.
133 ///
134 /// # Returns
135 ///
136 /// A error indicating whether the method names retrieval was successful or not.
137 ///
138 /// # Safety
139 ///
140 /// The `method_names_out` vector can be used only if the function returns `ET_Error::Ok`.
141 #[namespace = "executorch_rs"]
142 unsafe fn Module_method_names(
143 self_: Pin<&mut Module>,
144 method_names_out: *mut Vec<String>,
145 ) -> ET_Error;
146
147 /// Load a specific method from the program and set up memory management if
148 /// needed.
149 ///
150 /// The loaded method is cached to reuse the next time it's executed.
151 ///
152 /// # Arguments
153 ///
154 /// - `method_name`: The name of the method to load.
155 ///
156 /// # Returns
157 ///
158 /// An ET_Error to indicate success or failure.
159 #[namespace = "executorch_rs"]
160 unsafe fn Module_load_method(
161 self_: Pin<&mut Module>,
162 method_name: &CxxString,
163 planned_memory: *mut ET_HierarchicalAllocator,
164 event_tracer: *mut EventTracer,
165 ) -> ET_Error;
166
167 /// Unload a specific method from the program.
168 ///
169 /// # Arguments
170 /// - `method_name`: The name of the method to unload.
171 ///
172 /// # Returns
173 ///
174 /// True if the method is unloaded, false if no-op.
175 #[namespace = "executorch_rs"]
176 unsafe fn Module_unload_method(self_: Pin<&mut Module>, method_name: &CxxString) -> bool;
177
178 /// Checks if a specific method is loaded.
179 ///
180 /// # Arguments
181 ///
182 /// - `method_name`: The name of the method to check.
183 ///
184 /// # Returns
185 ///
186 /// `true` if the method specified by `method_name` is loaded, `false` otherwise.
187 #[namespace = "executorch_rs"]
188 fn Module_is_method_loaded(self_: &Module, method_name: &CxxString) -> bool;
189
190 /// Get a method metadata struct by method name.
191 ///
192 /// Loads the program if needed.
193 ///
194 /// # Arguments
195 ///
196 /// - `method_name`: The name of the method to get the metadata for.
197 /// - `method_meta_out`: A mutable reference to a `ET_MethodMeta` struct that will be filled with the metadata.
198 ///
199 /// # Returns
200 ///
201 /// A error indicating whether the metadata retrieval was successful or not.
202 ///
203 /// # Safety
204 ///
205 /// The `method_meta_out` struct must be valid for the lifetime of the function.
206 /// The `method_meta_out` struct can be used only if the function returns `ET_Error::Ok`.
207 #[namespace = "executorch_rs"]
208 unsafe fn Module_method_meta(
209 self_: Pin<&mut Module>,
210 method_name: &CxxString,
211 method_meta_out: *mut ET_MethodMeta,
212 ) -> ET_Error;
213
214 /// Execute a specific method with the given input values and retrieve the
215 /// output values. Loads the program and method before executing if needed.
216 ///
217 /// # Arguments
218 ///
219 /// - `method_name`: The name of the method to execute.
220 /// - `inputs`: A vector of input values to be passed to the method.
221 /// - `outputs`: A mutable reference to a vector that will be filled with the output values from the method.
222 ///
223 /// # Returns
224 ///
225 /// A error indicating whether the execution was successful or not.
226 ///
227 /// # Safety
228 ///
229 /// The `outputs` vector must be valid for the lifetime of the function.
230 /// The `outputs` vector can be used only if the function returns `ET_Error::Ok`.
231 #[namespace = "executorch_rs"]
232 unsafe fn Module_execute(
233 self_: Pin<&mut Module>,
234 method_name: &CxxString,
235 inputs: ET_ArrayRefEValue,
236 outputs: *mut ET_VecEValue,
237 ) -> ET_Error;
238 }
239}
240
241unsafe impl ExternType for crate::ET_HierarchicalAllocator {
242 type Id = type_id!("ET_HierarchicalAllocator");
243 type Kind = cxx::kind::Trivial;
244}
245
246unsafe impl ExternType for crate::ET_ModuleLoadMode {
247 type Id = type_id!("ET_ModuleLoadMode");
248 type Kind = cxx::kind::Trivial;
249}
250
251unsafe impl ExternType for crate::ET_LoadBackendOptionsMap {
252 type Id = type_id!("ET_LoadBackendOptionsMap");
253 type Kind = cxx::kind::Trivial;
254}