[Mlir-commits] [mlir] 2f98dfe - [mlir-reduce] Create MlirReduceLib

Chia-hung Duan llvmlistbot at llvm.org
Thu Jun 3 01:08:28 PDT 2021


Author: Chia-hung Duan
Date: 2021-06-03T15:58:26+08:00
New Revision: 2f98dfe5b6158379978a58d4266b997d3ec1e409

URL: https://github.com/llvm/llvm-project/commit/2f98dfe5b6158379978a58d4266b997d3ec1e409
DIFF: https://github.com/llvm/llvm-project/commit/2f98dfe5b6158379978a58d4266b997d3ec1e409.diff

LOG: [mlir-reduce] Create MlirReduceLib

Move the core reducer algorithm into a library so that it'll be easier
for porting to different projects.

Depends On D101046

Reviewed By: jpienaar, rriddle

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

Added: 
    mlir/include/mlir/Tools/mlir-reduce/MlirReduceMain.h
    mlir/lib/Tools/mlir-reduce/CMakeLists.txt
    mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp

Modified: 
    mlir/lib/Reducer/CMakeLists.txt
    mlir/lib/Tools/CMakeLists.txt
    mlir/tools/mlir-reduce/CMakeLists.txt
    mlir/tools/mlir-reduce/mlir-reduce.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Tools/mlir-reduce/MlirReduceMain.h b/mlir/include/mlir/Tools/mlir-reduce/MlirReduceMain.h
new file mode 100644
index 0000000000000..03ac6b84beaae
--- /dev/null
+++ b/mlir/include/mlir/Tools/mlir-reduce/MlirReduceMain.h
@@ -0,0 +1,22 @@
+//===- MlirReduceMain.h - MLIR Reducer driver -------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TOOLS_MLIRREDUCE_MLIRREDUCEMAIN_H
+#define MLIR_TOOLS_MLIRREDUCE_MLIRREDUCEMAIN_H
+
+#include "mlir/Support/LogicalResult.h"
+
+namespace mlir {
+
+class MLIRContext;
+
+LogicalResult mlirReduceMain(int argc, char **argv, MLIRContext &context);
+
+} // end namespace mlir
+
+#endif // MLIR_TOOLS_MLIRREDUCE_MLIRREDUCEMAIN_H

diff  --git a/mlir/lib/Reducer/CMakeLists.txt b/mlir/lib/Reducer/CMakeLists.txt
index bcc4062918dbc..a723263e4f41a 100644
--- a/mlir/lib/Reducer/CMakeLists.txt
+++ b/mlir/lib/Reducer/CMakeLists.txt
@@ -3,11 +3,13 @@ add_mlir_library(MLIRReduce
    ReductionNode.cpp
    ReductionTreePass.cpp
    Tester.cpp
+
    LINK_LIBS PUBLIC
    MLIRIR
    MLIRPass
    MLIRRewrite
    MLIRTransformUtils
+
    DEPENDS
    MLIRReducerIncGen
 )

diff  --git a/mlir/lib/Tools/CMakeLists.txt b/mlir/lib/Tools/CMakeLists.txt
index 7f9273e96d668..5d3591f3e28a1 100644
--- a/mlir/lib/Tools/CMakeLists.txt
+++ b/mlir/lib/Tools/CMakeLists.txt
@@ -1 +1,2 @@
 add_subdirectory(mlir-lsp-server)
+add_subdirectory(mlir-reduce)

diff  --git a/mlir/lib/Tools/mlir-reduce/CMakeLists.txt b/mlir/lib/Tools/mlir-reduce/CMakeLists.txt
new file mode 100644
index 0000000000000..fa0c2643878af
--- /dev/null
+++ b/mlir/lib/Tools/mlir-reduce/CMakeLists.txt
@@ -0,0 +1,18 @@
+set(LLVM_OPTIONAL_SOURCES
+  MlirReduceMain.cpp
+)
+
+set(LLVM_LINK_COMPONENTS
+  Support
+  )
+
+add_mlir_library(MLIRReduceLib
+  MlirReduceMain.cpp
+
+  LINK_LIBS PUBLIC
+  MLIRIR
+  MLIRParser
+  MLIRPass
+  MLIRReduce
+  MLIRSupport
+  )

diff  --git a/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp b/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp
new file mode 100644
index 0000000000000..3d137a467c251
--- /dev/null
+++ b/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp
@@ -0,0 +1,87 @@
+//===- mlir-reduce.cpp - The MLIR reducer ---------------------------------===//
+//
+// 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 the general framework of the MLIR reducer tool. It
+// parses the command line arguments, parses the initial MLIR test case and sets
+// up the testing environment. It  outputs the most reduced test case variant
+// after executing the reduction passes.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Tools/mlir-reduce/MlirReduceMain.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/Parser.h"
+#include "mlir/Pass/Pass.h"
+#include "mlir/Pass/PassManager.h"
+#include "mlir/Reducer/Passes.h"
+#include "mlir/Rewrite/FrozenRewritePatternSet.h"
+#include "mlir/Support/FileUtilities.h"
+#include "mlir/Support/LogicalResult.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/ToolOutputFile.h"
+
+using namespace mlir;
+
+// Parse and verify the input MLIR file.
+static LogicalResult loadModule(MLIRContext &context, OwningModuleRef &module,
+                                StringRef inputFilename) {
+  module = parseSourceFile(inputFilename, &context);
+  if (!module)
+    return failure();
+
+  return success();
+}
+
+LogicalResult mlir::mlirReduceMain(int argc, char **argv,
+                                   MLIRContext &context) {
+  static llvm::cl::opt<std::string> inputFilename(
+      llvm::cl::Positional, llvm::cl::Required, llvm::cl::desc("<input file>"));
+
+  static llvm::cl::opt<std::string> outputFilename(
+      "o", llvm::cl::desc("Output filename for the reduced test case"),
+      llvm::cl::init("-"));
+
+  llvm::InitLLVM y(argc, argv);
+
+  registerReducerPasses();
+  registerMLIRContextCLOptions();
+  registerPassManagerCLOptions();
+
+  PassPipelineCLParser parser("", "Reduction Passes to Run");
+  llvm::cl::ParseCommandLineOptions(argc, argv,
+                                    "MLIR test case reduction tool.\n");
+
+  std::string errorMessage;
+
+  auto output = openOutputFile(outputFilename, &errorMessage);
+  if (!output)
+    return failure();
+
+  mlir::OwningModuleRef moduleRef;
+  if (failed(loadModule(context, moduleRef, inputFilename)))
+    return failure();
+
+  auto errorHandler = [&](const Twine &msg) {
+    return emitError(UnknownLoc::get(&context)) << msg;
+  };
+
+  // Reduction pass pipeline.
+  PassManager pm(&context);
+  if (failed(parser.addToPipeline(pm, errorHandler)))
+    return failure();
+
+  ModuleOp m = moduleRef.get().clone();
+
+  if (failed(pm.run(m)))
+    return failure();
+
+  m.print(output->os());
+  output->keep();
+
+  return success();
+}

diff  --git a/mlir/tools/mlir-reduce/CMakeLists.txt b/mlir/tools/mlir-reduce/CMakeLists.txt
index b59bfcb441123..e2052c44973be 100644
--- a/mlir/tools/mlir-reduce/CMakeLists.txt
+++ b/mlir/tools/mlir-reduce/CMakeLists.txt
@@ -1,27 +1,9 @@
 get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
 get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
-set(LLVM_LINK_COMPONENTS
-  AllTargetsAsmParsers
-  AllTargetsCodeGens
-  AllTargetsDescs
-  AllTargetsInfos
-  AsmParser
-  Core
-  IRReader
-  Support
-  Target
-  TransformUtils
-  )
 
 if(MLIR_INCLUDE_TESTS)
   set(test_libs
-    MLIRAffineTransformsTestPasses
-    MLIRSPIRVTestPasses
     MLIRTestDialect
-    MLIRTestIR
-    MLIRTestPass
-    MLIRTestReducer
-    MLIRTestTransforms
     )
 endif()
 
@@ -29,27 +11,17 @@ set(LIBS
   ${dialect_libs}
   ${conversion_libs}
   ${test_libs}
-  MLIRAnalysis
   MLIRDialect
   MLIRIR
-  MLIRLoopAnalysis
-  MLIROptLib
-  MLIRParser
   MLIRPass
-  MLIRReduce
-  MLIRSupport
-  MLIRTransforms
-  MLIRTransformUtils
+  MLIRReduceLib
   )
 
 add_llvm_tool(mlir-reduce
   mlir-reduce.cpp
 
-  ADDITIONAL_HEADER_DIRS
-  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Reducer
-
   DEPENDS
-  MLIRReducerIncGen
+  ${LIBS}
   )
 
 target_link_libraries(mlir-reduce PRIVATE ${LIBS})

diff  --git a/mlir/tools/mlir-reduce/mlir-reduce.cpp b/mlir/tools/mlir-reduce/mlir-reduce.cpp
index 1801ce1ffb58a..01d7c96cd8bbc 100644
--- a/mlir/tools/mlir-reduce/mlir-reduce.cpp
+++ b/mlir/tools/mlir-reduce/mlir-reduce.cpp
@@ -13,90 +13,31 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "mlir/IR/Dialect.h"
+#include "mlir/IR/MLIRContext.h"
 #include "mlir/InitAllDialects.h"
 #include "mlir/InitAllPasses.h"
-#include "mlir/Parser.h"
-#include "mlir/Pass/Pass.h"
-#include "mlir/Pass/PassManager.h"
-#include "mlir/Reducer/Passes.h"
-#include "mlir/Support/FileUtilities.h"
-#include "mlir/Support/LogicalResult.h"
-#include "llvm/Support/InitLLVM.h"
-#include "llvm/Support/ToolOutputFile.h"
+#include "mlir/Tools/mlir-reduce/MlirReduceMain.h"
 
 using namespace mlir;
 
 namespace mlir {
 namespace test {
+#ifdef MLIR_INCLUDE_TESTS
 void registerTestDialect(DialectRegistry &);
+#endif
 } // namespace test
 } // namespace mlir
 
-static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional,
-                                                llvm::cl::Required,
-                                                llvm::cl::desc("<input file>"));
-
-static llvm::cl::opt<std::string>
-    outputFilename("o",
-                   llvm::cl::desc("Output filename for the reduced test case"),
-                   llvm::cl::init("-"));
-
-// Parse and verify the input MLIR file.
-static LogicalResult loadModule(MLIRContext &context, OwningModuleRef &module,
-                                StringRef inputFilename) {
-  module = parseSourceFile(inputFilename, &context);
-  if (!module)
-    return failure();
-
-  return success();
-}
-
 int main(int argc, char **argv) {
-
-  llvm::InitLLVM y(argc, argv);
-
-  registerMLIRContextCLOptions();
-  registerPassManagerCLOptions();
   registerAllPasses();
-  registerReducerPasses();
-  PassPipelineCLParser parser("", "Reduction Passes to Run");
 
-  llvm::cl::ParseCommandLineOptions(argc, argv,
-                                    "MLIR test case reduction tool.\n");
-
-  std::string errorMessage;
-
-  auto output = openOutputFile(outputFilename, &errorMessage);
-  if (!output)
-    llvm::report_fatal_error(errorMessage);
-
-  mlir::DialectRegistry registry;
+  DialectRegistry registry;
   registerAllDialects(registry);
 #ifdef MLIR_INCLUDE_TESTS
-  mlir::test::registerTestDialect(registry);
+  test::registerTestDialect(registry);
 #endif
-  mlir::MLIRContext context(registry);
-
-  mlir::OwningModuleRef moduleRef;
-  if (failed(loadModule(context, moduleRef, inputFilename)))
-    llvm::report_fatal_error("Input test case can't be parsed");
-
-  auto errorHandler = [&](const Twine &msg) {
-    return emitError(UnknownLoc::get(&context)) << msg;
-  };
-
-  // Reduction pass pipeline.
-  PassManager pm(&context);
-  if (failed(parser.addToPipeline(pm, errorHandler)))
-    llvm::report_fatal_error("Failed to add pipeline");
-
-  ModuleOp m = moduleRef.get().clone();
-
-  if (failed(pm.run(m)))
-    llvm::report_fatal_error("Error running the reduction pass pipeline");
-
-  m.print(output->os());
-  output->keep();
+  MLIRContext context(registry);
 
-  return 0;
+  return failed(mlirReduceMain(argc, argv, context));
 }


        


More information about the Mlir-commits mailing list