| 1 | //===- TypeAttrNamesTest.cpp - Type API unit tests ------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file test the lookup of AbstractType / AbstractAttribute by name. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/IR/Attributes.h" |
| 14 | #include "mlir/IR/BuiltinTypes.h" |
| 15 | #include "mlir/IR/Dialect.h" |
| 16 | #include "mlir/IR/Types.h" |
| 17 | #include "mlir/IR/Value.h" |
| 18 | #include "mlir/Support/TypeID.h" |
| 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | using namespace mlir; |
| 22 | |
| 23 | namespace { |
| 24 | struct : Type::TypeBase<FooType, Type, TypeStorage> { |
| 25 | using Base::Base; |
| 26 | |
| 27 | static constexpr StringLiteral = "fake.foo" ; |
| 28 | |
| 29 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(FooType) |
| 30 | }; |
| 31 | |
| 32 | struct BarAttr : Attribute::AttrBase<BarAttr, Attribute, AttributeStorage> { |
| 33 | using Base::Base; |
| 34 | |
| 35 | static constexpr StringLiteral name = "fake.bar" ; |
| 36 | |
| 37 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(BarAttr) |
| 38 | }; |
| 39 | |
| 40 | struct FakeDialect : Dialect { |
| 41 | FakeDialect(MLIRContext *context) |
| 42 | : Dialect(getDialectNamespace(), context, TypeID::get<FakeDialect>()) { |
| 43 | addTypes<FooType>(); |
| 44 | addAttributes<BarAttr>(); |
| 45 | } |
| 46 | |
| 47 | static constexpr ::llvm::StringLiteral getDialectNamespace() { |
| 48 | return ::llvm::StringLiteral("fake" ); |
| 49 | } |
| 50 | |
| 51 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(FakeDialect) |
| 52 | }; |
| 53 | } // namespace |
| 54 | |
| 55 | TEST(AbstractType, LookupWithString) { |
| 56 | MLIRContext ctx; |
| 57 | ctx.loadDialect<FakeDialect>(); |
| 58 | |
| 59 | // Check that we can lookup an abstract type by name. |
| 60 | auto fooAbstractType = AbstractType::lookup(name: "fake.foo" , context: &ctx); |
| 61 | EXPECT_TRUE(fooAbstractType.has_value()); |
| 62 | EXPECT_TRUE(fooAbstractType->get().getName() == "fake.foo" ); |
| 63 | |
| 64 | // Check that the abstract type is the same as the one used by the type. |
| 65 | auto = FooType::get(ctx: &ctx); |
| 66 | EXPECT_TRUE(&fooType.getAbstractType() == &fooAbstractType->get()); |
| 67 | |
| 68 | // Check that lookups of non-existing types returns nullopt. |
| 69 | // Even if an attribute with the same name exists. |
| 70 | EXPECT_FALSE(AbstractType::lookup("fake.bar" , &ctx).has_value()); |
| 71 | } |
| 72 | |
| 73 | TEST(AbstractAttribute, LookupWithString) { |
| 74 | MLIRContext ctx; |
| 75 | ctx.loadDialect<FakeDialect>(); |
| 76 | |
| 77 | // Check that we can lookup an abstract type by name. |
| 78 | auto barAbstractAttr = AbstractAttribute::lookup(name: "fake.bar" , context: &ctx); |
| 79 | EXPECT_TRUE(barAbstractAttr.has_value()); |
| 80 | EXPECT_TRUE(barAbstractAttr->get().getName() == "fake.bar" ); |
| 81 | |
| 82 | // Check that the abstract Attribute is the same as the one used by the |
| 83 | // Attribute. |
| 84 | auto barAttr = BarAttr::get(ctx: &ctx); |
| 85 | EXPECT_TRUE(&barAttr.getAbstractAttribute() == &barAbstractAttr->get()); |
| 86 | |
| 87 | // Check that lookups of non-existing Attributes returns nullopt. |
| 88 | // Even if an attribute with the same name exists. |
| 89 | EXPECT_FALSE(AbstractAttribute::lookup("fake.foo" , &ctx).has_value()); |
| 90 | } |
| 91 | |