blob: f1f633fb914efdc65090d6a9c2efbeb10b329fa2 [file] [log] [blame]
CindyLiu5b305c82021-06-23 13:28:28 +00001// 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 Vanik6e64b6e2022-06-07 09:14:53 -07007// A example of setting up the local-sync driver using VMVX for execution.
CindyLiu5b305c82021-06-23 13:28:28 +00008
9#include <stddef.h>
10
11#include "iree/base/api.h"
12#include "iree/hal/api.h"
Ben Vanik325b0442022-06-06 17:14:30 -070013#include "iree/hal/drivers/local_sync/sync_device.h"
CindyLiu5b305c82021-06-23 13:28:28 +000014#include "iree/hal/local/executable_loader.h"
15#include "iree/hal/local/loaders/vmvx_module_loader.h"
CindyLiu5b305c82021-06-23 13:28:28 +000016
17// Compiled module embedded here to avoid file IO:
Scott Toddc4b04e32022-05-23 09:53:20 -070018#include "samples/simple_embedding/simple_embedding_test_bytecode_module_vmvx_c.h"
CindyLiu5b305c82021-06-23 13:28:28 +000019
Ben Vanik0dd0cfc2021-11-29 16:32:50 -080020iree_status_t create_sample_device(iree_allocator_t host_allocator,
21 iree_hal_device_t** out_device) {
CindyLiu5b305c82021-06-23 13:28:28 +000022 // 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(&params);
25
26 iree_vm_instance_t* instance = NULL;
Ben Vanik09630d62023-04-13 14:21:40 -070027 IREE_RETURN_IF_ERROR(iree_vm_instance_create(IREE_VM_TYPE_CAPACITY_DEFAULT,
28 host_allocator, &instance));
CindyLiu5b305c82021-06-23 13:28:28 +000029
30 iree_hal_executable_loader_t* loader = NULL;
Ben Vanika25ca122022-06-28 12:02:39 -070031 iree_status_t status = iree_hal_vmvx_module_loader_create(
32 instance, /*user_module_count=*/0, /*user_modules=*/NULL, host_allocator,
33 &loader);
CindyLiu5b305c82021-06-23 13:28:28 +000034 iree_vm_instance_release(instance);
35
Ben Vanik0dd0cfc2021-11-29 16:32:50 -080036 // Use the default host allocator for buffer allocations.
Ben Vanike9006922024-07-16 11:00:47 -070037 iree_string_view_t identifier = iree_make_cstring_view("local-sync");
Ben Vanik0dd0cfc2021-11-29 16:32:50 -080038 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 Vanik315a0042021-11-30 13:11:09 -080041 host_allocator, &device_allocator);
Ben Vanik0dd0cfc2021-11-29 16:32:50 -080042 }
43
CindyLiu5b305c82021-06-23 13:28:28 +000044 if (iree_status_is_ok(status)) {
45 // Create the synchronous device.
Ben Vanik0dd0cfc2021-11-29 16:32:50 -080046 status = iree_hal_sync_device_create(
47 identifier, &params, /*loader_count=*/1, &loader, device_allocator,
48 host_allocator, out_device);
CindyLiu5b305c82021-06-23 13:28:28 +000049 }
Ben Vanik0dd0cfc2021-11-29 16:32:50 -080050
51 iree_hal_allocator_release(device_allocator);
CindyLiu5b305c82021-06-23 13:28:28 +000052 iree_hal_executable_loader_release(loader);
53 return status;
54}
55
56const 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}