1 //===- Dialect.cpp - Dialect implementation -------------------------------===// 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 #include "mlir/IR/Dialect.h" 10 #include "mlir/IR/Diagnostics.h" 11 #include "mlir/IR/DialectImplementation.h" 12 #include "mlir/IR/DialectInterface.h" 13 #include "mlir/IR/MLIRContext.h" 14 #include "mlir/IR/Operation.h" 15 #include "llvm/ADT/MapVector.h" 16 #include "llvm/ADT/Twine.h" 17 #include "llvm/Support/ManagedStatic.h" 18 #include "llvm/Support/Regex.h" 19 20 using namespace mlir; 21 using namespace detail; 22 23 DialectAsmParser::~DialectAsmParser() {} 24 25 //===----------------------------------------------------------------------===// 26 // DialectRegistry 27 //===----------------------------------------------------------------------===// 28 29 void DialectRegistry::addDialectInterface( 30 StringRef dialectName, InterfaceAllocatorFunction allocator) { 31 assert(allocator && "unexpected null interface allocation function"); 32 auto it = registry.find(dialectName.str()); 33 assert(it != registry.end() && 34 "adding an interface for an unregistered dialect"); 35 interfaces[it->second.first].push_back(allocator); 36 } 37 38 DialectAllocatorFunctionRef 39 DialectRegistry::getDialectAllocator(StringRef name) const { 40 auto it = registry.find(name.str()); 41 if (it == registry.end()) 42 return nullptr; 43 return it->second.second; 44 } 45 46 void DialectRegistry::insert(TypeID typeID, StringRef name, 47 DialectAllocatorFunction ctor) { 48 auto inserted = registry.insert( 49 std::make_pair(std::string(name), std::make_pair(typeID, ctor))); 50 if (!inserted.second && inserted.first->second.first != typeID) { 51 llvm::report_fatal_error( 52 "Trying to register different dialects for the same namespace: " + 53 name); 54 } 55 } 56 57 void DialectRegistry::registerDelayedInterfaces(Dialect *dialect) const { 58 auto it = interfaces.find(dialect->getTypeID()); 59 if (it == interfaces.end()) 60 return; 61 62 for (const InterfaceAllocatorFunction &createInterface : it->second) 63 dialect->addInterface(createInterface(dialect)); 64 } 65 66 //===----------------------------------------------------------------------===// 67 // Dialect 68 //===----------------------------------------------------------------------===// 69 70 Dialect::Dialect(StringRef name, MLIRContext *context, TypeID id) 71 : name(name), dialectID(id), context(context) { 72 assert(isValidNamespace(name) && "invalid dialect namespace"); 73 } 74 75 Dialect::~Dialect() {} 76 77 /// Verify an attribute from this dialect on the argument at 'argIndex' for 78 /// the region at 'regionIndex' on the given operation. Returns failure if 79 /// the verification failed, success otherwise. This hook may optionally be 80 /// invoked from any operation containing a region. 81 LogicalResult Dialect::verifyRegionArgAttribute(Operation *, unsigned, unsigned, 82 NamedAttribute) { 83 return success(); 84 } 85 86 /// Verify an attribute from this dialect on the result at 'resultIndex' for 87 /// the region at 'regionIndex' on the given operation. Returns failure if 88 /// the verification failed, success otherwise. This hook may optionally be 89 /// invoked from any operation containing a region. 90 LogicalResult Dialect::verifyRegionResultAttribute(Operation *, unsigned, 91 unsigned, NamedAttribute) { 92 return success(); 93 } 94 95 /// Parse an attribute registered to this dialect. 96 Attribute Dialect::parseAttribute(DialectAsmParser &parser, Type type) const { 97 parser.emitError(parser.getNameLoc()) 98 << "dialect '" << getNamespace() 99 << "' provides no attribute parsing hook"; 100 return Attribute(); 101 } 102 103 /// Parse a type registered to this dialect. 104 Type Dialect::parseType(DialectAsmParser &parser) const { 105 // If this dialect allows unknown types, then represent this with OpaqueType. 106 if (allowsUnknownTypes()) { 107 auto ns = Identifier::get(getNamespace(), getContext()); 108 return OpaqueType::get(getContext(), ns, parser.getFullSymbolSpec()); 109 } 110 111 parser.emitError(parser.getNameLoc()) 112 << "dialect '" << getNamespace() << "' provides no type parsing hook"; 113 return Type(); 114 } 115 116 /// Utility function that returns if the given string is a valid dialect 117 /// namespace. 118 bool Dialect::isValidNamespace(StringRef str) { 119 if (str.empty()) 120 return true; 121 llvm::Regex dialectNameRegex("^[a-zA-Z_][a-zA-Z_0-9\\$]*$"); 122 return dialectNameRegex.match(str); 123 } 124 125 /// Register a set of dialect interfaces with this dialect instance. 126 void Dialect::addInterface(std::unique_ptr<DialectInterface> interface) { 127 auto it = registeredInterfaces.try_emplace(interface->getID(), 128 std::move(interface)); 129 (void)it; 130 assert(it.second && "interface kind has already been registered"); 131 } 132 133 //===----------------------------------------------------------------------===// 134 // Dialect Interface 135 //===----------------------------------------------------------------------===// 136 137 DialectInterface::~DialectInterface() {} 138 139 DialectInterfaceCollectionBase::DialectInterfaceCollectionBase( 140 MLIRContext *ctx, TypeID interfaceKind) { 141 for (auto *dialect : ctx->getLoadedDialects()) { 142 if (auto *interface = dialect->getRegisteredInterface(interfaceKind)) { 143 interfaces.insert(interface); 144 orderedInterfaces.push_back(interface); 145 } 146 } 147 } 148 149 DialectInterfaceCollectionBase::~DialectInterfaceCollectionBase() {} 150 151 /// Get the interface for the dialect of given operation, or null if one 152 /// is not registered. 153 const DialectInterface * 154 DialectInterfaceCollectionBase::getInterfaceFor(Operation *op) const { 155 return getInterfaceFor(op->getDialect()); 156 } 157