2018-10-22 10:49:31 +08:00
|
|
|
//===- Dialect.cpp - Dialect implementation -------------------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
#include "mlir/IR/Dialect.h"
|
2019-05-04 01:01:01 +08:00
|
|
|
#include "mlir/IR/Diagnostics.h"
|
2019-02-12 14:51:34 +08:00
|
|
|
#include "mlir/IR/DialectHooks.h"
|
2019-08-15 11:48:35 +08:00
|
|
|
#include "mlir/IR/DialectInterface.h"
|
2018-10-22 10:49:31 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
2019-08-15 11:48:35 +08:00
|
|
|
#include "mlir/IR/Operation.h"
|
2019-02-26 05:16:24 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2018-10-22 10:49:31 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2019-02-27 08:43:12 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2019-08-15 11:48:35 +08:00
|
|
|
|
2018-10-22 10:49:31 +08:00
|
|
|
using namespace mlir;
|
2019-08-15 11:48:35 +08:00
|
|
|
using namespace detail;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dialect Registration
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-22 10:49:31 +08:00
|
|
|
|
|
|
|
// Registry for all dialect allocation functions.
|
|
|
|
static llvm::ManagedStatic<SmallVector<DialectAllocatorFunction, 8>>
|
|
|
|
dialectRegistry;
|
|
|
|
|
2019-02-12 14:51:34 +08:00
|
|
|
// Registry for functions that set dialect hooks.
|
|
|
|
static llvm::ManagedStatic<SmallVector<DialectHooksSetter, 8>>
|
|
|
|
dialectHooksRegistry;
|
|
|
|
|
2018-11-21 06:47:10 +08:00
|
|
|
/// Registers a specific dialect creation function with the system, typically
|
2018-10-22 10:49:31 +08:00
|
|
|
/// used through the DialectRegistration template.
|
|
|
|
void mlir::registerDialectAllocator(const DialectAllocatorFunction &function) {
|
2018-11-21 06:47:10 +08:00
|
|
|
assert(function &&
|
|
|
|
"Attempting to register an empty dialect initialize function");
|
2018-10-22 10:49:31 +08:00
|
|
|
dialectRegistry->push_back(function);
|
|
|
|
}
|
|
|
|
|
2019-02-12 14:51:34 +08:00
|
|
|
/// Registers a function to set specific hooks for a specific dialect, typically
|
2019-10-04 19:37:14 +08:00
|
|
|
/// used through the DialectHooksRegistration template.
|
2019-02-12 14:51:34 +08:00
|
|
|
void mlir::registerDialectHooksSetter(const DialectHooksSetter &function) {
|
|
|
|
assert(
|
|
|
|
function &&
|
|
|
|
"Attempting to register an empty dialect hooks initialization function");
|
|
|
|
|
|
|
|
dialectHooksRegistry->push_back(function);
|
|
|
|
}
|
|
|
|
|
2018-11-21 06:47:10 +08:00
|
|
|
/// Registers all dialects and their const folding hooks with the specified
|
|
|
|
/// MLIRContext.
|
2018-10-22 10:49:31 +08:00
|
|
|
void mlir::registerAllDialects(MLIRContext *context) {
|
|
|
|
for (const auto &fn : *dialectRegistry)
|
|
|
|
fn(context);
|
2019-02-12 14:51:34 +08:00
|
|
|
for (const auto &fn : *dialectHooksRegistry) {
|
|
|
|
fn(context);
|
|
|
|
}
|
2018-10-22 10:49:31 +08:00
|
|
|
}
|
|
|
|
|
2019-08-15 11:48:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dialect
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-30 13:30:54 +08:00
|
|
|
Dialect::Dialect(StringRef name, MLIRContext *context)
|
2019-08-08 02:49:56 +08:00
|
|
|
: name(name), context(context) {
|
2019-03-30 13:30:54 +08:00
|
|
|
assert(isValidNamespace(name) && "invalid dialect namespace");
|
2018-10-22 10:49:31 +08:00
|
|
|
registerDialect(context);
|
|
|
|
}
|
2018-11-10 06:04:03 +08:00
|
|
|
|
|
|
|
Dialect::~Dialect() {}
|
2019-02-26 05:16:24 +08:00
|
|
|
|
2019-07-02 01:29:09 +08:00
|
|
|
/// Verify an attribute from this dialect on the argument at 'argIndex' for
|
2019-07-12 06:05:19 +08:00
|
|
|
/// 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) {
|
2019-07-02 01:29:09 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-10-19 07:02:56 +08:00
|
|
|
/// 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();
|
|
|
|
}
|
|
|
|
|
2019-05-16 00:10:52 +08:00
|
|
|
/// Parse an attribute registered to this dialect.
|
2019-07-18 07:05:32 +08:00
|
|
|
Attribute Dialect::parseAttribute(StringRef attrData, Type type,
|
|
|
|
Location loc) const {
|
2019-06-26 12:31:54 +08:00
|
|
|
emitError(loc) << "dialect '" << getNamespace()
|
|
|
|
<< "' provides no attribute parsing hook";
|
2019-05-16 00:10:52 +08:00
|
|
|
return Attribute();
|
|
|
|
}
|
|
|
|
|
2019-02-26 05:16:24 +08:00
|
|
|
/// Parse a type registered to this dialect.
|
2019-03-30 05:06:51 +08:00
|
|
|
Type Dialect::parseType(StringRef tyData, Location loc) const {
|
2019-08-08 02:49:56 +08:00
|
|
|
// If this dialect allows unknown types, then represent this with OpaqueType.
|
|
|
|
if (allowsUnknownTypes()) {
|
|
|
|
auto ns = Identifier::get(getNamespace(), getContext());
|
|
|
|
return OpaqueType::get(ns, tyData, getContext());
|
|
|
|
}
|
|
|
|
|
2019-06-26 12:31:54 +08:00
|
|
|
emitError(loc) << "dialect '" << getNamespace()
|
|
|
|
<< "' provides no type parsing hook";
|
2019-02-26 05:16:24 +08:00
|
|
|
return Type();
|
|
|
|
}
|
2019-02-27 08:43:12 +08:00
|
|
|
|
|
|
|
/// Utility function that returns if the given string is a valid dialect
|
|
|
|
/// namespace.
|
|
|
|
bool Dialect::isValidNamespace(StringRef str) {
|
|
|
|
if (str.empty())
|
|
|
|
return true;
|
|
|
|
llvm::Regex dialectNameRegex("^[a-zA-Z_][a-zA-Z_0-9\\$]*$");
|
|
|
|
return dialectNameRegex.match(str);
|
|
|
|
}
|
2019-08-15 11:48:35 +08:00
|
|
|
|
|
|
|
/// Register a set of dialect interfaces with this dialect instance.
|
|
|
|
void Dialect::addInterface(std::unique_ptr<DialectInterface> interface) {
|
|
|
|
auto it = registeredInterfaces.try_emplace(interface->getID(),
|
|
|
|
std::move(interface));
|
|
|
|
(void)it;
|
|
|
|
assert(it.second && "interface kind has already been registered");
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dialect Interface
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
DialectInterface::~DialectInterface() {}
|
|
|
|
|
|
|
|
DialectInterfaceCollectionBase::DialectInterfaceCollectionBase(
|
|
|
|
MLIRContext *ctx, ClassID *interfaceKind) {
|
2019-08-22 00:41:37 +08:00
|
|
|
for (auto *dialect : ctx->getRegisteredDialects()) {
|
|
|
|
if (auto *interface = dialect->getRegisteredInterface(interfaceKind)) {
|
2019-08-21 09:49:08 +08:00
|
|
|
interfaces.insert(interface);
|
2019-08-22 00:41:37 +08:00
|
|
|
orderedInterfaces.push_back(interface);
|
|
|
|
}
|
|
|
|
}
|
2019-08-15 11:48:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DialectInterfaceCollectionBase::~DialectInterfaceCollectionBase() {}
|
|
|
|
|
|
|
|
/// Get the interface for the dialect of given operation, or null if one
|
|
|
|
/// is not registered.
|
|
|
|
const DialectInterface *
|
|
|
|
DialectInterfaceCollectionBase::getInterfaceFor(Operation *op) const {
|
2019-08-21 09:49:08 +08:00
|
|
|
return getInterfaceFor(op->getDialect());
|
2019-08-15 11:48:35 +08:00
|
|
|
}
|