blob: 67d53addb7faeadf8b5e41ced9442902275a0477 [file] [log] [blame]
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07001# Copyright 2020 The IREE Authors
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -07002#
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07003# 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-Noble35604542020-03-24 18:33:46 -07006
7"""Macros for defining tests that run a module using iree-check-module."""
8
Scott Todd2ca72b62022-01-12 15:36:12 -08009load("//build_tools/bazel:iree_bytecode_module.bzl", "iree_bytecode_module")
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -080010load("//build_tools/bazel:native_binary.bzl", "native_test")
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070011
12ALL_TARGET_BACKENDS_AND_DRIVERS = [
Ben Vanik6e64b6e2022-06-07 09:14:53 -070013 ("vmvx", "local-task"),
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070014 ("vulkan-spirv", "vulkan"),
Scott Todd352da3f2022-07-20 15:25:11 -070015 ("llvm-cpu", "local-task"),
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070016]
17
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -070018def iree_check_test(
19 name,
20 src,
21 target_backend,
Scott Todd0a561cd2022-03-14 09:55:30 -070022 driver = None,
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -070023 compiler_flags = [],
24 runner_args = [],
25 tags = [],
bjacob949eb892022-01-21 22:16:56 -050026 target_cpu_features = None,
Geoffrey Martin-Noblef81a36b2021-06-07 21:35:14 -070027 timeout = None,
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -070028 **kwargs):
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070029 """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 Todd0a561cd2022-03-14 09:55:30 -070035 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 Todd28146312022-06-10 13:07:58 -070038 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-Noblef81a36b2021-06-07 21:35:14 -070046 timeout: timeout for the generated tests.
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -080047 **kwargs: any additional attributes to pass to the underlying native_test.
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070048 """
bjacob949eb892022-01-21 22:16:56 -050049
50 if target_cpu_features:
51 fail("target_cpu_features must currently be empty")
52
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070053 bytecode_module_name = name + "_bytecode_module"
54 iree_bytecode_module(
55 name = bytecode_module_name,
56 src = src,
57 flags = [
Scott Todd52f62b82022-05-10 17:51:34 -070058 "--mlir-print-op-on-diagnostic=false",
59 "--iree-hal-target-backends=%s" % target_backend,
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -070060 ] + compiler_flags,
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070061 visibility = ["//visibility:private"],
62 )
63
Scott Todd0a561cd2022-03-14 09:55:30 -070064 if not driver:
65 return
66
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -080067 native_test(
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070068 name = name,
69 args = [
Ben Vanikbba52ae2022-06-10 10:15:56 -070070 "--device=%s" % driver,
Ben Vanik9461d3b2023-04-18 16:39:25 -070071 "--module=$(location :%s)" % bytecode_module_name,
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -070072 ] + runner_args,
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070073 data = [":%s" % bytecode_module_name],
Scott Toddf57ab752022-05-23 10:36:44 -070074 src = "//tools:iree-check-module",
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070075 tags = tags + ["driver=%s" % driver],
Geoffrey Martin-Noblef81a36b2021-06-07 21:35:14 -070076 timeout = timeout,
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -070077 **kwargs
78 )
79
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -070080def iree_check_single_backend_test_suite(
81 name,
82 srcs,
83 target_backend,
Scott Todd0a561cd2022-03-14 09:55:30 -070084 driver = None,
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -070085 compiler_flags = [],
86 runner_args = [],
87 tags = [],
bjacob949eb892022-01-21 22:16:56 -050088 target_cpu_features = None,
Geoffrey Martin-Noblef81a36b2021-06-07 21:35:14 -070089 timeout = None,
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -070090 **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 Todd0a561cd2022-03-14 09:55:30 -070099 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 Todd28146312022-06-10 13:07:58 -0700102 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-Noblef81a36b2021-06-07 21:35:14 -0700113 timeout: timeout for the generated tests.
Scott Todd28146312022-06-10 13:07:58 -0700114 **kwargs: any additional attributes to pass to the underlying tests and
115 test suite.
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -0700116 """
bjacob949eb892022-01-21 22:16:56 -0500117
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 Vanik380bde72023-03-08 10:55:58 -0800121 # string to be passed to --iree-llvmcpu-target-cpu-features
bjacob949eb892022-01-21 22:16:56 -0500122 if target_cpu_features:
123 fail("target_cpu_features must currently be empty")
124
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -0700125 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-Noblef81a36b2021-06-07 21:35:14 -0700136 timeout = timeout,
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -0700137 **kwargs
138 )
139 tests.append(test_name)
Scott Todd0a561cd2022-03-14 09:55:30 -0700140
141 if not driver:
142 return
143
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -0700144 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-Noble35604542020-03-24 18:33:46 -0700156def iree_check_test_suite(
157 name,
158 srcs,
159 target_backends_and_drivers = ALL_TARGET_BACKENDS_AND_DRIVERS,
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -0700160 compiler_flags = [],
161 runner_args = [],
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -0700162 tags = [],
bjacob949eb892022-01-21 22:16:56 -0500163 target_cpu_features_variants = [],
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -0700164 **kwargs):
165 """Creates a test suite of iree-check-module tests.
166
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -0700167 One test is generated per source file and backend/driver.
Geoffrey Martin-Noble35604542020-03-24 18:33:46 -0700168
169 Args:
170 name: name of the generated test suite.
171 srcs: source mlir files containing the module.
Scott Todd28146312022-06-10 13:07:58 -0700172 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-Noble35604542020-03-24 18:33:46 -0700189 """
190
bjacob949eb892022-01-21 22:16:56 -0500191 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-Noble00a390f2020-04-10 11:36:01 -0700195 # 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-Noble35604542020-03-24 18:33:46 -0700197 tests = []
198 for backend, driver in target_backends_and_drivers:
Stella Laurenzo794e5922022-02-03 17:56:53 -0800199 # CUDA backend/driver not supported by Bazel build.
200 if backend == "cuda" or driver == "cuda":
201 continue
Geoffrey Martin-Noble00a390f2020-04-10 11:36:01 -0700202 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-Noble35604542020-03-24 18:33:46 -0700214 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 )