aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/TableGen
diff options
context:
space:
mode:
authorMarkus Böck <markus.boeck02@gmail.com>2022-08-03 23:43:41 +0200
committerMarkus Böck <markus.boeck02@gmail.com>2022-08-06 14:07:37 +0200
commit1c5a50e32815a49a41d79ff529ca8611ee49c5c8 (patch)
treec78c1b736f0f442e6a4d10f99746eb3b66882e8c /mlir/lib/TableGen
parentc59c8a515f54e262ececfd1056d402212117f22c (diff)
[mlir][tblgen] Refact mlir-tblgen main into its own library
This has previously been done for `mlir-opt` and `mlir-reduce` and roughly the same approach has been done here. The use case for having a separate library is that it is easier for downstream to make custom TableGen backends/executable that work on top of the utilities that are defined in `mlir/TableGen`. The customization point here is the same one as for any upstream TableGen backends: One can add a new generator by simply creating a global instance of `mlir::GenRegistration`. Differential Revision: https://reviews.llvm.org/D131112
Diffstat (limited to 'mlir/lib/TableGen')
-rw-r--r--mlir/lib/TableGen/CMakeLists.txt1
-rw-r--r--mlir/lib/TableGen/GenInfo.cpp41
2 files changed, 42 insertions, 0 deletions
diff --git a/mlir/lib/TableGen/CMakeLists.txt b/mlir/lib/TableGen/CMakeLists.txt
index bb522d7d03f4..55a2e355d6a3 100644
--- a/mlir/lib/TableGen/CMakeLists.txt
+++ b/mlir/lib/TableGen/CMakeLists.txt
@@ -17,6 +17,7 @@ llvm_add_library(MLIRTableGen STATIC
Constraint.cpp
Dialect.cpp
Format.cpp
+ GenInfo.cpp
Interfaces.cpp
Operator.cpp
Pass.cpp
diff --git a/mlir/lib/TableGen/GenInfo.cpp b/mlir/lib/TableGen/GenInfo.cpp
new file mode 100644
index 000000000000..62a3d5283ef5
--- /dev/null
+++ b/mlir/lib/TableGen/GenInfo.cpp
@@ -0,0 +1,41 @@
+//===- GenInfo.cpp - Generator info -----------------------------*- C++ -*-===//
+//
+// Part of the LLVM 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/TableGen/GenInfo.h"
+
+#include "mlir/TableGen/GenNameParser.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ManagedStatic.h"
+
+using namespace mlir;
+
+static llvm::ManagedStatic<std::vector<GenInfo>> generatorRegistry;
+
+GenRegistration::GenRegistration(StringRef arg, StringRef description,
+ const 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);
+}