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/StringExtras.h"
15#include "llvm/ADT/StringSet.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/FormatVariadic.h"
18#include "llvm/Support/Regex.h"
19#include "llvm/TableGen/Error.h"
20
21using namespace llvm;
22using namespace mlir;
23using namespace mlir::tblgen;
24
25cl::OptionCategory opDefGenCat("Options for op definition generators");
26
27static cl::opt<std::string> opIncFilter(
28 "op-include-regex",
29 cl::desc("Regex of name of op's to include (no filter if empty)"),
30 cl::cat(opDefGenCat));
31static cl::opt<std::string> opExcFilter(
32 "op-exclude-regex",
33 cl::desc("Regex of name of op's to exclude (no filter if empty)"),
34 cl::cat(opDefGenCat));
35static cl::opt<unsigned> opShardCount(
36 "op-shard-count",
37 cl::desc("The number of shards into which the op classes will be divided"),
38 cl::cat(opDefGenCat), cl::init(Val: 1));
39static cl::opt<std::string> opPartition(
40 "op-partition",
41 cl::desc("Only consider the ops in this op partition (see `Op::partition`) "
42 "and emit the registration hooks of the partition"),
43 cl::cat(opDefGenCat));
44
45static std::string getOperationName(const Record &def) {
46 auto prefix = def.getValueAsDef(FieldName: "opDialect")->getValueAsString(FieldName: "name");
47 auto opName = def.getValueAsString(FieldName: "opName");
48 if (prefix.empty())
49 return std::string(opName);
50 return std::string(formatv(Fmt: "{0}.{1}", Vals&: prefix, Vals&: opName));
51}
52
53StringRef mlir::tblgen::getOpPartitionName(const Record &def) {
54 const Record *partition = def.getValueAsOptionalDef(FieldName: "partition");
55 return partition ? partition->getValueAsString(FieldName: "partitionName") : "";
56}
57
58std::string mlir::tblgen::getOpRegistrationHookName(const Dialect &dialect,
59 StringRef partition) {
60 return "register" + dialect.getCppClassName() + "Operations" +
61 convertToCamelFromSnakeCase(input: partition, /*capitalizeFirst=*/true);
62}
63
64/// Verifies the op partitions of `defs`, the ops requested by the filters other
65/// than "op-partition": every op must be in a partition, the specified
66/// partition must have ops and the registration hooks of different partitions
67/// must have different names.
68static void verifyOpPartitions(ArrayRef<const Record *> defs) {
69 // Index sharding does not emit the registration hooks of partitions.
70 if (opShardCount > 1)
71 PrintFatalError(Msg: "-op-partition cannot be combined with -op-shard-count");
72
73 bool hasSpecifiedPartition = false;
74 StringMap<StringRef> partitionsByHookName;
75 for (const Record *def : defs) {
76 StringRef partition = getOpPartitionName(def: *def);
77 // An op without a partition would not be generated by any partition.
78 if (partition.empty())
79 PrintFatalError(Rec: def, Msg: "op '" + def->getName() +
80 "' is in no partition, but its ops are "
81 "generated by partition with -op-partition");
82 hasSpecifiedPartition |= partition == opPartition;
83
84 std::string hookName = getOpRegistrationHookName(
85 dialect: Dialect(def->getValueAsDef(FieldName: "opDialect")), partition);
86 auto [it, inserted] = partitionsByHookName.try_emplace(Key: hookName, Args&: partition);
87 if (!inserted && it->second != partition)
88 PrintFatalError(
89 Rec: def, Msg: "op partitions '" + it->second + "' and '" + partition +
90 "' have the same registration hook '" + hookName + "'");
91 }
92
93 if (!hasSpecifiedPartition)
94 PrintFatalError(Msg: "no op is in the specified op partition '" + opPartition +
95 "'");
96}
97
98std::vector<const Record *> mlir::tblgen::getRequestedOpDefinitions(
99 const RecordKeeper &records,
100 std::vector<const Record *> *otherPartitionDefs) {
101 const Record *classDef = records.getClass(Name: "Op");
102 if (!classDef)
103 PrintFatalError(Msg: "ERROR: Couldn't find the 'Op' class!\n");
104
105 Regex includeRegex(opIncFilter), excludeRegex(opExcFilter);
106 std::vector<const Record *> defs;
107 for (const Record *def : records.getDefs()) {
108 if (!def->isSubClassOf(R: classDef))
109 continue;
110 // Include if no include filter or include filter matches.
111 if (!opIncFilter.empty() && !includeRegex.match(String: getOperationName(def: *def)))
112 continue;
113 // Unless there is an exclude filter and it matches.
114 if (!opExcFilter.empty() && excludeRegex.match(String: getOperationName(def: *def)))
115 continue;
116 defs.push_back(x: def);
117 }
118
119 if (opPartition.empty())
120 return defs;
121
122 // Only include ops of the specified partition.
123 verifyOpPartitions(defs);
124 auto otherPartitionsBegin =
125 std::stable_partition(first: defs.begin(), last: defs.end(), pred: [](const Record *def) {
126 return getOpPartitionName(def: *def) == opPartition;
127 });
128 if (otherPartitionDefs)
129 otherPartitionDefs->assign(first: otherPartitionsBegin, last: defs.end());
130 defs.erase(first: otherPartitionsBegin, last: defs.end());
131 return defs;
132}
133
134bool mlir::tblgen::isPythonReserved(StringRef str) {
135 static StringSet<> reserved({
136 "False", "None", "True", "and", "as", "assert", "async",
137 "await", "break", "class", "continue", "def", "del", "elif",
138 "else", "except", "finally", "for", "from", "global", "if",
139 "import", "in", "is", "lambda", "nonlocal", "not", "or",
140 "pass", "raise", "return", "try", "while", "with", "yield",
141 });
142 // These aren't Python keywords but builtin functions that shouldn't/can't be
143 // shadowed.
144 reserved.insert(key: "callable");
145 reserved.insert(key: "issubclass");
146 reserved.insert(key: "type");
147 return reserved.contains(key: str);
148}
149
150void mlir::tblgen::shardOpDefinitions(
151 ArrayRef<const Record *> defs,
152 SmallVectorImpl<ArrayRef<const Record *>> &shardedDefs) {
153 assert(opShardCount > 0 && "expected a positive shard count");
154 if (opShardCount == 1) {
155 shardedDefs.push_back(Elt: defs);
156 return;
157 }
158
159 unsigned minShardSize = defs.size() / opShardCount;
160 unsigned numMissing = defs.size() - minShardSize * opShardCount;
161 shardedDefs.reserve(N: opShardCount);
162 for (unsigned i = 0, start = 0; i < opShardCount; ++i) {
163 unsigned size = minShardSize + (i < numMissing);
164 shardedDefs.push_back(Elt: defs.slice(N: start, M: size));
165 start += size;
166 }
167}
168
169StringRef mlir::tblgen::getSpecifiedOpPartition() { return opPartition; }
170