blob: b214a35c79ec7541da732abbcf7c90ff8012bc55 [file] [log] [blame]
bjacob5feef482021-10-21 16:53:58 -04001# 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
7include(CMakeParseArguments)
8
9# iree_trace_runner_test()
10#
11# Creates a test using a specified trace-runner program for the specified
12# replay trace.
13#
14# Parameters:
15# NAME: Name of the target
16# SRC: mlir source file to be compiled to an IREE module.
17# TARGET_BACKEND: target backend to compile for.
18# DRIVER: driver to run the module with.
19# COMPILER_FLAGS: additional flags to pass to the compiler. Bytecode
20# translation and backend flags are passed automatically.
21# RUNNER_ARGS: additional args to pass to the trace-runner program. The driver
22# and input file flags are passed automatically.
23# LABELS: Additional labels to apply to the test. The package path and
24# "driver=${DRIVER}" are added automatically.
25# OPT_TOOL: Defaulting to iree-opt. Tool used to preprocess the source files
26# if OPT_FLAGS is specified.
27# OPT_FLAGS: If specified, source files are preprocessed with OPT_TOOL with
28# these flags.
29# TRACE_RUNNER: trace-runner program to run.
30# TRACE: trace file input to the trace-runner program.
31# MODULE_FILE_NAME: specifies the absolute path to the filename to use for the
32# generated IREE module (.vmfb). Mandatory, unlike in iree_check_test,
33# because trace files (.yaml) reference a specific module file path.
34function(iree_trace_runner_test)
35 if(NOT IREE_BUILD_TESTS)
36 return()
37 endif()
38
39 # See comment in iree_check_test about this condition.
40 if(NOT IREE_BUILD_COMPILER AND NOT CMAKE_CROSSCOMPILING)
41 return()
42 endif()
43
44 cmake_parse_arguments(
45 _RULE
46 ""
47 "NAME;SRC;TRACE;TARGET_BACKEND;DRIVER;OPT_TOOL;TRACE_RUNNER;MODULE_FILE_NAME"
48 "COMPILER_FLAGS;RUNNER_ARGS;LABELS;OPT_FLAGS"
49 ${ARGN}
50 )
51
52 iree_package_name(_PACKAGE_NAME)
53 set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}")
54
55 set(_MODULE_NAME "${_RULE_NAME}_module")
56
57 iree_bytecode_module_for_iree_check_test_and_friends(
58 MODULE_NAME
59 "${_MODULE_NAME}"
60 MODULE_FILE_NAME
61 "${_RULE_MODULE_FILE_NAME}"
62 SRC
63 "${_RULE_SRC}"
64 TARGET_BACKEND
65 "${_RULE_TARGET_BACKEND}"
66 FLAGS
67 ${_RULE_COMPILER_FLAGS}
68 OPT_TOOL
69 ${_RULE_OPT_TOOL}
70 OPT_FLAGS
71 ${_RULE_OPT_FLAGS}
72 )
73
74 # iree_bytecode_module does not define a target, only a custom command.
75 # We need to create a target that depends on the command to ensure the
76 # module gets built.
77 # TODO(b/146898896): Do this in iree_bytecode_module and avoid having to
78 # reach into the internals.
79 set(_MODULE_TARGET_NAME "${_NAME}_module")
80 add_custom_target(
81 "${_MODULE_TARGET_NAME}"
82 DEPENDS
83 "${_RULE_MODULE_FILE_NAME}"
84 )
85
86 # A target specifically for the test. We could combine this with the above,
87 # but we want that one to get pulled into iree_bytecode_module.
88 add_custom_target("${_NAME}" ALL)
89 add_dependencies(
90 "${_NAME}"
91 "${_MODULE_TARGET_NAME}"
92 "${_RULE_TRACE_RUNNER}"
93 )
94
95 iree_run_binary_test(
96 NAME
97 "${_RULE_NAME}"
98 DRIVER
99 "${_RULE_DRIVER}"
100 TEST_BINARY
101 "${_RULE_TRACE_RUNNER}"
102 TEST_INPUT_FILE_ARG
103 ${_RULE_TRACE}
104 DATA
105 ${_MODULE_FILE_NAME}
106 ARGS
107 ${_RULE_RUNNER_ARGS}
108 LABELS
109 ${_RULE_LABELS}
110 )
111endfunction()
112
113# iree_single_backend_generated_trace_runner_test()
114#
115# Variant of iree_trace_runner_test where instead of specifying
116# a source file (and possibly a trace file and module path), one passes a
117# generator program.
118#
119# Parameters:
120# NAME: Name of the target
121# GENERATOR: Program (at the moment, must be Python3) to run to generate the
122# source file (and possibly a trace file and module path). It will be
123# invoked with the following standard flags, in addition to GENERATOR_ARGS:
124# --output_code=${CMAKE_CURRENT_BINARY_DIR}/name.mlir
125# --output_trace=${CMAKE_CURRENT_BINARY_DIR}/name.yaml
126# --module_path=${CMAKE_CURRENT_BINARY_DIR}/name.vmfb
127# GENERATOR_ARGS: additional args to pass to the generator program.
128# TARGET_BACKEND: target backend to compile for.
129# DRIVER: driver to run the module with.
130# COMPILER_FLAGS: additional flags to pass to the compiler. Bytecode
131# translation and backend flags are passed automatically.
132# RUNNER_ARGS: additional args to pass to the trace-runner program. The driver
133# and input file flags are passed automatically.
134# LABELS: Additional labels to apply to the test. The package path and
135# "driver=${DRIVER}" are added automatically.
136# OPT_TOOL: Defaulting to iree-opt. Tool used to preprocess the source files
137# if OPT_FLAGS is specified.
138# OPT_FLAGS: If specified, source files are preprocessed with OPT_TOOL with
139# these flags.
140# TRACE_RUNNER: trace-runner program to run.
141function(iree_single_backend_generated_trace_runner_test)
142 if(NOT IREE_BUILD_TESTS)
143 return()
144 endif()
145
146 # Copied from iree_check_test. Refer to the comment there.
147 if(NOT IREE_BUILD_COMPILER AND NOT CMAKE_CROSSCOMPILING)
148 return()
149 endif()
150
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700151 # Traces are YAML files and we assume that PyYAML is required. See the
152 # warning that is emitted in aggregate in the main CMakeLists.txt if this
153 # is not true.
154 if(NOT IREE_PYYAML_FOUND)
155 return()
156 endif()
157
bjacob5feef482021-10-21 16:53:58 -0400158 cmake_parse_arguments(
159 _RULE
160 ""
161 "NAME;GENERATOR;TARGET_BACKEND;DRIVER;OPT_TOOL;TRACE_RUNNER"
162 "GENERATOR_ARGS;COMPILER_FLAGS;RUNNER_ARGS;LABELS;OPT_FLAGS"
163 ${ARGN}
164 )
165
166 # Omit tests for which the specified driver or target backend is not enabled.
167 # This overlaps with directory exclusions and other filtering mechanisms.
168 string(TOUPPER ${_RULE_DRIVER} _UPPERCASE_DRIVER)
169 if(NOT DEFINED IREE_HAL_DRIVER_${_UPPERCASE_DRIVER})
170 message(SEND_ERROR "Unknown driver '${_RULE_DRIVER}'. Check IREE_ALL_HAL_DRIVERS.")
171 endif()
172 if(NOT IREE_HAL_DRIVER_${_UPPERCASE_DRIVER})
173 return()
174 endif()
175 string(TOUPPER ${_RULE_TARGET_BACKEND} _UPPERCASE_TARGET_BACKEND)
176 if(NOT DEFINED IREE_TARGET_BACKEND_${_UPPERCASE_TARGET_BACKEND})
177 message(SEND_ERROR "Unknown backend '${_RULE_TARGET_BACKEND}'. Check IREE_ALL_TARGET_BACKENDS.")
178 endif()
179 if(DEFINED IREE_HOST_BINARY_ROOT)
180 # If we're not building the host tools from source under this configuration,
181 # such as when cross compiling, then we can't easily check for which
182 # compiler target backends are enabled. Just assume all are enabled and only
183 # rely on the runtime HAL driver check above for filtering.
184 else()
185 # We are building the host tools, so check enabled compiler target backends.
186 if(NOT IREE_TARGET_BACKEND_${_UPPERCASE_TARGET_BACKEND})
187 return()
188 endif()
189 endif()
190
191 iree_package_name(_PACKAGE_NAME)
192 set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}")
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700193
bjacob5feef482021-10-21 16:53:58 -0400194 set(_SRC "${CMAKE_CURRENT_BINARY_DIR}/${_RULE_NAME}.mlir")
195
196 set(_GENERATOR_OUTPUT "${_SRC}")
197 set(_TRACE "${CMAKE_CURRENT_BINARY_DIR}/${_RULE_NAME}.yaml")
198 set(_MODULE_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_RULE_NAME}.vmfb")
199 list(APPEND _GENERATOR_STANDARD_FLAGS "--output_code=${_SRC}")
200 list(APPEND _GENERATOR_STANDARD_FLAGS "--output_trace=${_TRACE}")
201 list(APPEND _GENERATOR_STANDARD_FLAGS "--module_path=${_MODULE_FILE_NAME}")
202 list(APPEND _GENERATOR_OUTPUT "${_TRACE}")
203
204 add_custom_command(
205 COMMAND
206 "${Python3_EXECUTABLE}"
207 "${CMAKE_CURRENT_SOURCE_DIR}/${_RULE_GENERATOR}"
208 ${_GENERATOR_STANDARD_FLAGS}
209 ${_RULE_GENERATOR_ARGS}
210 OUTPUT
211 ${_GENERATOR_OUTPUT}
212 DEPENDS
213 ${_RULE_GENERATOR}
214 )
215
216 add_custom_target(
217 "${_NAME}_generated_files"
218 DEPENDS
219 ${_GENERATOR_OUTPUT}
220 )
221
222 iree_trace_runner_test(
223 NAME
224 "${_RULE_NAME}"
225 SRC
226 "${_SRC}"
227 TRACE
228 "${_TRACE}"
229 TRACE_RUNNER
230 "${_RULE_TRACE_RUNNER}"
231 MODULE_FILE_NAME
232 "${_MODULE_FILE_NAME}"
233 TARGET_BACKEND
234 ${_RULE_TARGET_BACKEND}
235 DRIVER
236 ${_RULE_DRIVER}
237 COMPILER_FLAGS
238 ${_RULE_COMPILER_FLAGS}
239 RUNNER_ARGS
240 ${_RULE_RUNNER_ARGS}
241 LABELS
242 ${_RULE_LABELS}
243 OPT_TOOL
244 ${_RULE_OPT_TOOL}
245 OPT_FLAGS
246 ${_RULE_OPT_FLAGS}
247 )
248
249 # Note we are relying on the fact that the target created by
250 # iree_trace_runner_test is _NAME, even though we passed _RULE_NAME to it,
251 # i.e. we are relying on the prefixing to be identical.
252 add_dependencies("${_NAME}" "${_NAME}_generated_files")
253endfunction()
254
255
256# iree_generated_trace_runner_test()
257#
258# Creates a set of iree_single_backend_generated_trace_runner_test's differing
259# by target backend and driver.
260#
261# Mirrors the bzl rule of the same name.
262#
263# One test is generated per source and backend/driver pair.
264# Parameters:
265# NAME: Name of the target
266# GENERATOR: Program (at the moment, must be Python3) to run to generate the
267# source file (and possibly a trace file and module path). It will be
268# invoked with the following standard flags, in addition to GENERATOR_ARGS:
269# --output_code=${CMAKE_CURRENT_BINARY_DIR}/name.mlir
270# --output_trace=${CMAKE_CURRENT_BINARY_DIR}/name.yaml
271# --module_path=${CMAKE_CURRENT_BINARY_DIR}/name.vmfb
272# GENERATOR_ARGS: additional args to pass to the generator program.
273# TARGET_BACKENDS: backends to compile the module for. These form pairs with
274# the DRIVERS argument (due to cmake limitations they are separate list
275# arguments). The lengths must exactly match. If no backends or drivers are
276# specified, a test will be generated for every supported pair.
277# DRIVERS: drivers to run the module with. These form pairs with the
278# TARGET_BACKENDS argument (due to cmake limitations they are separate list
279# arguments). The lengths must exactly match. If no backends or drivers are
280# specified, a test will be generated for every supported pair.
281# COMPILER_FLAGS: additional flags to pass to the compiler. Bytecode
282# translation and backend flags are passed automatically.
283# RUNNER_ARGS: additional args to pass to the trace-runner program. The driver
284# and input file flags are passed automatically.
285# LABELS: Additional labels to apply to the test. The package path and
286# "driver=${DRIVER}" are added automatically.
287# OPT_TOOL: Defaulting to iree-opt. Tool used to preprocess the source files
288# if OPT_FLAGS is specified.
289# OPT_FLAGS: If specified, source files are preprocessed with OPT_TOOL with
290# these flags.
291# TRACE_RUNNER: trace-runner program to run.
292function(iree_generated_trace_runner_test)
293 if(NOT IREE_BUILD_TESTS)
294 return()
295 endif()
296
297 cmake_parse_arguments(
298 _RULE
299 ""
300 "NAME;GENERATOR;OPT_TOOL;TRACE_RUNNER"
301 "TARGET_BACKENDS;DRIVERS;GENERATOR_ARGS;COMPILER_FLAGS;RUNNER_ARGS;LABELS;OPT_FLAGS"
302 ${ARGN}
303 )
304
305 if(NOT DEFINED _RULE_TARGET_BACKENDS AND NOT DEFINED _RULE_DRIVERS)
306 set(_RULE_TARGET_BACKENDS "vmvx" "vulkan-spirv" "dylib-llvm-aot")
307 set(_RULE_DRIVERS "vmvx" "vulkan" "dylib")
308 endif()
309
310 list(LENGTH _RULE_TARGET_BACKENDS _TARGET_BACKEND_COUNT)
311 list(LENGTH _RULE_DRIVERS _DRIVER_COUNT)
312
313 if(NOT _TARGET_BACKEND_COUNT EQUAL _DRIVER_COUNT)
314 message(SEND_ERROR
315 "TARGET_BACKENDS count ${_TARGET_BACKEND_COUNT} does not match DRIVERS count ${_DRIVER_COUNT}")
316 endif()
317
318 math(EXPR _MAX_INDEX "${_TARGET_BACKEND_COUNT} - 1")
319 foreach(_INDEX RANGE "${_MAX_INDEX}")
320 list(GET _RULE_TARGET_BACKENDS ${_INDEX} _TARGET_BACKEND)
321 list(GET _RULE_DRIVERS ${_INDEX} _DRIVER)
322 set(_SINGLE_BACKEND_TEST_NAME "${_RULE_NAME}_${_TARGET_BACKEND}_${_DRIVER}")
323 iree_single_backend_generated_trace_runner_test(
324 NAME
325 ${_SINGLE_BACKEND_TEST_NAME}
326 GENERATOR
327 ${_RULE_GENERATOR}
328 GENERATOR_ARGS
329 ${_RULE_GENERATOR_ARGS}
330 TRACE_RUNNER
331 ${_RULE_TRACE_RUNNER}
332 TARGET_BACKEND
333 ${_TARGET_BACKEND}
334 DRIVER
335 ${_DRIVER}
336 COMPILER_FLAGS
337 ${_RULE_COMPILER_FLAGS}
338 RUNNER_ARGS
339 ${_RULE_RUNNER_ARGS}
340 LABELS
341 ${_RULE_LABELS}
342 OPT_TOOL
343 ${_RULE_OPT_TOOL}
344 OPT_FLAGS
345 ${_RULE_OPT_FLAGS}
346 )
347 endforeach()
348endfunction()