[Mlir-commits] [mlir] 16e9ccb - Create TestReducer pass

Mauricio Sifontes llvmlistbot at llvm.org
Fri Jul 10 17:47:18 PDT 2020


Author: Mauricio Sifontes
Date: 2020-07-11T00:46:57Z
New Revision: 16e9ccb2be7a2ebb04e781bbe4a4f9ce93a06c8e

URL: https://github.com/llvm/llvm-project/commit/16e9ccb2be7a2ebb04e781bbe4a4f9ce93a06c8e
DIFF: https://github.com/llvm/llvm-project/commit/16e9ccb2be7a2ebb04e781bbe4a4f9ce93a06c8e.diff

LOG: Create TestReducer pass

- Create a pass that generates bugs based on trivially defined behavior for the purpose of testing the MLIR Reduce Tool.
- Implement the functionality inside the pass to crash mlir-opt in the presence of an operation with the name "crashOp".
- Register the pass as a test pass in the mlir-opt tool.

Reviewed by: jpienaar

Differential Revision: https://reviews.llvm.org/D83422

Added: 
    mlir/test/lib/Reducer/CMakeLists.txt
    mlir/test/lib/Reducer/MLIRTestReducer.cpp
    mlir/test/mlir-reduce/test-reducer-pass.mlir

Modified: 
    mlir/test/lib/CMakeLists.txt
    mlir/tools/mlir-opt/CMakeLists.txt
    mlir/tools/mlir-opt/mlir-opt.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/test/lib/CMakeLists.txt b/mlir/test/lib/CMakeLists.txt
index 3e69c7514d8f..641a6218d1cc 100644
--- a/mlir/test/lib/CMakeLists.txt
+++ b/mlir/test/lib/CMakeLists.txt
@@ -2,4 +2,5 @@ add_subdirectory(DeclarativeTransforms)
 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
index 000000000000..61743b6097d6
--- /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
index 000000000000..cb9d21934909
--- /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, FunctionPass> {
+  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<TestReducer>(
+      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
index 000000000000..da5b0c963355
--- /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
index d509b23505d1..483dcfec0c0f 100644
--- a/mlir/tools/mlir-opt/CMakeLists.txt
+++ b/mlir/tools/mlir-opt/CMakeLists.txt
@@ -17,6 +17,7 @@ if(MLIR_INCLUDE_TESTS)
     MLIRTestDialect
     MLIRTestIR
     MLIRTestPass
+    MLIRTestReducer
     MLIRTestTransforms
     )
 endif()

diff  --git a/mlir/tools/mlir-opt/mlir-opt.cpp b/mlir/tools/mlir-opt/mlir-opt.cpp
index 2d753d8fd076..0ce5fac1223c 100644
--- a/mlir/tools/mlir-opt/mlir-opt.cpp
+++ b/mlir/tools/mlir-opt/mlir-opt.cpp
@@ -64,6 +64,7 @@ void registerTestMemRefStrideCalculation();
 void registerTestOpaqueLoc();
 void registerTestParallelismDetection();
 void registerTestPreparationPassWithAllowedMemrefResults();
+void registerTestReducer();
 void registerTestGpuParallelLoopMappingPass();
 void registerTestSCFUtilsPass();
 void registerTestVectorConversions();
@@ -139,6 +140,7 @@ void registerTestPasses() {
   registerTestOpaqueLoc();
   registerTestParallelismDetection();
   registerTestPreparationPassWithAllowedMemrefResults();
+  registerTestReducer();
   registerTestGpuParallelLoopMappingPass();
   registerTestSCFUtilsPass();
   registerTestVectorConversions();


        


More information about the Mlir-commits mailing list