From b539661f4e1a9815e1ba741ee683559bad2bcb81 Mon Sep 17 00:00:00 2001 From: xla authors Date: Fri, 28 Feb 2025 13:10:17 -0800 Subject: [PATCH] Make XLA C++ tests fail if no test case is linked in. This prevents the mistake of linking the test wrong. Also fix tests that have no test case linked in. PiperOrigin-RevId: 732236168 --- third_party/tsl/tsl/platform/setround_test.cc | 13 +++++++----- xla/backends/cpu/benchmarks/BUILD | 21 +++++++++++++++++++ .../sm_bandwidth_benchmark/sm_bw_test.cc | 10 ++++++--- .../cpu/tests/onednn_convolution_test.cc | 11 ++++++---- .../cpu/tests/onednn_layer_norm_test.cc | 10 ++++++--- xla/service/cpu/tests/onednn_softmax_test.cc | 10 ++++++--- xla/stream_executor/sycl/BUILD | 1 + xla/tests/fuzz/BUILD | 1 + xla/tsl/mkl/graph.bzl | 2 ++ xla/tsl/platform/default/build_config.bzl | 6 ++++++ 10 files changed, 67 insertions(+), 18 deletions(-) diff --git a/third_party/tsl/tsl/platform/setround_test.cc b/third_party/tsl/tsl/platform/setround_test.cc index 6bbe24e850086..1c53656b926e3 100644 --- a/third_party/tsl/tsl/platform/setround_test.cc +++ b/third_party/tsl/tsl/platform/setround_test.cc @@ -19,13 +19,13 @@ limitations under the License. #include "xla/tsl/platform/test.h" +namespace tsl { +namespace { + // LLVM does not support . Disable these tests when building with it. // See b/35384639 for more information. #if !defined(__clang__) || !defined(__OPTIMIZE__) -namespace tsl { -namespace { - void CheckDownward() { EXPECT_EQ(12, std::nearbyint(12.0)); EXPECT_EQ(12, std::nearbyint(12.1)); @@ -97,7 +97,10 @@ TEST(SetScopedSetRound, Scoped) { CheckToNearest(); } +#endif // !defined(__clang__) || !defined(__OPTIMIZE__) + +// Ensure at least one test case is linked to avoid test failures. +TEST(Dummy, Test) {} + } // namespace } // namespace tsl - -#endif // !defined(__clang__) || !defined(__OPTIMIZE__) diff --git a/xla/backends/cpu/benchmarks/BUILD b/xla/backends/cpu/benchmarks/BUILD index 143d747966860..94fef0fc78203 100644 --- a/xla/backends/cpu/benchmarks/BUILD +++ b/xla/backends/cpu/benchmarks/BUILD @@ -46,6 +46,7 @@ cc_library( xla_cc_test( name = "dag_execution_benchmark_test", srcs = ["dag_execution_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -64,6 +65,7 @@ xla_cc_test( xla_cc_test( name = "dot_benchmark_test", srcs = ["dot_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -82,6 +84,7 @@ xla_cc_test( xla_cc_test( name = "dynamic_update_slice_benchmark_test", srcs = ["dynamic_update_slice_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -100,6 +103,7 @@ xla_cc_test( xla_cc_test( name = "elementwise_benchmark_test", srcs = ["elementwise_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -118,6 +122,7 @@ xla_cc_test( xla_cc_test( name = "exp_benchmark_test", srcs = ["exp_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -136,6 +141,7 @@ xla_cc_test( xla_cc_test( name = "log_benchmark_test", srcs = ["log_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -154,6 +160,7 @@ xla_cc_test( xla_cc_test( name = "fusion_benchmark_test", srcs = ["fusion_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -173,6 +180,7 @@ xla_cc_test( xla_cc_test( name = "optimizer_benchmark_test", srcs = ["optimizer_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -191,6 +199,7 @@ xla_cc_test( xla_cc_test( name = "reduction_benchmark_test", srcs = ["reduction_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -209,6 +218,7 @@ xla_cc_test( xla_cc_test( name = "convolution_benchmark_test", srcs = ["convolution_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -225,6 +235,7 @@ xla_cc_test( xla_cc_test( name = "custom_call_benchmark_test", srcs = ["custom_call_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -246,6 +257,7 @@ xla_cc_test( xla_cc_test( name = "gather_benchmark_test", srcs = ["gather_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:array2d", @@ -265,6 +277,7 @@ xla_cc_test( xla_cc_test( name = "select_and_scatter_benchmark_test", srcs = ["select_and_scatter_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -283,6 +296,7 @@ xla_cc_test( xla_cc_test( name = "concatenate_benchmark_test", srcs = ["concatenate_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -301,6 +315,7 @@ xla_cc_test( xla_cc_test( name = "topk_benchmark_test", srcs = ["topk_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal_util", @@ -317,6 +332,7 @@ xla_cc_test( xla_cc_test( name = "pad_benchmark_test", srcs = ["pad_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -335,6 +351,7 @@ xla_cc_test( xla_cc_test( name = "tanh_benchmark_test", srcs = ["tanh_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -353,6 +370,7 @@ xla_cc_test( xla_cc_test( name = "scatter_benchmark_test", srcs = ["scatter_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:array2d", @@ -375,6 +393,7 @@ xla_cc_test( xla_cc_test( name = "transposed_copy_benchmark_test", srcs = ["transposed_copy_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -393,6 +412,7 @@ xla_cc_test( xla_cc_test( name = "transposed_dot_benchmark_test", srcs = ["transposed_dot_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", @@ -411,6 +431,7 @@ xla_cc_test( xla_cc_test( name = "xnn_fusion_benchmark_test", srcs = ["xnn_fusion_benchmark_test.cc"], + fail_if_no_test_linked = False, # NOLINT=This contains benchmarks only, no tests. deps = [ ":hlo_benchmark_runner", "//xla:literal", diff --git a/xla/experiments/sm_bandwidth_benchmark/sm_bw_test.cc b/xla/experiments/sm_bandwidth_benchmark/sm_bw_test.cc index 0246e86c7a22c..94b7f5477b1c0 100644 --- a/xla/experiments/sm_bandwidth_benchmark/sm_bw_test.cc +++ b/xla/experiments/sm_bandwidth_benchmark/sm_bw_test.cc @@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#if GOOGLE_CUDA #include @@ -24,6 +23,8 @@ namespace experiments { namespace benchmark { namespace { +#if GOOGLE_CUDA + constexpr int kNumSM = 108; constexpr int kNum32BitRegisters = 64 * 1024; constexpr int kMaxBlockSize = 1024; @@ -308,8 +309,11 @@ TEST(SMBandwidthTest, UseMaxNumberOfRegistersPerSm) { ForLoop<10>::iterate(); } +#endif // GOOGLE_CUDA + +// Ensure at least one test case is linked to avoid test failures. +TEST(Dummy, Test) {} + } // namespace } // namespace benchmark } // namespace experiments - -#endif // GOOGLE_CUDA diff --git a/xla/service/cpu/tests/onednn_convolution_test.cc b/xla/service/cpu/tests/onednn_convolution_test.cc index 1b1981272faf7..e512420e6bb52 100644 --- a/xla/service/cpu/tests/onednn_convolution_test.cc +++ b/xla/service/cpu/tests/onednn_convolution_test.cc @@ -13,8 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#if defined(INTEL_MKL) - #include #include "absl/strings/str_replace.h" @@ -33,6 +31,8 @@ limitations under the License. namespace xla { namespace cpu { +#if defined(INTEL_MKL) + class ConvolutionTest : public HloTestBase, public ::testing::WithParamInterface { protected: @@ -636,7 +636,10 @@ INSTANTIATE_TEST_SUITE_P( return test_name; }); +#endif // INTEL_MKL + +// Ensure at least one test case is linked to avoid test failures. +TEST(Dummy, Test) {} + } // namespace cpu } // namespace xla - -#endif // INTEL_MKL diff --git a/xla/service/cpu/tests/onednn_layer_norm_test.cc b/xla/service/cpu/tests/onednn_layer_norm_test.cc index 934fd228184b4..c066c74ef5407 100644 --- a/xla/service/cpu/tests/onednn_layer_norm_test.cc +++ b/xla/service/cpu/tests/onednn_layer_norm_test.cc @@ -13,8 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#if defined(INTEL_MKL) - #include "xla/hlo/testlib/test.h" #include "xla/service/cpu/onednn_util.h" #include "xla/tests/hlo_test_base.h" @@ -22,6 +20,8 @@ limitations under the License. namespace xla { namespace { +#if defined(INTEL_MKL) + class LayerNormTest : public HloTestBase { protected: DebugOptions GetDebugOptionsForTest() const override { @@ -335,7 +335,11 @@ TEST_F(LayerNormTest, LayerNormTest1_BF16) { MatchOptimizedHlo(layer_norm_module_str, onednn_layer_norm_); } +#endif // INTEL_MKL + +// Ensure at least one test case is linked to avoid test failures. +TEST(Dummy, Test) {} + } // namespace } // namespace xla -#endif // INTEL_MKL diff --git a/xla/service/cpu/tests/onednn_softmax_test.cc b/xla/service/cpu/tests/onednn_softmax_test.cc index a5aa800cba10a..c432a6530c569 100644 --- a/xla/service/cpu/tests/onednn_softmax_test.cc +++ b/xla/service/cpu/tests/onednn_softmax_test.cc @@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#if defined(INTEL_MKL) #include @@ -34,6 +33,8 @@ limitations under the License. namespace xla { namespace cpu { +#if defined(INTEL_MKL) + std::string TestParamsToString( const ::testing::TestParamInfo>& data) { PrimitiveType data_type; @@ -258,7 +259,10 @@ TEST_F(OneDnnSoftmaxTest, SoftmaxWithBF16ConvertOutputFP32Pattern) { TestSoftmaxPatternMatching(softmax_hlo_string, /*expected_softmax_axis=*/2); } +#endif // INTEL_MKL + +// Ensure at least one test case is linked to avoid test failures. +TEST(Dummy, Test) {} + } // namespace cpu } // namespace xla - -#endif // INTEL_MKL diff --git a/xla/stream_executor/sycl/BUILD b/xla/stream_executor/sycl/BUILD index 781a76a9e2fd7..4ccb686172602 100644 --- a/xla/stream_executor/sycl/BUILD +++ b/xla/stream_executor/sycl/BUILD @@ -52,6 +52,7 @@ cc_library( xla_cc_test( name = "sycl_platform_test", srcs = if_sycl_is_configured(["sycl_platform_test.cc"]), + fail_if_no_test_linked = False, # NOLINT=If not building with SYCL, we don't have any tests linked. deps = if_sycl_is_configured([ ":sycl_platform", "@tsl//tsl/platform:statusor", diff --git a/xla/tests/fuzz/BUILD b/xla/tests/fuzz/BUILD index 188fb34a1fcb4..28d7c74fbc1e6 100644 --- a/xla/tests/fuzz/BUILD +++ b/xla/tests/fuzz/BUILD @@ -17,6 +17,7 @@ cc_library( "//xla/tsl/platform:statusor", "//xla/tsl/platform:test", ], + alwayslink = True, # This library registers test cases at dynamic initialization time. ) [hlo_test( diff --git a/xla/tsl/mkl/graph.bzl b/xla/tsl/mkl/graph.bzl index 218cdb2e7e9d8..7ba2fee7502c8 100644 --- a/xla/tsl/mkl/graph.bzl +++ b/xla/tsl/mkl/graph.bzl @@ -25,5 +25,7 @@ def onednn_graph_cc_test( xla_cc_test( srcs = if_graph_api(srcs), deps = if_graph_api(deps) + ["@com_google_googletest//:gtest_main"], + # If not building with Graph API, we don't have any tests linked. + fail_if_no_test_linked = False, **kwargs ) diff --git a/xla/tsl/platform/default/build_config.bzl b/xla/tsl/platform/default/build_config.bzl index 7e5d31e8bee49..e5f8cbcbeab86 100644 --- a/xla/tsl/platform/default/build_config.bzl +++ b/xla/tsl/platform/default/build_config.bzl @@ -815,6 +815,7 @@ def strict_cc_test( linkstatic = True, shuffle_tests = True, args = None, + fail_if_no_test_linked = True, **kwargs): """A drop-in replacement for cc_test that enforces some good practices by default. @@ -825,8 +826,13 @@ def strict_cc_test( linkstatic: Whether to link statically. shuffle_tests: Whether to shuffle the test cases. args: The arguments to pass to the test. + fail_if_no_test_linked: Whether to fail if no tests are linked. Unimplemented in OSS as + --gtest_fail_if_no_test_linked is not available in the OSS build as of 2025-02-27. **kwargs: Other arguments to pass to the test. """ + + _ = fail_if_no_test_linked # buildifier: disable=unused-variable + if args == None: args = []