comparison mlir/unittests/TableGen/StructsGenTest.cpp @ 221:79ff65ed7e25

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