Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [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 | |
| 7 | // A example of static library loading in IREE. See the README.md for more info. |
Stella Laurenzo | 7f2972c | 2022-03-19 14:09:43 -0700 | [diff] [blame] | 8 | // Note: this demo requires artifacts from iree-compile before it will run. |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 9 | |
Ben Vanik | 325b044 | 2022-06-06 17:14:30 -0700 | [diff] [blame] | 10 | #include "iree/hal/drivers/local_sync/sync_device.h" |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 11 | #include "iree/hal/local/loaders/static_library_loader.h" |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 12 | #include "iree/modules/hal/module.h" |
| 13 | #include "iree/runtime/api.h" |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 14 | |
Simon Camphausen | 9928f89 | 2021-11-24 17:40:19 +0100 | [diff] [blame] | 15 | extern const iree_hal_executable_library_header_t** |
| 16 | simple_mul_dispatch_0_library_query( |
Ben Vanik | 6e015be | 2022-02-28 22:01:53 -0800 | [diff] [blame] | 17 | iree_hal_executable_library_version_t max_version, |
| 18 | const iree_hal_executable_environment_v0_t* environment); |
Simon Camphausen | 9928f89 | 2021-11-24 17:40:19 +0100 | [diff] [blame] | 19 | // A function to create the bytecode or C module. |
Ben Vanik | 9aa83ed | 2022-08-06 12:55:34 -0700 | [diff] [blame] | 20 | extern iree_status_t create_module(iree_vm_instance_t* instance, |
| 21 | iree_vm_module_t** out_module); |
Simon Camphausen | 9928f89 | 2021-11-24 17:40:19 +0100 | [diff] [blame] | 22 | |
| 23 | extern void print_success(); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 24 | |
| 25 | // A function to create the HAL device from the different backend targets. |
| 26 | // The HAL device is returned based on the implementation, and it must be |
| 27 | // released by the caller. |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 28 | iree_status_t create_device_with_static_loader(iree_allocator_t host_allocator, |
| 29 | iree_hal_device_t** out_device) { |
Marius Brehler | 1b72894 | 2023-02-21 18:09:57 +0100 | [diff] [blame] | 30 | // Set parameters for the device created in the next step. |
Marius Brehler | 0f338bf | 2021-11-08 23:55:09 +0100 | [diff] [blame] | 31 | iree_hal_sync_device_params_t params; |
| 32 | iree_hal_sync_device_params_initialize(¶ms); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 33 | |
Ben Vanik | 6e015be | 2022-02-28 22:01:53 -0800 | [diff] [blame] | 34 | // Register the statically linked executable library. |
| 35 | const iree_hal_executable_library_query_fn_t libraries[] = { |
| 36 | simple_mul_dispatch_0_library_query, |
| 37 | }; |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 38 | iree_hal_executable_loader_t* library_loader = NULL; |
Ben Vanik | 6e015be | 2022-02-28 22:01:53 -0800 | [diff] [blame] | 39 | iree_status_t status = iree_hal_static_library_loader_create( |
| 40 | IREE_ARRAYSIZE(libraries), libraries, |
| 41 | iree_hal_executable_import_provider_null(), host_allocator, |
| 42 | &library_loader); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 43 | |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 44 | // Use the default host allocator for buffer allocations. |
Ben Vanik | e900692 | 2024-07-16 11:00:47 -0700 | [diff] [blame] | 45 | iree_string_view_t identifier = iree_make_cstring_view("local-sync"); |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 46 | iree_hal_allocator_t* device_allocator = NULL; |
| 47 | if (iree_status_is_ok(status)) { |
| 48 | status = iree_hal_allocator_create_heap(identifier, host_allocator, |
Ben Vanik | 315a004 | 2021-11-30 13:11:09 -0800 | [diff] [blame] | 49 | host_allocator, &device_allocator); |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 52 | // Create the device and release the executor and loader afterwards. |
| 53 | if (iree_status_is_ok(status)) { |
Marius Brehler | 0f338bf | 2021-11-08 23:55:09 +0100 | [diff] [blame] | 54 | status = iree_hal_sync_device_create( |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 55 | identifier, ¶ms, /*loader_count=*/1, &library_loader, |
| 56 | device_allocator, host_allocator, out_device); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 57 | } |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 58 | |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 59 | iree_hal_allocator_release(device_allocator); |
| 60 | iree_hal_executable_loader_release(library_loader); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 61 | return status; |
| 62 | } |
| 63 | |
| 64 | iree_status_t Run() { |
| 65 | iree_status_t status = iree_ok_status(); |
| 66 | |
| 67 | // Instance configuration (this should be shared across sessions). |
| 68 | iree_runtime_instance_options_t instance_options; |
Ben Vanik | decb765 | 2022-10-28 16:01:14 +0000 | [diff] [blame] | 69 | iree_runtime_instance_options_initialize(&instance_options); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 70 | iree_runtime_instance_options_use_all_available_drivers(&instance_options); |
| 71 | iree_runtime_instance_t* instance = NULL; |
| 72 | |
| 73 | if (iree_status_is_ok(status)) { |
| 74 | status = iree_runtime_instance_create(&instance_options, |
| 75 | iree_allocator_system(), &instance); |
| 76 | } |
| 77 | |
Scott Todd | 55bec0d | 2022-07-28 11:45:18 -0700 | [diff] [blame] | 78 | // Create local device with static loader. |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 79 | iree_hal_device_t* device = NULL; |
| 80 | if (iree_status_is_ok(status)) { |
Ben Vanik | 0dd0cfc | 2021-11-29 16:32:50 -0800 | [diff] [blame] | 81 | status = create_device_with_static_loader(iree_allocator_system(), &device); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 82 | } |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 83 | |
| 84 | // Session configuration (one per loaded module to hold module state). |
| 85 | iree_runtime_session_options_t session_options; |
| 86 | iree_runtime_session_options_initialize(&session_options); |
| 87 | iree_runtime_session_t* session = NULL; |
| 88 | if (iree_status_is_ok(status)) { |
| 89 | status = iree_runtime_session_create_with_device( |
| 90 | instance, &session_options, device, |
| 91 | iree_runtime_instance_host_allocator(instance), &session); |
| 92 | } |
| 93 | |
| 94 | // Load bytecode module from the embedded data. Append to the session. |
Simon Camphausen | 9928f89 | 2021-11-24 17:40:19 +0100 | [diff] [blame] | 95 | iree_vm_module_t* module = NULL; |
| 96 | |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 97 | if (iree_status_is_ok(status)) { |
Ben Vanik | 9aa83ed | 2022-08-06 12:55:34 -0700 | [diff] [blame] | 98 | status = |
| 99 | create_module(iree_runtime_instance_vm_instance(instance), &module); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 100 | } |
Simon Camphausen | 9928f89 | 2021-11-24 17:40:19 +0100 | [diff] [blame] | 101 | |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 102 | if (iree_status_is_ok(status)) { |
Simon Camphausen | 9928f89 | 2021-11-24 17:40:19 +0100 | [diff] [blame] | 103 | status = iree_runtime_session_append_module(session, module); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Lookup the entry point function call. |
| 107 | const char kMainFunctionName[] = "module.simple_mul"; |
| 108 | iree_runtime_call_t call; |
| 109 | memset(&call, 0, sizeof(call)); |
| 110 | if (iree_status_is_ok(status)) { |
| 111 | status = iree_runtime_call_initialize_by_name( |
| 112 | session, iree_make_cstring_view(kMainFunctionName), &call); |
| 113 | } |
| 114 | |
| 115 | // Populate initial values for 4 * 2 = 8. |
| 116 | const int kElementCount = 4; |
| 117 | iree_hal_dim_t shape[1] = {kElementCount}; |
| 118 | iree_hal_buffer_view_t* arg0_buffer_view = NULL; |
| 119 | iree_hal_buffer_view_t* arg1_buffer_view = NULL; |
| 120 | float kFloat4[] = {4.0f, 4.0f, 4.0f, 4.0f}; |
| 121 | float kFloat2[] = {2.0f, 2.0f, 2.0f, 2.0f}; |
| 122 | |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 123 | if (iree_status_is_ok(status)) { |
Ben Vanik | 42b983c | 2023-08-15 21:31:09 -0700 | [diff] [blame] | 124 | status = iree_hal_buffer_view_allocate_buffer_copy( |
| 125 | device, iree_hal_device_allocator(device), IREE_ARRAYSIZE(shape), shape, |
Ben Vanik | e67c660 | 2021-08-13 12:47:13 -0700 | [diff] [blame] | 126 | IREE_HAL_ELEMENT_TYPE_FLOAT_32, IREE_HAL_ENCODING_TYPE_DENSE_ROW_MAJOR, |
Scott Todd | b461600 | 2022-02-24 09:46:44 -0800 | [diff] [blame] | 127 | (iree_hal_buffer_params_t){ |
Ben Vanik | 0339fa0 | 2022-04-05 10:44:33 -0700 | [diff] [blame] | 128 | .type = IREE_HAL_MEMORY_TYPE_DEVICE_LOCAL, |
Ben Vanik | 5844fd6 | 2022-06-13 08:45:41 -0700 | [diff] [blame] | 129 | .usage = IREE_HAL_BUFFER_USAGE_DEFAULT, |
Scott Todd | b461600 | 2022-02-24 09:46:44 -0800 | [diff] [blame] | 130 | }, |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 131 | iree_make_const_byte_span((void*)kFloat4, |
| 132 | sizeof(float) * kElementCount), |
| 133 | &arg0_buffer_view); |
| 134 | } |
| 135 | if (iree_status_is_ok(status)) { |
Ben Vanik | 42b983c | 2023-08-15 21:31:09 -0700 | [diff] [blame] | 136 | status = iree_hal_buffer_view_allocate_buffer_copy( |
| 137 | device, iree_hal_device_allocator(device), IREE_ARRAYSIZE(shape), shape, |
Ben Vanik | e67c660 | 2021-08-13 12:47:13 -0700 | [diff] [blame] | 138 | IREE_HAL_ELEMENT_TYPE_FLOAT_32, IREE_HAL_ENCODING_TYPE_DENSE_ROW_MAJOR, |
Scott Todd | b461600 | 2022-02-24 09:46:44 -0800 | [diff] [blame] | 139 | (iree_hal_buffer_params_t){ |
Ben Vanik | 0339fa0 | 2022-04-05 10:44:33 -0700 | [diff] [blame] | 140 | .type = IREE_HAL_MEMORY_TYPE_DEVICE_LOCAL, |
Ben Vanik | 5844fd6 | 2022-06-13 08:45:41 -0700 | [diff] [blame] | 141 | .usage = IREE_HAL_BUFFER_USAGE_DEFAULT, |
Scott Todd | b461600 | 2022-02-24 09:46:44 -0800 | [diff] [blame] | 142 | }, |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 143 | iree_make_const_byte_span((void*)kFloat2, |
| 144 | sizeof(float) * kElementCount), |
| 145 | &arg1_buffer_view); |
| 146 | } |
| 147 | |
| 148 | // Queue buffer views for input. |
| 149 | if (iree_status_is_ok(status)) { |
| 150 | status = |
| 151 | iree_runtime_call_inputs_push_back_buffer_view(&call, arg0_buffer_view); |
| 152 | } |
| 153 | iree_hal_buffer_view_release(arg0_buffer_view); |
| 154 | |
| 155 | if (iree_status_is_ok(status)) { |
| 156 | status = |
| 157 | iree_runtime_call_inputs_push_back_buffer_view(&call, arg1_buffer_view); |
| 158 | } |
| 159 | iree_hal_buffer_view_release(arg1_buffer_view); |
| 160 | |
| 161 | // Invoke call. |
| 162 | if (iree_status_is_ok(status)) { |
| 163 | status = iree_runtime_call_invoke(&call, /*flags=*/0); |
| 164 | } |
| 165 | |
Marius Brehler | 1b72894 | 2023-02-21 18:09:57 +0100 | [diff] [blame] | 166 | // Retrieve output buffer view with results from the invocation. |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 167 | iree_hal_buffer_view_t* ret_buffer_view = NULL; |
| 168 | if (iree_status_is_ok(status)) { |
| 169 | status = iree_runtime_call_outputs_pop_front_buffer_view(&call, |
| 170 | &ret_buffer_view); |
| 171 | } |
| 172 | |
| 173 | // Read back the results and ensure we got the right values. |
Ben Vanik | fb9fb32 | 2021-12-15 18:35:12 -0800 | [diff] [blame] | 174 | float results[] = {0.0f, 0.0f, 0.0f, 0.0f}; |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 175 | if (iree_status_is_ok(status)) { |
Ben Vanik | 7536cb6 | 2022-03-15 15:52:07 -0700 | [diff] [blame] | 176 | status = iree_hal_device_transfer_d2h( |
| 177 | device, iree_hal_buffer_view_buffer(ret_buffer_view), 0, results, |
| 178 | sizeof(results), IREE_HAL_TRANSFER_BUFFER_FLAG_DEFAULT, |
| 179 | iree_infinite_timeout()); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 180 | } |
| 181 | if (iree_status_is_ok(status)) { |
Ben Vanik | fb9fb32 | 2021-12-15 18:35:12 -0800 | [diff] [blame] | 182 | for (iree_host_size_t i = 0; i < IREE_ARRAYSIZE(results); ++i) { |
| 183 | if (results[i] != 8.0f) { |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 184 | status = iree_make_status(IREE_STATUS_UNKNOWN, "result mismatches"); |
Ben Vanik | fb9fb32 | 2021-12-15 18:35:12 -0800 | [diff] [blame] | 185 | break; |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Cleanup call and buffers. |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 191 | iree_hal_buffer_view_release(ret_buffer_view); |
| 192 | iree_runtime_call_deinitialize(&call); |
| 193 | |
| 194 | // Cleanup session and instance. |
| 195 | iree_hal_device_release(device); |
| 196 | iree_runtime_session_release(session); |
| 197 | iree_runtime_instance_release(instance); |
Simon Camphausen | 9928f89 | 2021-11-24 17:40:19 +0100 | [diff] [blame] | 198 | iree_vm_module_release(module); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 199 | |
| 200 | return status; |
| 201 | } |
| 202 | |
| 203 | int main() { |
| 204 | const iree_status_t result = Run(); |
| 205 | if (!iree_status_is_ok(result)) { |
| 206 | iree_status_fprint(stderr, result); |
| 207 | iree_status_free(result); |
| 208 | return -1; |
| 209 | } |
Simon Camphausen | 9928f89 | 2021-11-24 17:40:19 +0100 | [diff] [blame] | 210 | print_success(); |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 211 | return 0; |
| 212 | } |