Adam Jesionowski | 6e273a7 | 2022-04-14 12:20:20 -0700 | [diff] [blame] | 1 | /* |
Lun Dong | 26c106a | 2023-10-24 09:14:25 -0700 | [diff] [blame] | 2 | * Copyright 2023 Google LLC |
Adam Jesionowski | 6e273a7 | 2022-04-14 12:20:20 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
Lun Dong | 26c106a | 2023-10-24 09:14:25 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Adam Jesionowski | 6e273a7 | 2022-04-14 12:20:20 -0700 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 17 | // Static library loading in IREE. |
| 18 | |
Cindy Liu | 3c4d627 | 2022-08-04 18:54:12 -0700 | [diff] [blame] | 19 | #include "device/device.h" |
Lun Dong | e7281bd | 2022-06-13 11:53:13 -0700 | [diff] [blame] | 20 | #include "iree/hal/drivers/local_sync/sync_device.h" |
Lun Dong | 96e5654 | 2023-05-13 13:25:12 -0700 | [diff] [blame] | 21 | #include "iree/hal/local/executable_plugin_manager.h" |
Cindy Liu | 3c4d627 | 2022-08-04 18:54:12 -0700 | [diff] [blame] | 22 | #include "iree/hal/local/loaders/static_library_loader.h" |
Cindy Liu | 3c4d627 | 2022-08-04 18:54:12 -0700 | [diff] [blame] | 23 | #include "model_util/model_api.h" |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 24 | |
Lun Dong | 96e5654 | 2023-05-13 13:25:12 -0700 | [diff] [blame] | 25 | extern iree_hal_executable_import_provider_t importer_query(void); |
| 26 | |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 27 | // A function to create the HAL device from the different backend targets. |
Lun Dong | c393789 | 2022-10-11 13:19:37 -0700 | [diff] [blame] | 28 | // The HAL device and loader are returned based on the implementation, and they |
| 29 | // must be released by the caller. |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 30 | iree_status_t create_sample_device(iree_allocator_t host_allocator, |
Lun Dong | c393789 | 2022-10-11 13:19:37 -0700 | [diff] [blame] | 31 | iree_hal_device_t** out_device, |
| 32 | iree_hal_executable_loader_t** loader) { |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 33 | iree_status_t status = iree_ok_status(); |
| 34 | |
| 35 | // Set paramters for the device created in the next step. |
| 36 | iree_hal_sync_device_params_t params; |
| 37 | iree_hal_sync_device_params_initialize(¶ms); |
| 38 | |
Lun Dong | 96e5654 | 2023-05-13 13:25:12 -0700 | [diff] [blame] | 39 | // Create plugin manager |
| 40 | iree_hal_executable_plugin_manager_t* plugin_manager = NULL; |
| 41 | |
| 42 | #ifdef BUILD_KELVIN |
| 43 | // Enable plugin manager only in Kelvin for now |
| 44 | if (iree_status_is_ok(status)) { |
| 45 | status = iree_hal_executable_plugin_manager_create( |
| 46 | /*capacity=*/1, host_allocator, &plugin_manager); |
| 47 | } |
| 48 | |
| 49 | // Register import provider |
| 50 | if (iree_status_is_ok(status)) { |
| 51 | status = iree_hal_executable_plugin_manager_register_provider( |
| 52 | plugin_manager, importer_query()); |
| 53 | } |
| 54 | #endif // # ifdef BUILD_KELVIN |
| 55 | |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 56 | // Load the statically embedded library |
Lun Dong | 96ec2e0 | 2022-03-08 23:30:36 +0000 | [diff] [blame] | 57 | const iree_hal_executable_library_query_fn_t libraries[] = {library_query()}; |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 58 | |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 59 | if (iree_status_is_ok(status)) { |
| 60 | status = iree_hal_static_library_loader_create( |
| 61 | IREE_ARRAYSIZE(libraries), libraries, |
Lun Dong | 96e5654 | 2023-05-13 13:25:12 -0700 | [diff] [blame] | 62 | iree_hal_executable_plugin_manager_provider(plugin_manager), |
| 63 | host_allocator, loader); |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Use the default host allocator for buffer allocations. |
| 67 | iree_string_view_t identifier = iree_make_cstring_view("sync"); |
| 68 | iree_hal_allocator_t* device_allocator = NULL; |
| 69 | if (iree_status_is_ok(status)) { |
| 70 | status = iree_hal_allocator_create_heap(identifier, host_allocator, |
| 71 | host_allocator, &device_allocator); |
| 72 | } |
| 73 | |
| 74 | // Create the device and release the executor and loader afterwards. |
| 75 | if (iree_status_is_ok(status)) { |
| 76 | status = iree_hal_sync_device_create( |
Lun Dong | c393789 | 2022-10-11 13:19:37 -0700 | [diff] [blame] | 77 | identifier, ¶ms, /*loader_count=*/1, loader, device_allocator, |
| 78 | host_allocator, out_device); |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | iree_hal_allocator_release(device_allocator); |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 82 | return status; |
| 83 | } |