@@ -234,3 +234,185 @@ IREE_API_EXPORT iree_status_t iree_hal_allocator_export_buffer(
234234 IREE_TRACE_ZONE_END (z0 );
235235 return status ;
236236}
237+
238+ //===----------------------------------------------------------------------===//
239+ // Virtual Memory Management
240+ //===----------------------------------------------------------------------===//
241+
242+ IREE_API_EXPORT bool iree_hal_allocator_supports_virtual_memory (
243+ iree_hal_allocator_t * IREE_RESTRICT allocator ) {
244+ IREE_ASSERT_ARGUMENT (allocator );
245+ if (!_VTABLE_DISPATCH (allocator , supports_virtual_memory )) {
246+ return false;
247+ }
248+ return _VTABLE_DISPATCH (allocator , supports_virtual_memory )(allocator );
249+ }
250+
251+ IREE_API_EXPORT iree_status_t
252+ iree_hal_allocator_virtual_memory_query_granularity (
253+ iree_hal_allocator_t * IREE_RESTRICT allocator ,
254+ iree_hal_buffer_params_t params ,
255+ iree_device_size_t * IREE_RESTRICT out_minimum_page_size ,
256+ iree_device_size_t * IREE_RESTRICT out_recommended_page_size ) {
257+ IREE_ASSERT_ARGUMENT (allocator );
258+ IREE_ASSERT_ARGUMENT (out_minimum_page_size );
259+ IREE_ASSERT_ARGUMENT (out_recommended_page_size );
260+ * out_minimum_page_size = 0 ;
261+ * out_recommended_page_size = 0 ;
262+ if (!_VTABLE_DISPATCH (allocator , virtual_memory_query_granularity )) {
263+ return iree_make_status (IREE_STATUS_UNAVAILABLE ,
264+ "virtual memory not supported by allocator" );
265+ }
266+ IREE_TRACE_ZONE_BEGIN (z0 );
267+ iree_status_t status =
268+ _VTABLE_DISPATCH (allocator , virtual_memory_query_granularity )(
269+ allocator , params , out_minimum_page_size , out_recommended_page_size );
270+ IREE_TRACE_ZONE_END (z0 );
271+ return status ;
272+ }
273+
274+ IREE_API_EXPORT iree_status_t iree_hal_allocator_virtual_memory_reserve (
275+ iree_hal_allocator_t * IREE_RESTRICT allocator ,
276+ iree_hal_queue_affinity_t queue_affinity , iree_device_size_t size ,
277+ iree_hal_buffer_t * * IREE_RESTRICT out_virtual_buffer ) {
278+ IREE_ASSERT_ARGUMENT (allocator );
279+ IREE_ASSERT_ARGUMENT (out_virtual_buffer );
280+ * out_virtual_buffer = NULL ;
281+ if (!_VTABLE_DISPATCH (allocator , virtual_memory_reserve )) {
282+ return iree_make_status (IREE_STATUS_UNAVAILABLE ,
283+ "virtual memory not supported by allocator" );
284+ }
285+ IREE_TRACE_ZONE_BEGIN (z0 );
286+ IREE_TRACE_ZONE_APPEND_VALUE_I64 (z0 , (int64_t )size );
287+ iree_status_t status = _VTABLE_DISPATCH (allocator , virtual_memory_reserve )(
288+ allocator , queue_affinity , size , out_virtual_buffer );
289+ IREE_TRACE_ZONE_END (z0 );
290+ return status ;
291+ }
292+
293+ IREE_API_EXPORT iree_status_t iree_hal_allocator_virtual_memory_release (
294+ iree_hal_allocator_t * IREE_RESTRICT allocator ,
295+ iree_hal_buffer_t * IREE_RESTRICT virtual_buffer ) {
296+ IREE_ASSERT_ARGUMENT (allocator );
297+ IREE_ASSERT_ARGUMENT (virtual_buffer );
298+ if (!_VTABLE_DISPATCH (allocator , virtual_memory_release )) {
299+ return iree_make_status (IREE_STATUS_UNAVAILABLE ,
300+ "virtual memory not supported by allocator" );
301+ }
302+ IREE_TRACE_ZONE_BEGIN (z0 );
303+ iree_status_t status = _VTABLE_DISPATCH (allocator , virtual_memory_release )(
304+ allocator , virtual_buffer );
305+ IREE_TRACE_ZONE_END (z0 );
306+ return status ;
307+ }
308+
309+ IREE_API_EXPORT iree_status_t iree_hal_allocator_physical_memory_allocate (
310+ iree_hal_allocator_t * IREE_RESTRICT allocator ,
311+ iree_hal_buffer_params_t params , iree_device_size_t size ,
312+ iree_allocator_t host_allocator ,
313+ iree_hal_physical_memory_t * * IREE_RESTRICT out_physical_memory ) {
314+ IREE_ASSERT_ARGUMENT (allocator );
315+ IREE_ASSERT_ARGUMENT (out_physical_memory );
316+ * out_physical_memory = NULL ;
317+ if (!_VTABLE_DISPATCH (allocator , physical_memory_allocate )) {
318+ return iree_make_status (IREE_STATUS_UNAVAILABLE ,
319+ "virtual memory not supported by allocator" );
320+ }
321+ IREE_TRACE_ZONE_BEGIN (z0 );
322+ IREE_TRACE_ZONE_APPEND_VALUE_I64 (z0 , (int64_t )size );
323+ iree_status_t status = _VTABLE_DISPATCH (allocator , physical_memory_allocate )(
324+ allocator , params , size , host_allocator , out_physical_memory );
325+ IREE_TRACE_ZONE_END (z0 );
326+ return status ;
327+ }
328+
329+ IREE_API_EXPORT iree_status_t iree_hal_allocator_physical_memory_free (
330+ iree_hal_allocator_t * IREE_RESTRICT allocator ,
331+ iree_hal_physical_memory_t * IREE_RESTRICT physical_memory ) {
332+ IREE_ASSERT_ARGUMENT (allocator );
333+ IREE_ASSERT_ARGUMENT (physical_memory );
334+ if (!_VTABLE_DISPATCH (allocator , physical_memory_free )) {
335+ return iree_make_status (IREE_STATUS_UNAVAILABLE ,
336+ "virtual memory not supported by allocator" );
337+ }
338+ IREE_TRACE_ZONE_BEGIN (z0 );
339+ iree_status_t status = _VTABLE_DISPATCH (allocator , physical_memory_free )(
340+ allocator , physical_memory );
341+ IREE_TRACE_ZONE_END (z0 );
342+ return status ;
343+ }
344+
345+ IREE_API_EXPORT iree_status_t iree_hal_allocator_virtual_memory_map (
346+ iree_hal_allocator_t * IREE_RESTRICT allocator ,
347+ iree_hal_buffer_t * IREE_RESTRICT virtual_buffer ,
348+ iree_device_size_t virtual_offset ,
349+ iree_hal_physical_memory_t * IREE_RESTRICT physical_memory ,
350+ iree_device_size_t physical_offset , iree_device_size_t size ) {
351+ IREE_ASSERT_ARGUMENT (allocator );
352+ IREE_ASSERT_ARGUMENT (virtual_buffer );
353+ IREE_ASSERT_ARGUMENT (physical_memory );
354+ if (!_VTABLE_DISPATCH (allocator , virtual_memory_map )) {
355+ return iree_make_status (IREE_STATUS_UNAVAILABLE ,
356+ "virtual memory not supported by allocator" );
357+ }
358+ IREE_TRACE_ZONE_BEGIN (z0 );
359+ IREE_TRACE_ZONE_APPEND_VALUE_I64 (z0 , (int64_t )size );
360+ iree_status_t status = _VTABLE_DISPATCH (allocator , virtual_memory_map )(
361+ allocator , virtual_buffer , virtual_offset , physical_memory ,
362+ physical_offset , size );
363+ IREE_TRACE_ZONE_END (z0 );
364+ return status ;
365+ }
366+
367+ IREE_API_EXPORT iree_status_t iree_hal_allocator_virtual_memory_unmap (
368+ iree_hal_allocator_t * IREE_RESTRICT allocator ,
369+ iree_hal_buffer_t * IREE_RESTRICT virtual_buffer ,
370+ iree_device_size_t virtual_offset , iree_device_size_t size ) {
371+ IREE_ASSERT_ARGUMENT (allocator );
372+ IREE_ASSERT_ARGUMENT (virtual_buffer );
373+ if (!_VTABLE_DISPATCH (allocator , virtual_memory_unmap )) {
374+ return iree_make_status (IREE_STATUS_UNAVAILABLE ,
375+ "virtual memory not supported by allocator" );
376+ }
377+ IREE_TRACE_ZONE_BEGIN (z0 );
378+ iree_status_t status = _VTABLE_DISPATCH (allocator , virtual_memory_unmap )(
379+ allocator , virtual_buffer , virtual_offset , size );
380+ IREE_TRACE_ZONE_END (z0 );
381+ return status ;
382+ }
383+
384+ IREE_API_EXPORT iree_status_t iree_hal_allocator_virtual_memory_protect (
385+ iree_hal_allocator_t * IREE_RESTRICT allocator ,
386+ iree_hal_buffer_t * IREE_RESTRICT virtual_buffer ,
387+ iree_device_size_t virtual_offset , iree_device_size_t size ,
388+ iree_hal_queue_affinity_t queue_affinity ,
389+ iree_hal_memory_protection_t protection ) {
390+ IREE_ASSERT_ARGUMENT (allocator );
391+ IREE_ASSERT_ARGUMENT (virtual_buffer );
392+ if (!_VTABLE_DISPATCH (allocator , virtual_memory_protect )) {
393+ return iree_make_status (IREE_STATUS_UNAVAILABLE ,
394+ "virtual memory not supported by allocator" );
395+ }
396+ IREE_TRACE_ZONE_BEGIN (z0 );
397+ iree_status_t status = _VTABLE_DISPATCH (allocator , virtual_memory_protect )(
398+ allocator , virtual_buffer , virtual_offset , size , queue_affinity ,
399+ protection );
400+ IREE_TRACE_ZONE_END (z0 );
401+ return status ;
402+ }
403+
404+ IREE_API_EXPORT iree_status_t iree_hal_allocator_virtual_memory_advise (
405+ iree_hal_allocator_t * IREE_RESTRICT allocator ,
406+ iree_hal_buffer_t * IREE_RESTRICT virtual_buffer ,
407+ iree_device_size_t virtual_offset , iree_device_size_t size ,
408+ iree_hal_queue_affinity_t queue_affinity , iree_hal_memory_advice_t advice ) {
409+ IREE_ASSERT_ARGUMENT (allocator );
410+ IREE_ASSERT_ARGUMENT (virtual_buffer );
411+ if (!_VTABLE_DISPATCH (allocator , virtual_memory_advise )) {
412+ return iree_make_status (IREE_STATUS_UNAVAILABLE ,
413+ "virtual memory not supported by allocator" );
414+ }
415+ // No tracing for advise - it's a hint that may be called frequently.
416+ return _VTABLE_DISPATCH (allocator , virtual_memory_advise )(
417+ allocator , virtual_buffer , virtual_offset , size , queue_affinity , advice );
418+ }
0 commit comments