• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Test definitions for Lit, the LLVM test runner.
2#
3# This is reusing the LLVM Lit test runner in the interim until the new build
4# rules are upstreamed.
5# TODO(b/136126535): remove this custom rule.
6"""Lit runner globbing test
7"""
8
9load("@bazel_skylib//lib:paths.bzl", "paths")
10
11# Default values used by the test runner.
12_default_test_file_exts = ["mlir", ".pbtxt", ".td"]
13_default_driver = "@llvm-project//mlir:run_lit.sh"
14_default_size = "small"
15_default_tags = []
16
17# These are patterns which we should never match, for tests, subdirectories, or
18# test input data files.
19_ALWAYS_EXCLUDE = [
20    "**/LICENSE.txt",
21    "**/README.txt",
22    "**/lit.local.cfg",
23    # Exclude input files that have spaces in their names, since bazel
24    # cannot cope with such "targets" in the srcs list.
25    "**/* *",
26    "**/* */**",
27]
28
29def _run_lit_test(name, data, size, tags, driver, features, exec_properties):
30    """Runs lit on all tests it can find in `data` under tensorflow/compiler/mlir.
31
32    Note that, due to Bazel's hermetic builds, lit only sees the tests that
33    are included in the `data` parameter, regardless of what other tests might
34    exist in the directory searched.
35
36    Args:
37      name: str, the name of the test, including extension.
38      data: [str], the data input to the test.
39      size: str, the size of the test.
40      tags: [str], tags to attach to the test.
41      driver: str, label of the driver shell script.
42              Note: use of a custom driver is not currently supported
43              and specifying a default driver will abort the tests.
44      features: [str], list of extra features to enable.
45    """
46
47    # Remove the default_driver from the data: it does not exist as a file and is
48    # just a placeholder from the copybara rewrite.
49    data = [d for d in data if d != _default_driver]
50
51    # Disable tests on windows for now, to enable testing rest of all xla and mlir.
52    native.py_test(
53        name = name,
54        srcs = ["@llvm-project//llvm:lit"],
55        tags = tags + ["no_pip", "no_windows"],
56        args = [
57            "tensorflow/compiler/mlir/" + paths.basename(data[-1]) + " --config-prefix=runlit -v",
58        ] + features,
59        data = data + [
60            "//tensorflow/compiler/mlir:litfiles",
61            "@llvm-project//llvm:FileCheck",
62            "@llvm-project//llvm:count",
63            "@llvm-project//llvm:not",
64        ],
65        size = size,
66        main = "lit.py",
67        exec_properties = exec_properties,
68    )
69
70def glob_lit_tests(
71        exclude = [],
72        test_file_exts = _default_test_file_exts,
73        default_size = _default_size,
74        size_override = {},
75        data = [],
76        per_test_extra_data = {},
77        default_tags = _default_tags,
78        tags_override = {},
79        driver = _default_driver,
80        features = [],
81        exec_properties = {}):
82    """Creates all plausible Lit tests (and their inputs) under this directory.
83
84    Args:
85      exclude: [str], paths to exclude (for tests and inputs).
86      test_file_exts: [str], extensions for files that are tests.
87      default_size: str, the test size for targets not in "size_override".
88      size_override: {str: str}, sizes to use for specific tests.
89      data: [str], additional input data to the test.
90      per_test_extra_data: {str: [str]}, extra data to attach to a given file.
91      default_tags: [str], additional tags to attach to the test.
92      tags_override: {str: str}, tags to add to specific tests.
93      driver: str, label of the driver shell script.
94              Note: use of a custom driver is not currently supported
95              and specifying a default driver will abort the tests.
96      features: [str], list of extra features to enable.
97      exec_properties: a dictionary of properties to pass on.
98    """
99
100    # Ignore some patterns by default for tests and input data.
101    exclude = _ALWAYS_EXCLUDE + exclude
102
103    tests = native.glob(
104        ["*." + ext for ext in test_file_exts],
105        exclude = exclude,
106    )
107
108    # Run tests individually such that errors can be attributed to a specific
109    # failure.
110    for i in range(len(tests)):
111        curr_test = tests[i]
112
113        # Instantiate this test with updated parameters.
114        lit_test(
115            name = curr_test,
116            data = data + per_test_extra_data.pop(curr_test, []),
117            size = size_override.pop(curr_test, default_size),
118            tags = default_tags + tags_override.pop(curr_test, []),
119            driver = driver,
120            features = features,
121            exec_properties = exec_properties,
122        )
123
124def lit_test(
125        name,
126        data = [],
127        size = _default_size,
128        tags = _default_tags,
129        driver = _default_driver,
130        features = [],
131        exec_properties = {}):
132    """Runs test files under lit.
133
134    Args:
135      name: str, the name of the test.
136      data: [str], labels that should be provided as data inputs.
137      size: str, the size of the test.
138      tags: [str], tags to attach to the test.
139      driver: str, label of the driver shell script.
140              Note: use of a custom driver is not currently supported
141              and specifying a default driver will abort the tests.
142      features: [str], list of extra features to enable.
143    """
144    _run_lit_test(name + ".test", data + [name], size, tags, driver, features, exec_properties)
145