1 //===- TestReducer.cpp - Test MLIR Reduce ---------------------------------===//
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 a pass that reproduces errors based on trivially defined
10 // patterns. It is used as a buggy optimization pass for the purpose of testing
11 // the MLIR Reduce tool.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "mlir/Pass/Pass.h"
16 
17 #define PASS_NAME "test-mlir-reducer"
18 
19 using namespace mlir;
20 
21 static llvm::cl::OptionCategory clOptionsCategory(PASS_NAME " options");
22 
23 namespace {
24 
25 /// This pass looks for for the presence of an operation with the name
26 /// "crashOp" in the input MLIR file and crashes the mlir-opt tool if the
27 /// operation is found.
28 struct TestReducer : public PassWrapper<TestReducer, FunctionPass> {
29   StringRef getArgument() const final { return PASS_NAME; }
30   StringRef getDescription() const final {
31     return "Tests MLIR Reduce tool by generating failures";
32   }
33   TestReducer() = default;
34   TestReducer(const TestReducer &pass){};
35   void runOnFunction() override;
36 };
37 
38 } // end anonymous namespace
39 
40 void TestReducer::runOnFunction() {
41   for (auto &op : getOperation())
42     op.walk([&](Operation *op) {
43       StringRef opName = op->getName().getStringRef();
44 
45       if (opName.contains("op_crash")) {
46         llvm::errs() << "MLIR Reducer Test generated failure: Found "
47                         "\"crashOp\" operation\n";
48         exit(1);
49       }
50     });
51 }
52 
53 namespace mlir {
54 void registerTestReducer() { PassRegistration<TestReducer>(); }
55 } // namespace mlir
56