150
|
1 //===- StructsGenTest.cpp - TableGen StructsGen 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 #include "mlir/IR/Attributes.h"
|
221
|
10 #include "mlir/IR/Builders.h"
|
|
11 #include "mlir/IR/BuiltinTypes.h"
|
150
|
12 #include "mlir/IR/Identifier.h"
|
|
13 #include "llvm/ADT/DenseMap.h"
|
|
14 #include "llvm/ADT/Optional.h"
|
|
15 #include "llvm/ADT/StringSwitch.h"
|
|
16 #include "gmock/gmock.h"
|
|
17 #include <type_traits>
|
|
18
|
|
19 namespace mlir {
|
|
20
|
221
|
21 /// Pull in generated enum utility declarations and definitions.
|
150
|
22 #include "StructAttrGenTest.h.inc"
|
|
23 #include "StructAttrGenTest.cpp.inc"
|
221
|
24
|
|
25 /// Helper that returns an example test::TestStruct for testing its
|
|
26 /// implementation.
|
150
|
27 static test::TestStruct getTestStruct(mlir::MLIRContext *context) {
|
221
|
28 auto integerType = mlir::IntegerType::get(context, 32);
|
150
|
29 auto integerAttr = mlir::IntegerAttr::get(integerType, 127);
|
|
30
|
|
31 auto floatType = mlir::FloatType::getF32(context);
|
|
32 auto floatAttr = mlir::FloatAttr::get(floatType, 0.25);
|
|
33
|
|
34 auto elementsType = mlir::RankedTensorType::get({2, 3}, integerType);
|
|
35 auto elementsAttr =
|
|
36 mlir::DenseIntElementsAttr::get(elementsType, {1, 2, 3, 4, 5, 6});
|
173
|
37 auto optionalAttr = nullptr;
|
221
|
38 auto defaultValuedAttr = nullptr;
|
150
|
39
|
173
|
40 return test::TestStruct::get(integerAttr, floatAttr, elementsAttr,
|
221
|
41 optionalAttr, defaultValuedAttr, context);
|
150
|
42 }
|
|
43
|
221
|
44 /// Validates that test::TestStruct::classof correctly identifies a valid
|
|
45 /// test::TestStruct.
|
150
|
46 TEST(StructsGenTest, ClassofTrue) {
|
|
47 mlir::MLIRContext context;
|
|
48 auto structAttr = getTestStruct(&context);
|
|
49 ASSERT_TRUE(test::TestStruct::classof(structAttr));
|
|
50 }
|
|
51
|
221
|
52 /// Validates that test::TestStruct::classof fails when an extra attribute is in
|
|
53 /// the class.
|
150
|
54 TEST(StructsGenTest, ClassofExtraFalse) {
|
|
55 mlir::MLIRContext context;
|
|
56 mlir::DictionaryAttr structAttr = getTestStruct(&context);
|
|
57 auto expectedValues = structAttr.getValue();
|
|
58 ASSERT_EQ(expectedValues.size(), 3u);
|
|
59
|
|
60 // Copy the set of named attributes.
|
|
61 llvm::SmallVector<mlir::NamedAttribute, 5> newValues(expectedValues.begin(),
|
|
62 expectedValues.end());
|
|
63
|
|
64 // Add an extra NamedAttribute.
|
|
65 auto wrongId = mlir::Identifier::get("wrong", &context);
|
|
66 auto wrongAttr = mlir::NamedAttribute(wrongId, expectedValues[0].second);
|
|
67 newValues.push_back(wrongAttr);
|
|
68
|
|
69 // Make a new DictionaryAttr and validate.
|
221
|
70 auto badDictionary = mlir::DictionaryAttr::get(&context, newValues);
|
150
|
71 ASSERT_FALSE(test::TestStruct::classof(badDictionary));
|
|
72 }
|
|
73
|
221
|
74 /// Validates that test::TestStruct::classof fails when a NamedAttribute has an
|
|
75 /// incorrect name.
|
150
|
76 TEST(StructsGenTest, ClassofBadNameFalse) {
|
|
77 mlir::MLIRContext context;
|
|
78 mlir::DictionaryAttr structAttr = getTestStruct(&context);
|
|
79 auto expectedValues = structAttr.getValue();
|
|
80 ASSERT_EQ(expectedValues.size(), 3u);
|
|
81
|
|
82 // Create a copy of all but the first NamedAttributes.
|
|
83 llvm::SmallVector<mlir::NamedAttribute, 4> newValues(
|
|
84 expectedValues.begin() + 1, expectedValues.end());
|
|
85
|
|
86 // Add a copy of the first attribute with the wrong Identifier.
|
|
87 auto wrongId = mlir::Identifier::get("wrong", &context);
|
|
88 auto wrongAttr = mlir::NamedAttribute(wrongId, expectedValues[0].second);
|
|
89 newValues.push_back(wrongAttr);
|
|
90
|
221
|
91 auto badDictionary = mlir::DictionaryAttr::get(&context, newValues);
|
150
|
92 ASSERT_FALSE(test::TestStruct::classof(badDictionary));
|
|
93 }
|
|
94
|
221
|
95 /// Validates that test::TestStruct::classof fails when a NamedAttribute has an
|
|
96 /// incorrect type.
|
150
|
97 TEST(StructsGenTest, ClassofBadTypeFalse) {
|
|
98 mlir::MLIRContext context;
|
|
99 mlir::DictionaryAttr structAttr = getTestStruct(&context);
|
|
100 auto expectedValues = structAttr.getValue();
|
|
101 ASSERT_EQ(expectedValues.size(), 3u);
|
|
102
|
|
103 // Create a copy of all but the last NamedAttributes.
|
|
104 llvm::SmallVector<mlir::NamedAttribute, 4> newValues(
|
|
105 expectedValues.begin(), expectedValues.end() - 1);
|
|
106
|
|
107 // Add a copy of the last attribute with the wrong type.
|
221
|
108 auto i64Type = mlir::IntegerType::get(&context, 64);
|
150
|
109 auto elementsType = mlir::RankedTensorType::get({3}, i64Type);
|
|
110 auto elementsAttr =
|
|
111 mlir::DenseIntElementsAttr::get(elementsType, ArrayRef<int64_t>{1, 2, 3});
|
|
112 mlir::Identifier id = expectedValues.back().first;
|
|
113 auto wrongAttr = mlir::NamedAttribute(id, elementsAttr);
|
|
114 newValues.push_back(wrongAttr);
|
|
115
|
221
|
116 auto badDictionary = mlir::DictionaryAttr::get(&context, newValues);
|
150
|
117 ASSERT_FALSE(test::TestStruct::classof(badDictionary));
|
|
118 }
|
|
119
|
221
|
120 /// Validates that test::TestStruct::classof fails when a NamedAttribute is
|
|
121 /// missing.
|
150
|
122 TEST(StructsGenTest, ClassofMissingFalse) {
|
|
123 mlir::MLIRContext context;
|
|
124 mlir::DictionaryAttr structAttr = getTestStruct(&context);
|
|
125 auto expectedValues = structAttr.getValue();
|
|
126 ASSERT_EQ(expectedValues.size(), 3u);
|
|
127
|
|
128 // Copy a subset of the structures Named Attributes.
|
|
129 llvm::SmallVector<mlir::NamedAttribute, 3> newValues(
|
|
130 expectedValues.begin() + 1, expectedValues.end());
|
|
131
|
|
132 // Make a new DictionaryAttr and validate it is not a validate TestStruct.
|
221
|
133 auto badDictionary = mlir::DictionaryAttr::get(&context, newValues);
|
150
|
134 ASSERT_FALSE(test::TestStruct::classof(badDictionary));
|
|
135 }
|
|
136
|
221
|
137 /// Validate the accessor for the FloatAttr value.
|
150
|
138 TEST(StructsGenTest, GetFloat) {
|
|
139 mlir::MLIRContext context;
|
|
140 auto structAttr = getTestStruct(&context);
|
|
141 auto returnedAttr = structAttr.sample_float();
|
|
142 EXPECT_EQ(returnedAttr.getValueAsDouble(), 0.25);
|
|
143 }
|
|
144
|
221
|
145 /// Validate the accessor for the IntegerAttr value.
|
150
|
146 TEST(StructsGenTest, GetInteger) {
|
|
147 mlir::MLIRContext context;
|
|
148 auto structAttr = getTestStruct(&context);
|
|
149 auto returnedAttr = structAttr.sample_integer();
|
|
150 EXPECT_EQ(returnedAttr.getInt(), 127);
|
|
151 }
|
|
152
|
221
|
153 /// Validate the accessor for the ElementsAttr value.
|
150
|
154 TEST(StructsGenTest, GetElements) {
|
|
155 mlir::MLIRContext context;
|
|
156 auto structAttr = getTestStruct(&context);
|
|
157 auto returnedAttr = structAttr.sample_elements();
|
|
158 auto denseAttr = returnedAttr.dyn_cast<mlir::DenseElementsAttr>();
|
|
159 ASSERT_TRUE(denseAttr);
|
|
160
|
|
161 for (const auto &valIndexIt : llvm::enumerate(denseAttr.getIntValues())) {
|
|
162 EXPECT_EQ(valIndexIt.value(), valIndexIt.index() + 1);
|
|
163 }
|
|
164 }
|
|
165
|
173
|
166 TEST(StructsGenTest, EmptyOptional) {
|
|
167 mlir::MLIRContext context;
|
|
168 auto structAttr = getTestStruct(&context);
|
|
169 EXPECT_EQ(structAttr.sample_optional_integer(), nullptr);
|
|
170 }
|
|
171
|
221
|
172 TEST(StructsGenTest, GetDefaultValuedAttr) {
|
|
173 mlir::MLIRContext context;
|
|
174 mlir::Builder builder(&context);
|
|
175 auto structAttr = getTestStruct(&context);
|
|
176 EXPECT_EQ(structAttr.sample_default_valued_integer(),
|
|
177 builder.getI32IntegerAttr(42));
|
|
178 }
|
|
179
|
150
|
180 } // namespace mlir
|