CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 1 | // Copyright 2021 The IREE Authors |
| 2 | // |
| 3 | // Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | |
Ben Vanik | 6e64b6e | 2022-06-07 09:14:53 -0700 | [diff] [blame] | 7 | // A example of setting up the local-sync driver using VMVX for execution. |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 8 | |
| 9 | #include <stddef.h> |
| 10 | |
| 11 | #include "iree/base/api.h" |
| 12 | #include "iree/hal/api.h" |
Ben Vanik | 325b044 | 2022-06-06 17:14:30 -0700 | [diff] [blame] | 13 | #include "iree/hal/drivers/local_sync/sync_device.h" |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 14 | #include "iree/hal/local/executable_loader.h" |
| 15 | #include "iree/hal/local/loaders/vmvx_module_loader.h" |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 16 | |
| 17 | // Compiled module embedded here to avoid file IO: |
Scott Todd | c4b04e3 | 2022-05-23 09:53:20 -0700 | [diff] [blame] | 18 | #include "samples/simple_embedding/simple_embedding_test_bytecode_module_vmvx_c.h" |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 19 | |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 20 | iree_status_t create_sample_device(iree_allocator_t host_allocator, |
| 21 | iree_hal_device_t** out_device) { |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 22 | // Set parameters for the device created in the next step. |
| 23 | iree_hal_sync_device_params_t params; |
| 24 | iree_hal_sync_device_params_initialize(¶ms); |
| 25 | |
| 26 | iree_vm_instance_t* instance = NULL; |
Ben Vanik | 09630d6 | 2023-04-13 14:21:40 -0700 | [diff] [blame] | 27 | IREE_RETURN_IF_ERROR(iree_vm_instance_create(IREE_VM_TYPE_CAPACITY_DEFAULT, |
| 28 | host_allocator, &instance)); |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 29 | |
| 30 | iree_hal_executable_loader_t* loader = NULL; |
Ben Vanik | a25ca12 | 2022-06-28 12:02:39 -0700 | [diff] [blame] | 31 | iree_status_t status = iree_hal_vmvx_module_loader_create( |
| 32 | instance, /*user_module_count=*/0, /*user_modules=*/NULL, host_allocator, |
| 33 | &loader); |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 34 | iree_vm_instance_release(instance); |
| 35 | |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 36 | // Use the default host allocator for buffer allocations. |
Ben Vanik | e900692 | 2024-07-16 11:00:47 -0700 | [diff] [blame] | 37 | iree_string_view_t identifier = iree_make_cstring_view("local-sync"); |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 38 | iree_hal_allocator_t* device_allocator = NULL; |
| 39 | if (iree_status_is_ok(status)) { |
| 40 | status = iree_hal_allocator_create_heap(identifier, host_allocator, |
Ben Vanik | 315a004 | 2021-11-30 13:11:09 -0800 | [diff] [blame] | 41 | host_allocator, &device_allocator); |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 42 | } |
| 43 | |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 44 | if (iree_status_is_ok(status)) { |
| 45 | // Create the synchronous device. |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 46 | status = iree_hal_sync_device_create( |
| 47 | identifier, ¶ms, /*loader_count=*/1, &loader, device_allocator, |
| 48 | host_allocator, out_device); |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 49 | } |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 50 | |
| 51 | iree_hal_allocator_release(device_allocator); |
CindyLiu | 5b305c8 | 2021-06-23 13:28:28 +0000 | [diff] [blame] | 52 | iree_hal_executable_loader_release(loader); |
| 53 | return status; |
| 54 | } |
| 55 | |
| 56 | const iree_const_byte_span_t load_bytecode_module_data() { |
| 57 | const struct iree_file_toc_t* module_file_toc = |
| 58 | iree_samples_simple_embedding_test_module_vmvx_create(); |
| 59 | return iree_make_const_byte_span(module_file_toc->data, |
| 60 | module_file_toc->size); |
| 61 | } |