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 using namespace mlir;
18 
19 namespace {
20 
21 /// This pass looks for for the presence of an operation with the name
22 /// "crashOp" in the input MLIR file and crashes the mlir-opt tool if the
23 /// operation is found.
24 struct TestReducer : public PassWrapper<TestReducer, OperationPass<>> {
25   StringRef getArgument() const final { return "test-mlir-reducer"; }
26   StringRef getDescription() const final {
27     return "Tests MLIR Reduce tool by generating failures";
28   }
29   void runOnOperation() override;
30 };
31 
32 } // end anonymous namespace
33 
34 void TestReducer::runOnOperation() {
35   getOperation()->walk([&](Operation *op) {
36     StringRef opName = op->getName().getStringRef();
37 
38     if (opName.contains("op_crash")) {
39       llvm::errs() << "MLIR Reducer Test generated failure: Found "
40                       "\"crashOp\" operation\n";
41       exit(1);
42     }
43   });
44 }
45 
46 namespace mlir {
47 void registerTestReducer() { PassRegistration<TestReducer>(); }
48 } // namespace mlir
49