[NFC] Add libcxx python reformat SHA to .git-blame-ignore-revs
[llvm-project.git] / mlir / tools / mlir-tblgen / OpGenHelpers.cpp
blobb08f3fb7768c9cf6ea0c34a171435b7f06cf0c18
1 //===- OpGenHelpers.cpp - MLIR operation generator helpers ----------------===//
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 defines helpers used in the op generators.
11 //===----------------------------------------------------------------------===//
13 #include "OpGenHelpers.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/FormatVariadic.h"
16 #include "llvm/Support/Regex.h"
17 #include "llvm/TableGen/Error.h"
19 using namespace llvm;
20 using namespace mlir;
21 using namespace mlir::tblgen;
23 cl::OptionCategory opDefGenCat("Options for op definition generators");
25 static cl::opt<std::string> opIncFilter(
26 "op-include-regex",
27 cl::desc("Regex of name of op's to include (no filter if empty)"),
28 cl::cat(opDefGenCat));
29 static cl::opt<std::string> opExcFilter(
30 "op-exclude-regex",
31 cl::desc("Regex of name of op's to exclude (no filter if empty)"),
32 cl::cat(opDefGenCat));
34 static std::string getOperationName(const Record &def) {
35 auto prefix = def.getValueAsDef("opDialect")->getValueAsString("name");
36 auto opName = def.getValueAsString("opName");
37 if (prefix.empty())
38 return std::string(opName);
39 return std::string(llvm::formatv("{0}.{1}", prefix, opName));
42 std::vector<Record *>
43 mlir::tblgen::getRequestedOpDefinitions(const RecordKeeper &recordKeeper) {
44 Record *classDef = recordKeeper.getClass("Op");
45 if (!classDef)
46 PrintFatalError("ERROR: Couldn't find the 'Op' class!\n");
48 llvm::Regex includeRegex(opIncFilter), excludeRegex(opExcFilter);
49 std::vector<Record *> defs;
50 for (const auto &def : recordKeeper.getDefs()) {
51 if (!def.second->isSubClassOf(classDef))
52 continue;
53 // Include if no include filter or include filter matches.
54 if (!opIncFilter.empty() &&
55 !includeRegex.match(getOperationName(*def.second)))
56 continue;
57 // Unless there is an exclude filter and it matches.
58 if (!opExcFilter.empty() &&
59 excludeRegex.match(getOperationName(*def.second)))
60 continue;
61 defs.push_back(def.second.get());
64 return defs;