1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/compiler/xla/tests/hlo_verified_test_base.h"
17
18#include "tensorflow/compiler/xla/service/hlo_verifier.h"
19#include "tensorflow/compiler/xla/shape_util.h"
20#include "tensorflow/compiler/xla/status_macros.h"
21#include "tensorflow/core/platform/logging.h"
22#include "tensorflow/core/platform/test.h"
23
24namespace xla {
25
26HloVerifiedTestBase::HloVerifiedTestBase()
27    : shape_verifier_(MakeUnique<ShapeVerifier>()) {}
28
29HloVerifiedTestBase::~HloVerifiedTestBase() {
30  // We can't call the ASSERT or EXPECT test macros in destructors, so we
31  // perform HLO verification in TearDown, and use the CHECK here to ensure
32  // users don't accidentally override the verification.
33  CHECK(tear_down_called_)
34      << "TearDown was never called; subclasses of HloVerifiedTestBase that "
35      << "override TearDown must call the superclass TearDown.";
36}
37
38void HloVerifiedTestBase::TearDown() {
39  EXPECT_FALSE(tear_down_called_)
40      << "TearDown called more than once; it should be called exactly once.";
41  tear_down_called_ = true;
42  if (module_) {
43    HloVerifier verifier;
44    xla::StatusOr<bool> mutated = verifier.Run(module_.get());
45    if (!mutated.ok()) {
46      ADD_FAILURE() << "HloVerifier failed: " << mutated.status();
47    } else {
48      EXPECT_FALSE(mutated.ValueOrDie())
49          << "HloVerifier should never mutate the HloModule";
50    }
51  }
52  HloTestBase::TearDown();
53}
54
55HloModule& HloVerifiedTestBase::module() {
56  if (!module_) {
57    module_ = CreateNewModule();
58  }
59  return *module_;
60}
61
62}  // namespace xla
63