This makes it easier to iterate over tests, because the test doesn't have to be recompiled all the time. PiperOrigin-RevId: 295913906 Change-Id: I975a8db086aceb862498f2d63138cb0bf4859c00
88 lines
3.4 KiB
C++
88 lines
3.4 KiB
C++
/* 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/path.h"
|
|
#include "tensorflow/core/platform/resource_loader.h"
|
|
#include "tensorflow/core/platform/subprocess.h"
|
|
|
|
namespace xla {
|
|
|
|
StatusOr<bool> RunFileCheck(const std::string& input,
|
|
absl::string_view pattern) {
|
|
// Generate an input file for the FileCheck pattern.
|
|
std::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));
|
|
|
|
return RunFileCheckWithPatternFile(input, pattern_path);
|
|
}
|
|
|
|
StatusOr<bool> RunFileCheckWithPatternFile(const std::string& input,
|
|
const std::string& pattern_file) {
|
|
// Invoke FileCheck to check whether input matches `pattern`.
|
|
std::string file_check_path = tensorflow::GetDataDependencyFilepath(
|
|
tensorflow::io::JoinPath("external", "llvm-project", "llvm", "FileCheck"));
|
|
|
|
tensorflow::SubProcess file_check_process;
|
|
file_check_process.SetProgram(
|
|
file_check_path,
|
|
{file_check_path, "-v", "-dump-input=fail", pattern_file});
|
|
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");
|
|
}
|
|
|
|
std::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);
|
|
auto env = tensorflow::Env::Default();
|
|
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:\n" << standard_error;
|
|
} 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
|