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"
|
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"
|
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;
|
|
|
|
|
2019-11-02 05:47:42 +08:00
|
|
|
DialectAsmParser::~DialectAsmParser() {}
|
|
|
|
|
2019-08-15 11:48:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-28 10:26:27 +08:00
|
|
|
// Dialect Registration (DEPRECATED)
|
2019-08-15 11:48:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-22 10:49:31 +08:00
|
|
|
|
2020-03-19 10:51:13 +08:00
|
|
|
/// Registry for all dialect allocation functions.
|
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
|
|
|
static llvm::ManagedStatic<DialectRegistry> dialectRegistry;
|
|
|
|
DialectRegistry &mlir::getGlobalDialectRegistry() { return *dialectRegistry; }
|
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
|
|
|
|
2020-08-28 10:26:27 +08:00
|
|
|
// Note: deprecated, will be removed soon.
|
2020-09-01 06:39:39 +08:00
|
|
|
static bool isGlobalDialectRegistryEnabledFlag = false;
|
2020-08-28 10:26:27 +08:00
|
|
|
void mlir::enableGlobalDialectRegistry(bool enable) {
|
|
|
|
isGlobalDialectRegistryEnabledFlag = enable;
|
|
|
|
}
|
|
|
|
bool mlir::isGlobalDialectRegistryEnabled() {
|
|
|
|
return isGlobalDialectRegistryEnabledFlag;
|
|
|
|
}
|
|
|
|
|
2020-08-19 08:32:30 +08:00
|
|
|
void mlir::registerAllDialects(MLIRContext *context) {
|
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
|
|
|
dialectRegistry->appendTo(context->getDialectRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
Dialect *DialectRegistry::loadByName(StringRef name, MLIRContext *context) {
|
|
|
|
auto it = registry.find(name.str());
|
|
|
|
if (it == registry.end())
|
|
|
|
return nullptr;
|
|
|
|
return it->second.second(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
auto ns = Identifier::get(getNamespace(), getContext());
|
2019-11-02 05:47:42 +08:00
|
|
|
return OpaqueType::get(ns, parser.getFullSymbolSpec(), getContext());
|
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
|
|
|
|
|
|
|
/// 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
|
|
|
}
|