1ec04ce46SMauricio Sifontes //===- Tester.cpp ---------------------------------------------------------===//
2ec04ce46SMauricio Sifontes //
3ec04ce46SMauricio Sifontes // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ec04ce46SMauricio Sifontes // See https://llvm.org/LICENSE.txt for license information.
5ec04ce46SMauricio Sifontes // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ec04ce46SMauricio Sifontes //
7ec04ce46SMauricio Sifontes //===----------------------------------------------------------------------===//
8ec04ce46SMauricio Sifontes //
9ec04ce46SMauricio Sifontes // This file defines the Tester class used in the MLIR Reduce tool.
10ec04ce46SMauricio Sifontes //
11ec04ce46SMauricio Sifontes // A Tester object is passed as an argument to the reduction passes and it is
1241b09f4eSKazuaki Ishizaki // used to run the interestingness testing script on the different generated
1327d0e14dSMauricio Sifontes // reduced variants of the test case.
14ec04ce46SMauricio Sifontes //
15ec04ce46SMauricio Sifontes //===----------------------------------------------------------------------===//
16ec04ce46SMauricio Sifontes
17ec04ce46SMauricio Sifontes #include "mlir/Reducer/Tester.h"
18c484c7ddSChia-hung Duan #include "mlir/IR/Verifier.h"
196b0cef3eSChia-hung Duan #include "llvm/Support/ToolOutputFile.h"
206b0cef3eSChia-hung Duan
21ec04ce46SMauricio Sifontes using namespace mlir;
22ec04ce46SMauricio Sifontes
Tester(StringRef scriptName,ArrayRef<std::string> scriptArgs)23ec04ce46SMauricio Sifontes Tester::Tester(StringRef scriptName, ArrayRef<std::string> scriptArgs)
24ec04ce46SMauricio Sifontes : testScript(scriptName), testScriptArgs(scriptArgs) {}
25ec04ce46SMauricio Sifontes
266b0cef3eSChia-hung Duan std::pair<Tester::Interestingness, size_t>
isInteresting(ModuleOp module) const276b0cef3eSChia-hung Duan Tester::isInteresting(ModuleOp module) const {
28c484c7ddSChia-hung Duan // The reduced module should always be vaild, or we may end up retaining the
29c484c7ddSChia-hung Duan // error message by an invalid case. Besides, an invalid module may not be
30c484c7ddSChia-hung Duan // able to print properly.
31c484c7ddSChia-hung Duan if (failed(verify(module)))
32c484c7ddSChia-hung Duan return std::make_pair(Interestingness::False, /*size=*/0);
33c484c7ddSChia-hung Duan
346b0cef3eSChia-hung Duan SmallString<128> filepath;
356b0cef3eSChia-hung Duan int fd;
366b0cef3eSChia-hung Duan
376b0cef3eSChia-hung Duan // Print module to temporary file.
386b0cef3eSChia-hung Duan std::error_code ec =
396b0cef3eSChia-hung Duan llvm::sys::fs::createTemporaryFile("mlir-reduce", "mlir", fd, filepath);
406b0cef3eSChia-hung Duan
416b0cef3eSChia-hung Duan if (ec)
42e244a6feSSimon Pilgrim llvm::report_fatal_error(llvm::Twine("Error making unique filename: ") +
43e244a6feSSimon Pilgrim ec.message());
446b0cef3eSChia-hung Duan
456b0cef3eSChia-hung Duan llvm::ToolOutputFile out(filepath, fd);
466b0cef3eSChia-hung Duan module.print(out.os());
476b0cef3eSChia-hung Duan out.os().close();
486b0cef3eSChia-hung Duan
496b0cef3eSChia-hung Duan if (out.os().has_error())
50e244a6feSSimon Pilgrim llvm::report_fatal_error(llvm::Twine("Error emitting the IR to file '") +
51e244a6feSSimon Pilgrim filepath);
526b0cef3eSChia-hung Duan
536b0cef3eSChia-hung Duan size_t size = out.os().tell();
546b0cef3eSChia-hung Duan return std::make_pair(isInteresting(filepath), size);
556b0cef3eSChia-hung Duan }
566b0cef3eSChia-hung Duan
57ec04ce46SMauricio Sifontes /// Runs the interestingness testing script on a MLIR test case file. Returns
58ec04ce46SMauricio Sifontes /// true if the interesting behavior is present in the test case or false
59ec04ce46SMauricio Sifontes /// otherwise.
isInteresting(StringRef testCase) const606b0cef3eSChia-hung Duan Tester::Interestingness Tester::isInteresting(StringRef testCase) const {
61ec04ce46SMauricio Sifontes std::vector<StringRef> testerArgs;
62ec04ce46SMauricio Sifontes testerArgs.push_back(testCase);
63ec04ce46SMauricio Sifontes
64ec04ce46SMauricio Sifontes for (const std::string &arg : testScriptArgs)
65e5639b3fSMehdi Amini testerArgs.emplace_back(arg);
66ec04ce46SMauricio Sifontes
6727d0e14dSMauricio Sifontes testerArgs.push_back(testCase);
6827d0e14dSMauricio Sifontes
69ec04ce46SMauricio Sifontes std::string errMsg;
70ec04ce46SMauricio Sifontes int result = llvm::sys::ExecuteAndWait(
71*1a36588eSKazu Hirata testScript, testerArgs, /*Env=*/std::nullopt, /*Redirects=*/std::nullopt,
72ec04ce46SMauricio Sifontes /*SecondsToWait=*/0, /*MemoryLimit=*/0, &errMsg);
73ec04ce46SMauricio Sifontes
74ec04ce46SMauricio Sifontes if (result < 0)
75e244a6feSSimon Pilgrim llvm::report_fatal_error(
76e244a6feSSimon Pilgrim llvm::Twine("Error running interestingness test: ") + errMsg, false);
77ec04ce46SMauricio Sifontes
78ec04ce46SMauricio Sifontes if (!result)
796b0cef3eSChia-hung Duan return Interestingness::False;
80ec04ce46SMauricio Sifontes
816b0cef3eSChia-hung Duan return Interestingness::True;
82ec04ce46SMauricio Sifontes }
83