File: TypeAttrNamesTest.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (90 lines) | stat: -rw-r--r-- 3,000 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//===- TypeAttrNamesTest.cpp - Type API unit tests ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file test the lookup of AbstractType / AbstractAttribute by name.
//
//===----------------------------------------------------------------------===//

#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Types.h"
#include "mlir/IR/Value.h"
#include "mlir/Support/TypeID.h"
#include "gtest/gtest.h"

using namespace mlir;

namespace {
struct FooType : Type::TypeBase<FooType, Type, TypeStorage> {
  using Base::Base;

  static constexpr StringLiteral name = "fake.foo";

  MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(FooType)
};

struct BarAttr : Attribute::AttrBase<BarAttr, Attribute, AttributeStorage> {
  using Base::Base;

  static constexpr StringLiteral name = "fake.bar";

  MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(BarAttr)
};

struct FakeDialect : Dialect {
  FakeDialect(MLIRContext *context)
      : Dialect(getDialectNamespace(), context, TypeID::get<FakeDialect>()) {
    addTypes<FooType>();
    addAttributes<BarAttr>();
  }

  static constexpr ::llvm::StringLiteral getDialectNamespace() {
    return ::llvm::StringLiteral("fake");
  }

  MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(FakeDialect)
};
} // namespace

TEST(AbstractType, LookupWithString) {
  MLIRContext ctx;
  ctx.loadDialect<FakeDialect>();

  // Check that we can lookup an abstract type by name.
  auto fooAbstractType = AbstractType::lookup("fake.foo", &ctx);
  EXPECT_TRUE(fooAbstractType.has_value());
  EXPECT_TRUE(fooAbstractType->get().getName() == "fake.foo");

  // Check that the abstract type is the same as the one used by the type.
  auto fooType = FooType::get(&ctx);
  EXPECT_TRUE(&fooType.getAbstractType() == &fooAbstractType->get());

  // Check that lookups of non-existing types returns nullopt.
  // Even if an attribute with the same name exists.
  EXPECT_FALSE(AbstractType::lookup("fake.bar", &ctx).has_value());
}

TEST(AbstractAttribute, LookupWithString) {
  MLIRContext ctx;
  ctx.loadDialect<FakeDialect>();

  // Check that we can lookup an abstract type by name.
  auto barAbstractAttr = AbstractAttribute::lookup("fake.bar", &ctx);
  EXPECT_TRUE(barAbstractAttr.has_value());
  EXPECT_TRUE(barAbstractAttr->get().getName() == "fake.bar");

  // Check that the abstract Attribute is the same as the one used by the
  // Attribute.
  auto barAttr = BarAttr::get(&ctx);
  EXPECT_TRUE(&barAttr.getAbstractAttribute() == &barAbstractAttr->get());

  // Check that lookups of non-existing Attributes returns nullopt.
  // Even if an attribute with the same name exists.
  EXPECT_FALSE(AbstractAttribute::lookup("fake.foo", &ctx).has_value());
}