| 1 | //===- Operator.cpp - Operator class --------------------------------------===// |
| 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 | // Operator wrapper to simplify using TableGen Record defining a MLIR Op. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/TableGen/Operator.h" |
| 14 | #include "mlir/TableGen/Argument.h" |
| 15 | #include "mlir/TableGen/Predicate.h" |
| 16 | #include "mlir/TableGen/Trait.h" |
| 17 | #include "mlir/TableGen/Type.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/Sequence.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/ADT/TypeSwitch.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/FormatVariadic.h" |
| 26 | #include "llvm/TableGen/Error.h" |
| 27 | #include "llvm/TableGen/Record.h" |
| 28 | |
| 29 | #define DEBUG_TYPE "mlir-tblgen-operator" |
| 30 | |
| 31 | using namespace mlir; |
| 32 | using namespace mlir::tblgen; |
| 33 | |
| 34 | using llvm::DagInit; |
| 35 | using llvm::DefInit; |
| 36 | using llvm::Init; |
| 37 | using llvm::ListInit; |
| 38 | using llvm::Record; |
| 39 | using llvm::StringInit; |
| 40 | |
| 41 | Operator::Operator(const Record &def) |
| 42 | : dialect(def.getValueAsDef(FieldName: "opDialect" )), def(def) { |
| 43 | // The first `_` in the op's TableGen def name is treated as separating the |
| 44 | // dialect prefix and the op class name. The dialect prefix will be ignored if |
| 45 | // not empty. Otherwise, if def name starts with a `_`, the `_` is considered |
| 46 | // as part of the class name. |
| 47 | StringRef prefix; |
| 48 | std::tie(args&: prefix, args&: cppClassName) = def.getName().split(Separator: '_'); |
| 49 | if (prefix.empty()) { |
| 50 | // Class name with a leading underscore and without dialect prefix |
| 51 | cppClassName = def.getName(); |
| 52 | } else if (cppClassName.empty()) { |
| 53 | // Class name without dialect prefix |
| 54 | cppClassName = prefix; |
| 55 | } |
| 56 | |
| 57 | cppNamespace = def.getValueAsString(FieldName: "cppNamespace" ); |
| 58 | |
| 59 | populateOpStructure(); |
| 60 | assertInvariants(); |
| 61 | } |
| 62 | |
| 63 | std::string Operator::getOperationName() const { |
| 64 | auto prefix = dialect.getName(); |
| 65 | auto opName = def.getValueAsString(FieldName: "opName" ); |
| 66 | if (prefix.empty()) |
| 67 | return std::string(opName); |
| 68 | return std::string(llvm::formatv(Fmt: "{0}.{1}" , Vals&: prefix, Vals&: opName)); |
| 69 | } |
| 70 | |
| 71 | std::string Operator::getAdaptorName() const { |
| 72 | return std::string(llvm::formatv(Fmt: "{0}Adaptor" , Vals: getCppClassName())); |
| 73 | } |
| 74 | |
| 75 | std::string Operator::getGenericAdaptorName() const { |
| 76 | return std::string(llvm::formatv(Fmt: "{0}GenericAdaptor" , Vals: getCppClassName())); |
| 77 | } |
| 78 | |
| 79 | /// Assert the invariants of accessors generated for the given name. |
| 80 | static void assertAccessorInvariants(const Operator &op, StringRef name) { |
| 81 | std::string accessorName = |
| 82 | convertToCamelFromSnakeCase(input: name, /*capitalizeFirst=*/true); |
| 83 | |
| 84 | // Functor used to detect when an accessor will cause an overlap with an |
| 85 | // operation API. |
| 86 | // |
| 87 | // There are a little bit more invasive checks possible for cases where not |
| 88 | // all ops have the trait that would cause overlap. For many cases here, |
| 89 | // renaming would be better (e.g., we can only guard in limited manner |
| 90 | // against methods from traits and interfaces here, so avoiding these in op |
| 91 | // definition is safer). |
| 92 | auto nameOverlapsWithOpAPI = [&](StringRef newName) { |
| 93 | if (newName == "AttributeNames" || newName == "Attributes" || |
| 94 | newName == "Operation" ) |
| 95 | return true; |
| 96 | if (newName == "Operands" ) |
| 97 | return op.getNumOperands() != 1 || op.getNumVariableLengthOperands() != 1; |
| 98 | if (newName == "Regions" ) |
| 99 | return op.getNumRegions() != 1 || op.getNumVariadicRegions() != 1; |
| 100 | if (newName == "Type" ) |
| 101 | return op.getNumResults() != 1; |
| 102 | return false; |
| 103 | }; |
| 104 | if (nameOverlapsWithOpAPI(accessorName)) { |
| 105 | // This error could be avoided in situations where the final function is |
| 106 | // identical, but preferably the op definition should avoid using generic |
| 107 | // names. |
| 108 | PrintFatalError(ErrorLoc: op.getLoc(), Msg: "generated accessor for `" + name + |
| 109 | "` overlaps with a default one; please " |
| 110 | "rename to avoid overlap" ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void Operator::assertInvariants() const { |
| 115 | // Check that the name of arguments/results/regions/successors don't overlap. |
| 116 | DenseMap<StringRef, StringRef> existingNames; |
| 117 | auto checkName = [&](StringRef name, StringRef entity) { |
| 118 | if (name.empty()) |
| 119 | return; |
| 120 | auto insertion = existingNames.insert(KV: {name, entity}); |
| 121 | if (insertion.second) { |
| 122 | // Assert invariants for accessors generated for this name. |
| 123 | assertAccessorInvariants(op: *this, name); |
| 124 | return; |
| 125 | } |
| 126 | if (entity == insertion.first->second) |
| 127 | PrintFatalError(ErrorLoc: getLoc(), Msg: "op has a conflict with two " + entity + |
| 128 | " having the same name '" + name + "'" ); |
| 129 | PrintFatalError(ErrorLoc: getLoc(), Msg: "op has a conflict with " + |
| 130 | insertion.first->second + " and " + entity + |
| 131 | " both having an entry with the name '" + |
| 132 | name + "'" ); |
| 133 | }; |
| 134 | // Check operands amongst themselves. |
| 135 | for (int i : llvm::seq<int>(Begin: 0, End: getNumOperands())) |
| 136 | checkName(getOperand(index: i).name, "operands" ); |
| 137 | |
| 138 | // Check results amongst themselves and against operands. |
| 139 | for (int i : llvm::seq<int>(Begin: 0, End: getNumResults())) |
| 140 | checkName(getResult(index: i).name, "results" ); |
| 141 | |
| 142 | // Check regions amongst themselves and against operands and results. |
| 143 | for (int i : llvm::seq<int>(Begin: 0, End: getNumRegions())) |
| 144 | checkName(getRegion(index: i).name, "regions" ); |
| 145 | |
| 146 | // Check successors amongst themselves and against operands, results, and |
| 147 | // regions. |
| 148 | for (int i : llvm::seq<int>(Begin: 0, End: getNumSuccessors())) |
| 149 | checkName(getSuccessor(index: i).name, "successors" ); |
| 150 | } |
| 151 | |
| 152 | StringRef Operator::getDialectName() const { return dialect.getName(); } |
| 153 | |
| 154 | StringRef Operator::getCppClassName() const { return cppClassName; } |
| 155 | |
| 156 | std::string Operator::getQualCppClassName() const { |
| 157 | if (cppNamespace.empty()) |
| 158 | return std::string(cppClassName); |
| 159 | return std::string(llvm::formatv(Fmt: "{0}::{1}" , Vals: cppNamespace, Vals: cppClassName)); |
| 160 | } |
| 161 | |
| 162 | StringRef Operator::getCppNamespace() const { return cppNamespace; } |
| 163 | |
| 164 | int Operator::getNumResults() const { |
| 165 | const DagInit *results = def.getValueAsDag(FieldName: "results" ); |
| 166 | return results->getNumArgs(); |
| 167 | } |
| 168 | |
| 169 | StringRef Operator::() const { |
| 170 | constexpr auto attr = "extraClassDeclaration" ; |
| 171 | if (def.isValueUnset(FieldName: attr)) |
| 172 | return {}; |
| 173 | return def.getValueAsString(FieldName: attr); |
| 174 | } |
| 175 | |
| 176 | StringRef Operator::() const { |
| 177 | constexpr auto attr = "extraClassDefinition" ; |
| 178 | if (def.isValueUnset(FieldName: attr)) |
| 179 | return {}; |
| 180 | return def.getValueAsString(FieldName: attr); |
| 181 | } |
| 182 | |
| 183 | const Record &Operator::getDef() const { return def; } |
| 184 | |
| 185 | bool Operator::skipDefaultBuilders() const { |
| 186 | return def.getValueAsBit(FieldName: "skipDefaultBuilders" ); |
| 187 | } |
| 188 | |
| 189 | bool Operator::hasCustomPropertiesPrinter() const { |
| 190 | return def.getValueAsBit(FieldName: "hasCustomPropertiesPrinter" ); |
| 191 | } |
| 192 | |
| 193 | auto Operator::result_begin() const -> const_value_iterator { |
| 194 | return results.begin(); |
| 195 | } |
| 196 | |
| 197 | auto Operator::result_end() const -> const_value_iterator { |
| 198 | return results.end(); |
| 199 | } |
| 200 | |
| 201 | auto Operator::getResults() const -> const_value_range { |
| 202 | return {result_begin(), result_end()}; |
| 203 | } |
| 204 | |
| 205 | TypeConstraint Operator::getResultTypeConstraint(int index) const { |
| 206 | const DagInit *results = def.getValueAsDag(FieldName: "results" ); |
| 207 | return TypeConstraint(cast<DefInit>(Val: results->getArg(Num: index))); |
| 208 | } |
| 209 | |
| 210 | StringRef Operator::getResultName(int index) const { |
| 211 | const DagInit *results = def.getValueAsDag(FieldName: "results" ); |
| 212 | return results->getArgNameStr(Num: index); |
| 213 | } |
| 214 | |
| 215 | auto Operator::getResultDecorators(int index) const -> var_decorator_range { |
| 216 | const Record *result = |
| 217 | cast<DefInit>(Val: def.getValueAsDag(FieldName: "results" )->getArg(Num: index))->getDef(); |
| 218 | if (!result->isSubClassOf(Name: "OpVariable" )) |
| 219 | return var_decorator_range(nullptr, nullptr); |
| 220 | return *result->getValueAsListInit(FieldName: "decorators" ); |
| 221 | } |
| 222 | |
| 223 | unsigned Operator::getNumVariableLengthResults() const { |
| 224 | return llvm::count_if(Range: results, P: [](const NamedTypeConstraint &c) { |
| 225 | return c.constraint.isVariableLength(); |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | unsigned Operator::getNumVariableLengthOperands() const { |
| 230 | return llvm::count_if(Range: operands, P: [](const NamedTypeConstraint &c) { |
| 231 | return c.constraint.isVariableLength(); |
| 232 | }); |
| 233 | } |
| 234 | |
| 235 | bool Operator::hasSingleVariadicArg() const { |
| 236 | return getNumArgs() == 1 && isa<NamedTypeConstraint *>(Val: getArg(index: 0)) && |
| 237 | getOperand(index: 0).isVariadic(); |
| 238 | } |
| 239 | |
| 240 | Operator::arg_iterator Operator::arg_begin() const { return arguments.begin(); } |
| 241 | |
| 242 | Operator::arg_iterator Operator::arg_end() const { return arguments.end(); } |
| 243 | |
| 244 | Operator::arg_range Operator::getArgs() const { |
| 245 | return {arg_begin(), arg_end()}; |
| 246 | } |
| 247 | |
| 248 | StringRef Operator::getArgName(int index) const { |
| 249 | const DagInit *argumentValues = def.getValueAsDag(FieldName: "arguments" ); |
| 250 | return argumentValues->getArgNameStr(Num: index); |
| 251 | } |
| 252 | |
| 253 | auto Operator::getArgDecorators(int index) const -> var_decorator_range { |
| 254 | const Record *arg = |
| 255 | cast<DefInit>(Val: def.getValueAsDag(FieldName: "arguments" )->getArg(Num: index))->getDef(); |
| 256 | if (!arg->isSubClassOf(Name: "OpVariable" )) |
| 257 | return var_decorator_range(nullptr, nullptr); |
| 258 | return *arg->getValueAsListInit(FieldName: "decorators" ); |
| 259 | } |
| 260 | |
| 261 | const Trait *Operator::getTrait(StringRef trait) const { |
| 262 | for (const auto &t : traits) { |
| 263 | if (const auto *traitDef = dyn_cast<NativeTrait>(Val: &t)) { |
| 264 | if (traitDef->getFullyQualifiedTraitName() == trait) |
| 265 | return traitDef; |
| 266 | } else if (const auto *traitDef = dyn_cast<InternalTrait>(Val: &t)) { |
| 267 | if (traitDef->getFullyQualifiedTraitName() == trait) |
| 268 | return traitDef; |
| 269 | } else if (const auto *traitDef = dyn_cast<InterfaceTrait>(Val: &t)) { |
| 270 | if (traitDef->getFullyQualifiedTraitName() == trait) |
| 271 | return traitDef; |
| 272 | } |
| 273 | } |
| 274 | return nullptr; |
| 275 | } |
| 276 | |
| 277 | bool Operator::hasNonEmptyProperties() const { |
| 278 | if (!properties.empty()) |
| 279 | return true; |
| 280 | if (getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments" ) || |
| 281 | getTrait(trait: "::mlir::OpTrait::AttrSizedResultSegments" )) |
| 282 | return true; |
| 283 | return llvm::any_of(Range: attributes, P: [](const NamedAttribute &attr) { |
| 284 | return !attr.attr.isDerivedAttr(); |
| 285 | }); |
| 286 | } |
| 287 | |
| 288 | SmallVector<StringRef> Operator::getInherentAttrNames() const { |
| 289 | SmallVector<StringRef> names; |
| 290 | for (const NamedAttribute &attr : attributes) |
| 291 | if (!attr.attr.isDerivedAttr()) |
| 292 | names.push_back(Elt: attr.name); |
| 293 | for (const NamedProperty &property : properties) |
| 294 | names.push_back(Elt: property.name); |
| 295 | if (getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments" )) { |
| 296 | names.push_back(Elt: operandSegmentAttrName); |
| 297 | names.push_back(Elt: legacyOperandSegmentAttrName); |
| 298 | } |
| 299 | if (getTrait(trait: "::mlir::OpTrait::AttrSizedResultSegments" )) { |
| 300 | names.push_back(Elt: resultSegmentAttrName); |
| 301 | names.push_back(Elt: legacyResultSegmentAttrName); |
| 302 | } |
| 303 | return names; |
| 304 | } |
| 305 | |
| 306 | auto Operator::region_begin() const -> const_region_iterator { |
| 307 | return regions.begin(); |
| 308 | } |
| 309 | auto Operator::region_end() const -> const_region_iterator { |
| 310 | return regions.end(); |
| 311 | } |
| 312 | auto Operator::getRegions() const |
| 313 | -> llvm::iterator_range<const_region_iterator> { |
| 314 | return {region_begin(), region_end()}; |
| 315 | } |
| 316 | |
| 317 | unsigned Operator::getNumRegions() const { return regions.size(); } |
| 318 | |
| 319 | const NamedRegion &Operator::getRegion(unsigned index) const { |
| 320 | return regions[index]; |
| 321 | } |
| 322 | |
| 323 | unsigned Operator::getNumVariadicRegions() const { |
| 324 | return llvm::count_if(Range: regions, |
| 325 | P: [](const NamedRegion &c) { return c.isVariadic(); }); |
| 326 | } |
| 327 | |
| 328 | auto Operator::successor_begin() const -> const_successor_iterator { |
| 329 | return successors.begin(); |
| 330 | } |
| 331 | auto Operator::successor_end() const -> const_successor_iterator { |
| 332 | return successors.end(); |
| 333 | } |
| 334 | auto Operator::getSuccessors() const |
| 335 | -> llvm::iterator_range<const_successor_iterator> { |
| 336 | return {successor_begin(), successor_end()}; |
| 337 | } |
| 338 | |
| 339 | unsigned Operator::getNumSuccessors() const { return successors.size(); } |
| 340 | |
| 341 | const NamedSuccessor &Operator::getSuccessor(unsigned index) const { |
| 342 | return successors[index]; |
| 343 | } |
| 344 | |
| 345 | unsigned Operator::getNumVariadicSuccessors() const { |
| 346 | return llvm::count_if(Range: successors, |
| 347 | P: [](const NamedSuccessor &c) { return c.isVariadic(); }); |
| 348 | } |
| 349 | |
| 350 | auto Operator::trait_begin() const -> const_trait_iterator { |
| 351 | return traits.begin(); |
| 352 | } |
| 353 | auto Operator::trait_end() const -> const_trait_iterator { |
| 354 | return traits.end(); |
| 355 | } |
| 356 | auto Operator::getTraits() const -> llvm::iterator_range<const_trait_iterator> { |
| 357 | return {trait_begin(), trait_end()}; |
| 358 | } |
| 359 | |
| 360 | auto Operator::attribute_begin() const -> const_attribute_iterator { |
| 361 | return attributes.begin(); |
| 362 | } |
| 363 | auto Operator::attribute_end() const -> const_attribute_iterator { |
| 364 | return attributes.end(); |
| 365 | } |
| 366 | auto Operator::getAttributes() const |
| 367 | -> llvm::iterator_range<const_attribute_iterator> { |
| 368 | return {attribute_begin(), attribute_end()}; |
| 369 | } |
| 370 | auto Operator::attribute_begin() -> attribute_iterator { |
| 371 | return attributes.begin(); |
| 372 | } |
| 373 | auto Operator::attribute_end() -> attribute_iterator { |
| 374 | return attributes.end(); |
| 375 | } |
| 376 | auto Operator::getAttributes() -> llvm::iterator_range<attribute_iterator> { |
| 377 | return {attribute_begin(), attribute_end()}; |
| 378 | } |
| 379 | |
| 380 | auto Operator::operand_begin() const -> const_value_iterator { |
| 381 | return operands.begin(); |
| 382 | } |
| 383 | auto Operator::operand_end() const -> const_value_iterator { |
| 384 | return operands.end(); |
| 385 | } |
| 386 | auto Operator::getOperands() const -> const_value_range { |
| 387 | return {operand_begin(), operand_end()}; |
| 388 | } |
| 389 | |
| 390 | auto Operator::getArg(int index) const -> Argument { return arguments[index]; } |
| 391 | |
| 392 | bool Operator::isVariadic() const { |
| 393 | return any_of(Range: llvm::concat<const NamedTypeConstraint>(Ranges: operands, Ranges: results), |
| 394 | P: [](const NamedTypeConstraint &op) { return op.isVariadic(); }); |
| 395 | } |
| 396 | |
| 397 | void Operator::populateTypeInferenceInfo( |
| 398 | const llvm::StringMap<int> &argumentsAndResultsIndex) { |
| 399 | // If the type inference op interface is not registered, then do not attempt |
| 400 | // to determine if the result types an be inferred. |
| 401 | auto &recordKeeper = def.getRecords(); |
| 402 | auto *inferTrait = recordKeeper.getDef(Name: inferTypeOpInterface); |
| 403 | allResultsHaveKnownTypes = false; |
| 404 | if (!inferTrait) |
| 405 | return; |
| 406 | |
| 407 | // If there are no results, the skip this else the build method generated |
| 408 | // overlaps with another autogenerated builder. |
| 409 | if (getNumResults() == 0) |
| 410 | return; |
| 411 | |
| 412 | // Skip ops with variadic or optional results. |
| 413 | if (getNumVariableLengthResults() > 0) |
| 414 | return; |
| 415 | |
| 416 | // Skip cases currently being custom generated. |
| 417 | // TODO: Remove special cases. |
| 418 | if (getTrait(trait: "::mlir::OpTrait::SameOperandsAndResultType" )) { |
| 419 | // Check for a non-variable length operand to use as the type anchor. |
| 420 | auto *operandI = llvm::find_if(Range&: arguments, P: [](const Argument &arg) { |
| 421 | NamedTypeConstraint *operand = |
| 422 | llvm::dyn_cast_if_present<NamedTypeConstraint *>(Val: arg); |
| 423 | return operand && !operand->isVariableLength(); |
| 424 | }); |
| 425 | if (operandI == arguments.end()) |
| 426 | return; |
| 427 | |
| 428 | // All result types are inferred from the operand type. |
| 429 | int operandIdx = operandI - arguments.begin(); |
| 430 | for (int i = 0; i < getNumResults(); ++i) |
| 431 | resultTypeMapping.emplace_back(Args&: operandIdx, Args: "$_self" ); |
| 432 | |
| 433 | allResultsHaveKnownTypes = true; |
| 434 | traits.push_back(Elt: Trait::create(init: inferTrait->getDefInit())); |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | /// This struct represents a node in this operation's result type inferenece |
| 439 | /// graph. Each node has a list of incoming type inference edges `sources`. |
| 440 | /// Each edge represents a "source" from which the result type can be |
| 441 | /// inferred, either an operand (leaf) or another result (node). When a node |
| 442 | /// is known to have a fully-inferred type, `inferred` is set to true. |
| 443 | struct ResultTypeInference { |
| 444 | /// The list of incoming type inference edges. |
| 445 | SmallVector<InferredResultType> sources; |
| 446 | /// This flag is set to true when the result type is known to be inferrable. |
| 447 | bool inferred = false; |
| 448 | }; |
| 449 | |
| 450 | // This vector represents the type inference graph, with one node for each |
| 451 | // operation result. The nth element is the node for the nth result. |
| 452 | SmallVector<ResultTypeInference> inference(getNumResults(), |
| 453 | llvm::for_overwrite); |
| 454 | |
| 455 | // For all results whose types are buildable, initialize their type inference |
| 456 | // nodes with an edge to themselves. Mark those nodes are fully-inferred. |
| 457 | for (auto [idx, infer] : llvm::enumerate(First&: inference)) { |
| 458 | if (getResult(index: idx).constraint.getBuilderCall()) { |
| 459 | infer.sources.emplace_back(Args: InferredResultType::mapResultIndex(i: idx), |
| 460 | Args: "$_self" ); |
| 461 | infer.inferred = true; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // Use `AllTypesMatch` and `TypesMatchWith` operation traits to build the |
| 466 | // result type inference graph. |
| 467 | for (const Trait &trait : traits) { |
| 468 | const Record &def = trait.getDef(); |
| 469 | |
| 470 | // If the infer type op interface was manually added, then treat it as |
| 471 | // intention that the op needs special handling. |
| 472 | // TODO: Reconsider whether to always generate, this is more conservative |
| 473 | // and keeps existing behavior so starting that way for now. |
| 474 | if (def.isSubClassOf( |
| 475 | Name: llvm::formatv(Fmt: "{0}::Trait" , Vals&: inferTypeOpInterface).str())) |
| 476 | return; |
| 477 | if (const auto *traitDef = dyn_cast<InterfaceTrait>(Val: &trait)) |
| 478 | if (&traitDef->getDef() == inferTrait) |
| 479 | return; |
| 480 | |
| 481 | // The `TypesMatchWith` trait represents a 1 -> 1 type inference edge with a |
| 482 | // type transformer. |
| 483 | if (def.isSubClassOf(Name: "TypesMatchWith" )) { |
| 484 | int target = argumentsAndResultsIndex.lookup(Key: def.getValueAsString(FieldName: "rhs" )); |
| 485 | // Ignore operand type inference. |
| 486 | if (InferredResultType::isArgIndex(i: target)) |
| 487 | continue; |
| 488 | int resultIndex = InferredResultType::unmapResultIndex(i: target); |
| 489 | ResultTypeInference &infer = inference[resultIndex]; |
| 490 | // If the type of the result has already been inferred, do nothing. |
| 491 | if (infer.inferred) |
| 492 | continue; |
| 493 | int sourceIndex = |
| 494 | argumentsAndResultsIndex.lookup(Key: def.getValueAsString(FieldName: "lhs" )); |
| 495 | infer.sources.emplace_back(Args&: sourceIndex, |
| 496 | Args: def.getValueAsString(FieldName: "transformer" ).str()); |
| 497 | // Locally propagate inferredness. |
| 498 | infer.inferred = |
| 499 | InferredResultType::isArgIndex(i: sourceIndex) || |
| 500 | inference[InferredResultType::unmapResultIndex(i: sourceIndex)].inferred; |
| 501 | continue; |
| 502 | } |
| 503 | |
| 504 | // The `ShapedTypeMatchesElementCountAndTypes` trait represents a 1 -> 1 |
| 505 | // type inference edge where a shaped type matches element count and types |
| 506 | // of variadic elements. |
| 507 | if (def.isSubClassOf(Name: "ShapedTypeMatchesElementCountAndTypes" )) { |
| 508 | StringRef shapedArg = def.getValueAsString(FieldName: "shaped" ); |
| 509 | StringRef elementsArg = def.getValueAsString(FieldName: "elements" ); |
| 510 | |
| 511 | int shapedIndex = argumentsAndResultsIndex.lookup(Key: shapedArg); |
| 512 | int elementsIndex = argumentsAndResultsIndex.lookup(Key: elementsArg); |
| 513 | |
| 514 | // Handle result type inference from shaped type to variadic elements. |
| 515 | if (InferredResultType::isResultIndex(i: elementsIndex) && |
| 516 | InferredResultType::isArgIndex(i: shapedIndex)) { |
| 517 | int resultIndex = InferredResultType::unmapResultIndex(i: elementsIndex); |
| 518 | ResultTypeInference &infer = inference[resultIndex]; |
| 519 | if (!infer.inferred) { |
| 520 | infer.sources.emplace_back( |
| 521 | Args&: shapedIndex, |
| 522 | Args: "::llvm::SmallVector<::mlir::Type>(::llvm::cast<::mlir::" |
| 523 | "ShapedType>($_self).getNumElements(), " |
| 524 | "::llvm::cast<::mlir::ShapedType>($_self).getElementType())" ); |
| 525 | infer.inferred = true; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // Type inference in the opposite direction is not possible as the actual |
| 530 | // shaped type can't be inferred from the variadic elements. |
| 531 | |
| 532 | continue; |
| 533 | } |
| 534 | |
| 535 | if (!def.isSubClassOf(Name: "AllTypesMatch" )) |
| 536 | continue; |
| 537 | |
| 538 | auto values = def.getValueAsListOfStrings(FieldName: "values" ); |
| 539 | // The `AllTypesMatch` trait represents an N <-> N fanin and fanout. That |
| 540 | // is, every result type has an edge from every other type. However, if any |
| 541 | // one of the values refers to an operand or a result with a fully-inferred |
| 542 | // type, we can infer all other types from that value. Try to find a |
| 543 | // fully-inferred type in the list. |
| 544 | std::optional<int> fullyInferredIndex; |
| 545 | SmallVector<int> resultIndices; |
| 546 | for (StringRef name : values) { |
| 547 | int index = argumentsAndResultsIndex.lookup(Key: name); |
| 548 | if (InferredResultType::isResultIndex(i: index)) |
| 549 | resultIndices.push_back(Elt: InferredResultType::unmapResultIndex(i: index)); |
| 550 | if (InferredResultType::isArgIndex(i: index) || |
| 551 | inference[InferredResultType::unmapResultIndex(i: index)].inferred) |
| 552 | fullyInferredIndex = index; |
| 553 | } |
| 554 | if (fullyInferredIndex) { |
| 555 | // Make the fully-inferred type the only source for all results that |
| 556 | // aren't already inferred -- a 1 -> N fanout. |
| 557 | for (int resultIndex : resultIndices) { |
| 558 | ResultTypeInference &infer = inference[resultIndex]; |
| 559 | if (!infer.inferred) { |
| 560 | infer.sources.assign(NumElts: 1, Elt: {*fullyInferredIndex, "$_self" }); |
| 561 | infer.inferred = true; |
| 562 | } |
| 563 | } |
| 564 | } else { |
| 565 | // Add an edge between every result and every other type; N <-> N. |
| 566 | for (int resultIndex : resultIndices) { |
| 567 | for (int otherResultIndex : resultIndices) { |
| 568 | if (resultIndex == otherResultIndex) |
| 569 | continue; |
| 570 | inference[resultIndex].sources.emplace_back( |
| 571 | Args: InferredResultType::unmapResultIndex(i: otherResultIndex), Args: "$_self" ); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | // Propagate inferredness until a fixed point. |
| 578 | std::vector<ResultTypeInference *> worklist; |
| 579 | for (ResultTypeInference &infer : inference) |
| 580 | if (!infer.inferred) |
| 581 | worklist.push_back(x: &infer); |
| 582 | bool changed; |
| 583 | do { |
| 584 | changed = false; |
| 585 | for (auto cur = worklist.begin(); cur != worklist.end();) { |
| 586 | ResultTypeInference &infer = **cur; |
| 587 | |
| 588 | InferredResultType *iter = |
| 589 | llvm::find_if(Range&: infer.sources, P: [&](const InferredResultType &source) { |
| 590 | assert(InferredResultType::isResultIndex(source.getIndex())); |
| 591 | return inference[InferredResultType::unmapResultIndex( |
| 592 | i: source.getIndex())] |
| 593 | .inferred; |
| 594 | }); |
| 595 | if (iter == infer.sources.end()) { |
| 596 | ++cur; |
| 597 | continue; |
| 598 | } |
| 599 | |
| 600 | changed = true; |
| 601 | infer.inferred = true; |
| 602 | // Make this the only source for the result. This breaks any cycles. |
| 603 | infer.sources.assign(NumElts: 1, Elt: *iter); |
| 604 | cur = worklist.erase(position: cur); |
| 605 | } |
| 606 | } while (changed); |
| 607 | |
| 608 | allResultsHaveKnownTypes = worklist.empty(); |
| 609 | |
| 610 | // If the types could be computed, then add type inference trait. |
| 611 | if (allResultsHaveKnownTypes) { |
| 612 | traits.push_back(Elt: Trait::create(init: inferTrait->getDefInit())); |
| 613 | for (const ResultTypeInference &infer : inference) |
| 614 | resultTypeMapping.push_back(Elt: infer.sources.front()); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | void Operator::populateOpStructure() { |
| 619 | auto &recordKeeper = def.getRecords(); |
| 620 | auto *typeConstraintClass = recordKeeper.getClass(Name: "TypeConstraint" ); |
| 621 | auto *attrClass = recordKeeper.getClass(Name: "Attr" ); |
| 622 | auto *propertyClass = recordKeeper.getClass(Name: "Property" ); |
| 623 | auto *derivedAttrClass = recordKeeper.getClass(Name: "DerivedAttr" ); |
| 624 | auto *opVarClass = recordKeeper.getClass(Name: "OpVariable" ); |
| 625 | numNativeAttributes = 0; |
| 626 | |
| 627 | const DagInit *argumentValues = def.getValueAsDag(FieldName: "arguments" ); |
| 628 | unsigned numArgs = argumentValues->getNumArgs(); |
| 629 | |
| 630 | // Mapping from name of to argument or result index. Arguments are indexed |
| 631 | // to match getArg index, while the results are negatively indexed. |
| 632 | llvm::StringMap<int> argumentsAndResultsIndex; |
| 633 | |
| 634 | // Handle operands and native attributes. |
| 635 | for (unsigned i = 0; i != numArgs; ++i) { |
| 636 | auto *arg = argumentValues->getArg(Num: i); |
| 637 | auto givenName = argumentValues->getArgNameStr(Num: i); |
| 638 | auto *argDefInit = dyn_cast<DefInit>(Val: arg); |
| 639 | if (!argDefInit) |
| 640 | PrintFatalError(ErrorLoc: def.getLoc(), |
| 641 | Msg: Twine("undefined type for argument #" ) + Twine(i)); |
| 642 | const Record *argDef = argDefInit->getDef(); |
| 643 | if (argDef->isSubClassOf(R: opVarClass)) |
| 644 | argDef = argDef->getValueAsDef(FieldName: "constraint" ); |
| 645 | |
| 646 | if (argDef->isSubClassOf(R: typeConstraintClass)) { |
| 647 | operands.push_back( |
| 648 | Elt: NamedTypeConstraint{.name: givenName, .constraint: TypeConstraint(argDef)}); |
| 649 | } else if (argDef->isSubClassOf(R: attrClass)) { |
| 650 | if (givenName.empty()) |
| 651 | PrintFatalError(ErrorLoc: argDef->getLoc(), Msg: "attributes must be named" ); |
| 652 | if (argDef->isSubClassOf(R: derivedAttrClass)) |
| 653 | PrintFatalError(ErrorLoc: argDef->getLoc(), |
| 654 | Msg: "derived attributes not allowed in argument list" ); |
| 655 | attributes.push_back(Elt: {.name: givenName, .attr: Attribute(argDef)}); |
| 656 | ++numNativeAttributes; |
| 657 | } else if (argDef->isSubClassOf(R: propertyClass)) { |
| 658 | if (givenName.empty()) |
| 659 | PrintFatalError(ErrorLoc: argDef->getLoc(), Msg: "properties must be named" ); |
| 660 | properties.push_back(Elt: {.name: givenName, .prop: Property(argDef)}); |
| 661 | } else { |
| 662 | PrintFatalError(ErrorLoc: def.getLoc(), |
| 663 | Msg: "unexpected def type; only defs deriving " |
| 664 | "from TypeConstraint or Attr or Property are allowed" ); |
| 665 | } |
| 666 | if (!givenName.empty()) |
| 667 | argumentsAndResultsIndex[givenName] = i; |
| 668 | } |
| 669 | |
| 670 | // Handle derived attributes. |
| 671 | for (const auto &val : def.getValues()) { |
| 672 | if (auto *record = dyn_cast<llvm::RecordRecTy>(Val: val.getType())) { |
| 673 | if (!record->isSubClassOf(Class: attrClass)) |
| 674 | continue; |
| 675 | if (!record->isSubClassOf(Class: derivedAttrClass)) |
| 676 | PrintFatalError(ErrorLoc: def.getLoc(), |
| 677 | Msg: "unexpected Attr where only DerivedAttr is allowed" ); |
| 678 | |
| 679 | if (record->getClasses().size() != 1) { |
| 680 | PrintFatalError( |
| 681 | ErrorLoc: def.getLoc(), |
| 682 | Msg: "unsupported attribute modelling, only single class expected" ); |
| 683 | } |
| 684 | attributes.push_back(Elt: {.name: cast<StringInit>(Val: val.getNameInit())->getValue(), |
| 685 | .attr: Attribute(cast<DefInit>(Val: val.getValue()))}); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | // Populate `arguments`. This must happen after we've finalized `operands` and |
| 690 | // `attributes` because we will put their elements' pointers in `arguments`. |
| 691 | // SmallVector may perform re-allocation under the hood when adding new |
| 692 | // elements. |
| 693 | int operandIndex = 0, attrIndex = 0, propIndex = 0; |
| 694 | for (unsigned i = 0; i != numArgs; ++i) { |
| 695 | const Record *argDef = |
| 696 | dyn_cast<DefInit>(Val: argumentValues->getArg(Num: i))->getDef(); |
| 697 | if (argDef->isSubClassOf(R: opVarClass)) |
| 698 | argDef = argDef->getValueAsDef(FieldName: "constraint" ); |
| 699 | |
| 700 | if (argDef->isSubClassOf(R: typeConstraintClass)) { |
| 701 | attrPropOrOperandMapping.push_back( |
| 702 | Elt: {OperandAttrOrProp::Kind::Operand, operandIndex}); |
| 703 | arguments.emplace_back(Args: &operands[operandIndex++]); |
| 704 | } else if (argDef->isSubClassOf(R: attrClass)) { |
| 705 | attrPropOrOperandMapping.push_back( |
| 706 | Elt: {OperandAttrOrProp::Kind::Attribute, attrIndex}); |
| 707 | arguments.emplace_back(Args: &attributes[attrIndex++]); |
| 708 | } else { |
| 709 | assert(argDef->isSubClassOf(propertyClass)); |
| 710 | attrPropOrOperandMapping.push_back( |
| 711 | Elt: {OperandAttrOrProp::Kind::Property, propIndex}); |
| 712 | arguments.emplace_back(Args: &properties[propIndex++]); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | auto *resultsDag = def.getValueAsDag(FieldName: "results" ); |
| 717 | auto *outsOp = dyn_cast<DefInit>(Val: resultsDag->getOperator()); |
| 718 | if (!outsOp || outsOp->getDef()->getName() != "outs" ) { |
| 719 | PrintFatalError(ErrorLoc: def.getLoc(), Msg: "'results' must have 'outs' directive" ); |
| 720 | } |
| 721 | |
| 722 | // Handle results. |
| 723 | for (unsigned i = 0, e = resultsDag->getNumArgs(); i < e; ++i) { |
| 724 | auto name = resultsDag->getArgNameStr(Num: i); |
| 725 | auto *resultInit = dyn_cast<DefInit>(Val: resultsDag->getArg(Num: i)); |
| 726 | if (!resultInit) { |
| 727 | PrintFatalError(ErrorLoc: def.getLoc(), |
| 728 | Msg: Twine("undefined type for result #" ) + Twine(i)); |
| 729 | } |
| 730 | auto *resultDef = resultInit->getDef(); |
| 731 | if (resultDef->isSubClassOf(R: opVarClass)) |
| 732 | resultDef = resultDef->getValueAsDef(FieldName: "constraint" ); |
| 733 | results.push_back(Elt: {.name: name, .constraint: TypeConstraint(resultDef)}); |
| 734 | if (!name.empty()) |
| 735 | argumentsAndResultsIndex[name] = InferredResultType::mapResultIndex(i); |
| 736 | |
| 737 | // We currently only support VariadicOfVariadic operands. |
| 738 | if (results.back().constraint.isVariadicOfVariadic()) { |
| 739 | PrintFatalError( |
| 740 | ErrorLoc: def.getLoc(), |
| 741 | Msg: "'VariadicOfVariadic' results are currently not supported" ); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | // Handle successors |
| 746 | auto *successorsDag = def.getValueAsDag(FieldName: "successors" ); |
| 747 | auto *successorsOp = dyn_cast<DefInit>(Val: successorsDag->getOperator()); |
| 748 | if (!successorsOp || successorsOp->getDef()->getName() != "successor" ) { |
| 749 | PrintFatalError(ErrorLoc: def.getLoc(), |
| 750 | Msg: "'successors' must have 'successor' directive" ); |
| 751 | } |
| 752 | |
| 753 | for (unsigned i = 0, e = successorsDag->getNumArgs(); i < e; ++i) { |
| 754 | auto name = successorsDag->getArgNameStr(Num: i); |
| 755 | auto *successorInit = dyn_cast<DefInit>(Val: successorsDag->getArg(Num: i)); |
| 756 | if (!successorInit) { |
| 757 | PrintFatalError(ErrorLoc: def.getLoc(), |
| 758 | Msg: Twine("undefined kind for successor #" ) + Twine(i)); |
| 759 | } |
| 760 | Successor successor(successorInit->getDef()); |
| 761 | |
| 762 | // Only support variadic successors if it is the last one for now. |
| 763 | if (i != e - 1 && successor.isVariadic()) |
| 764 | PrintFatalError(ErrorLoc: def.getLoc(), Msg: "only the last successor can be variadic" ); |
| 765 | successors.push_back(Elt: {.name: name, .constraint: successor}); |
| 766 | } |
| 767 | |
| 768 | // Create list of traits, skipping over duplicates: appending to lists in |
| 769 | // tablegen is easy, making them unique less so, so dedupe here. |
| 770 | if (auto *traitList = def.getValueAsListInit(FieldName: "traits" )) { |
| 771 | // This is uniquing based on pointers of the trait. |
| 772 | SmallPtrSet<const Init *, 32> traitSet; |
| 773 | traits.reserve(N: traitSet.size()); |
| 774 | |
| 775 | // The declaration order of traits imply the verification order of traits. |
| 776 | // Some traits may require other traits to be verified first then they can |
| 777 | // do further verification based on those verified facts. If you see this |
| 778 | // error, fix the traits declaration order by checking the `dependentTraits` |
| 779 | // field. |
| 780 | auto verifyTraitValidity = [&](const Record *trait) { |
| 781 | auto *dependentTraits = trait->getValueAsListInit(FieldName: "dependentTraits" ); |
| 782 | for (auto *traitInit : *dependentTraits) |
| 783 | if (!traitSet.contains(Ptr: traitInit)) |
| 784 | PrintFatalError( |
| 785 | ErrorLoc: def.getLoc(), |
| 786 | Msg: trait->getValueAsString(FieldName: "trait" ) + " requires " + |
| 787 | cast<DefInit>(Val: traitInit)->getDef()->getValueAsString( |
| 788 | FieldName: "trait" ) + |
| 789 | " to precede it in traits list" ); |
| 790 | }; |
| 791 | |
| 792 | std::function<void(const ListInit *)> insert; |
| 793 | insert = [&](const ListInit *traitList) { |
| 794 | for (auto *traitInit : *traitList) { |
| 795 | auto *def = cast<DefInit>(Val: traitInit)->getDef(); |
| 796 | if (def->isSubClassOf(Name: "TraitList" )) { |
| 797 | insert(def->getValueAsListInit(FieldName: "traits" )); |
| 798 | continue; |
| 799 | } |
| 800 | |
| 801 | // Ignore duplicates. |
| 802 | if (!traitSet.insert(Ptr: traitInit).second) |
| 803 | continue; |
| 804 | |
| 805 | // If this is an interface with base classes, add the bases to the |
| 806 | // trait list. |
| 807 | if (def->isSubClassOf(Name: "Interface" )) |
| 808 | insert(def->getValueAsListInit(FieldName: "baseInterfaces" )); |
| 809 | |
| 810 | // Verify if the trait has all the dependent traits declared before |
| 811 | // itself. |
| 812 | verifyTraitValidity(def); |
| 813 | traits.push_back(Elt: Trait::create(init: traitInit)); |
| 814 | } |
| 815 | }; |
| 816 | insert(traitList); |
| 817 | } |
| 818 | |
| 819 | populateTypeInferenceInfo(argumentsAndResultsIndex); |
| 820 | |
| 821 | // Handle regions |
| 822 | auto *regionsDag = def.getValueAsDag(FieldName: "regions" ); |
| 823 | auto *regionsOp = dyn_cast<DefInit>(Val: regionsDag->getOperator()); |
| 824 | if (!regionsOp || regionsOp->getDef()->getName() != "region" ) { |
| 825 | PrintFatalError(ErrorLoc: def.getLoc(), Msg: "'regions' must have 'region' directive" ); |
| 826 | } |
| 827 | |
| 828 | for (unsigned i = 0, e = regionsDag->getNumArgs(); i < e; ++i) { |
| 829 | auto name = regionsDag->getArgNameStr(Num: i); |
| 830 | auto *regionInit = dyn_cast<DefInit>(Val: regionsDag->getArg(Num: i)); |
| 831 | if (!regionInit) { |
| 832 | PrintFatalError(ErrorLoc: def.getLoc(), |
| 833 | Msg: Twine("undefined kind for region #" ) + Twine(i)); |
| 834 | } |
| 835 | Region region(regionInit->getDef()); |
| 836 | if (region.isVariadic()) { |
| 837 | // Only support variadic regions if it is the last one for now. |
| 838 | if (i != e - 1) |
| 839 | PrintFatalError(ErrorLoc: def.getLoc(), Msg: "only the last region can be variadic" ); |
| 840 | if (name.empty()) |
| 841 | PrintFatalError(ErrorLoc: def.getLoc(), Msg: "variadic regions must be named" ); |
| 842 | } |
| 843 | |
| 844 | regions.push_back(Elt: {.name: name, .constraint: region}); |
| 845 | } |
| 846 | |
| 847 | // Populate the builders. |
| 848 | auto *builderList = dyn_cast_or_null<ListInit>(Val: def.getValueInit(FieldName: "builders" )); |
| 849 | if (builderList && !builderList->empty()) { |
| 850 | for (const Init *init : builderList->getElements()) |
| 851 | builders.emplace_back(Args: cast<DefInit>(Val: init)->getDef(), Args: def.getLoc()); |
| 852 | } else if (skipDefaultBuilders()) { |
| 853 | PrintFatalError( |
| 854 | ErrorLoc: def.getLoc(), |
| 855 | Msg: "default builders are skipped and no custom builders provided" ); |
| 856 | } |
| 857 | |
| 858 | LLVM_DEBUG(print(llvm::dbgs())); |
| 859 | } |
| 860 | |
| 861 | const InferredResultType &Operator::getInferredResultType(int index) const { |
| 862 | assert(allResultTypesKnown()); |
| 863 | return resultTypeMapping[index]; |
| 864 | } |
| 865 | |
| 866 | ArrayRef<SMLoc> Operator::getLoc() const { return def.getLoc(); } |
| 867 | |
| 868 | bool Operator::hasDescription() const { |
| 869 | return !getDescription().trim().empty(); |
| 870 | } |
| 871 | |
| 872 | StringRef Operator::getDescription() const { |
| 873 | return def.getValueAsString(FieldName: "description" ); |
| 874 | } |
| 875 | |
| 876 | bool Operator::hasSummary() const { return !getSummary().trim().empty(); } |
| 877 | |
| 878 | StringRef Operator::getSummary() const { |
| 879 | return def.getValueAsString(FieldName: "summary" ); |
| 880 | } |
| 881 | |
| 882 | bool Operator::hasAssemblyFormat() const { |
| 883 | auto *valueInit = def.getValueInit(FieldName: "assemblyFormat" ); |
| 884 | return isa<StringInit>(Val: valueInit); |
| 885 | } |
| 886 | |
| 887 | StringRef Operator::getAssemblyFormat() const { |
| 888 | return TypeSwitch<const Init *, StringRef>(def.getValueInit(FieldName: "assemblyFormat" )) |
| 889 | .Case(caseFn: [&](const StringInit *init) { return init->getValue(); }); |
| 890 | } |
| 891 | |
| 892 | void Operator::print(llvm::raw_ostream &os) const { |
| 893 | os << "op '" << getOperationName() << "'\n" ; |
| 894 | for (Argument arg : arguments) { |
| 895 | if (auto *attr = llvm::dyn_cast_if_present<NamedAttribute *>(Val&: arg)) |
| 896 | os << "[attribute] " << attr->name << '\n'; |
| 897 | else |
| 898 | os << "[operand] " << cast<NamedTypeConstraint *>(Val&: arg)->name << '\n'; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | auto Operator::VariableDecoratorIterator::unwrap(const Init *init) |
| 903 | -> VariableDecorator { |
| 904 | return VariableDecorator(cast<DefInit>(Val: init)->getDef()); |
| 905 | } |
| 906 | |
| 907 | auto Operator::getArgToOperandAttrOrProp(int index) const -> OperandAttrOrProp { |
| 908 | return attrPropOrOperandMapping[index]; |
| 909 | } |
| 910 | |
| 911 | std::string Operator::getGetterName(StringRef name) const { |
| 912 | return "get" + convertToCamelFromSnakeCase(input: name, /*capitalizeFirst=*/true); |
| 913 | } |
| 914 | |
| 915 | std::string Operator::getSetterName(StringRef name) const { |
| 916 | return "set" + convertToCamelFromSnakeCase(input: name, /*capitalizeFirst=*/true); |
| 917 | } |
| 918 | |
| 919 | std::string Operator::getRemoverName(StringRef name) const { |
| 920 | return "remove" + convertToCamelFromSnakeCase(input: name, /*capitalizeFirst=*/true); |
| 921 | } |
| 922 | |
| 923 | bool Operator::hasFolder() const { return def.getValueAsBit(FieldName: "hasFolder" ); } |
| 924 | |
| 925 | bool Operator::useCustomPropertiesEncoding() const { |
| 926 | return def.getValueAsBit(FieldName: "useCustomPropertiesEncoding" ); |
| 927 | } |
| 928 | |