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