xref: /llvm-project/mlir/test/lib/Reducer/MLIRTestReducer.cpp (revision 7557530f428a2f226d8d925c33d527dfcfdcb0c5)
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 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<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anoned9bf4820111::TestReducer25   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestReducer)
26 
27   StringRef getArgument() const final { return "test-mlir-reducer"; }
getDescription__anoned9bf4820111::TestReducer28   StringRef getDescription() const final {
29     return "Tests MLIR Reduce tool by generating failures";
30   }
31   void runOnOperation() override;
32 };
33 
34 } // namespace
35 
runOnOperation()36 void TestReducer::runOnOperation() {
37   getOperation()->walk([&](Operation *op) {
38     StringRef opName = op->getName().getStringRef();
39 
40     if (opName.contains("op_crash")) {
41       llvm::errs() << "MLIR Reducer Test generated failure: Found "
42                       "\"crashOp\" operation\n";
43       exit(1);
44     }
45   });
46 }
47 
48 namespace mlir {
registerTestReducer()49 void registerTestReducer() { PassRegistration<TestReducer>(); }
50 } // namespace mlir
51