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

     1  //===- mlir-tblgen.cpp - Top-Level TableGen implementation for MLIR -------===//
     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 the main function for MLIR's TableGen.
    19  //
    20  //===----------------------------------------------------------------------===//
    21  
    22  #include "mlir/TableGen/GenInfo.h"
    23  #include "mlir/TableGen/GenNameParser.h"
    24  #include "llvm/ADT/StringExtras.h"
    25  #include "llvm/Support/CommandLine.h"
    26  #include "llvm/Support/FormatVariadic.h"
    27  #include "llvm/Support/InitLLVM.h"
    28  #include "llvm/Support/ManagedStatic.h"
    29  #include "llvm/Support/Signals.h"
    30  #include "llvm/TableGen/Error.h"
    31  #include "llvm/TableGen/Main.h"
    32  #include "llvm/TableGen/Record.h"
    33  #include "llvm/TableGen/TableGenBackend.h"
    34  
    35  using namespace llvm;
    36  using namespace mlir;
    37  
    38  static llvm::ManagedStatic<std::vector<GenInfo>> generatorRegistry;
    39  
    40  mlir::GenRegistration::GenRegistration(StringRef arg, StringRef description,
    41                                         GenFunction function) {
    42    generatorRegistry->emplace_back(arg, description, function);
    43  }
    44  
    45  GenNameParser::GenNameParser(llvm::cl::Option &opt)
    46      : llvm::cl::parser<const GenInfo *>(opt) {
    47    for (const auto &kv : *generatorRegistry) {
    48      addLiteralOption(kv.getGenArgument(), &kv, kv.getGenDescription());
    49    }
    50  }
    51  
    52  void GenNameParser::printOptionInfo(const llvm::cl::Option &O,
    53                                      size_t GlobalWidth) const {
    54    GenNameParser *TP = const_cast<GenNameParser *>(this);
    55    llvm::array_pod_sort(TP->Values.begin(), TP->Values.end(),
    56                         [](const GenNameParser::OptionInfo *VT1,
    57                            const GenNameParser::OptionInfo *VT2) {
    58                           return VT1->Name.compare(VT2->Name);
    59                         });
    60    using llvm::cl::parser;
    61    parser<const GenInfo *>::printOptionInfo(O, GlobalWidth);
    62  }
    63  
    64  // Generator that prints records.
    65  GenRegistration printRecords("print-records", "Print all records to stdout",
    66                               [](const RecordKeeper &records, raw_ostream &os) {
    67                                 os << records;
    68                                 return false;
    69                               });
    70  
    71  // Generator to invoke.
    72  const mlir::GenInfo *generator;
    73  
    74  // TableGenMain requires a function pointer so this function is passed in which
    75  // simply wraps the call to the generator.
    76  static bool MlirTableGenMain(raw_ostream &os, RecordKeeper &records) {
    77    assert(generator && "no generator specified");
    78    return generator->invoke(records, os);
    79  }
    80  
    81  int main(int argc, char **argv) {
    82    llvm::InitLLVM y(argc, argv);
    83    llvm::cl::opt<const mlir::GenInfo *, false, mlir::GenNameParser> generator(
    84        "", llvm::cl::desc("Generator to run"));
    85    cl::ParseCommandLineOptions(argc, argv);
    86    ::generator = generator.getValue();
    87  
    88    return TableGenMain(argv[0], &MlirTableGenMain);
    89  }