33load ("@bazel_skylib//lib:paths.bzl" , "paths" )
44load ("@rules_cc//cc/common:cc_info.bzl" , "CcInfo" )
55load ("@xla//third_party/rules_python/python:defs.bzl" , "py_binary" )
6+ load (
7+ "//xla:native_test.bzl" ,
8+ "native_test" ,
9+ )
10+ load (
11+ "//xla:platform_native_rule.default.bzl" ,
12+ "platform_native_rule" ,
13+ )
614load (
715 "//xla/tests:build_defs.bzl" ,
816 "prepare_gpu_backend_data" ,
@@ -65,6 +73,7 @@ def lit_test_suite(
6573 exec_properties = {},
6674 tags = [],
6775 gpu_suffix = "" ,
76+ native_test_rule = native_test ,
6877 ** kwargs ):
6978 """Creates one lit test per source file and a test suite that bundles them.
7079
@@ -98,6 +107,8 @@ def lit_test_suite(
98107 requirement to run on a GPU.
99108 gpu_suffix: string. A suffix derived from the gpu name that can be added
100109 to make (file) names unique.
110+ native_test_rule: callable. A rule or macro to create the test target,
111+ instead of `native_test`.
101112 **kwargs: additional keyword arguments to pass to all generated rules.
102113
103114 See https://llvm.org/docs/CommandGuide/lit.html for details on lit
@@ -132,6 +143,7 @@ def lit_test_suite(
132143 hermetic_cuda_data_dir = hermetic_cuda_data_dir ,
133144 exec_properties = exec_properties ,
134145 gpu_suffix = gpu_suffix ,
146+ native_test_rule = native_test_rule ,
135147 ** kwargs
136148 )
137149
@@ -314,8 +326,10 @@ def lit_device_test(
314326 "XLA_TEST_DEVICE" : device ,
315327 "XLA_TEST_MODIFIERS" : "," .join (modifiers ),
316328 })
329+ this_backend_tags = ["xla_%s" % backend , "xla_device_%s" % backend ] + tags + backend_tags .get (backend , [])
330+ test_name = "%s_%s" % (name , backend )
317331 lit_test_suite (
318- name = "%s_%s" % ( name , backend ) ,
332+ name = test_name ,
319333 srcs = srcs ,
320334 cfg = cfg ,
321335 tools = tools ,
@@ -328,8 +342,9 @@ def lit_device_test(
328342 tags_override = tags_override ,
329343 hermetic_cuda_data_dir = hermetic_cuda_data_dir ,
330344 exec_properties = exec_properties ,
331- tags = [ "xla_%s" % backend ] + tags + backend_tags . get ( backend , []) ,
345+ tags = this_backend_tags ,
332346 gpu_suffix = "_%s" % (backend ),
347+ native_test_rule = platform_native_rule (name = test_name , backend = backend ),
333348 ** kwargs
334349 )
335350
@@ -363,6 +378,7 @@ def lit_test(
363378 hermetic_cuda_data_dir = None ,
364379 exec_properties = {},
365380 gpu_suffix = "" ,
381+ native_test_rule = native_test ,
366382 ** kwargs ):
367383 """Runs a single test file with LLVM's lit tool.
368384
@@ -393,6 +409,8 @@ def lit_test(
393409 requirement to run on a GPU.
394410 gpu_suffix: string. A suffix derived from the gpu name that can be added
395411 to make (file) names unique.
412+ native_test_rule: callable. A rule or macro to create the test target,
413+ instead of `native_test`.
396414 **kwargs: additional keyword arguments to pass to all generated rules.
397415
398416 See https://llvm.org/docs/CommandGuide/lit.html for details on lit
@@ -470,7 +488,7 @@ def lit_test(
470488 )
471489 test_file = output_file
472490
473- native_test (
491+ native_test_rule (
474492 name = name ,
475493 src = lit_name ,
476494 args = [
@@ -498,37 +516,6 @@ def lit_test(
498516 ** kwargs
499517 )
500518
501- def _shared_impl (ctx ):
502- out = ctx .attr .out
503- if not out :
504- out = ctx .attr .name
505- output = ctx .actions .declare_file (out )
506- ctx .actions .symlink (
507- target_file = ctx .executable .src ,
508- output = output ,
509- is_executable = True ,
510- )
511-
512- runfiles = ctx .runfiles (files = ctx .files .data )
513-
514- # For Bazel 4.x support. Drop when Bazel 4.x is no longer supported
515- to_merge = ([d [DefaultInfo ].default_runfiles for d in ctx .attr .data ] +
516- [ctx .attr .src [DefaultInfo ].default_runfiles ])
517- if hasattr (runfiles , "merge_all" ):
518- runfiles = runfiles .merge_all (to_merge )
519- else :
520- for m in to_merge :
521- runfiles = runfiles .merge (m )
522- return DefaultInfo (
523- executable = output ,
524- files = depset ([output ]),
525- runfiles = runfiles ,
526- )
527-
528- def _native_test_impl (ctx ):
529- default_info = _shared_impl (ctx )
530- return [default_info , testing .TestEnvironment (ctx .attr .env )]
531-
532519def _tools_on_path_impl (ctx ):
533520 runfiles = ctx .runfiles ()
534521
@@ -592,25 +579,3 @@ _tools_on_path = rule(
592579# We have to manually set "env" on the test rule because the builtin one is only
593580# available in native rules. See
594581# https://docs.bazel.build/versions/main/be/common-definitions.html#test.env
595- _TEST_ATTRS = {
596- "src" : attr .label (
597- executable = True ,
598- allow_files = True ,
599- mandatory = True ,
600- cfg = "target" ,
601- ),
602- "data" : attr .label_list (allow_files = True ),
603- # "out" is attr.string instead of attr.output, so that it is select()'able.
604- "out" : attr .string (),
605- "env" : attr .string_dict (
606- doc = "Mirrors the common env attribute that otherwise is" +
607- " only available on native rules. See" +
608- " https://docs.bazel.build/versions/main/be/common-definitions.html#test.env" ,
609- ),
610- }
611-
612- native_test = rule (
613- implementation = _native_test_impl ,
614- attrs = _TEST_ATTRS ,
615- test = True ,
616- )
0 commit comments