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. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "OpGenHelpers.h" |
14 | #include "llvm/ADT/StringSet.h" |
15 | #include "llvm/Support/CommandLine.h" |
16 | #include "llvm/Support/FormatVariadic.h" |
17 | #include "llvm/Support/Regex.h" |
18 | #include "llvm/TableGen/Error.h" |
19 | |
20 | using namespace llvm; |
21 | using namespace mlir; |
22 | using namespace mlir::tblgen; |
23 | |
24 | cl::OptionCategory opDefGenCat("Options for op definition generators" ); |
25 | |
26 | static cl::opt<std::string> opIncFilter( |
27 | "op-include-regex" , |
28 | cl::desc("Regex of name of op's to include (no filter if empty)" ), |
29 | cl::cat(opDefGenCat)); |
30 | static cl::opt<std::string> opExcFilter( |
31 | "op-exclude-regex" , |
32 | cl::desc("Regex of name of op's to exclude (no filter if empty)" ), |
33 | cl::cat(opDefGenCat)); |
34 | |
35 | static std::string getOperationName(const Record &def) { |
36 | auto prefix = def.getValueAsDef(FieldName: "opDialect" )->getValueAsString(FieldName: "name" ); |
37 | auto opName = def.getValueAsString(FieldName: "opName" ); |
38 | if (prefix.empty()) |
39 | return std::string(opName); |
40 | return std::string(llvm::formatv(Fmt: "{0}.{1}" , Vals&: prefix, Vals&: opName)); |
41 | } |
42 | |
43 | std::vector<Record *> |
44 | mlir::tblgen::getRequestedOpDefinitions(const RecordKeeper &recordKeeper) { |
45 | Record *classDef = recordKeeper.getClass(Name: "Op" ); |
46 | if (!classDef) |
47 | PrintFatalError(Msg: "ERROR: Couldn't find the 'Op' class!\n" ); |
48 | |
49 | llvm::Regex includeRegex(opIncFilter), excludeRegex(opExcFilter); |
50 | std::vector<Record *> defs; |
51 | for (const auto &def : recordKeeper.getDefs()) { |
52 | if (!def.second->isSubClassOf(R: classDef)) |
53 | continue; |
54 | // Include if no include filter or include filter matches. |
55 | if (!opIncFilter.empty() && |
56 | !includeRegex.match(String: getOperationName(def: *def.second))) |
57 | continue; |
58 | // Unless there is an exclude filter and it matches. |
59 | if (!opExcFilter.empty() && |
60 | excludeRegex.match(String: getOperationName(def: *def.second))) |
61 | continue; |
62 | defs.push_back(x: def.second.get()); |
63 | } |
64 | |
65 | return defs; |
66 | } |
67 | |
68 | bool mlir::tblgen::isPythonReserved(StringRef str) { |
69 | static llvm::StringSet<> reserved({ |
70 | "False" , "None" , "True" , "and" , "as" , "assert" , "async" , |
71 | "await" , "break" , "class" , "continue" , "def" , "del" , "elif" , |
72 | "else" , "except" , "finally" , "for" , "from" , "global" , "if" , |
73 | "import" , "in" , "is" , "lambda" , "nonlocal" , "not" , "or" , |
74 | "pass" , "raise" , "return" , "try" , "while" , "with" , "yield" , |
75 | }); |
76 | // These aren't Python keywords but builtin functions that shouldn't/can't be |
77 | // shadowed. |
78 | reserved.insert(key: "callable" ); |
79 | reserved.insert(key: "issubclass" ); |
80 | reserved.insert(key: "type" ); |
81 | return reserved.contains(key: str); |
82 | } |