blob: 0bfb6be2961a43782a1633cd54267f17881c2b72 [file] [log] [blame]
CindyLiuae72b952022-08-23 15:26:08 -07001// 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// clang-format off
8#cmakedefine IREE_STATIC_LIB_QUERY_FN @IREE_STATIC_LIB_QUERY_FN@
9#cmakedefine IREE_STATIC_LIB_HDR @IREE_STATIC_LIB_HDR@
10#cmakedefine IREE_MODULE_CREATE_FN @IREE_MODULE_CREATE_FN@
11#cmakedefine IREE_MODULE_HDR "@IREE_MODULE_HDR@"
12#cmakedefine IREE_EMITC_HDR "@IREE_EMITC_HDR@"
13#cmakedefine IREE_MODULE_MAIN_FN @IREE_MODULE_MAIN_FN@
14#cmakedefine IREE_INPUT_NUM @IREE_INPUT_NUM@
15#cmakedefine IREE_INPUT_DIM_MAX @IREE_INPUT_DIM_MAX@
16#cmakedefine IREE_INPUT_DIM_ARR @IREE_INPUT_DIM_ARR@
17#cmakedefine IREE_INPUT_TYPE @IREE_INPUT_TYPE@
18#cmakedefine IREE_INPUT_SHAPE_ARR @IREE_INPUT_SHAPE_ARR@
19#cmakedefine IREE_INPUT_SIZE_ARR @IREE_INPUT_SIZE_ARR@
20#cmakedefine IREE_HAL_TYPE @IREE_HAL_TYPE@
21#cmakedefine IREE_EXE_NAME @IREE_EXE_NAME@
22// clang-format on
23
24#include <stdio.h>
25
26#include IREE_STATIC_LIB_HDR
27
28#include "iree/hal/drivers/local_sync/sync_device.h"
29#include "iree/hal/local/loaders/static_library_loader.h"
30#include "iree/modules/hal/module.h"
31#include "iree/runtime/api.h"
32
33#ifdef EMITC_IMPLEMENTATION
34#include IREE_EMITC_HDR
35#else
36#include IREE_MODULE_HDR
Ben Vanikcf49d692023-02-24 20:24:09 -080037#include "iree/vm/bytecode/module.h"
CindyLiuae72b952022-08-23 15:26:08 -070038#endif
39
40
41// A function to create the bytecode or c module.
42iree_status_t create_module(iree_vm_instance_t* instance,
43 iree_vm_module_t** out_module) {
44#ifdef EMITC_IMPLEMENTATION
45 return module_create(instance, iree_vm_instance_allocator(instance),
46 out_module);
47#else
48 const struct iree_file_toc_t* module_file_toc =
49 IREE_MODULE_CREATE_FN;
50 iree_const_byte_span_t module_data =
51 iree_make_const_byte_span(module_file_toc->data, module_file_toc->size);
52 return iree_vm_bytecode_module_create(
53 instance, module_data, iree_allocator_null(),
54 iree_vm_instance_allocator(instance), out_module);
55#endif
56}
57
58iree_status_t create_device_with_static_loader(iree_allocator_t host_allocator,
59 iree_hal_device_t** out_device) {
60 // Set paramters for the device created in the next step.
61 iree_hal_sync_device_params_t params;
62 iree_hal_sync_device_params_initialize(&params);
63
64 // Register the statically linked executable library.
65 const iree_hal_executable_library_query_fn_t libraries[] = {
66 IREE_STATIC_LIB_QUERY_FN,
67 };
68 iree_hal_executable_loader_t* library_loader = NULL;
69 iree_status_t status = iree_hal_static_library_loader_create(
70 IREE_ARRAYSIZE(libraries), libraries,
71 iree_hal_executable_import_provider_null(), host_allocator,
72 &library_loader);
73
74 // Use the default host allocator for buffer allocations.
75 iree_string_view_t identifier = iree_make_cstring_view("local-sync");
76 iree_hal_allocator_t* device_allocator = NULL;
77 if (iree_status_is_ok(status)) {
78 status = iree_hal_allocator_create_heap(identifier, host_allocator,
79 host_allocator, &device_allocator);
80 }
81
82 // Create the device and release the executor and loader afterwards.
83 if (iree_status_is_ok(status)) {
84 status = iree_hal_sync_device_create(
85 identifier, &params, /*loader_count=*/1, &library_loader,
86 device_allocator, host_allocator, out_device);
87 }
88
89 iree_hal_allocator_release(device_allocator);
90 iree_hal_executable_loader_release(library_loader);
91 return status;
92}
93
94
95iree_status_t Run() {
96 iree_status_t status = iree_ok_status();
97
98 // Instance configuration (this should be shared across sessions).
99 iree_runtime_instance_options_t instance_options;
Ben Vanikdecb7652022-10-28 16:01:14 +0000100 iree_runtime_instance_options_initialize(&instance_options);
CindyLiuae72b952022-08-23 15:26:08 -0700101 iree_runtime_instance_options_use_all_available_drivers(&instance_options);
102 iree_runtime_instance_t* instance = NULL;
103
104 if (iree_status_is_ok(status)) {
105 status = iree_runtime_instance_create(&instance_options,
106 iree_allocator_system(), &instance);
107 }
108
109 // Create local device with static loader.
110 iree_hal_device_t* device = NULL;
111 if (iree_status_is_ok(status)) {
112 status = create_device_with_static_loader(iree_allocator_system(), &device);
113 }
114
115 // Session configuration (one per loaded module to hold module state).
116 iree_runtime_session_options_t session_options;
117 iree_runtime_session_options_initialize(&session_options);
118 iree_runtime_session_t* session = NULL;
119 if (iree_status_is_ok(status)) {
120 status = iree_runtime_session_create_with_device(
121 instance, &session_options, device,
122 iree_runtime_instance_host_allocator(instance), &session);
123 }
124
125 // Load bytecode/c module from the embedded data. Append to the session.
126 iree_vm_module_t* module = NULL;
127
128 if (iree_status_is_ok(status)) {
129 status =
130 create_module(iree_runtime_instance_vm_instance(instance), &module);
131 }
132
133 if (iree_status_is_ok(status)) {
134 status = iree_runtime_session_append_module(session, module);
135 }
136
137 // Lookup the entry point function call.
138 const char kMainFunctionName[] = IREE_MODULE_MAIN_FN;
139 iree_runtime_call_t call;
140 memset(&call, 0, sizeof(call));
141 if (iree_status_is_ok(status)) {
142 status = iree_runtime_call_initialize_by_name(
143 session, iree_make_cstring_view(kMainFunctionName), &call);
144 }
145
146 // Prepare input.
147 iree_hal_dim_t shape[][IREE_INPUT_DIM_MAX] = IREE_INPUT_SHAPE_ARR;
148 iree_hal_dim_t size[] = IREE_INPUT_SIZE_ARR;
149 iree_hal_dim_t rank[] = IREE_INPUT_DIM_ARR;
150 iree_hal_buffer_view_t* arg_buffer_view = NULL;
151 IREE_INPUT_TYPE* arg_buffers[IREE_INPUT_NUM] = {NULL};
152
153 for (int i = 0; i < IREE_INPUT_NUM; ++i) {
154 if (iree_status_is_ok(status)) {
Ben Vanik85a9f5f2022-10-07 15:16:35 -0700155 arg_buffers[i] = (IREE_INPUT_TYPE*)malloc(
156 sizeof(IREE_INPUT_TYPE) * size[i]);
CindyLiuae72b952022-08-23 15:26:08 -0700157 if (arg_buffers[i] == NULL) {
158 status = iree_make_status(IREE_STATUS_RESOURCE_EXHAUSTED);
159 }
160 }
161 if (iree_status_is_ok(status)) {
162 status = iree_hal_buffer_view_allocate_buffer(
163 iree_hal_device_allocator(device), rank[i], shape[i],
164 IREE_HAL_TYPE, IREE_HAL_ENCODING_TYPE_DENSE_ROW_MAJOR,
165 (iree_hal_buffer_params_t){
166 .type = IREE_HAL_MEMORY_TYPE_DEVICE_LOCAL,
167 .usage = IREE_HAL_BUFFER_USAGE_DEFAULT,
168 },
169 iree_make_const_byte_span((void*)arg_buffers[i],
170 sizeof(IREE_INPUT_TYPE) * size[i]),
171 &arg_buffer_view);
172 }
173
174 // Queue buffer views for input.
175 if (iree_status_is_ok(status)) {
176 status =
177 iree_runtime_call_inputs_push_back_buffer_view(&call,
178 arg_buffer_view);
179 }
180 iree_hal_buffer_view_release(arg_buffer_view);
181 }
182
183 // Invoke call.
184 if (iree_status_is_ok(status)) {
185 status = iree_runtime_call_invoke(&call, /*flags=*/0);
186 }
187
188 // Cleanup call and buffers.
189 iree_runtime_call_deinitialize(&call);
190 for (int i = 0; i < IREE_INPUT_NUM; ++i) {
191 if (arg_buffers[i] != NULL) {
192 free(arg_buffers[i]);
193 }
194 }
195
196 // Cleanup session and instance.
197 iree_hal_device_release(device);
198 iree_runtime_session_release(session);
199 iree_runtime_instance_release(instance);
200 iree_vm_module_release(module);
201
202 return status;
203}
204
205
206int main() {
207 const char* kName = IREE_EXE_NAME;
208 const iree_status_t result = Run();
209 if (!iree_status_is_ok(result)) {
210 fprintf(stderr, "%s encountered error:\n", kName);
211 iree_status_fprint(stderr, result);
212 iree_status_free(result);
213 return -1;
214 }
215 fprintf(stdout, "%s finished successfully\n", kName);
216 return 0;
217}