Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 1 | # Copyright 2020 The IREE Authors |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [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 | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 6 | |
| 7 | """Macros for defining tests that run a module using iree-check-module.""" |
| 8 | |
Scott Todd | 2ca72b6 | 2022-01-12 15:36:12 -0800 | [diff] [blame] | 9 | load("//build_tools/bazel:iree_bytecode_module.bzl", "iree_bytecode_module") |
Geoffrey Martin-Noble | 435c270 | 2022-01-24 15:56:56 -0800 | [diff] [blame] | 10 | load("//build_tools/bazel:native_binary.bzl", "native_test") |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 11 | |
| 12 | ALL_TARGET_BACKENDS_AND_DRIVERS = [ |
Ben Vanik | 6e64b6e | 2022-06-07 09:14:53 -0700 | [diff] [blame] | 13 | ("vmvx", "local-task"), |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 14 | ("vulkan-spirv", "vulkan"), |
Scott Todd | 352da3f | 2022-07-20 15:25:11 -0700 | [diff] [blame] | 15 | ("llvm-cpu", "local-task"), |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 16 | ] |
| 17 | |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 18 | def iree_check_test( |
| 19 | name, |
| 20 | src, |
| 21 | target_backend, |
Scott Todd | 0a561cd | 2022-03-14 09:55:30 -0700 | [diff] [blame] | 22 | driver = None, |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 23 | compiler_flags = [], |
| 24 | runner_args = [], |
| 25 | tags = [], |
bjacob | 949eb89 | 2022-01-21 22:16:56 -0500 | [diff] [blame] | 26 | target_cpu_features = None, |
Geoffrey Martin-Noble | f81a36b | 2021-06-07 21:35:14 -0700 | [diff] [blame] | 27 | timeout = None, |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 28 | **kwargs): |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 29 | """Creates an iree-check-module test for the specified source file. |
| 30 | |
| 31 | Args: |
| 32 | name: name of the generated test. |
| 33 | src: source mlir file containing the module. |
| 34 | target_backend: target backend to compile for. |
Scott Todd | 0a561cd | 2022-03-14 09:55:30 -0700 | [diff] [blame] | 35 | driver: driver to run the module with. This can be omitted to test only |
| 36 | compilation, but consider omiting the driver as a hacky abuse of the |
| 37 | rule since compilation on its own not use iree-check-module. |
Scott Todd | 2814631 | 2022-06-10 13:07:58 -0700 | [diff] [blame] | 38 | compiler_flags: additional flags to pass to the compiler. Bytecode output |
| 39 | format and backend flags are passed automatically. |
| 40 | runner_args: additional runner_args to pass to iree-check-module. The |
| 41 | driver and input file are passed automatically. |
| 42 | tags: additional tags to apply to the generated test. A tag |
| 43 | "driver=DRIVER" is added automatically. |
| 44 | target_cpu_features: currently unimplemented (must be empty), will |
| 45 | eventually allow specifying target CPU features. |
Geoffrey Martin-Noble | f81a36b | 2021-06-07 21:35:14 -0700 | [diff] [blame] | 46 | timeout: timeout for the generated tests. |
Geoffrey Martin-Noble | 435c270 | 2022-01-24 15:56:56 -0800 | [diff] [blame] | 47 | **kwargs: any additional attributes to pass to the underlying native_test. |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 48 | """ |
bjacob | 949eb89 | 2022-01-21 22:16:56 -0500 | [diff] [blame] | 49 | |
| 50 | if target_cpu_features: |
| 51 | fail("target_cpu_features must currently be empty") |
| 52 | |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 53 | bytecode_module_name = name + "_bytecode_module" |
| 54 | iree_bytecode_module( |
| 55 | name = bytecode_module_name, |
| 56 | src = src, |
| 57 | flags = [ |
Scott Todd | 52f62b8 | 2022-05-10 17:51:34 -0700 | [diff] [blame] | 58 | "--mlir-print-op-on-diagnostic=false", |
| 59 | "--iree-hal-target-backends=%s" % target_backend, |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 60 | ] + compiler_flags, |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 61 | visibility = ["//visibility:private"], |
| 62 | ) |
| 63 | |
Scott Todd | 0a561cd | 2022-03-14 09:55:30 -0700 | [diff] [blame] | 64 | if not driver: |
| 65 | return |
| 66 | |
Geoffrey Martin-Noble | 435c270 | 2022-01-24 15:56:56 -0800 | [diff] [blame] | 67 | native_test( |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 68 | name = name, |
| 69 | args = [ |
Ben Vanik | bba52ae | 2022-06-10 10:15:56 -0700 | [diff] [blame] | 70 | "--device=%s" % driver, |
Ben Vanik | 9461d3b | 2023-04-18 16:39:25 -0700 | [diff] [blame] | 71 | "--module=$(location :%s)" % bytecode_module_name, |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 72 | ] + runner_args, |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 73 | data = [":%s" % bytecode_module_name], |
Scott Todd | f57ab75 | 2022-05-23 10:36:44 -0700 | [diff] [blame] | 74 | src = "//tools:iree-check-module", |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 75 | tags = tags + ["driver=%s" % driver], |
Geoffrey Martin-Noble | f81a36b | 2021-06-07 21:35:14 -0700 | [diff] [blame] | 76 | timeout = timeout, |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 77 | **kwargs |
| 78 | ) |
| 79 | |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 80 | def iree_check_single_backend_test_suite( |
| 81 | name, |
| 82 | srcs, |
| 83 | target_backend, |
Scott Todd | 0a561cd | 2022-03-14 09:55:30 -0700 | [diff] [blame] | 84 | driver = None, |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 85 | compiler_flags = [], |
| 86 | runner_args = [], |
| 87 | tags = [], |
bjacob | 949eb89 | 2022-01-21 22:16:56 -0500 | [diff] [blame] | 88 | target_cpu_features = None, |
Geoffrey Martin-Noble | f81a36b | 2021-06-07 21:35:14 -0700 | [diff] [blame] | 89 | timeout = None, |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 90 | **kwargs): |
| 91 | """Creates a test suite of iree-check-module tests for a single backend/driver pair. |
| 92 | |
| 93 | One test is generated per source file. |
| 94 | |
| 95 | Args: |
| 96 | name: name of the generated test suite. |
| 97 | srcs: source mlir files containing the module. |
| 98 | target_backend: target backend to compile for. |
Scott Todd | 0a561cd | 2022-03-14 09:55:30 -0700 | [diff] [blame] | 99 | driver: driver to run the module with. This can be omitted to test only |
| 100 | compilation, but consider omiting the driver as a hacky abuse of the |
| 101 | rule since compilation on its own not use iree-check-module. |
Scott Todd | 2814631 | 2022-06-10 13:07:58 -0700 | [diff] [blame] | 102 | compiler_flags: additional flags to pass to the compiler. Bytecode output |
| 103 | format and backend flags are passed automatically. |
| 104 | runner_args: additional runner_args to pass to the underlying |
| 105 | iree-check-module tests. The driver and input file are passed |
| 106 | automatically. To use different runner_args per test, create a |
| 107 | separate suite or iree_check_test. |
| 108 | target_cpu_features: currently unimplemented (must be empty), will |
| 109 | eventually allow specifying target CPU features. |
| 110 | tags: tags to apply to the generated tests. Note that as in standard test |
| 111 | suites, manual is treated specially and will also apply to the test |
| 112 | suite itself. |
Geoffrey Martin-Noble | f81a36b | 2021-06-07 21:35:14 -0700 | [diff] [blame] | 113 | timeout: timeout for the generated tests. |
Scott Todd | 2814631 | 2022-06-10 13:07:58 -0700 | [diff] [blame] | 114 | **kwargs: any additional attributes to pass to the underlying tests and |
| 115 | test suite. |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 116 | """ |
bjacob | 949eb89 | 2022-01-21 22:16:56 -0500 | [diff] [blame] | 117 | |
| 118 | # We haven't implemented this so far because we have been using target_cpu_features so far only |
| 119 | # for aarch64 targets, for which we use the CMake build. To future people implementing this: |
| 120 | # target_cpu_features should be a list, and here it should be joined into a comma-separated |
Ben Vanik | 380bde7 | 2023-03-08 10:55:58 -0800 | [diff] [blame] | 121 | # string to be passed to --iree-llvmcpu-target-cpu-features |
bjacob | 949eb89 | 2022-01-21 22:16:56 -0500 | [diff] [blame] | 122 | if target_cpu_features: |
| 123 | fail("target_cpu_features must currently be empty") |
| 124 | |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 125 | tests = [] |
| 126 | for src in srcs: |
| 127 | test_name = "_".join([name, src]) |
| 128 | iree_check_test( |
| 129 | name = test_name, |
| 130 | src = src, |
| 131 | target_backend = target_backend, |
| 132 | driver = driver, |
| 133 | compiler_flags = compiler_flags, |
| 134 | runner_args = runner_args, |
| 135 | tags = tags, |
Geoffrey Martin-Noble | f81a36b | 2021-06-07 21:35:14 -0700 | [diff] [blame] | 136 | timeout = timeout, |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 137 | **kwargs |
| 138 | ) |
| 139 | tests.append(test_name) |
Scott Todd | 0a561cd | 2022-03-14 09:55:30 -0700 | [diff] [blame] | 140 | |
| 141 | if not driver: |
| 142 | return |
| 143 | |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 144 | native.test_suite( |
| 145 | name = name, |
| 146 | tests = tests, |
| 147 | # Note that only the manual tag really has any effect here. Others are |
| 148 | # used for test suite filtering, but all tests are passed the same tags. |
| 149 | tags = tags, |
| 150 | # If there are kwargs that need to be passed here which only apply to |
| 151 | # the generated tests and not to test_suite, they should be extracted |
| 152 | # into separate named arguments. |
| 153 | **kwargs |
| 154 | ) |
| 155 | |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 156 | def iree_check_test_suite( |
| 157 | name, |
| 158 | srcs, |
| 159 | target_backends_and_drivers = ALL_TARGET_BACKENDS_AND_DRIVERS, |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 160 | compiler_flags = [], |
| 161 | runner_args = [], |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 162 | tags = [], |
bjacob | 949eb89 | 2022-01-21 22:16:56 -0500 | [diff] [blame] | 163 | target_cpu_features_variants = [], |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 164 | **kwargs): |
| 165 | """Creates a test suite of iree-check-module tests. |
| 166 | |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 167 | One test is generated per source file and backend/driver. |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 168 | |
| 169 | Args: |
| 170 | name: name of the generated test suite. |
| 171 | srcs: source mlir files containing the module. |
Scott Todd | 2814631 | 2022-06-10 13:07:58 -0700 | [diff] [blame] | 172 | target_backends_and_drivers: backend/driver pairs to compile and run the |
| 173 | module, respectively. |
| 174 | compiler_flags: additional flags to pass to the compiler. Bytecode output |
| 175 | format and backend flags are passed automatically. |
| 176 | runner_args: additional runner_args to pass to the underlying |
| 177 | iree-check-module tests. The driver and input file are passed |
| 178 | automatically. To use different runner_args per test, create a |
| 179 | separate suite or iree_check_test. |
| 180 | tags: tags to apply to the generated tests. Note that as in standard test |
| 181 | suites, manual is treated specially and will also apply to the test |
| 182 | suite itself. |
| 183 | target_cpu_features_variants: list of target cpu features variants. |
| 184 | Currently unimplemented, so each entry must be either "default" or |
| 185 | start with "aarch64:" so as Bazel builds are currently x86-only, we |
| 186 | know that it is correct to ignore this. |
| 187 | **kwargs: any additional attributes to pass to the underlying tests and |
| 188 | test suite. |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 189 | """ |
| 190 | |
bjacob | 949eb89 | 2022-01-21 22:16:56 -0500 | [diff] [blame] | 191 | for target_cpu_features in target_cpu_features_variants: |
| 192 | if not (target_cpu_features == "default" or target_cpu_features.startswith("aarch64:")): |
| 193 | fail("Entry %s in target_cpu_features_variants: unimplemented" % target_cpu_features) |
| 194 | |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 195 | # We could have complicated argument override logic for runner_args and such, or... the client |
| 196 | # could just create a test suite. The latter seems simpler and more readable. |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 197 | tests = [] |
| 198 | for backend, driver in target_backends_and_drivers: |
Stella Laurenzo | 794e592 | 2022-02-03 17:56:53 -0800 | [diff] [blame] | 199 | # CUDA backend/driver not supported by Bazel build. |
| 200 | if backend == "cuda" or driver == "cuda": |
| 201 | continue |
Geoffrey Martin-Noble | 00a390f | 2020-04-10 11:36:01 -0700 | [diff] [blame] | 202 | suite_name = "_".join([name, backend, driver]) |
| 203 | iree_check_single_backend_test_suite( |
| 204 | name = suite_name, |
| 205 | srcs = srcs, |
| 206 | driver = driver, |
| 207 | target_backend = backend, |
| 208 | compiler_flags = compiler_flags, |
| 209 | runner_args = runner_args, |
| 210 | tags = tags, |
| 211 | **kwargs |
| 212 | ) |
| 213 | tests.append(suite_name) |
Geoffrey Martin-Noble | 3560454 | 2020-03-24 18:33:46 -0700 | [diff] [blame] | 214 | native.test_suite( |
| 215 | name = name, |
| 216 | tests = tests, |
| 217 | # Note that only the manual tag really has any effect here. Others are |
| 218 | # used for test suite filtering, but all tests are passed the same tags. |
| 219 | tags = tags, |
| 220 | # If there are kwargs that need to be passed here which only apply to |
| 221 | # the generated tests and not to test_suite, they should be extracted |
| 222 | # into separate named arguments. |
| 223 | **kwargs |
| 224 | ) |