StructsGenTest.cpp
6.27 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//===- StructsGenTest.cpp - TableGen StructsGen 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
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Identifier.h"
#include "mlir/IR/StandardTypes.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringSwitch.h"
#include "gmock/gmock.h"
#include <type_traits>
namespace mlir {
/// Pull in generated enum utility declarations and definitions.
#include "StructAttrGenTest.h.inc"
#include "StructAttrGenTest.cpp.inc"
/// Helper that returns an example test::TestStruct for testing its
/// implementation.
static test::TestStruct getTestStruct(mlir::MLIRContext *context) {
auto integerType = mlir::IntegerType::get(32, context);
auto integerAttr = mlir::IntegerAttr::get(integerType, 127);
auto floatType = mlir::FloatType::getF32(context);
auto floatAttr = mlir::FloatAttr::get(floatType, 0.25);
auto elementsType = mlir::RankedTensorType::get({2, 3}, integerType);
auto elementsAttr =
mlir::DenseIntElementsAttr::get(elementsType, {1, 2, 3, 4, 5, 6});
auto optionalAttr = nullptr;
return test::TestStruct::get(integerAttr, floatAttr, elementsAttr,
optionalAttr, context);
}
/// Validates that test::TestStruct::classof correctly identifies a valid
/// test::TestStruct.
TEST(StructsGenTest, ClassofTrue) {
mlir::MLIRContext context;
auto structAttr = getTestStruct(&context);
ASSERT_TRUE(test::TestStruct::classof(structAttr));
}
/// Validates that test::TestStruct::classof fails when an extra attribute is in
/// the class.
TEST(StructsGenTest, ClassofExtraFalse) {
mlir::MLIRContext context;
mlir::DictionaryAttr structAttr = getTestStruct(&context);
auto expectedValues = structAttr.getValue();
ASSERT_EQ(expectedValues.size(), 3u);
// Copy the set of named attributes.
llvm::SmallVector<mlir::NamedAttribute, 5> newValues(expectedValues.begin(),
expectedValues.end());
// Add an extra NamedAttribute.
auto wrongId = mlir::Identifier::get("wrong", &context);
auto wrongAttr = mlir::NamedAttribute(wrongId, expectedValues[0].second);
newValues.push_back(wrongAttr);
// Make a new DictionaryAttr and validate.
auto badDictionary = mlir::DictionaryAttr::get(newValues, &context);
ASSERT_FALSE(test::TestStruct::classof(badDictionary));
}
/// Validates that test::TestStruct::classof fails when a NamedAttribute has an
/// incorrect name.
TEST(StructsGenTest, ClassofBadNameFalse) {
mlir::MLIRContext context;
mlir::DictionaryAttr structAttr = getTestStruct(&context);
auto expectedValues = structAttr.getValue();
ASSERT_EQ(expectedValues.size(), 3u);
// Create a copy of all but the first NamedAttributes.
llvm::SmallVector<mlir::NamedAttribute, 4> newValues(
expectedValues.begin() + 1, expectedValues.end());
// Add a copy of the first attribute with the wrong Identifier.
auto wrongId = mlir::Identifier::get("wrong", &context);
auto wrongAttr = mlir::NamedAttribute(wrongId, expectedValues[0].second);
newValues.push_back(wrongAttr);
auto badDictionary = mlir::DictionaryAttr::get(newValues, &context);
ASSERT_FALSE(test::TestStruct::classof(badDictionary));
}
/// Validates that test::TestStruct::classof fails when a NamedAttribute has an
/// incorrect type.
TEST(StructsGenTest, ClassofBadTypeFalse) {
mlir::MLIRContext context;
mlir::DictionaryAttr structAttr = getTestStruct(&context);
auto expectedValues = structAttr.getValue();
ASSERT_EQ(expectedValues.size(), 3u);
// Create a copy of all but the last NamedAttributes.
llvm::SmallVector<mlir::NamedAttribute, 4> newValues(
expectedValues.begin(), expectedValues.end() - 1);
// Add a copy of the last attribute with the wrong type.
auto i64Type = mlir::IntegerType::get(64, &context);
auto elementsType = mlir::RankedTensorType::get({3}, i64Type);
auto elementsAttr =
mlir::DenseIntElementsAttr::get(elementsType, ArrayRef<int64_t>{1, 2, 3});
mlir::Identifier id = expectedValues.back().first;
auto wrongAttr = mlir::NamedAttribute(id, elementsAttr);
newValues.push_back(wrongAttr);
auto badDictionary = mlir::DictionaryAttr::get(newValues, &context);
ASSERT_FALSE(test::TestStruct::classof(badDictionary));
}
/// Validates that test::TestStruct::classof fails when a NamedAttribute is
/// missing.
TEST(StructsGenTest, ClassofMissingFalse) {
mlir::MLIRContext context;
mlir::DictionaryAttr structAttr = getTestStruct(&context);
auto expectedValues = structAttr.getValue();
ASSERT_EQ(expectedValues.size(), 3u);
// Copy a subset of the structures Named Attributes.
llvm::SmallVector<mlir::NamedAttribute, 3> newValues(
expectedValues.begin() + 1, expectedValues.end());
// Make a new DictionaryAttr and validate it is not a validate TestStruct.
auto badDictionary = mlir::DictionaryAttr::get(newValues, &context);
ASSERT_FALSE(test::TestStruct::classof(badDictionary));
}
/// Validate the accessor for the FloatAttr value.
TEST(StructsGenTest, GetFloat) {
mlir::MLIRContext context;
auto structAttr = getTestStruct(&context);
auto returnedAttr = structAttr.sample_float();
EXPECT_EQ(returnedAttr.getValueAsDouble(), 0.25);
}
/// Validate the accessor for the IntegerAttr value.
TEST(StructsGenTest, GetInteger) {
mlir::MLIRContext context;
auto structAttr = getTestStruct(&context);
auto returnedAttr = structAttr.sample_integer();
EXPECT_EQ(returnedAttr.getInt(), 127);
}
/// Validate the accessor for the ElementsAttr value.
TEST(StructsGenTest, GetElements) {
mlir::MLIRContext context;
auto structAttr = getTestStruct(&context);
auto returnedAttr = structAttr.sample_elements();
auto denseAttr = returnedAttr.dyn_cast<mlir::DenseElementsAttr>();
ASSERT_TRUE(denseAttr);
for (const auto &valIndexIt : llvm::enumerate(denseAttr.getIntValues())) {
EXPECT_EQ(valIndexIt.value(), valIndexIt.index() + 1);
}
}
TEST(StructsGenTest, EmptyOptional) {
mlir::MLIRContext context;
auto structAttr = getTestStruct(&context);
EXPECT_EQ(structAttr.sample_optional_integer(), nullptr);
}
} // namespace mlir