Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 1 | // Copyright 2020 The IREE Authors |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 2 | // |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 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 |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 6 | |
Ben Vanik | 5a58aa4 | 2021-05-07 12:46:29 -0700 | [diff] [blame] | 7 | #include <array> |
Ben Vanik | 931a3b1 | 2021-05-20 13:27:13 -0700 | [diff] [blame] | 8 | #include <cstdio> |
Ben Vanik | 931a3b1 | 2021-05-20 13:27:13 -0700 | [diff] [blame] | 9 | #include <iterator> |
| 10 | #include <string> |
| 11 | #include <type_traits> |
| 12 | #include <utility> |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 13 | |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 14 | #include "iree/base/api.h" |
Ben Vanik | 5507c6e | 2021-02-02 21:27:08 -0800 | [diff] [blame] | 15 | #include "iree/base/internal/file_io.h" |
Ben Vanik | e28d253 | 2021-02-03 13:44:24 -0800 | [diff] [blame] | 16 | #include "iree/base/internal/flags.h" |
Ben Vanik | 931a3b1 | 2021-05-20 13:27:13 -0700 | [diff] [blame] | 17 | #include "iree/hal/api.h" |
Ben Vanik | d2f24f0 | 2021-06-21 09:13:53 -0700 | [diff] [blame] | 18 | #include "iree/modules/check/module.h" |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 19 | #include "iree/testing/gtest.h" |
Ben Vanik | 371a865 | 2020-08-09 01:36:48 -0700 | [diff] [blame] | 20 | #include "iree/testing/status_matchers.h" |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 21 | #include "iree/tooling/context_util.h" |
Ben Vanik | 7859d63 | 2022-10-24 14:37:28 -0700 | [diff] [blame] | 22 | #include "iree/tooling/device_util.h" |
Ben Vanik | 931a3b1 | 2021-05-20 13:27:13 -0700 | [diff] [blame] | 23 | #include "iree/vm/api.h" |
Ben Vanik | cf49d69 | 2023-02-24 20:24:09 -0800 | [diff] [blame] | 24 | #include "iree/vm/bytecode/module.h" |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 25 | |
Ben Vanik | ebeb5fc | 2021-04-24 09:40:50 -0700 | [diff] [blame] | 26 | IREE_FLAG( |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 27 | bool, expect_failure, false, |
| 28 | "Whether running module is expected to fail. If set, failing " |
| 29 | "statuses from function evaluation are logged and ignored and all " |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 30 | "evaluations succeeding is considered an error and will return a failure. " |
| 31 | "Mostly useful for testing the binary doesn't crash for failing tests."); |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 32 | |
| 33 | namespace iree { |
| 34 | namespace { |
| 35 | |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 36 | class CheckModuleTest : public ::testing::Test { |
| 37 | public: |
| 38 | explicit CheckModuleTest(iree_vm_instance_t* instance, |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 39 | const iree_tooling_module_list_t* module_list, |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 40 | iree_vm_function_t function) |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 41 | : instance_(instance), function_(function) { |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 42 | iree_tooling_module_list_clone(module_list, &module_list_); |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 43 | } |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 44 | ~CheckModuleTest() { iree_tooling_module_list_reset(&module_list_); } |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 45 | |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 46 | void SetUp() override { |
Ben Vanik | 007109f | 2022-08-03 07:26:50 -0700 | [diff] [blame] | 47 | IREE_CHECK_OK(iree_tooling_create_context_from_flags( |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 48 | instance_, module_list_.count, module_list_.values, |
Ben Vanik | 007109f | 2022-08-03 07:26:50 -0700 | [diff] [blame] | 49 | /*default_device_uri=*/iree_string_view_empty(), |
Ben Vanik | 7859d63 | 2022-10-24 14:37:28 -0700 | [diff] [blame] | 50 | iree_vm_instance_allocator(instance_), &context_, &device_, |
| 51 | /*out_device_allocator=*/NULL)); |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 52 | } |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 53 | |
Ben Vanik | 7859d63 | 2022-10-24 14:37:28 -0700 | [diff] [blame] | 54 | void TearDown() override { |
| 55 | iree_vm_context_release(context_); |
| 56 | iree_hal_device_release(device_); |
| 57 | } |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 58 | |
| 59 | void TestBody() override { |
Ben Vanik | 7859d63 | 2022-10-24 14:37:28 -0700 | [diff] [blame] | 60 | IREE_ASSERT_OK(iree_hal_begin_profiling_from_flags(device_)); |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 61 | IREE_EXPECT_OK(iree_vm_invoke(context_, function_, |
| 62 | IREE_VM_INVOCATION_FLAG_NONE, |
| 63 | /*policy=*/nullptr, |
| 64 | /*inputs=*/nullptr, /*outputs=*/nullptr, |
| 65 | iree_vm_instance_allocator(instance_))); |
Ben Vanik | 7859d63 | 2022-10-24 14:37:28 -0700 | [diff] [blame] | 66 | IREE_ASSERT_OK(iree_hal_end_profiling_from_flags(device_)); |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private: |
| 70 | iree_vm_instance_t* instance_ = nullptr; |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 71 | iree_tooling_module_list_t module_list_; |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 72 | iree_vm_function_t function_; |
| 73 | |
| 74 | iree_vm_context_t* context_ = nullptr; |
Ben Vanik | 7859d63 | 2022-10-24 14:37:28 -0700 | [diff] [blame] | 75 | iree_hal_device_t* device_ = nullptr; |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 78 | iree_status_t Run(iree_allocator_t host_allocator, int* out_exit_code) { |
Ben Vanik | ab989fc | 2021-02-03 10:30:08 -0800 | [diff] [blame] | 79 | *out_exit_code = 1; |
Ben Vanik | 2b26f8b | 2020-04-01 12:10:06 -0700 | [diff] [blame] | 80 | |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 81 | iree_vm_instance_t* instance = nullptr; |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 82 | IREE_RETURN_IF_ERROR(iree_tooling_create_instance(host_allocator, &instance), |
| 83 | "creating instance"); |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 84 | |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 85 | iree_vm_module_t* check_module = nullptr; |
Ben Vanik | 9aa83ed | 2022-08-06 12:55:34 -0700 | [diff] [blame] | 86 | IREE_RETURN_IF_ERROR( |
| 87 | iree_check_module_create(instance, host_allocator, &check_module)); |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 88 | |
Ben Vanik | 007109f | 2022-08-03 07:26:50 -0700 | [diff] [blame] | 89 | // Resolve all system modules required by the user and check modules. |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 90 | iree_tooling_module_list_t module_list; |
| 91 | iree_tooling_module_list_initialize(&module_list); |
| 92 | IREE_RETURN_IF_ERROR( |
| 93 | iree_tooling_module_list_push_back(&module_list, check_module)); |
| 94 | IREE_RETURN_IF_ERROR(iree_tooling_load_modules_from_flags( |
| 95 | instance, host_allocator, &module_list)); |
| 96 | iree_vm_module_t* main_module = iree_tooling_module_list_back(&module_list); |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 97 | |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 98 | auto module_signature = iree_vm_module_signature(main_module); |
Ben Vanik | 7f3a7e3 | 2020-11-14 14:16:07 -0800 | [diff] [blame] | 99 | for (iree_host_size_t ordinal = 0; |
| 100 | ordinal < module_signature.export_function_count; ++ordinal) { |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 101 | iree_vm_function_t function; |
Ben Vanik | 6c4dd5b | 2021-10-05 15:29:23 -0700 | [diff] [blame] | 102 | IREE_RETURN_IF_ERROR( |
| 103 | iree_vm_module_lookup_function_by_ordinal( |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 104 | main_module, IREE_VM_FUNCTION_LINKAGE_EXPORT, ordinal, &function), |
Scott Todd | 60b0764 | 2023-06-15 09:41:01 -0700 | [diff] [blame] | 105 | "looking up function export %" PRIhsz, ordinal); |
Ben Vanik | 6c4dd5b | 2021-10-05 15:29:23 -0700 | [diff] [blame] | 106 | iree_string_view_t function_name = iree_vm_function_name(&function); |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 107 | |
Ben Vanik | 6c4dd5b | 2021-10-05 15:29:23 -0700 | [diff] [blame] | 108 | if (iree_string_view_starts_with(function_name, |
Ben Vanik | 1ead648 | 2021-05-01 17:44:00 -0700 | [diff] [blame] | 109 | iree_make_cstring_view("__")) || |
Ben Vanik | 6c4dd5b | 2021-10-05 15:29:23 -0700 | [diff] [blame] | 110 | iree_string_view_find_char(function_name, '$', 0) != |
Ben Vanik | 1ead648 | 2021-05-01 17:44:00 -0700 | [diff] [blame] | 111 | IREE_STRING_VIEW_NPOS) { |
Stella Laurenzo | 7a4a852 | 2020-03-23 18:19:44 -0700 | [diff] [blame] | 112 | // Skip internal or special functions. |
| 113 | continue; |
| 114 | } |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 115 | |
Ben Vanik | 5a58aa4 | 2021-05-07 12:46:29 -0700 | [diff] [blame] | 116 | iree_vm_function_signature_t signature = |
| 117 | iree_vm_function_signature(&function); |
| 118 | iree_host_size_t argument_count = 0; |
| 119 | iree_host_size_t result_count = 0; |
| 120 | IREE_RETURN_IF_ERROR(iree_vm_function_call_count_arguments_and_results( |
| 121 | &signature, &argument_count, &result_count)); |
| 122 | if (argument_count || result_count) { |
Ben Vanik | bb7607f | 2021-02-03 11:58:09 -0800 | [diff] [blame] | 123 | return iree_make_status(IREE_STATUS_INVALID_ARGUMENT, |
| 124 | "expected function with no inputs or outputs, " |
| 125 | "but export '%.*s' has signature '%.*s'", |
Ben Vanik | 6c4dd5b | 2021-10-05 15:29:23 -0700 | [diff] [blame] | 126 | (int)function_name.size, function_name.data, |
Ben Vanik | 5a58aa4 | 2021-05-07 12:46:29 -0700 | [diff] [blame] | 127 | (int)signature.calling_convention.size, |
| 128 | signature.calling_convention.data); |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 129 | } |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 130 | |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 131 | iree_string_view_t module_name = iree_vm_module_name(main_module); |
| 132 | ::testing::RegisterTest(module_name.data, function_name.data, nullptr, |
| 133 | std::to_string(ordinal).c_str(), __FILE__, __LINE__, |
| 134 | [=]() -> CheckModuleTest* { |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 135 | return new CheckModuleTest(instance, &module_list, |
Ben Vanik | bcec0b5 | 2022-08-06 08:45:46 -0700 | [diff] [blame] | 136 | function); |
| 137 | }); |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 138 | } |
Ben Vanik | ab989fc | 2021-02-03 10:30:08 -0800 | [diff] [blame] | 139 | *out_exit_code = RUN_ALL_TESTS(); |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 140 | |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 141 | iree_tooling_module_list_reset(&module_list); |
Ben Vanik | 2838464 | 2020-05-19 10:23:49 -0700 | [diff] [blame] | 142 | iree_vm_module_release(check_module); |
Ben Vanik | 2838464 | 2020-05-19 10:23:49 -0700 | [diff] [blame] | 143 | iree_vm_instance_release(instance); |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 144 | |
Ben Vanik | 5a26619 | 2021-05-01 15:22:06 -0700 | [diff] [blame] | 145 | return iree_ok_status(); |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | } // namespace |
| 149 | |
| 150 | extern "C" int main(int argc, char** argv) { |
Ben Vanik | 7ed4f4b | 2023-06-14 13:33:54 -0700 | [diff] [blame] | 151 | IREE_TRACE_APP_ENTER(); |
| 152 | |
Ben Vanik | 1cb2f7a | 2021-04-26 16:32:53 -0700 | [diff] [blame] | 153 | // Pass through flags to gtest (allowing --help to fall through). |
| 154 | iree_flags_parse_checked(IREE_FLAGS_PARSE_MODE_UNDEFINED_OK | |
| 155 | IREE_FLAGS_PARSE_MODE_CONTINUE_AFTER_HELP, |
| 156 | &argc, &argv); |
Scott Todd | 5470635 | 2020-08-19 17:06:22 -0700 | [diff] [blame] | 157 | ::testing::InitGoogleTest(&argc, argv); |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 158 | |
Ben Vanik | cc43680 | 2023-06-10 08:53:52 -0700 | [diff] [blame] | 159 | IREE_TRACE_ZONE_BEGIN_NAMED(z0, "iree-check-module"); |
Ben Vanik | ab989fc | 2021-02-03 10:30:08 -0800 | [diff] [blame] | 160 | int exit_code = 1; |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 161 | iree_status_t status = Run(iree_allocator_system(), &exit_code); |
Ben Vanik | 14308b1 | 2023-06-13 10:22:28 -0700 | [diff] [blame] | 162 | exit_code = iree_status_is_ok(status) ? exit_code : EXIT_FAILURE; |
Ben Vanik | cc43680 | 2023-06-10 08:53:52 -0700 | [diff] [blame] | 163 | IREE_TRACE_ZONE_END(z0); |
Ben Vanik | 7ed4f4b | 2023-06-14 13:33:54 -0700 | [diff] [blame] | 164 | |
Ben Vanik | 14308b1 | 2023-06-13 10:22:28 -0700 | [diff] [blame] | 165 | IREE_TRACE_APP_EXIT(exit_code); |
Ben Vanik | cc43680 | 2023-06-10 08:53:52 -0700 | [diff] [blame] | 166 | |
Ben Vanik | ebeb5fc | 2021-04-24 09:40:50 -0700 | [diff] [blame] | 167 | if (FLAG_expect_failure) { |
Ben Vanik | 14308b1 | 2023-06-13 10:22:28 -0700 | [diff] [blame] | 168 | if (exit_code == 0) { |
bjacob | 1cb92dd | 2022-09-26 16:21:02 +0000 | [diff] [blame] | 169 | printf("Test passed but expected failure\n"); |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 170 | return 1; |
| 171 | } |
bjacob | 1cb92dd | 2022-09-26 16:21:02 +0000 | [diff] [blame] | 172 | printf("Test failed as expected\n"); |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 173 | return 0; |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 174 | } |
Geoffrey Martin-Noble | c9db858 | 2020-03-04 14:16:46 -0800 | [diff] [blame] | 175 | |
Ben Vanik | 14308b1 | 2023-06-13 10:22:28 -0700 | [diff] [blame] | 176 | if (exit_code != 0) { |
bjacob | 1cb92dd | 2022-09-26 16:21:02 +0000 | [diff] [blame] | 177 | printf("Test failed\n%s\n", Status(std::move(status)).ToString().c_str()); |
Thomas | f1aa6f4 | 2021-04-05 19:53:56 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Ben Vanik | 14308b1 | 2023-06-13 10:22:28 -0700 | [diff] [blame] | 180 | return exit_code; |
Geoffrey Martin-Noble | 94d7b6e | 2020-02-20 13:43:10 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | } // namespace iree |