2019-03-30 05:18:08 +08:00
|
|
|
//===- DialectTest.cpp - Dialect unit tests -------------------------------===//
|
|
|
|
//
|
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
|
2019-03-30 05:18:08 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-03-30 05:18:08 +08:00
|
|
|
|
|
|
|
#include "mlir/IR/Dialect.h"
|
2021-02-10 17:11:40 +08:00
|
|
|
#include "mlir/IR/DialectInterface.h"
|
2019-03-30 05:18:08 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::detail;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct TestDialect : public Dialect {
|
2020-08-07 10:41:44 +08:00
|
|
|
static StringRef getDialectNamespace() { return "test"; };
|
|
|
|
TestDialect(MLIRContext *context)
|
|
|
|
: Dialect(getDialectNamespace(), context, TypeID::get<TestDialect>()) {}
|
|
|
|
};
|
|
|
|
struct AnotherTestDialect : public Dialect {
|
|
|
|
static StringRef getDialectNamespace() { return "test"; };
|
|
|
|
AnotherTestDialect(MLIRContext *context)
|
|
|
|
: Dialect(getDialectNamespace(), context,
|
|
|
|
TypeID::get<AnotherTestDialect>()) {}
|
2019-03-30 05:18:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(DialectDeathTest, MultipleDialectsWithSameNamespace) {
|
2020-10-24 04:19:35 +08:00
|
|
|
MLIRContext context;
|
2019-03-30 05:18:08 +08:00
|
|
|
|
|
|
|
// Registering a dialect with the same namespace twice should result in a
|
|
|
|
// failure.
|
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
|
|
|
context.loadDialect<TestDialect>();
|
|
|
|
ASSERT_DEATH(context.loadDialect<AnotherTestDialect>(), "");
|
2019-03-30 05:18:08 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:11:40 +08:00
|
|
|
struct SecondTestDialect : public Dialect {
|
|
|
|
static StringRef getDialectNamespace() { return "test2"; }
|
|
|
|
SecondTestDialect(MLIRContext *context)
|
|
|
|
: Dialect(getDialectNamespace(), context,
|
|
|
|
TypeID::get<SecondTestDialect>()) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestDialectInterfaceBase
|
|
|
|
: public DialectInterface::Base<TestDialectInterfaceBase> {
|
|
|
|
TestDialectInterfaceBase(Dialect *dialect) : Base(dialect) {}
|
|
|
|
virtual int function() const { return 42; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestDialectInterface : public TestDialectInterfaceBase {
|
|
|
|
using TestDialectInterfaceBase::TestDialectInterfaceBase;
|
|
|
|
int function() const final { return 56; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SecondTestDialectInterface : public TestDialectInterfaceBase {
|
|
|
|
using TestDialectInterfaceBase::TestDialectInterfaceBase;
|
|
|
|
int function() const final { return 78; }
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Dialect, DelayedInterfaceRegistration) {
|
|
|
|
DialectRegistry registry;
|
|
|
|
registry.insert<TestDialect, SecondTestDialect>();
|
|
|
|
|
|
|
|
// Delayed registration of an interface for TestDialect.
|
|
|
|
registry.addDialectInterface<TestDialect, TestDialectInterface>();
|
|
|
|
|
2021-02-10 17:11:50 +08:00
|
|
|
MLIRContext context(registry);
|
2021-02-10 17:11:40 +08:00
|
|
|
|
|
|
|
// Load the TestDialect and check that the interface got registered for it.
|
|
|
|
auto *testDialect = context.getOrLoadDialect<TestDialect>();
|
|
|
|
ASSERT_TRUE(testDialect != nullptr);
|
|
|
|
auto *testDialectInterface =
|
|
|
|
testDialect->getRegisteredInterface<TestDialectInterfaceBase>();
|
|
|
|
EXPECT_TRUE(testDialectInterface != nullptr);
|
|
|
|
|
|
|
|
// Load the SecondTestDialect and check that the interface is not registered
|
|
|
|
// for it.
|
|
|
|
auto *secondTestDialect = context.getOrLoadDialect<SecondTestDialect>();
|
|
|
|
ASSERT_TRUE(secondTestDialect != nullptr);
|
|
|
|
auto *secondTestDialectInterface =
|
|
|
|
secondTestDialect->getRegisteredInterface<SecondTestDialectInterface>();
|
|
|
|
EXPECT_TRUE(secondTestDialectInterface == nullptr);
|
|
|
|
|
|
|
|
// Use the same mechanism as for delayed registration but for an already
|
|
|
|
// loaded dialect and check that the interface is now registered.
|
2021-02-10 17:11:50 +08:00
|
|
|
DialectRegistry secondRegistry;
|
|
|
|
secondRegistry.insert<SecondTestDialect>();
|
|
|
|
secondRegistry
|
2021-02-10 17:11:40 +08:00
|
|
|
.addDialectInterface<SecondTestDialect, SecondTestDialectInterface>();
|
2021-02-10 17:11:50 +08:00
|
|
|
context.appendDialectRegistry(secondRegistry);
|
2021-02-10 17:11:40 +08:00
|
|
|
secondTestDialectInterface =
|
|
|
|
secondTestDialect->getRegisteredInterface<SecondTestDialectInterface>();
|
|
|
|
EXPECT_TRUE(secondTestDialectInterface != nullptr);
|
|
|
|
}
|
|
|
|
|
2021-02-15 17:39:13 +08:00
|
|
|
TEST(Dialect, RepeatedDelayedRegistration) {
|
|
|
|
// Set up the delayed registration.
|
|
|
|
DialectRegistry registry;
|
|
|
|
registry.insert<TestDialect>();
|
|
|
|
registry.addDialectInterface<TestDialect, TestDialectInterface>();
|
|
|
|
MLIRContext context(registry);
|
|
|
|
|
|
|
|
// Load the TestDialect and check that the interface got registered for it.
|
|
|
|
auto *testDialect = context.getOrLoadDialect<TestDialect>();
|
|
|
|
ASSERT_TRUE(testDialect != nullptr);
|
|
|
|
auto *testDialectInterface =
|
|
|
|
testDialect->getRegisteredInterface<TestDialectInterfaceBase>();
|
|
|
|
EXPECT_TRUE(testDialectInterface != nullptr);
|
|
|
|
|
|
|
|
// Try adding the same dialect interface again and check that we don't crash
|
|
|
|
// on repeated interface registration.
|
|
|
|
DialectRegistry secondRegistry;
|
|
|
|
secondRegistry.insert<TestDialect>();
|
|
|
|
secondRegistry.addDialectInterface<TestDialect, TestDialectInterface>();
|
|
|
|
context.appendDialectRegistry(secondRegistry);
|
|
|
|
testDialectInterface =
|
|
|
|
testDialect->getRegisteredInterface<TestDialectInterfaceBase>();
|
|
|
|
EXPECT_TRUE(testDialectInterface != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A dialect that registers two interfaces with the same InterfaceID, triggering
|
|
|
|
// an assertion failure.
|
|
|
|
struct RepeatedRegistrationDialect : public Dialect {
|
|
|
|
static StringRef getDialectNamespace() { return "repeatedreg"; }
|
|
|
|
RepeatedRegistrationDialect(MLIRContext *context)
|
|
|
|
: Dialect(getDialectNamespace(), context,
|
|
|
|
TypeID::get<RepeatedRegistrationDialect>()) {
|
|
|
|
addInterfaces<TestDialectInterface>();
|
|
|
|
addInterfaces<SecondTestDialectInterface>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Dialect, RepeatedInterfaceRegistrationDeath) {
|
|
|
|
MLIRContext context;
|
|
|
|
(void)context;
|
|
|
|
|
|
|
|
// This triggers an assertion in debug mode.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
ASSERT_DEATH(context.loadDialect<RepeatedRegistrationDialect>(),
|
|
|
|
"interface kind has already been registered");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-08 02:27:58 +08:00
|
|
|
} // namespace
|