| 1 | //===- ExtensibleDialect.cpp - Extensible dialect ---------------*- C++ -*-===// |
| 2 | // |
| 3 | // This file is licensed 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 | #include "mlir/IR/ExtensibleDialect.h" |
| 10 | #include "mlir/IR/AttributeSupport.h" |
| 11 | #include "mlir/IR/DialectImplementation.h" |
| 12 | #include "mlir/IR/OperationSupport.h" |
| 13 | #include "mlir/IR/StorageUniquerSupport.h" |
| 14 | #include "llvm/Support/InterleavedRange.h" |
| 15 | |
| 16 | using namespace mlir; |
| 17 | |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | // Dynamic types and attributes shared functions |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | /// Default parser for dynamic attribute or type parameters. |
| 23 | /// Parse in the format '(<>)?' or '<attr (,attr)*>'. |
| 24 | static LogicalResult |
| 25 | typeOrAttrParser(AsmParser &parser, SmallVectorImpl<Attribute> &parsedParams) { |
| 26 | // No parameters |
| 27 | if (parser.parseOptionalLess() || !parser.parseOptionalGreater()) |
| 28 | return success(); |
| 29 | |
| 30 | Attribute attr; |
| 31 | if (parser.parseAttribute(result&: attr)) |
| 32 | return failure(); |
| 33 | parsedParams.push_back(Elt: attr); |
| 34 | |
| 35 | while (parser.parseOptionalGreater()) { |
| 36 | Attribute attr; |
| 37 | if (parser.parseComma() || parser.parseAttribute(result&: attr)) |
| 38 | return failure(); |
| 39 | parsedParams.push_back(Elt: attr); |
| 40 | } |
| 41 | |
| 42 | return success(); |
| 43 | } |
| 44 | |
| 45 | /// Default printer for dynamic attribute or type parameters. |
| 46 | /// Print in the format '(<>)?' or '<attr (,attr)*>'. |
| 47 | static void typeOrAttrPrinter(AsmPrinter &printer, ArrayRef<Attribute> params) { |
| 48 | if (params.empty()) |
| 49 | return; |
| 50 | |
| 51 | printer << "<" << llvm::interleaved(R: params) << ">" ; |
| 52 | } |
| 53 | |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | // Dynamic type |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | |
| 58 | std::unique_ptr<DynamicTypeDefinition> |
| 59 | DynamicTypeDefinition::get(StringRef name, ExtensibleDialect *dialect, |
| 60 | VerifierFn &&verifier) { |
| 61 | return DynamicTypeDefinition::get(name, dialect, verifier: std::move(verifier), |
| 62 | parser: typeOrAttrParser, printer: typeOrAttrPrinter); |
| 63 | } |
| 64 | |
| 65 | std::unique_ptr<DynamicTypeDefinition> |
| 66 | DynamicTypeDefinition::get(StringRef name, ExtensibleDialect *dialect, |
| 67 | VerifierFn &&verifier, ParserFn &&parser, |
| 68 | PrinterFn &&printer) { |
| 69 | return std::unique_ptr<DynamicTypeDefinition>( |
| 70 | new DynamicTypeDefinition(name, dialect, std::move(verifier), |
| 71 | std::move(parser), std::move(printer))); |
| 72 | } |
| 73 | |
| 74 | DynamicTypeDefinition::DynamicTypeDefinition(StringRef nameRef, |
| 75 | ExtensibleDialect *dialect, |
| 76 | VerifierFn &&verifier, |
| 77 | ParserFn &&parser, |
| 78 | PrinterFn &&printer) |
| 79 | : name(nameRef), dialect(dialect), verifier(std::move(verifier)), |
| 80 | parser(std::move(parser)), printer(std::move(printer)), |
| 81 | ctx(dialect->getContext()) {} |
| 82 | |
| 83 | DynamicTypeDefinition::DynamicTypeDefinition(ExtensibleDialect *dialect, |
| 84 | StringRef nameRef) |
| 85 | : name(nameRef), dialect(dialect), ctx(dialect->getContext()) {} |
| 86 | |
| 87 | void DynamicTypeDefinition::registerInTypeUniquer() { |
| 88 | detail::TypeUniquer::registerType<DynamicType>(ctx: &getContext(), typeID: getTypeID()); |
| 89 | } |
| 90 | |
| 91 | namespace mlir { |
| 92 | namespace detail { |
| 93 | /// Storage of DynamicType. |
| 94 | /// Contains a pointer to the type definition and type parameters. |
| 95 | struct DynamicTypeStorage : public TypeStorage { |
| 96 | |
| 97 | using KeyTy = std::pair<DynamicTypeDefinition *, ArrayRef<Attribute>>; |
| 98 | |
| 99 | explicit DynamicTypeStorage(DynamicTypeDefinition *typeDef, |
| 100 | ArrayRef<Attribute> params) |
| 101 | : typeDef(typeDef), params(params) {} |
| 102 | |
| 103 | bool operator==(const KeyTy &key) const { |
| 104 | return typeDef == key.first && params == key.second; |
| 105 | } |
| 106 | |
| 107 | static llvm::hash_code hashKey(const KeyTy &key) { |
| 108 | return llvm::hash_value(arg: key); |
| 109 | } |
| 110 | |
| 111 | static DynamicTypeStorage *construct(TypeStorageAllocator &alloc, |
| 112 | const KeyTy &key) { |
| 113 | return new (alloc.allocate<DynamicTypeStorage>()) |
| 114 | DynamicTypeStorage(key.first, alloc.copyInto(elements: key.second)); |
| 115 | } |
| 116 | |
| 117 | /// Definition of the type. |
| 118 | DynamicTypeDefinition *typeDef; |
| 119 | |
| 120 | /// The type parameters. |
| 121 | ArrayRef<Attribute> params; |
| 122 | }; |
| 123 | } // namespace detail |
| 124 | } // namespace mlir |
| 125 | |
| 126 | DynamicType DynamicType::get(DynamicTypeDefinition *typeDef, |
| 127 | ArrayRef<Attribute> params) { |
| 128 | auto &ctx = typeDef->getContext(); |
| 129 | auto emitError = detail::getDefaultDiagnosticEmitFn(ctx: &ctx); |
| 130 | assert(succeeded(typeDef->verify(emitError, params))); |
| 131 | return detail::TypeUniquer::getWithTypeID<DynamicType>( |
| 132 | ctx: &ctx, typeID: typeDef->getTypeID(), args&: typeDef, args&: params); |
| 133 | } |
| 134 | |
| 135 | DynamicType |
| 136 | DynamicType::getChecked(function_ref<InFlightDiagnostic()> emitError, |
| 137 | DynamicTypeDefinition *typeDef, |
| 138 | ArrayRef<Attribute> params) { |
| 139 | if (failed(Result: typeDef->verify(emitError, params))) |
| 140 | return {}; |
| 141 | auto &ctx = typeDef->getContext(); |
| 142 | return detail::TypeUniquer::getWithTypeID<DynamicType>( |
| 143 | ctx: &ctx, typeID: typeDef->getTypeID(), args&: typeDef, args&: params); |
| 144 | } |
| 145 | |
| 146 | DynamicTypeDefinition *DynamicType::getTypeDef() { return getImpl()->typeDef; } |
| 147 | |
| 148 | ArrayRef<Attribute> DynamicType::getParams() { return getImpl()->params; } |
| 149 | |
| 150 | bool DynamicType::classof(Type type) { |
| 151 | return type.hasTrait<TypeTrait::IsDynamicType>(); |
| 152 | } |
| 153 | |
| 154 | ParseResult DynamicType::parse(AsmParser &parser, |
| 155 | DynamicTypeDefinition *typeDef, |
| 156 | DynamicType &parsedType) { |
| 157 | SmallVector<Attribute> params; |
| 158 | if (failed(Result: typeDef->parser(parser, params))) |
| 159 | return failure(); |
| 160 | parsedType = parser.getChecked<DynamicType>(params&: typeDef, params); |
| 161 | if (!parsedType) |
| 162 | return failure(); |
| 163 | return success(); |
| 164 | } |
| 165 | |
| 166 | void DynamicType::print(AsmPrinter &printer) { |
| 167 | printer << getTypeDef()->getName(); |
| 168 | getTypeDef()->printer(printer, getParams()); |
| 169 | } |
| 170 | |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | // Dynamic attribute |
| 173 | //===----------------------------------------------------------------------===// |
| 174 | |
| 175 | std::unique_ptr<DynamicAttrDefinition> |
| 176 | DynamicAttrDefinition::get(StringRef name, ExtensibleDialect *dialect, |
| 177 | VerifierFn &&verifier) { |
| 178 | return DynamicAttrDefinition::get(name, dialect, verifier: std::move(verifier), |
| 179 | parser: typeOrAttrParser, printer: typeOrAttrPrinter); |
| 180 | } |
| 181 | |
| 182 | std::unique_ptr<DynamicAttrDefinition> |
| 183 | DynamicAttrDefinition::get(StringRef name, ExtensibleDialect *dialect, |
| 184 | VerifierFn &&verifier, ParserFn &&parser, |
| 185 | PrinterFn &&printer) { |
| 186 | return std::unique_ptr<DynamicAttrDefinition>( |
| 187 | new DynamicAttrDefinition(name, dialect, std::move(verifier), |
| 188 | std::move(parser), std::move(printer))); |
| 189 | } |
| 190 | |
| 191 | DynamicAttrDefinition::DynamicAttrDefinition(StringRef nameRef, |
| 192 | ExtensibleDialect *dialect, |
| 193 | VerifierFn &&verifier, |
| 194 | ParserFn &&parser, |
| 195 | PrinterFn &&printer) |
| 196 | : name(nameRef), dialect(dialect), verifier(std::move(verifier)), |
| 197 | parser(std::move(parser)), printer(std::move(printer)), |
| 198 | ctx(dialect->getContext()) {} |
| 199 | |
| 200 | DynamicAttrDefinition::DynamicAttrDefinition(ExtensibleDialect *dialect, |
| 201 | StringRef nameRef) |
| 202 | : name(nameRef), dialect(dialect), ctx(dialect->getContext()) {} |
| 203 | |
| 204 | void DynamicAttrDefinition::registerInAttrUniquer() { |
| 205 | detail::AttributeUniquer::registerAttribute<DynamicAttr>(ctx: &getContext(), |
| 206 | typeID: getTypeID()); |
| 207 | } |
| 208 | |
| 209 | namespace mlir { |
| 210 | namespace detail { |
| 211 | /// Storage of DynamicAttr. |
| 212 | /// Contains a pointer to the attribute definition and attribute parameters. |
| 213 | struct DynamicAttrStorage : public AttributeStorage { |
| 214 | using KeyTy = std::pair<DynamicAttrDefinition *, ArrayRef<Attribute>>; |
| 215 | |
| 216 | explicit DynamicAttrStorage(DynamicAttrDefinition *attrDef, |
| 217 | ArrayRef<Attribute> params) |
| 218 | : attrDef(attrDef), params(params) {} |
| 219 | |
| 220 | bool operator==(const KeyTy &key) const { |
| 221 | return attrDef == key.first && params == key.second; |
| 222 | } |
| 223 | |
| 224 | static llvm::hash_code hashKey(const KeyTy &key) { |
| 225 | return llvm::hash_value(arg: key); |
| 226 | } |
| 227 | |
| 228 | static DynamicAttrStorage *construct(AttributeStorageAllocator &alloc, |
| 229 | const KeyTy &key) { |
| 230 | return new (alloc.allocate<DynamicAttrStorage>()) |
| 231 | DynamicAttrStorage(key.first, alloc.copyInto(elements: key.second)); |
| 232 | } |
| 233 | |
| 234 | /// Definition of the type. |
| 235 | DynamicAttrDefinition *attrDef; |
| 236 | |
| 237 | /// The type parameters. |
| 238 | ArrayRef<Attribute> params; |
| 239 | }; |
| 240 | } // namespace detail |
| 241 | } // namespace mlir |
| 242 | |
| 243 | DynamicAttr DynamicAttr::get(DynamicAttrDefinition *attrDef, |
| 244 | ArrayRef<Attribute> params) { |
| 245 | auto &ctx = attrDef->getContext(); |
| 246 | return detail::AttributeUniquer::getWithTypeID<DynamicAttr>( |
| 247 | ctx: &ctx, typeID: attrDef->getTypeID(), args&: attrDef, args&: params); |
| 248 | } |
| 249 | |
| 250 | DynamicAttr |
| 251 | DynamicAttr::getChecked(function_ref<InFlightDiagnostic()> emitError, |
| 252 | DynamicAttrDefinition *attrDef, |
| 253 | ArrayRef<Attribute> params) { |
| 254 | if (failed(Result: attrDef->verify(emitError, params))) |
| 255 | return {}; |
| 256 | return get(attrDef, params); |
| 257 | } |
| 258 | |
| 259 | DynamicAttrDefinition *DynamicAttr::getAttrDef() { return getImpl()->attrDef; } |
| 260 | |
| 261 | ArrayRef<Attribute> DynamicAttr::getParams() { return getImpl()->params; } |
| 262 | |
| 263 | bool DynamicAttr::classof(Attribute attr) { |
| 264 | return attr.hasTrait<AttributeTrait::IsDynamicAttr>(); |
| 265 | } |
| 266 | |
| 267 | ParseResult DynamicAttr::parse(AsmParser &parser, |
| 268 | DynamicAttrDefinition *attrDef, |
| 269 | DynamicAttr &parsedAttr) { |
| 270 | SmallVector<Attribute> params; |
| 271 | if (failed(Result: attrDef->parser(parser, params))) |
| 272 | return failure(); |
| 273 | parsedAttr = parser.getChecked<DynamicAttr>(params&: attrDef, params); |
| 274 | if (!parsedAttr) |
| 275 | return failure(); |
| 276 | return success(); |
| 277 | } |
| 278 | |
| 279 | void DynamicAttr::print(AsmPrinter &printer) { |
| 280 | printer << getAttrDef()->getName(); |
| 281 | getAttrDef()->printer(printer, getParams()); |
| 282 | } |
| 283 | |
| 284 | //===----------------------------------------------------------------------===// |
| 285 | // Dynamic operation |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | |
| 288 | DynamicOpDefinition::DynamicOpDefinition( |
| 289 | StringRef name, ExtensibleDialect *dialect, |
| 290 | OperationName::VerifyInvariantsFn &&verifyFn, |
| 291 | OperationName::VerifyRegionInvariantsFn &&verifyRegionFn, |
| 292 | OperationName::ParseAssemblyFn &&parseFn, |
| 293 | OperationName::PrintAssemblyFn &&printFn, |
| 294 | OperationName::FoldHookFn &&foldHookFn, |
| 295 | GetCanonicalizationPatternsFn &&getCanonicalizationPatternsFn, |
| 296 | OperationName::PopulateDefaultAttrsFn &&populateDefaultAttrsFn) |
| 297 | : Impl(StringAttr::get(context: dialect->getContext(), |
| 298 | bytes: (dialect->getNamespace() + "." + name).str()), |
| 299 | dialect, dialect->allocateTypeID(), |
| 300 | /*interfaceMap=*/detail::InterfaceMap()), |
| 301 | verifyFn(std::move(verifyFn)), verifyRegionFn(std::move(verifyRegionFn)), |
| 302 | parseFn(std::move(parseFn)), printFn(std::move(printFn)), |
| 303 | foldHookFn(std::move(foldHookFn)), |
| 304 | getCanonicalizationPatternsFn(std::move(getCanonicalizationPatternsFn)), |
| 305 | populateDefaultAttrsFn(std::move(populateDefaultAttrsFn)) { |
| 306 | typeID = dialect->allocateTypeID(); |
| 307 | // DynamicOpDefinition doesn't support properties, so use the ID of void. |
| 308 | propertiesTypeID = TypeID(); |
| 309 | } |
| 310 | |
| 311 | std::unique_ptr<DynamicOpDefinition> DynamicOpDefinition::get( |
| 312 | StringRef name, ExtensibleDialect *dialect, |
| 313 | OperationName::VerifyInvariantsFn &&verifyFn, |
| 314 | OperationName::VerifyRegionInvariantsFn &&verifyRegionFn) { |
| 315 | auto parseFn = [](OpAsmParser &parser, OperationState &result) { |
| 316 | return parser.emitError( |
| 317 | loc: parser.getCurrentLocation(), |
| 318 | message: "dynamic operation do not define any parser function" ); |
| 319 | }; |
| 320 | |
| 321 | auto printFn = [](Operation *op, OpAsmPrinter &printer, StringRef) { |
| 322 | printer.printGenericOp(op); |
| 323 | }; |
| 324 | |
| 325 | return DynamicOpDefinition::get(name, dialect, verifyFn: std::move(verifyFn), |
| 326 | verifyRegionFn: std::move(verifyRegionFn), parseFn: std::move(parseFn), |
| 327 | printFn: std::move(printFn)); |
| 328 | } |
| 329 | |
| 330 | std::unique_ptr<DynamicOpDefinition> DynamicOpDefinition::get( |
| 331 | StringRef name, ExtensibleDialect *dialect, |
| 332 | OperationName::VerifyInvariantsFn &&verifyFn, |
| 333 | OperationName::VerifyRegionInvariantsFn &&verifyRegionFn, |
| 334 | OperationName::ParseAssemblyFn &&parseFn, |
| 335 | OperationName::PrintAssemblyFn &&printFn) { |
| 336 | auto foldHookFn = [](Operation *op, ArrayRef<Attribute> operands, |
| 337 | SmallVectorImpl<OpFoldResult> &results) { |
| 338 | return failure(); |
| 339 | }; |
| 340 | |
| 341 | auto getCanonicalizationPatternsFn = [](RewritePatternSet &, MLIRContext *) { |
| 342 | }; |
| 343 | |
| 344 | auto populateDefaultAttrsFn = [](const OperationName &, NamedAttrList &) {}; |
| 345 | |
| 346 | return DynamicOpDefinition::get(name, dialect, verifyFn: std::move(verifyFn), |
| 347 | verifyRegionFn: std::move(verifyRegionFn), parseFn: std::move(parseFn), |
| 348 | printFn: std::move(printFn), foldHookFn: std::move(foldHookFn), |
| 349 | getCanonicalizationPatternsFn: std::move(getCanonicalizationPatternsFn), |
| 350 | populateDefaultAttrsFn: std::move(populateDefaultAttrsFn)); |
| 351 | } |
| 352 | |
| 353 | std::unique_ptr<DynamicOpDefinition> DynamicOpDefinition::get( |
| 354 | StringRef name, ExtensibleDialect *dialect, |
| 355 | OperationName::VerifyInvariantsFn &&verifyFn, |
| 356 | OperationName::VerifyInvariantsFn &&verifyRegionFn, |
| 357 | OperationName::ParseAssemblyFn &&parseFn, |
| 358 | OperationName::PrintAssemblyFn &&printFn, |
| 359 | OperationName::FoldHookFn &&foldHookFn, |
| 360 | GetCanonicalizationPatternsFn &&getCanonicalizationPatternsFn, |
| 361 | OperationName::PopulateDefaultAttrsFn &&populateDefaultAttrsFn) { |
| 362 | return std::unique_ptr<DynamicOpDefinition>(new DynamicOpDefinition( |
| 363 | name, dialect, std::move(verifyFn), std::move(verifyRegionFn), |
| 364 | std::move(parseFn), std::move(printFn), std::move(foldHookFn), |
| 365 | std::move(getCanonicalizationPatternsFn), |
| 366 | std::move(populateDefaultAttrsFn))); |
| 367 | } |
| 368 | |
| 369 | //===----------------------------------------------------------------------===// |
| 370 | // Extensible dialect |
| 371 | //===----------------------------------------------------------------------===// |
| 372 | |
| 373 | namespace { |
| 374 | /// Interface that can only be implemented by extensible dialects. |
| 375 | /// The interface is used to check if a dialect is extensible or not. |
| 376 | class IsExtensibleDialect : public DialectInterface::Base<IsExtensibleDialect> { |
| 377 | public: |
| 378 | IsExtensibleDialect(Dialect *dialect) : Base(dialect) {} |
| 379 | |
| 380 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(IsExtensibleDialect) |
| 381 | }; |
| 382 | } // namespace |
| 383 | |
| 384 | ExtensibleDialect::ExtensibleDialect(StringRef name, MLIRContext *ctx, |
| 385 | TypeID typeID) |
| 386 | : Dialect(name, ctx, typeID) { |
| 387 | addInterfaces<IsExtensibleDialect>(); |
| 388 | } |
| 389 | |
| 390 | void ExtensibleDialect::registerDynamicType( |
| 391 | std::unique_ptr<DynamicTypeDefinition> &&type) { |
| 392 | DynamicTypeDefinition *typePtr = type.get(); |
| 393 | TypeID typeID = type->getTypeID(); |
| 394 | StringRef name = type->getName(); |
| 395 | ExtensibleDialect *dialect = type->getDialect(); |
| 396 | |
| 397 | assert(dialect == this && |
| 398 | "trying to register a dynamic type in the wrong dialect" ); |
| 399 | |
| 400 | // If a type with the same name is already defined, fail. |
| 401 | auto registered = dynTypes.try_emplace(Key: typeID, Args: std::move(type)).second; |
| 402 | (void)registered; |
| 403 | assert(registered && "type TypeID was not unique" ); |
| 404 | |
| 405 | registered = nameToDynTypes.insert(KV: {name, typePtr}).second; |
| 406 | (void)registered; |
| 407 | assert(registered && |
| 408 | "Trying to create a new dynamic type with an existing name" ); |
| 409 | |
| 410 | // The StringAttr allocates the type name StringRef for the duration of the |
| 411 | // MLIR context. |
| 412 | MLIRContext *ctx = getContext(); |
| 413 | auto nameAttr = |
| 414 | StringAttr::get(context: ctx, bytes: getNamespace() + "." + typePtr->getName()); |
| 415 | |
| 416 | auto abstractType = AbstractType::get( |
| 417 | dialect&: *dialect, interfaceMap: DynamicAttr::getInterfaceMap(), hasTrait: DynamicType::getHasTraitFn(), |
| 418 | walkImmediateSubElementsFn: DynamicType::getWalkImmediateSubElementsFn(), |
| 419 | replaceImmediateSubElementsFn: DynamicType::getReplaceImmediateSubElementsFn(), typeID, name: nameAttr); |
| 420 | |
| 421 | /// Add the type to the dialect and the type uniquer. |
| 422 | addType(typeID, typeInfo: std::move(abstractType)); |
| 423 | typePtr->registerInTypeUniquer(); |
| 424 | } |
| 425 | |
| 426 | void ExtensibleDialect::registerDynamicAttr( |
| 427 | std::unique_ptr<DynamicAttrDefinition> &&attr) { |
| 428 | auto *attrPtr = attr.get(); |
| 429 | auto typeID = attr->getTypeID(); |
| 430 | auto name = attr->getName(); |
| 431 | auto *dialect = attr->getDialect(); |
| 432 | |
| 433 | assert(dialect == this && |
| 434 | "trying to register a dynamic attribute in the wrong dialect" ); |
| 435 | |
| 436 | // If an attribute with the same name is already defined, fail. |
| 437 | auto registered = dynAttrs.try_emplace(Key: typeID, Args: std::move(attr)).second; |
| 438 | (void)registered; |
| 439 | assert(registered && "attribute TypeID was not unique" ); |
| 440 | |
| 441 | registered = nameToDynAttrs.insert(KV: {name, attrPtr}).second; |
| 442 | (void)registered; |
| 443 | assert(registered && |
| 444 | "Trying to create a new dynamic attribute with an existing name" ); |
| 445 | |
| 446 | // The StringAttr allocates the attribute name StringRef for the duration of |
| 447 | // the MLIR context. |
| 448 | MLIRContext *ctx = getContext(); |
| 449 | auto nameAttr = |
| 450 | StringAttr::get(context: ctx, bytes: getNamespace() + "." + attrPtr->getName()); |
| 451 | |
| 452 | auto abstractAttr = AbstractAttribute::get( |
| 453 | dialect&: *dialect, interfaceMap: DynamicAttr::getInterfaceMap(), hasTrait: DynamicAttr::getHasTraitFn(), |
| 454 | walkImmediateSubElementsFn: DynamicAttr::getWalkImmediateSubElementsFn(), |
| 455 | replaceImmediateSubElementsFn: DynamicAttr::getReplaceImmediateSubElementsFn(), typeID, name: nameAttr); |
| 456 | |
| 457 | /// Add the type to the dialect and the type uniquer. |
| 458 | addAttribute(typeID, attrInfo: std::move(abstractAttr)); |
| 459 | attrPtr->registerInAttrUniquer(); |
| 460 | } |
| 461 | |
| 462 | void ExtensibleDialect::registerDynamicOp( |
| 463 | std::unique_ptr<DynamicOpDefinition> &&op) { |
| 464 | assert(op->dialect == this && |
| 465 | "trying to register a dynamic op in the wrong dialect" ); |
| 466 | RegisteredOperationName::insert(ownedImpl: std::move(op), /*attrNames=*/{}); |
| 467 | } |
| 468 | |
| 469 | bool ExtensibleDialect::classof(const Dialect *dialect) { |
| 470 | return const_cast<Dialect *>(dialect) |
| 471 | ->getRegisteredInterface<IsExtensibleDialect>(); |
| 472 | } |
| 473 | |
| 474 | OptionalParseResult ExtensibleDialect::parseOptionalDynamicType( |
| 475 | StringRef typeName, AsmParser &parser, Type &resultType) const { |
| 476 | DynamicTypeDefinition *typeDef = lookupTypeDefinition(name: typeName); |
| 477 | if (!typeDef) |
| 478 | return std::nullopt; |
| 479 | |
| 480 | DynamicType dynType; |
| 481 | if (DynamicType::parse(parser, typeDef, parsedType&: dynType)) |
| 482 | return failure(); |
| 483 | resultType = dynType; |
| 484 | return success(); |
| 485 | } |
| 486 | |
| 487 | LogicalResult ExtensibleDialect::printIfDynamicType(Type type, |
| 488 | AsmPrinter &printer) { |
| 489 | if (auto dynType = llvm::dyn_cast<DynamicType>(Val&: type)) { |
| 490 | dynType.print(printer); |
| 491 | return success(); |
| 492 | } |
| 493 | return failure(); |
| 494 | } |
| 495 | |
| 496 | OptionalParseResult ExtensibleDialect::parseOptionalDynamicAttr( |
| 497 | StringRef attrName, AsmParser &parser, Attribute &resultAttr) const { |
| 498 | DynamicAttrDefinition *attrDef = lookupAttrDefinition(name: attrName); |
| 499 | if (!attrDef) |
| 500 | return std::nullopt; |
| 501 | |
| 502 | DynamicAttr dynAttr; |
| 503 | if (DynamicAttr::parse(parser, attrDef, parsedAttr&: dynAttr)) |
| 504 | return failure(); |
| 505 | resultAttr = dynAttr; |
| 506 | return success(); |
| 507 | } |
| 508 | |
| 509 | LogicalResult ExtensibleDialect::printIfDynamicAttr(Attribute attribute, |
| 510 | AsmPrinter &printer) { |
| 511 | if (auto dynAttr = llvm::dyn_cast<DynamicAttr>(Val&: attribute)) { |
| 512 | dynAttr.print(printer); |
| 513 | return success(); |
| 514 | } |
| 515 | return failure(); |
| 516 | } |
| 517 | |
| 518 | //===----------------------------------------------------------------------===// |
| 519 | // Dynamic dialect |
| 520 | //===----------------------------------------------------------------------===// |
| 521 | |
| 522 | namespace { |
| 523 | /// Interface that can only be implemented by extensible dialects. |
| 524 | /// The interface is used to check if a dialect is extensible or not. |
| 525 | class IsDynamicDialect : public DialectInterface::Base<IsDynamicDialect> { |
| 526 | public: |
| 527 | IsDynamicDialect(Dialect *dialect) : Base(dialect) {} |
| 528 | |
| 529 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(IsDynamicDialect) |
| 530 | }; |
| 531 | } // namespace |
| 532 | |
| 533 | DynamicDialect::DynamicDialect(StringRef name, MLIRContext *ctx) |
| 534 | : SelfOwningTypeID(), |
| 535 | ExtensibleDialect(name, ctx, SelfOwningTypeID::getTypeID()) { |
| 536 | addInterfaces<IsDynamicDialect>(); |
| 537 | } |
| 538 | |
| 539 | bool DynamicDialect::classof(const Dialect *dialect) { |
| 540 | return const_cast<Dialect *>(dialect) |
| 541 | ->getRegisteredInterface<IsDynamicDialect>(); |
| 542 | } |
| 543 | |
| 544 | Type DynamicDialect::parseType(DialectAsmParser &parser) const { |
| 545 | auto loc = parser.getCurrentLocation(); |
| 546 | StringRef typeTag; |
| 547 | if (failed(Result: parser.parseKeyword(keyword: &typeTag))) |
| 548 | return Type(); |
| 549 | |
| 550 | { |
| 551 | Type dynType; |
| 552 | auto parseResult = parseOptionalDynamicType(typeName: typeTag, parser, resultType&: dynType); |
| 553 | if (parseResult.has_value()) { |
| 554 | if (succeeded(Result: parseResult.value())) |
| 555 | return dynType; |
| 556 | return Type(); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | parser.emitError(loc, message: "expected dynamic type" ); |
| 561 | return Type(); |
| 562 | } |
| 563 | |
| 564 | void DynamicDialect::printType(Type type, DialectAsmPrinter &printer) const { |
| 565 | auto wasDynamic = printIfDynamicType(type, printer); |
| 566 | (void)wasDynamic; |
| 567 | assert(succeeded(wasDynamic) && |
| 568 | "non-dynamic type defined in dynamic dialect" ); |
| 569 | } |
| 570 | |
| 571 | Attribute DynamicDialect::parseAttribute(DialectAsmParser &parser, |
| 572 | Type type) const { |
| 573 | auto loc = parser.getCurrentLocation(); |
| 574 | StringRef typeTag; |
| 575 | if (failed(Result: parser.parseKeyword(keyword: &typeTag))) |
| 576 | return Attribute(); |
| 577 | |
| 578 | { |
| 579 | Attribute dynAttr; |
| 580 | auto parseResult = parseOptionalDynamicAttr(attrName: typeTag, parser, resultAttr&: dynAttr); |
| 581 | if (parseResult.has_value()) { |
| 582 | if (succeeded(Result: parseResult.value())) |
| 583 | return dynAttr; |
| 584 | return Attribute(); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | parser.emitError(loc, message: "expected dynamic attribute" ); |
| 589 | return Attribute(); |
| 590 | } |
| 591 | void DynamicDialect::printAttribute(Attribute attr, |
| 592 | DialectAsmPrinter &printer) const { |
| 593 | auto wasDynamic = printIfDynamicAttr(attribute: attr, printer); |
| 594 | (void)wasDynamic; |
| 595 | assert(succeeded(wasDynamic) && |
| 596 | "non-dynamic attribute defined in dynamic dialect" ); |
| 597 | } |
| 598 | |