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   TestReducer() = default;
30   TestReducer(const TestReducer &pass){};
31   void runOnFunction() override;
32 };
33 
34 } // end anonymous namespace
35 
36 void TestReducer::runOnFunction() {
37   for (auto &op : getOperation())
38     op.walk([&](Operation *op) {
39       StringRef opName = op->getName().getStringRef();
40 
41       if (opName == "test.crashOp") {
42         llvm::errs() << "MLIR Reducer Test generated failure: Found "
43                         "\"crashOp\" operation\n";
44         exit(1);
45       }
46     });
47 }
48 
49 namespace mlir {
50 void registerTestReducer() {
51   PassRegistration<TestReducer>(
52       PASS_NAME, "Tests MLIR Reduce tool by generating failures");
53 }
54 } // namespace mlir
55