brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.0 KiB · 34459b8 Raw
120 lines · cpp
1//===- mlir-reduce.cpp - The MLIR reducer ---------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the general framework of the MLIR reducer tool. It10// parses the command line arguments, parses the initial MLIR test case and sets11// up the testing environment. It  outputs the most reduced test case variant12// after executing the reduction passes.13//14//===----------------------------------------------------------------------===//15 16#include "mlir/Tools/mlir-reduce/MlirReduceMain.h"17#include "mlir/Parser/Parser.h"18#include "mlir/Pass/PassManager.h"19#include "mlir/Reducer/Passes.h"20#include "mlir/Support/FileUtilities.h"21#include "mlir/Tools/ParseUtilities.h"22#include "llvm/Support/InitLLVM.h"23#include "llvm/Support/SourceMgr.h"24#include "llvm/Support/ToolOutputFile.h"25 26using namespace mlir;27 28// Parse and verify the input MLIR file. Returns null on error.29static OwningOpRef<Operation *> loadModule(MLIRContext &context,30                                           StringRef inputFilename,31                                           bool insertImplictModule) {32  // Set up the input file.33  std::string errorMessage;34  auto file = openInputFile(inputFilename, &errorMessage);35  if (!file) {36    llvm::errs() << errorMessage << "\n";37    return nullptr;38  }39 40  auto sourceMgr = std::make_shared<llvm::SourceMgr>();41  sourceMgr->AddNewSourceBuffer(std::move(file), SMLoc());42  return parseSourceFileForTool(sourceMgr, &context, insertImplictModule);43}44 45LogicalResult mlir::mlirReduceMain(int argc, char **argv,46                                   MLIRContext &context) {47  // Override the default '-h' and use the default PrintHelpMessage() which48  // won't print options in categories.49  static llvm::cl::opt<bool> help("h", llvm::cl::desc("Alias for -help"),50                                  llvm::cl::Hidden);51 52  static llvm::cl::OptionCategory mlirReduceCategory("mlir-reduce options");53 54  static llvm::cl::opt<std::string> inputFilename(55      llvm::cl::Positional, llvm::cl::desc("<input file>"),56      llvm::cl::cat(mlirReduceCategory));57 58  static llvm::cl::opt<std::string> outputFilename(59      "o", llvm::cl::desc("Output filename for the reduced test case"),60      llvm::cl::init("-"), llvm::cl::cat(mlirReduceCategory));61 62  static llvm::cl::opt<bool> noImplicitModule{63      "no-implicit-module",64      llvm::cl::desc(65          "Disable implicit addition of a top-level module op during parsing"),66      llvm::cl::init(false)};67 68  static llvm::cl::opt<bool> allowUnregisteredDialects(69      "allow-unregistered-dialect",70      llvm::cl::desc("Allow operation with no registered dialects"),71      llvm::cl::init(false));72 73  llvm::cl::HideUnrelatedOptions(mlirReduceCategory);74 75  llvm::InitLLVM y(argc, argv);76 77  registerReducerPasses();78 79  PassPipelineCLParser parser("", "Reduction Passes to Run");80  llvm::cl::ParseCommandLineOptions(argc, argv,81                                    "MLIR test case reduction tool.\n");82 83  if (help) {84    llvm::cl::PrintHelpMessage();85    return success();86  }87  if (allowUnregisteredDialects)88    context.allowUnregisteredDialects();89 90  std::string errorMessage;91 92  auto output = openOutputFile(outputFilename, &errorMessage);93  if (!output)94    return failure();95 96  OwningOpRef<Operation *> opRef =97      loadModule(context, inputFilename, !noImplicitModule);98  if (!opRef)99    return failure();100 101  auto errorHandler = [&](const Twine &msg) {102    return emitError(UnknownLoc::get(&context)) << msg;103  };104 105  // Reduction pass pipeline.106  PassManager pm(&context, opRef.get()->getName().getStringRef());107  if (failed(parser.addToPipeline(pm, errorHandler)))108    return failure();109 110  OwningOpRef<Operation *> op = opRef.get()->clone();111 112  if (failed(pm.run(op.get())))113    return failure();114 115  op.get()->print(output->os());116  output->keep();117 118  return success();119}120