2018-10-22 10:49:31 +08:00
|
|
|
//===- Dialect.cpp - Dialect implementation -------------------------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-22 10:49:31 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-22 10:49:31 +08:00
|
|
|
|
|
|
|
#include "mlir/IR/Dialect.h"
|
2021-06-17 00:53:21 +08:00
|
|
|
#include "mlir/IR/BuiltinDialect.h"
|
2019-05-04 01:01:01 +08:00
|
|
|
#include "mlir/IR/Diagnostics.h"
|
2019-11-02 05:47:42 +08:00
|
|
|
#include "mlir/IR/DialectImplementation.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"
|
2020-03-19 10:51:13 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2019-02-26 05:16:24 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2021-02-15 17:39:13 +08:00
|
|
|
#include "llvm/Support/Debug.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
|
|
|
|
2021-02-15 17:39:13 +08:00
|
|
|
#define DEBUG_TYPE "dialect"
|
|
|
|
|
2018-10-22 10:49:31 +08:00
|
|
|
using namespace mlir;
|
2019-08-15 11:48:35 +08:00
|
|
|
using namespace detail;
|
|
|
|
|
2019-11-02 05:47:42 +08:00
|
|
|
DialectAsmParser::~DialectAsmParser() {}
|
|
|
|
|
2021-02-10 17:11:40 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DialectRegistry
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void DialectRegistry::addDialectInterface(
|
2021-02-15 17:39:13 +08:00
|
|
|
StringRef dialectName, TypeID interfaceTypeID,
|
2021-06-17 00:53:21 +08:00
|
|
|
DialectInterfaceAllocatorFunction allocator) {
|
2021-02-10 17:11:40 +08:00
|
|
|
assert(allocator && "unexpected null interface allocation function");
|
|
|
|
auto it = registry.find(dialectName.str());
|
|
|
|
assert(it != registry.end() &&
|
|
|
|
"adding an interface for an unregistered dialect");
|
2021-02-15 17:39:13 +08:00
|
|
|
|
|
|
|
// Bail out if the interface with the given ID is already in the registry for
|
|
|
|
// the given dialect. We expect a small number (dozens) of interfaces so a
|
|
|
|
// linear search is fine here.
|
2021-06-17 00:53:21 +08:00
|
|
|
auto &ifaces = interfaces[it->second.first];
|
|
|
|
for (const auto &kvp : ifaces.dialectInterfaces) {
|
2021-02-15 17:39:13 +08:00
|
|
|
if (kvp.first == interfaceTypeID) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "[" DEBUG_TYPE
|
|
|
|
"] repeated interface registration for dialect "
|
|
|
|
<< dialectName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 00:53:21 +08:00
|
|
|
ifaces.dialectInterfaces.emplace_back(interfaceTypeID, allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialectRegistry::addObjectInterface(
|
|
|
|
StringRef dialectName, TypeID interfaceTypeID,
|
|
|
|
ObjectInterfaceAllocatorFunction allocator) {
|
|
|
|
assert(allocator && "unexpected null interface allocation function");
|
|
|
|
|
|
|
|
// Builtin dialect has an empty prefix and is always registered.
|
|
|
|
TypeID dialectTypeID;
|
|
|
|
if (!dialectName.empty()) {
|
|
|
|
auto it = registry.find(dialectName.str());
|
|
|
|
assert(it != registry.end() &&
|
|
|
|
"adding an interface for an op from an unregistered dialect");
|
|
|
|
dialectTypeID = it->second.first;
|
|
|
|
} else {
|
|
|
|
dialectTypeID = TypeID::get<BuiltinDialect>();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &ifaces = interfaces[dialectTypeID];
|
|
|
|
for (const auto &kvp : ifaces.objectInterfaces) {
|
|
|
|
if (kvp.first == interfaceTypeID) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "[" DEBUG_TYPE
|
|
|
|
"] repeated interface object interface registration");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaces.objectInterfaces.emplace_back(interfaceTypeID, allocator);
|
2021-02-10 17:11:40 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:11:50 +08:00
|
|
|
DialectAllocatorFunctionRef
|
|
|
|
DialectRegistry::getDialectAllocator(StringRef name) const {
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
auto it = registry.find(name.str());
|
|
|
|
if (it == registry.end())
|
|
|
|
return nullptr;
|
2021-02-10 17:11:50 +08:00
|
|
|
return it->second.second;
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DialectRegistry::insert(TypeID typeID, StringRef name,
|
|
|
|
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);
|
|
|
|
}
|
2018-10-22 10:49:31 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:11:50 +08:00
|
|
|
void DialectRegistry::registerDelayedInterfaces(Dialect *dialect) const {
|
2021-02-10 17:11:40 +08:00
|
|
|
auto it = interfaces.find(dialect->getTypeID());
|
|
|
|
if (it == interfaces.end())
|
|
|
|
return;
|
|
|
|
|
2021-02-15 17:39:13 +08:00
|
|
|
// Add an interface if it is not already present.
|
2021-06-17 00:53:21 +08:00
|
|
|
for (const auto &kvp : it->getSecond().dialectInterfaces) {
|
2021-02-15 17:39:13 +08:00
|
|
|
if (dialect->getRegisteredInterface(kvp.first))
|
|
|
|
continue;
|
|
|
|
dialect->addInterface(kvp.second(dialect));
|
|
|
|
}
|
2021-06-17 00:53:21 +08:00
|
|
|
|
|
|
|
// Add attribute, operation and type interfaces.
|
|
|
|
for (const auto &kvp : it->getSecond().objectInterfaces)
|
|
|
|
kvp.second(dialect->getContext());
|
2021-02-10 17:11:40 +08:00
|
|
|
}
|
|
|
|
|
2019-08-15 11:48:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dialect
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-08-07 10:41:44 +08:00
|
|
|
Dialect::Dialect(StringRef name, MLIRContext *context, TypeID id)
|
|
|
|
: name(name), dialectID(id), context(context) {
|
2019-03-30 13:30:54 +08:00
|
|
|
assert(isValidNamespace(name) && "invalid dialect namespace");
|
2018-10-22 10:49:31 +08:00
|
|
|
}
|
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-11-02 06:39:30 +08:00
|
|
|
Attribute Dialect::parseAttribute(DialectAsmParser &parser, Type type) const {
|
|
|
|
parser.emitError(parser.getNameLoc())
|
|
|
|
<< "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-11-02 06:39:30 +08:00
|
|
|
Type Dialect::parseType(DialectAsmParser &parser) const {
|
2019-08-08 02:49:56 +08:00
|
|
|
// If this dialect allows unknown types, then represent this with OpaqueType.
|
|
|
|
if (allowsUnknownTypes()) {
|
2021-02-27 09:57:03 +08:00
|
|
|
Identifier ns = Identifier::get(getNamespace(), getContext());
|
|
|
|
return OpaqueType::get(ns, parser.getFullSymbolSpec());
|
2019-08-08 02:49:56 +08:00
|
|
|
}
|
|
|
|
|
2019-11-02 06:39:30 +08:00
|
|
|
parser.emitError(parser.getNameLoc())
|
|
|
|
<< "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
|
|
|
|
2021-03-23 08:33:03 +08:00
|
|
|
Optional<Dialect::ParseOpHook>
|
|
|
|
Dialect::getParseOperationHook(StringRef opName) const {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult Dialect::printOperation(Operation *op,
|
|
|
|
OpAsmPrinter &printer) const {
|
|
|
|
assert(op->getDialect() == this &&
|
|
|
|
"Dialect hook invoked on non-dialect owned operation");
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
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(
|
2020-04-11 14:46:52 +08:00
|
|
|
MLIRContext *ctx, TypeID interfaceKind) {
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
for (auto *dialect : ctx->getLoadedDialects()) {
|
2019-08-22 00:41:37 +08:00
|
|
|
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
|
|
|
}
|