forked from OSchip/llvm-project
Assert that registered dialects have unique names. This creates a guarantee that the namespace of a dialect can be used a unique key.
-- PiperOrigin-RevId: 241049578
This commit is contained in:
parent
0ae68a3ccf
commit
6f24bf82f9
|
@ -127,11 +127,10 @@ public:
|
||||||
static bool isValidNamespace(StringRef str);
|
static bool isValidNamespace(StringRef str);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// Note: The namePrefix can be empty, but it must not contain '.' characters.
|
/// Note: The namePrefix must not contain '.' characters.
|
||||||
/// Note: If the name is non empty, then all operations belonging to this
|
/// Note: All operations belonging to this dialect will need to have names
|
||||||
/// dialect will need to start with the namePrefix followed by a '.'.
|
/// starting with the namePrefix followed by '.'.
|
||||||
/// Example:
|
/// Example:
|
||||||
/// - "" for the standard operation set.
|
|
||||||
/// - "tf" for the TensorFlow ops like "tf.add".
|
/// - "tf" for the TensorFlow ops like "tf.add".
|
||||||
Dialect(StringRef namePrefix, MLIRContext *context);
|
Dialect(StringRef namePrefix, MLIRContext *context);
|
||||||
|
|
||||||
|
|
|
@ -704,6 +704,11 @@ void Dialect::registerDialect(MLIRContext *context) {
|
||||||
|
|
||||||
// Lock access to the context registry.
|
// Lock access to the context registry.
|
||||||
llvm::sys::SmartScopedWriter<true> registryLock(impl.contextMutex);
|
llvm::sys::SmartScopedWriter<true> registryLock(impl.contextMutex);
|
||||||
|
assert(llvm::none_of(impl.dialects,
|
||||||
|
[this](std::unique_ptr<Dialect> &dialect) {
|
||||||
|
return dialect->getNamespace() == getNamespace();
|
||||||
|
}) &&
|
||||||
|
"a dialect with the given namespace has already been registered");
|
||||||
impl.dialects.push_back(std::unique_ptr<Dialect>(this));
|
impl.dialects.push_back(std::unique_ptr<Dialect>(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -737,7 +742,7 @@ std::vector<AbstractOperation *> MLIRContext::getRegisteredOperations() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dialect::addOperation(AbstractOperation opInfo) {
|
void Dialect::addOperation(AbstractOperation opInfo) {
|
||||||
assert((namePrefix.empty() || (opInfo.name.split('.').first == namePrefix)) &&
|
assert(opInfo.name.split('.').first == namePrefix &&
|
||||||
"op name doesn't start with dialect prefix");
|
"op name doesn't start with dialect prefix");
|
||||||
assert(&opInfo.dialect == this && "Dialect object mismatch");
|
assert(&opInfo.dialect == this && "Dialect object mismatch");
|
||||||
auto &impl = context->getImpl();
|
auto &impl = context->getImpl();
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
//===- DialectTest.cpp - Dialect unit tests -------------------------------===//
|
||||||
|
//
|
||||||
|
// 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"
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
using namespace mlir;
|
||||||
|
using namespace mlir::detail;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
struct TestDialect : public Dialect {
|
||||||
|
TestDialect(MLIRContext *context) : Dialect(/*namePrefix=*/"test", context) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST(DialectDeathTest, MultipleDialectsWithSameNamespace) {
|
||||||
|
MLIRContext context;
|
||||||
|
|
||||||
|
// Registering a dialect with the same namespace twice should result in a
|
||||||
|
// failure.
|
||||||
|
new TestDialect(&context);
|
||||||
|
ASSERT_DEATH(new TestDialect(&context), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end namespace
|
Loading…
Reference in New Issue