blob: cff099fa5e3c8a118b6202cdb278403b7d927933 [file] [log] [blame]
Adam Jesionowski6e273a72022-04-14 12:20:20 -07001/*
Lun Dong26c106a2023-10-24 09:14:25 -07002 * Copyright 2023 Google LLC
Adam Jesionowski6e273a72022-04-14 12:20:20 -07003 *
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 Dong26c106a2023-10-24 09:14:25 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Adam Jesionowski6e273a72022-04-14 12:20:20 -07009 *
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 Dong72890d02021-12-03 18:51:03 -080017// Static library loading in IREE.
18
Cindy Liu3c4d6272022-08-04 18:54:12 -070019#include "device/device.h"
Lun Donge7281bd2022-06-13 11:53:13 -070020#include "iree/hal/drivers/local_sync/sync_device.h"
Lun Dong96e56542023-05-13 13:25:12 -070021#include "iree/hal/local/executable_plugin_manager.h"
Cindy Liu3c4d6272022-08-04 18:54:12 -070022#include "iree/hal/local/loaders/static_library_loader.h"
Cindy Liu3c4d6272022-08-04 18:54:12 -070023#include "model_util/model_api.h"
Lun Dong72890d02021-12-03 18:51:03 -080024
Lun Dong96e56542023-05-13 13:25:12 -070025extern iree_hal_executable_import_provider_t importer_query(void);
26
Lun Dong72890d02021-12-03 18:51:03 -080027// A function to create the HAL device from the different backend targets.
Lun Dongc3937892022-10-11 13:19:37 -070028// The HAL device and loader are returned based on the implementation, and they
29// must be released by the caller.
Lun Dong72890d02021-12-03 18:51:03 -080030iree_status_t create_sample_device(iree_allocator_t host_allocator,
Lun Dongc3937892022-10-11 13:19:37 -070031 iree_hal_device_t** out_device,
32 iree_hal_executable_loader_t** loader) {
Lun Dong72890d02021-12-03 18:51:03 -080033 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(&params);
38
Lun Dong96e56542023-05-13 13:25:12 -070039 // 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 Dong72890d02021-12-03 18:51:03 -080056 // Load the statically embedded library
Lun Dong96ec2e02022-03-08 23:30:36 +000057 const iree_hal_executable_library_query_fn_t libraries[] = {library_query()};
Lun Dong72890d02021-12-03 18:51:03 -080058
Lun Dong72890d02021-12-03 18:51:03 -080059 if (iree_status_is_ok(status)) {
60 status = iree_hal_static_library_loader_create(
61 IREE_ARRAYSIZE(libraries), libraries,
Lun Dong96e56542023-05-13 13:25:12 -070062 iree_hal_executable_plugin_manager_provider(plugin_manager),
63 host_allocator, loader);
Lun Dong72890d02021-12-03 18:51:03 -080064 }
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 Dongc3937892022-10-11 13:19:37 -070077 identifier, &params, /*loader_count=*/1, loader, device_allocator,
78 host_allocator, out_device);
Lun Dong72890d02021-12-03 18:51:03 -080079 }
80
81 iree_hal_allocator_release(device_allocator);
Lun Dong72890d02021-12-03 18:51:03 -080082 return status;
83}