xref: /llvm-project/mlir/test/lib/Reducer/MLIRTestReducer.cpp (revision 7557530f428a2f226d8d925c33d527dfcfdcb0c5)
116e9ccb2SMauricio Sifontes //===- TestReducer.cpp - Test MLIR Reduce ---------------------------------===//
216e9ccb2SMauricio Sifontes //
316e9ccb2SMauricio Sifontes // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
416e9ccb2SMauricio Sifontes // See https://llvm.org/LICENSE.txt for license information.
516e9ccb2SMauricio Sifontes // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
616e9ccb2SMauricio Sifontes //
716e9ccb2SMauricio Sifontes //===----------------------------------------------------------------------===//
816e9ccb2SMauricio Sifontes //
916e9ccb2SMauricio Sifontes // This file implements a pass that reproduces errors based on trivially defined
1016e9ccb2SMauricio Sifontes // patterns. It is used as a buggy optimization pass for the purpose of testing
1116e9ccb2SMauricio Sifontes // the MLIR Reduce tool.
1216e9ccb2SMauricio Sifontes //
1316e9ccb2SMauricio Sifontes //===----------------------------------------------------------------------===//
1416e9ccb2SMauricio Sifontes 
1516e9ccb2SMauricio Sifontes #include "mlir/Pass/Pass.h"
1616e9ccb2SMauricio Sifontes 
1716e9ccb2SMauricio Sifontes using namespace mlir;
1816e9ccb2SMauricio Sifontes 
1916e9ccb2SMauricio Sifontes namespace {
2016e9ccb2SMauricio Sifontes 
21*7557530fSFangrui Song /// This pass looks for the presence of an operation with the name
2216e9ccb2SMauricio Sifontes /// "crashOp" in the input MLIR file and crashes the mlir-opt tool if the
2316e9ccb2SMauricio Sifontes /// operation is found.
2418465bcfSRiver Riddle struct TestReducer : public PassWrapper<TestReducer, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anoned9bf4820111::TestReducer255e50dd04SRiver Riddle   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestReducer)
265e50dd04SRiver Riddle 
2718465bcfSRiver Riddle   StringRef getArgument() const final { return "test-mlir-reducer"; }
getDescription__anoned9bf4820111::TestReducer28b5e22e6dSMehdi Amini   StringRef getDescription() const final {
29b5e22e6dSMehdi Amini     return "Tests MLIR Reduce tool by generating failures";
30b5e22e6dSMehdi Amini   }
3118465bcfSRiver Riddle   void runOnOperation() override;
3216e9ccb2SMauricio Sifontes };
3316e9ccb2SMauricio Sifontes 
34be0a7e9fSMehdi Amini } // namespace
3516e9ccb2SMauricio Sifontes 
runOnOperation()3618465bcfSRiver Riddle void TestReducer::runOnOperation() {
3718465bcfSRiver Riddle   getOperation()->walk([&](Operation *op) {
3816e9ccb2SMauricio Sifontes     StringRef opName = op->getName().getStringRef();
3916e9ccb2SMauricio Sifontes 
40c484c7ddSChia-hung Duan     if (opName.contains("op_crash")) {
4116e9ccb2SMauricio Sifontes       llvm::errs() << "MLIR Reducer Test generated failure: Found "
4216e9ccb2SMauricio Sifontes                       "\"crashOp\" operation\n";
4316e9ccb2SMauricio Sifontes       exit(1);
4416e9ccb2SMauricio Sifontes     }
4516e9ccb2SMauricio Sifontes   });
4616e9ccb2SMauricio Sifontes }
4716e9ccb2SMauricio Sifontes 
4816e9ccb2SMauricio Sifontes namespace mlir {
registerTestReducer()49b5e22e6dSMehdi Amini void registerTestReducer() { PassRegistration<TestReducer>(); }
5016e9ccb2SMauricio Sifontes } // namespace mlir
51