Skip to content

Commit

Permalink
Make XLA C++ tests fail if no test case is linked in. This prevents t…
Browse files Browse the repository at this point in the history
…he mistake of linking the test wrong.

Also fix tests that have no test case linked in.

PiperOrigin-RevId: 732236168
  • Loading branch information
Google-ML-Automation committed Feb 28, 2025
1 parent 644bb8b commit b539661
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 18 deletions.
13 changes: 8 additions & 5 deletions third_party/tsl/tsl/platform/setround_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ limitations under the License.

#include "xla/tsl/platform/test.h"

namespace tsl {
namespace {

// LLVM does not support <cfenv>. 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));
Expand Down Expand Up @@ -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__)
21 changes: 21 additions & 0 deletions xla/backends/cpu/benchmarks/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
10 changes: 7 additions & 3 deletions xla/experiments/sm_bandwidth_benchmark/sm_bw_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <memory>

Expand All @@ -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;
Expand Down Expand Up @@ -308,8 +309,11 @@ TEST(SMBandwidthTest, UseMaxNumberOfRegistersPerSm) {
ForLoop<10>::iterate<UseMaxNumberOfRegistersPerSmImpl>();
}

#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
11 changes: 7 additions & 4 deletions xla/service/cpu/tests/onednn_convolution_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#if defined(INTEL_MKL)

#include <utility>

#include "absl/strings/str_replace.h"
Expand All @@ -33,6 +31,8 @@ limitations under the License.
namespace xla {
namespace cpu {

#if defined(INTEL_MKL)

class ConvolutionTest : public HloTestBase,
public ::testing::WithParamInterface<PrimitiveType> {
protected:
Expand Down Expand Up @@ -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
10 changes: 7 additions & 3 deletions xla/service/cpu/tests/onednn_layer_norm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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"

namespace xla {
namespace {

#if defined(INTEL_MKL)

class LayerNormTest : public HloTestBase {
protected:
DebugOptions GetDebugOptionsForTest() const override {
Expand Down Expand Up @@ -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
10 changes: 7 additions & 3 deletions xla/service/cpu/tests/onednn_softmax_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <utility>

Expand All @@ -34,6 +33,8 @@ limitations under the License.
namespace xla {
namespace cpu {

#if defined(INTEL_MKL)

std::string TestParamsToString(
const ::testing::TestParamInfo<std::tuple<PrimitiveType, int>>& data) {
PrimitiveType data_type;
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions xla/stream_executor/sycl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions xla/tests/fuzz/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions xla/tsl/mkl/graph.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
6 changes: 6 additions & 0 deletions xla/tsl/platform/default/build_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = []

Expand Down

0 comments on commit b539661

Please sign in to comment.