1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
//===- Dialect.cpp - Dialect implementation -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/Dialect.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/DialectInterface.h"
#include "mlir/IR/ExtensibleDialect.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Operation.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Regex.h"
#define DEBUG_TYPE "dialect"
using namespace mlir;
using namespace detail;
//===----------------------------------------------------------------------===//
// Dialect
//===----------------------------------------------------------------------===//
Dialect::Dialect(StringRef name, MLIRContext *context, TypeID id)
: name(name), dialectID(id), context(context) {
assert(isValidNamespace(name) && "invalid dialect namespace");
}
Dialect::~Dialect() = default;
/// Verify an attribute from this dialect on the argument at 'argIndex' for
/// the region at 'regionIndex' on the given operation. Returns failure if
/// the verification failed, success otherwise. This hook may optionally be
/// invoked from any operation containing a region.
LogicalResult Dialect::verifyRegionArgAttribute(Operation *, unsigned, unsigned,
NamedAttribute) {
return success();
}
/// Verify an attribute from this dialect on the result at 'resultIndex' for
/// the region at 'regionIndex' on the given operation. Returns failure if
/// the verification failed, success otherwise. This hook may optionally be
/// invoked from any operation containing a region.
LogicalResult Dialect::verifyRegionResultAttribute(Operation *, unsigned,
unsigned, NamedAttribute) {
return success();
}
/// Parse an attribute registered to this dialect.
Attribute Dialect::parseAttribute(DialectAsmParser &parser, Type type) const {
parser.emitError(parser.getNameLoc())
<< "dialect '" << getNamespace()
<< "' provides no attribute parsing hook";
return Attribute();
}
/// Parse a type registered to this dialect.
Type Dialect::parseType(DialectAsmParser &parser) const {
// If this dialect allows unknown types, then represent this with OpaqueType.
if (allowsUnknownTypes()) {
StringAttr ns = StringAttr::get(getContext(), getNamespace());
return OpaqueType::get(ns, parser.getFullSymbolSpec());
}
parser.emitError(parser.getNameLoc())
<< "dialect '" << getNamespace() << "' provides no type parsing hook";
return Type();
}
std::optional<Dialect::ParseOpHook>
Dialect::getParseOperationHook(StringRef opName) const {
return std::nullopt;
}
llvm::unique_function<void(Operation *, OpAsmPrinter &printer)>
Dialect::getOperationPrinter(Operation *op) const {
assert(op->getDialect() == this &&
"Dialect hook invoked on non-dialect owned operation");
return nullptr;
}
/// Utility function that returns if the given string is a valid dialect
/// namespace
bool Dialect::isValidNamespace(StringRef str) {
llvm::Regex dialectNameRegex("^[a-zA-Z_][a-zA-Z_0-9\\$]*$");
return dialectNameRegex.match(str);
}
/// Register a set of dialect interfaces with this dialect instance.
void Dialect::addInterface(std::unique_ptr<DialectInterface> interface) {
// Handle the case where the models resolve a promised interface.
handleAdditionOfUndefinedPromisedInterface(interface->getID());
auto it = registeredInterfaces.try_emplace(interface->getID(),
std::move(interface));
(void)it;
LLVM_DEBUG({
if (!it.second) {
llvm::dbgs() << "[" DEBUG_TYPE
"] repeated interface registration for dialect "
<< getNamespace();
}
});
}
//===----------------------------------------------------------------------===//
// Dialect Interface
//===----------------------------------------------------------------------===//
DialectInterface::~DialectInterface() = default;
MLIRContext *DialectInterface::getContext() const {
return dialect->getContext();
}
DialectInterfaceCollectionBase::DialectInterfaceCollectionBase(
MLIRContext *ctx, TypeID interfaceKind, StringRef interfaceName) {
for (auto *dialect : ctx->getLoadedDialects()) {
#ifndef NDEBUG
dialect->handleUseOfUndefinedPromisedInterface(interfaceKind,
interfaceName);
#endif
if (auto *interface = dialect->getRegisteredInterface(interfaceKind)) {
interfaces.insert(interface);
orderedInterfaces.push_back(interface);
}
}
}
DialectInterfaceCollectionBase::~DialectInterfaceCollectionBase() = default;
/// Get the interface for the dialect of given operation, or null if one
/// is not registered.
const DialectInterface *
DialectInterfaceCollectionBase::getInterfaceFor(Operation *op) const {
return getInterfaceFor(op->getDialect());
}
//===----------------------------------------------------------------------===//
// DialectExtension
//===----------------------------------------------------------------------===//
DialectExtensionBase::~DialectExtensionBase() = default;
void dialect_extension_detail::handleUseOfUndefinedPromisedInterface(
Dialect &dialect, TypeID interfaceID, StringRef interfaceName) {
dialect.handleUseOfUndefinedPromisedInterface(interfaceID, interfaceName);
}
void dialect_extension_detail::handleAdditionOfUndefinedPromisedInterface(
Dialect &dialect, TypeID interfaceID) {
dialect.handleAdditionOfUndefinedPromisedInterface(interfaceID);
}
//===----------------------------------------------------------------------===//
// DialectRegistry
//===----------------------------------------------------------------------===//
DialectRegistry::DialectRegistry() { insert<BuiltinDialect>(); }
DialectAllocatorFunctionRef
DialectRegistry::getDialectAllocator(StringRef name) const {
auto it = registry.find(name.str());
if (it == registry.end())
return nullptr;
return it->second.second;
}
void DialectRegistry::insert(TypeID typeID, StringRef name,
const DialectAllocatorFunction &ctor) {
auto inserted = registry.insert(
std::make_pair(std::string(name), std::make_pair(typeID, ctor)));
if (!inserted.second && inserted.first->second.first != typeID) {
llvm::report_fatal_error(
"Trying to register different dialects for the same namespace: " +
name);
}
}
void DialectRegistry::insertDynamic(
StringRef name, const DynamicDialectPopulationFunction &ctor) {
// This TypeID marks dynamic dialects. We cannot give a TypeID for the
// dialect yet, since the TypeID of a dynamic dialect is defined at its
// construction.
TypeID typeID = TypeID::get<void>();
// Create the dialect, and then call ctor, which allocates its components.
auto constructor = [nameStr = name.str(), ctor](MLIRContext *ctx) {
auto *dynDialect = ctx->getOrLoadDynamicDialect(
nameStr, [ctx, ctor](DynamicDialect *dialect) { ctor(ctx, dialect); });
assert(dynDialect && "Dynamic dialect creation unexpectedly failed");
return dynDialect;
};
insert(typeID, name, constructor);
}
void DialectRegistry::applyExtensions(Dialect *dialect) const {
MLIRContext *ctx = dialect->getContext();
StringRef dialectName = dialect->getNamespace();
// Functor used to try to apply the given extension.
auto applyExtension = [&](const DialectExtensionBase &extension) {
ArrayRef<StringRef> dialectNames = extension.getRequiredDialects();
// Handle the simple case of a single dialect name. In this case, the
// required dialect should be the current dialect.
if (dialectNames.size() == 1) {
if (dialectNames.front() == dialectName)
extension.apply(ctx, dialect);
return;
}
// Otherwise, check to see if this extension requires this dialect.
const StringRef *nameIt = llvm::find(dialectNames, dialectName);
if (nameIt == dialectNames.end())
return;
// If it does, ensure that all of the other required dialects have been
// loaded.
SmallVector<Dialect *> requiredDialects;
requiredDialects.reserve(dialectNames.size());
for (auto it = dialectNames.begin(), e = dialectNames.end(); it != e;
++it) {
// The current dialect is known to be loaded.
if (it == nameIt) {
requiredDialects.push_back(dialect);
continue;
}
// Otherwise, check if it is loaded.
Dialect *loadedDialect = ctx->getLoadedDialect(*it);
if (!loadedDialect)
return;
requiredDialects.push_back(loadedDialect);
}
extension.apply(ctx, requiredDialects);
};
// Note: Additional extensions may be added while applying an extension.
for (int i = 0; i < static_cast<int>(extensions.size()); ++i)
applyExtension(*extensions[i]);
}
void DialectRegistry::applyExtensions(MLIRContext *ctx) const {
// Functor used to try to apply the given extension.
auto applyExtension = [&](const DialectExtensionBase &extension) {
ArrayRef<StringRef> dialectNames = extension.getRequiredDialects();
// Check to see if all of the dialects for this extension are loaded.
SmallVector<Dialect *> requiredDialects;
requiredDialects.reserve(dialectNames.size());
for (StringRef dialectName : dialectNames) {
Dialect *loadedDialect = ctx->getLoadedDialect(dialectName);
if (!loadedDialect)
return;
requiredDialects.push_back(loadedDialect);
}
extension.apply(ctx, requiredDialects);
};
// Note: Additional extensions may be added while applying an extension.
for (int i = 0; i < static_cast<int>(extensions.size()); ++i)
applyExtension(*extensions[i]);
}
bool DialectRegistry::isSubsetOf(const DialectRegistry &rhs) const {
// Treat any extensions conservatively.
if (!extensions.empty())
return false;
// Check that the current dialects fully overlap with the dialects in 'rhs'.
return llvm::all_of(
registry, [&](const auto &it) { return rhs.registry.count(it.first); });
}
|