3edef61f1d728872881938bc338a3eeab3cdbac1
[lldb.git] / mlir / tools / mlir-reduce / mlir-reduce.cpp
1 //===- mlir-reduce.cpp - The MLIR reducer ---------------------------------===//
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 implements the general framework of the MLIR reducer tool. It
10 // parses the command line arguments, parses the initial MLIR test case and sets
11 // up the testing environment. It  outputs the most reduced test case variant
12 // after executing the reduction passes.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include <vector>
17
18 #include "mlir/InitAllDialects.h"
19 #include "mlir/Parser.h"
20 #include "mlir/Pass/Pass.h"
21 #include "mlir/Pass/PassManager.h"
22 #include "mlir/Reducer/Tester.h"
23 #include "mlir/Support/FileUtilities.h"
24 #include "mlir/Support/LogicalResult.h"
25 #include "mlir/Transforms/Passes.h"
26 #include "llvm/Support/InitLLVM.h"
27 #include "llvm/Support/ToolOutputFile.h"
28
29 using namespace mlir;
30
31 static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional,
32                                                 llvm::cl::Required,
33                                                 llvm::cl::desc("<input file>"));
34
35 static llvm::cl::opt<std::string>
36     testFilename("test", llvm::cl::Required, llvm::cl::desc("Testing script"));
37
38 static llvm::cl::list<std::string>
39     testArguments("test-args", llvm::cl::ZeroOrMore,
40                   llvm::cl::desc("Testing script arguments"));
41
42 static llvm::cl::opt<std::string>
43     outputFilename("o",
44                    llvm::cl::desc("Output filename for the reduced test case"),
45                    llvm::cl::init("-"));
46
47 // Parse and verify the input MLIR file.
48 static LogicalResult loadModule(MLIRContext &context, OwningModuleRef &module,
49                                 StringRef inputFilename) {
50   module = parseSourceFile(inputFilename, &context);
51   if (!module)
52     return failure();
53
54   return success();
55 }
56
57 int main(int argc, char **argv) {
58
59   llvm::InitLLVM y(argc, argv);
60
61   registerAllDialects();
62   registerMLIRContextCLOptions();
63   registerPassManagerCLOptions();
64
65   llvm::cl::ParseCommandLineOptions(argc, argv,
66                                     "MLIR test case reduction tool.\n");
67
68   std::string errorMessage;
69
70   auto testscript = openInputFile(testFilename, &errorMessage);
71   if (!testscript)
72     llvm::report_fatal_error(errorMessage);
73
74   auto output = openOutputFile(outputFilename, &errorMessage);
75   if (!output)
76     llvm::report_fatal_error(errorMessage);
77
78   mlir::MLIRContext context;
79   mlir::OwningModuleRef moduleRef;
80   context.allowUnregisteredDialects(true);
81
82   if (failed(loadModule(context, moduleRef, inputFilename)))
83     llvm::report_fatal_error("Input test case can't be parsed");
84
85   // Initialize test environment.
86   Tester test(testFilename, testArguments);
87   test.setMostReduced(moduleRef.get());
88
89   if (!test.isInteresting(inputFilename))
90     llvm::report_fatal_error(
91         "Input test case does not exhibit interesting behavior");
92
93   test.getMostReduced().print(output->os());
94   output->keep();
95
96   return 0;
97 }