blob: 5394d2b7b359edae343fe5e3d3faa3203716d7ed [file] [log] [blame]
Stella Laurenzod318c542023-04-27 17:08:22 -07001// Copyright 2020 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#include "iree/base/api.h"
8#include "iree/base/internal/flags.h"
Stella Laurenzod318c542023-04-27 17:08:22 -07009#include "iree/tooling/context_util.h"
10#include "iree/tooling/run_module.h"
11#include "iree/vm/api.h"
12
13int main(int argc, char** argv) {
Ben Vanik7ed4f4b2023-06-14 13:33:54 -070014 IREE_TRACE_APP_ENTER();
Stella Laurenzod318c542023-04-27 17:08:22 -070015 IREE_TRACE_ZONE_BEGIN(z0);
16
17 // Parse command line flags.
18 iree_flags_set_usage(
19 "iree-run-module",
20 "Runs a function within a compiled IREE module and handles I/O parsing\n"
21 "and optional expected value verification/output processing. Modules\n"
22 "can be provided by file path (`--module=file.vmfb`) or read from stdin\n"
23 "(`--module=-`) and the function to execute matches the original name\n"
24 "provided to the compiler (`--function=foo` for `func.func @foo`).\n");
25 iree_flags_parse_checked(IREE_FLAGS_PARSE_MODE_DEFAULT, &argc, &argv);
26
27 // Hosting applications can provide their own allocators to pool resources or
28 // track allocation statistics related to IREE code.
29 iree_allocator_t host_allocator = iree_allocator_system();
30 // Hosting applications should reuse instances across multiple contexts that
31 // have similar composition (similar types/modules/etc). Most applications can
32 // get by with a single shared instance.
33 iree_vm_instance_t* instance = NULL;
34 iree_status_t status =
35 iree_tooling_create_instance(host_allocator, &instance);
36
37 // Utility to run the module with the command line flags. This particular
38 // method is only useful in these IREE tools that want consistent flags -
39 // a real application will need to do what this is doing with its own setup
40 // and I/O handling.
Ben Vanik7ed4f4b2023-06-14 13:33:54 -070041 int exit_code = EXIT_SUCCESS;
Stella Laurenzod318c542023-04-27 17:08:22 -070042 if (iree_status_is_ok(status)) {
43 status = iree_tooling_run_module_from_flags(instance, host_allocator,
44 &exit_code);
45 }
46
47 iree_vm_instance_release(instance);
48
49 if (!iree_status_is_ok(status)) {
50 iree_status_fprint(stderr, status);
51 iree_status_free(status);
52 exit_code = EXIT_FAILURE;
53 }
54
55 IREE_TRACE_ZONE_END(z0);
Ben Vanik14308b12023-06-13 10:22:28 -070056 IREE_TRACE_APP_EXIT(exit_code);
Stella Laurenzod318c542023-04-27 17:08:22 -070057 return exit_code;
58}