github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/tools/mlir-opt/mlir-opt.cpp (about)

     1  //===- mlir-opt.cpp - MLIR Optimizer Driver -------------------------------===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  //
    18  // Main entry function for mlir-opt for when built as standalone binary.
    19  //
    20  //===----------------------------------------------------------------------===//
    21  
    22  #include "mlir/Analysis/Passes.h"
    23  #include "mlir/Pass/Pass.h"
    24  #include "mlir/Pass/PassManager.h"
    25  #include "mlir/Support/FileUtilities.h"
    26  #include "mlir/Support/MlirOptMain.h"
    27  #include "llvm/Support/CommandLine.h"
    28  #include "llvm/Support/InitLLVM.h"
    29  #include "llvm/Support/SourceMgr.h"
    30  #include "llvm/Support/ToolOutputFile.h"
    31  
    32  using namespace llvm;
    33  using namespace mlir;
    34  
    35  static cl::opt<std::string>
    36      inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
    37  
    38  static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
    39                                             cl::value_desc("filename"),
    40                                             cl::init("-"));
    41  
    42  static cl::opt<bool>
    43      splitInputFile("split-input-file",
    44                     cl::desc("Split the input file into pieces and process each "
    45                              "chunk independently"),
    46                     cl::init(false));
    47  
    48  static cl::opt<bool>
    49      verifyDiagnostics("verify-diagnostics",
    50                        cl::desc("Check that emitted diagnostics match "
    51                                 "expected-* lines on the corresponding line"),
    52                        cl::init(false));
    53  
    54  static cl::opt<bool>
    55      verifyPasses("verify-each",
    56                   cl::desc("Run the verifier after each transformation pass"),
    57                   cl::init(true));
    58  
    59  static std::vector<const PassRegistryEntry *> *passList;
    60  
    61  int main(int argc, char **argv) {
    62    InitLLVM y(argc, argv);
    63  
    64    // Register any pass manager command line options.
    65    registerPassManagerCLOptions();
    66  
    67    // Parse pass names in main to ensure static initialization completed.
    68    llvm::cl::list<const PassRegistryEntry *, bool, PassNameParser> passList(
    69        "", llvm::cl::desc("Compiler passes to run"));
    70    ::passList = &passList;
    71    cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
    72  
    73    // Set up the input file.
    74    std::string errorMessage;
    75    auto file = openInputFile(inputFilename, &errorMessage);
    76    if (!file) {
    77      llvm::errs() << errorMessage << "\n";
    78      return 1;
    79    }
    80  
    81    auto output = openOutputFile(outputFilename, &errorMessage);
    82    if (!output) {
    83      llvm::errs() << errorMessage << "\n";
    84      exit(1);
    85    }
    86  
    87    return failed(MlirOptMain(output->os(), std::move(file), passList,
    88                              splitInputFile, verifyDiagnostics, verifyPasses));
    89  }