aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/tests/filecheck.cc
blob: dcb469087e0064d17ce3b04fdeaf0b6136069a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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.
==============================================================================*/

#include "tensorflow/compiler/xla/tests/filecheck.h"

#include <cstdlib>

#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/compiler/xla/util.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/subprocess.h"

namespace xla {

StatusOr<bool> RunFileCheck(const string& input, const string& pattern) {
  using tensorflow::io::JoinPath;

  // Generate an input file for the FileCheck pattern.
  string pattern_path;
  auto env = tensorflow::Env::Default();
  if (!env->LocalTempFilename(&pattern_path)) {
    return tensorflow::errors::Internal("couldn't get a pattern file name");
  }
  TF_RETURN_IF_ERROR(tensorflow::WriteStringToFile(env, pattern_path, pattern));

  // Invoke FileCheck to check whether input matches `pattern`.
  const char* file_check_path_suffix = "org_tensorflow/external/llvm/FileCheck";
  string file_check_path;
  if (const char* test_srcdir = getenv("TEST_SRCDIR")) {
    file_check_path = JoinPath(test_srcdir, file_check_path_suffix);
  } else {
    file_check_path = file_check_path_suffix;
  }

  tensorflow::SubProcess file_check_process;
  file_check_process.SetProgram(file_check_path,
                                {file_check_path, pattern_path});
  file_check_process.SetChannelAction(tensorflow::CHAN_STDIN,
                                      tensorflow::ACTION_PIPE);
  file_check_process.SetChannelAction(tensorflow::CHAN_STDERR,
                                      tensorflow::ACTION_PIPE);
  if (!file_check_process.Start()) {
    return tensorflow::errors::Internal("couldn't start FileCheck");
  }

  string standard_error;
  int exit_status = file_check_process.Communicate(
      /*stdin_input=*/&input, /*stdout_output=*/nullptr,
      /*stderr_output=*/&standard_error);

  // FileCheck returns 0 when the inputs match. If matching failed, log
  // the error message generated by FileCheck and the inputs.
  bool succeeded = (exit_status == 0);
  if (!succeeded) {
    LOG(WARNING) << "Tried to execute FileCheck at " << file_check_path;
    if (!env->FileExists(file_check_path).ok()) {
      LOG(WARNING) << "NOTE: FileCheck binary does not exist!";
    }

    LOG(WARNING) << "FileCheck error: " << standard_error;
    LOG(WARNING) << "FileCheck input was:";
    XLA_LOG_LINES(tensorflow::WARNING, input);
    LOG(WARNING) << "FileCheck pattern was:";
    XLA_LOG_LINES(tensorflow::WARNING, pattern);
  } else if (!standard_error.empty()) {
    LOG(INFO) << "FileCheck stderr:";
    XLA_LOG_LINES(tensorflow::INFO, standard_error);
    LOG(INFO) << "FileCheck input was:";
    XLA_LOG_LINES(tensorflow::INFO, input);
  }
  return succeeded;
}

}  // namespace xla