#include "mlir/Reducer/Tester.h"
#include "mlir/IR/Verifier.h"
#include "llvm/Support/ToolOutputFile.h"
using namespace mlir;
Tester::Tester(StringRef scriptName, ArrayRef<std::string> scriptArgs)
: testScript(scriptName), testScriptArgs(scriptArgs) {}
std::pair<Tester::Interestingness, size_t>
Tester::isInteresting(ModuleOp module) const {
if (failed(verify(module)))
return std::make_pair(Interestingness::False, 0);
SmallString<128> filepath;
int fd;
std::error_code ec =
llvm::sys::fs::createTemporaryFile("mlir-reduce", "mlir", fd, filepath);
if (ec)
llvm::report_fatal_error(llvm::Twine("Error making unique filename: ") +
ec.message());
llvm::ToolOutputFile out(filepath, fd);
module.print(out.os());
out.os().close();
if (out.os().has_error())
llvm::report_fatal_error(llvm::Twine("Error emitting the IR to file '") +
filepath);
size_t size = out.os().tell();
return std::make_pair(isInteresting(filepath), size);
}
Tester::Interestingness Tester::isInteresting(StringRef testCase) const {
std::vector<StringRef> testerArgs;
testerArgs.push_back(testCase);
for (const std::string &arg : testScriptArgs)
testerArgs.emplace_back(arg);
testerArgs.push_back(testCase);
std::string errMsg;
int result = llvm::sys::ExecuteAndWait(
testScript, testerArgs, std::nullopt, std::nullopt,
0, 0, &errMsg);
if (result < 0)
llvm::report_fatal_error(
llvm::Twine("Error running interestingness test: ") + errMsg, false);
if (!result)
return Interestingness::False;
return Interestingness::True;
}