diff --git a/mlir/test/lib/CMakeLists.txt b/mlir/test/lib/CMakeLists.txt --- a/mlir/test/lib/CMakeLists.txt +++ b/mlir/test/lib/CMakeLists.txt @@ -2,4 +2,5 @@ add_subdirectory(Dialect) add_subdirectory(IR) add_subdirectory(Pass) +add_subdirectory(Reducer) add_subdirectory(Transforms) diff --git a/mlir/test/lib/Reducer/CMakeLists.txt b/mlir/test/lib/Reducer/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/mlir/test/lib/Reducer/CMakeLists.txt @@ -0,0 +1,17 @@ +# Exclude tests from libMLIR.so +add_mlir_library(MLIRTestReducer + MLIRTestReducer.cpp + + EXCLUDE_FROM_LIBMLIR + + ADDITIONAL_HEADER_DIRS + ${MLIR_MAIN_INCLUDE_DIR}/mlir/IR + + LINK_COMPONENTS + Core + + LINK_LIBS PUBLIC + MLIRIR + MLIRPass + MLIRSupport + ) diff --git a/mlir/test/lib/Reducer/MLIRTestReducer.cpp b/mlir/test/lib/Reducer/MLIRTestReducer.cpp new file mode 100644 --- /dev/null +++ b/mlir/test/lib/Reducer/MLIRTestReducer.cpp @@ -0,0 +1,54 @@ +//===- TestReducer.cpp - Test MLIR Reduce ---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements a pass that reproduces errors based on trivially defined +// patterns. It is used as a buggy optimization pass for the purpose of testing +// the MLIR Reduce tool. +// +//===----------------------------------------------------------------------===// + +#include "mlir/Pass/Pass.h" + +#define PASS_NAME "test-mlir-reducer" + +using namespace mlir; + +static llvm::cl::OptionCategory clOptionsCategory(PASS_NAME " options"); + +namespace { + +/// This pass looks for for the presence of an operation with the name +/// "crashOp" in the input MLIR file and crashes the mlir-opt tool if the +/// operation is found. +struct TestReducer : public PassWrapper { + TestReducer() = default; + TestReducer(const TestReducer &pass){}; + void runOnFunction() override; +}; + +} // end anonymous namespace + +void TestReducer::runOnFunction() { + for (auto &op : getOperation()) + op.walk([&](Operation *op) { + StringRef opName = op->getName().getStringRef(); + + if (opName == "test.crashOp") { + llvm::errs() << "MLIR Reducer Test generated failure: Found " + "\"crashOp\" operation\n"; + exit(1); + } + }); +} + +namespace mlir { +void registerTestReducer() { + PassRegistration( + PASS_NAME, "Tests MLIR Reduce tool by generating failures"); +} +} // namespace mlir diff --git a/mlir/test/mlir-reduce/test-reducer-pass.mlir b/mlir/test/mlir-reduce/test-reducer-pass.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/mlir-reduce/test-reducer-pass.mlir @@ -0,0 +1,7 @@ +// RUN: mlir-opt %s +// RUN: not mlir-opt %s -test-mlir-reducer + +func @test() { + "test.crashOp"() : () -> () + return +} diff --git a/mlir/tools/mlir-opt/CMakeLists.txt b/mlir/tools/mlir-opt/CMakeLists.txt --- a/mlir/tools/mlir-opt/CMakeLists.txt +++ b/mlir/tools/mlir-opt/CMakeLists.txt @@ -17,6 +17,7 @@ MLIRTestDialect MLIRTestIR MLIRTestPass + MLIRTestReducer MLIRTestTransforms ) endif() diff --git a/mlir/tools/mlir-opt/mlir-opt.cpp b/mlir/tools/mlir-opt/mlir-opt.cpp --- a/mlir/tools/mlir-opt/mlir-opt.cpp +++ b/mlir/tools/mlir-opt/mlir-opt.cpp @@ -64,6 +64,7 @@ void registerTestOpaqueLoc(); void registerTestParallelismDetection(); void registerTestPreparationPassWithAllowedMemrefResults(); +void registerTestReducer(); void registerTestGpuParallelLoopMappingPass(); void registerTestSCFUtilsPass(); void registerTestVectorConversions(); @@ -139,6 +140,7 @@ registerTestOpaqueLoc(); registerTestParallelismDetection(); registerTestPreparationPassWithAllowedMemrefResults(); + registerTestReducer(); registerTestGpuParallelLoopMappingPass(); registerTestSCFUtilsPass(); registerTestVectorConversions();