github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/Support/TranslateClParser.cpp (about)

     1  //===- TranslateClParser.h - Translations command line parser -------------===//
     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  // This file contains custom command line parser for translations.
    19  //
    20  //===----------------------------------------------------------------------===//
    21  
    22  #include "mlir/Support/TranslateClParser.h"
    23  
    24  #include "mlir/Analysis/Verifier.h"
    25  #include "mlir/IR/MLIRContext.h"
    26  #include "mlir/IR/Module.h"
    27  #include "mlir/Parser.h"
    28  #include "mlir/Support/FileUtilities.h"
    29  #include "mlir/Support/LogicalResult.h"
    30  #include "mlir/Translation.h"
    31  #include "llvm/Support/CommandLine.h"
    32  #include "llvm/Support/FileUtilities.h"
    33  #include "llvm/Support/SourceMgr.h"
    34  #include "llvm/Support/ToolOutputFile.h"
    35  
    36  using namespace mlir;
    37  
    38  // Storage for the translation function wrappers that survive the parser.
    39  static llvm::SmallVector<TranslateFunction, 16> wrapperStorage;
    40  
    41  static LogicalResult printMLIROutput(ModuleOp module,
    42                                       llvm::StringRef outputFilename) {
    43    if (failed(verify(module)))
    44      return failure();
    45    auto file = openOutputFile(outputFilename);
    46    if (!file)
    47      return failure();
    48    module.print(file->os());
    49    file->keep();
    50    return success();
    51  }
    52  
    53  TranslationParser::TranslationParser(llvm::cl::Option &opt)
    54      : llvm::cl::parser<const TranslateFunction *>(opt) {
    55    const auto &toMLIRRegistry = getTranslationToMLIRRegistry();
    56    const auto &fromMLIRRegistry = getTranslationFromMLIRRegistry();
    57  
    58    // Reserve the required capacity upfront so that pointers are not
    59    // invalidated on reallocation.
    60    wrapperStorage.reserve(toMLIRRegistry.size() + fromMLIRRegistry.size());
    61    for (const auto &kv : toMLIRRegistry) {
    62      TranslateToMLIRFunction function = kv.second;
    63      TranslateFunction wrapper = [function](StringRef inputFilename,
    64                                             StringRef outputFilename,
    65                                             MLIRContext *context) {
    66        OwningModuleRef module = function(inputFilename, context);
    67        if (!module)
    68          return failure();
    69        return printMLIROutput(*module, outputFilename);
    70      };
    71      wrapperStorage.emplace_back(std::move(wrapper));
    72  
    73      addLiteralOption(kv.first(), &wrapperStorage.back(), kv.first());
    74    }
    75  
    76    for (const auto &kv : fromMLIRRegistry) {
    77      TranslateFromMLIRFunction function = kv.second;
    78      TranslateFunction wrapper = [function](StringRef inputFilename,
    79                                             StringRef outputFilename,
    80                                             MLIRContext *context) {
    81        llvm::SourceMgr sourceMgr;
    82        SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, context);
    83        auto module =
    84            OwningModuleRef(parseSourceFile(inputFilename, sourceMgr, context));
    85        if (!module)
    86          return failure();
    87        return function(module.get(), outputFilename);
    88      };
    89      wrapperStorage.emplace_back(std::move(wrapper));
    90  
    91      addLiteralOption(kv.first(), &wrapperStorage.back(), kv.first());
    92    }
    93  }
    94  
    95  void TranslationParser::printOptionInfo(const llvm::cl::Option &O,
    96                                          size_t GlobalWidth) const {
    97    TranslationParser *TP = const_cast<TranslationParser *>(this);
    98    llvm::array_pod_sort(TP->Values.begin(), TP->Values.end(),
    99                         [](const TranslationParser::OptionInfo *VT1,
   100                            const TranslationParser::OptionInfo *VT2) {
   101                           return VT1->Name.compare(VT2->Name);
   102                         });
   103    using llvm::cl::parser;
   104    parser<const TranslateFunction *>::printOptionInfo(O, GlobalWidth);
   105  }