[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
This commit is contained in:
Chia-hung Duan 2021-06-03 15:51:49 +08:00
parent b40908e639
commit 2f98dfe5b6
7 changed files with 141 additions and 98 deletions

View File

@ -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

View File

@ -3,11 +3,13 @@ add_mlir_library(MLIRReduce
ReductionNode.cpp
ReductionTreePass.cpp
Tester.cpp
LINK_LIBS PUBLIC
MLIRIR
MLIRPass
MLIRRewrite
MLIRTransformUtils
DEPENDS
MLIRReducerIncGen
)

View File

@ -1 +1,2 @@
add_subdirectory(mlir-lsp-server)
add_subdirectory(mlir-reduce)

View File

@ -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
)

View File

@ -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();
}

View File

@ -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})

View File

@ -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);
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));
}