xref: /llvm-project/mlir/lib/Reducer/Tester.cpp (revision 41b09f4efff1a9cd82af2d7d7eeb9916a88332e5)
1 //===- Tester.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the Tester class used in the MLIR Reduce tool.
10 //
11 // A Tester object is passed as an argument to the reduction passes and it is
12 // used to run the interestingness testing script on the different generated
13 // reduced variants of the test case.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "mlir/Reducer/Tester.h"
18 
19 using namespace mlir;
20 
21 Tester::Tester(StringRef scriptName, ArrayRef<std::string> scriptArgs)
22     : testScript(scriptName), testScriptArgs(scriptArgs) {}
23 
24 /// Runs the interestingness testing script on a MLIR test case file. Returns
25 /// true if the interesting behavior is present in the test case or false
26 /// otherwise.
27 bool Tester::isInteresting(StringRef testCase) const {
28 
29   std::vector<StringRef> testerArgs;
30   testerArgs.push_back(testCase);
31 
32   for (const std::string &arg : testScriptArgs)
33     testerArgs.push_back(arg);
34 
35   testerArgs.push_back(testCase);
36 
37   std::string errMsg;
38   int result = llvm::sys::ExecuteAndWait(
39       testScript, testerArgs, /*Env=*/None, /*Redirects=*/None,
40       /*SecondsToWait=*/0, /*MemoryLimit=*/0, &errMsg);
41 
42   if (result < 0)
43     llvm::report_fatal_error("Error running interestingness test: " + errMsg,
44                              false);
45 
46   if (!result)
47     return false;
48 
49   return true;
50 }
51