diff --git a/mlir/include/mlir/Tools/mlir-reduce/MlirReduceMain.h b/mlir/include/mlir/Tools/mlir-reduce/MlirReduceMain.h new file mode 100644 --- /dev/null +++ b/mlir/include/mlir/Tools/mlir-reduce/MlirReduceMain.h @@ -0,0 +1,23 @@ +//===- 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; +class FrozenRewritePatternSet; + +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 --- a/mlir/lib/Reducer/CMakeLists.txt +++ b/mlir/lib/Reducer/CMakeLists.txt @@ -3,11 +3,15 @@ ReductionNode.cpp ReductionTreePass.cpp Tester.cpp + LINK_LIBS PUBLIC MLIRIR MLIRPass MLIRRewrite MLIRTransformUtils + + DEPENDS + MLIRReducerIncGen ) mlir_check_all_link_libraries(MLIRReduce) diff --git a/mlir/lib/Tools/CMakeLists.txt b/mlir/lib/Tools/CMakeLists.txt --- 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 --- /dev/null +++ b/mlir/lib/Tools/mlir-reduce/CMakeLists.txt @@ -0,0 +1,29 @@ +set(LLVM_OPTIONAL_SOURCES + MlirReduceMain.cpp +) + +set(LLVM_LINK_COMPONENTS + AsmParser + Core + Support + ) + +if(MLIR_INCLUDE_TESTS) + set(test_libs + MLIRTestDialect + MLIRTestIR + MLIRTestReducer + ) +endif() + +add_mlir_library(MLIRReduceLib + MlirReduceMain.cpp + + LINK_LIBS PUBLIC + MLIRParser + MLIRPass + MLIRReduce + MLIRSupport + MLIRTransforms + MLIRTransformUtils + ) diff --git a/mlir/tools/mlir-reduce/mlir-reduce.cpp b/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp copy from mlir/tools/mlir-reduce/mlir-reduce.cpp copy to mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp --- a/mlir/tools/mlir-reduce/mlir-reduce.cpp +++ b/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp @@ -13,12 +13,13 @@ // //===----------------------------------------------------------------------===// -#include "mlir/InitAllDialects.h" -#include "mlir/InitAllPasses.h" +#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" @@ -26,21 +27,6 @@ using namespace mlir; -namespace mlir { -namespace test { -void registerTestDialect(DialectRegistry &); -} // namespace test -} // namespace mlir - -static llvm::cl::opt inputFilename(llvm::cl::Positional, - llvm::cl::Required, - llvm::cl::desc("")); - -static llvm::cl::opt - 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) { @@ -51,16 +37,22 @@ return success(); } -int main(int argc, char **argv) { +LogicalResult mlir::MlirReduceMain(int argc, char **argv, + MLIRContext &context) { + static llvm::cl::opt inputFilename( + llvm::cl::Positional, llvm::cl::Required, llvm::cl::desc("")); + + static llvm::cl::opt outputFilename( + "o", llvm::cl::desc("Output filename for the reduced test case"), + llvm::cl::init("-")); llvm::InitLLVM y(argc, argv); + registerReducerPasses(); registerMLIRContextCLOptions(); registerPassManagerCLOptions(); - registerAllPasses(); - registerReducerPasses(); - PassPipelineCLParser parser("", "Reduction Passes to Run"); + PassPipelineCLParser parser("", "Reduction Passes to Run"); llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR test case reduction tool.\n"); @@ -68,18 +60,11 @@ auto output = openOutputFile(outputFilename, &errorMessage); if (!output) - llvm::report_fatal_error(errorMessage); - - mlir::DialectRegistry registry; - registerAllDialects(registry); -#ifdef MLIR_INCLUDE_TESTS - mlir::test::registerTestDialect(registry); -#endif - mlir::MLIRContext context(registry); + return failure(); mlir::OwningModuleRef moduleRef; if (failed(loadModule(context, moduleRef, inputFilename))) - llvm::report_fatal_error("Input test case can't be parsed"); + return failure(); auto errorHandler = [&](const Twine &msg) { return emitError(UnknownLoc::get(&context)) << msg; @@ -88,15 +73,15 @@ // Reduction pass pipeline. PassManager pm(&context); if (failed(parser.addToPipeline(pm, errorHandler))) - llvm::report_fatal_error("Failed to add pipeline"); + return failure(); ModuleOp m = moduleRef.get().clone(); if (failed(pm.run(m))) - llvm::report_fatal_error("Error running the reduction pass pipeline"); + return failure(); m.print(output->os()); output->keep(); - return 0; + return success(); } diff --git a/mlir/tools/mlir-reduce/CMakeLists.txt b/mlir/tools/mlir-reduce/CMakeLists.txt --- a/mlir/tools/mlir-reduce/CMakeLists.txt +++ b/mlir/tools/mlir-reduce/CMakeLists.txt @@ -1,27 +1,15 @@ 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,15 +17,13 @@ ${dialect_libs} ${conversion_libs} ${test_libs} - MLIRAnalysis MLIRDialect MLIREDSC MLIRIR - MLIRLoopAnalysis - MLIROptLib MLIRParser MLIRPass MLIRReduce + MLIRReduceLib MLIRSupport MLIRTransforms MLIRTransformUtils @@ -46,11 +32,8 @@ 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 --- 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 inputFilename(llvm::cl::Positional, - llvm::cl::Required, - llvm::cl::desc("")); - -static llvm::cl::opt - 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); #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(); - - return 0; + return failed(MlirReduceMain(argc, argv, context)); }