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