From: Chia-hung Duan Date: Thu, 3 Jun 2021 07:51:49 +0000 (+0800) Subject: [mlir-reduce] Create MlirReduceLib X-Git-Tag: llvmorg-14-init~4996 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2f98dfe5b6158379978a58d4266b997d3ec1e409;p=platform%2Fupstream%2Fllvm.git [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 --- 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 0000000..03ac6b8 --- /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 bcc4062..a723263 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 7f9273e..5d3591f 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 0000000..fa0c264 --- /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 0000000..3d137a4 --- /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 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(); + + 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 b59bfcb..e2052c4 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 1801ce1..01d7c96 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 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); + 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)); }