StructsGen.cpp
9.68 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//===- StructsGen.cpp - MLIR struct utility generator ---------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// StructsGen generates common utility functions for grouping attributes into a
// set of structured data.
//
//===----------------------------------------------------------------------===//
#include "mlir/TableGen/Attribute.h"
#include "mlir/TableGen/Format.h"
#include "mlir/TableGen/GenInfo.h"
#include "mlir/TableGen/Operator.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenBackend.h"
using llvm::raw_ostream;
using llvm::Record;
using llvm::RecordKeeper;
using llvm::StringRef;
using mlir::tblgen::FmtContext;
using mlir::tblgen::StructAttr;
static void
emitStructClass(const Record &structDef, StringRef structName,
llvm::ArrayRef<mlir::tblgen::StructFieldAttr> fields,
StringRef description, raw_ostream &os) {
const char *structInfo = R"(
// {0}
class {1} : public ::mlir::DictionaryAttr)";
const char *structInfoEnd = R"( {
public:
using ::mlir::DictionaryAttr::DictionaryAttr;
static bool classof(::mlir::Attribute attr);
)";
os << formatv(structInfo, description, structName) << structInfoEnd;
// Declares a constructor function for the tablegen structure.
// TblgenStruct::get(MLIRContext context, Type1 Field1, Type2 Field2, ...);
const char *getInfoDecl = " static {0} get(\n";
const char *getInfoDeclArg = " {0} {1},\n";
const char *getInfoDeclEnd = " ::mlir::MLIRContext* context);\n\n";
os << llvm::formatv(getInfoDecl, structName);
for (auto field : fields) {
auto name = field.getName();
auto type = field.getType();
auto storage = type.getStorageType();
os << llvm::formatv(getInfoDeclArg, storage, name);
}
os << getInfoDeclEnd;
// Declares an accessor for the fields owned by the tablegen structure.
// namespace::storage TblgenStruct::field1() const;
const char *fieldInfo = R"( {0} {1}() const;
)";
for (auto field : fields) {
auto name = field.getName();
auto type = field.getType();
auto storage = type.getStorageType();
os << formatv(fieldInfo, storage, name);
}
os << "};\n\n";
}
static void emitStructDecl(const Record &structDef, raw_ostream &os) {
StructAttr structAttr(&structDef);
StringRef structName = structAttr.getStructClassName();
StringRef cppNamespace = structAttr.getCppNamespace();
StringRef description = structAttr.getDescription();
auto fields = structAttr.getAllFields();
// Wrap in the appropriate namespace.
llvm::SmallVector<StringRef, 2> namespaces;
llvm::SplitString(cppNamespace, namespaces, "::");
for (auto ns : namespaces)
os << "namespace " << ns << " {\n";
// Emit the struct class definition
emitStructClass(structDef, structName, fields, description, os);
// Close the declared namespace.
for (auto ns : namespaces)
os << "} // namespace " << ns << "\n";
}
static bool emitStructDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
llvm::emitSourceFileHeader("Struct Utility Declarations", os);
auto defs = recordKeeper.getAllDerivedDefinitions("StructAttr");
for (const auto *def : defs) {
emitStructDecl(*def, os);
}
return false;
}
static void emitFactoryDef(llvm::StringRef structName,
llvm::ArrayRef<mlir::tblgen::StructFieldAttr> fields,
raw_ostream &os) {
const char *getInfoDecl = "{0} {0}::get(\n";
const char *getInfoDeclArg = " {0} {1},\n";
const char *getInfoDeclEnd = " ::mlir::MLIRContext* context) {";
os << llvm::formatv(getInfoDecl, structName);
for (auto field : fields) {
auto name = field.getName();
auto type = field.getType();
auto storage = type.getStorageType();
os << llvm::formatv(getInfoDeclArg, storage, name);
}
os << getInfoDeclEnd;
const char *fieldStart = R"(
::llvm::SmallVector<::mlir::NamedAttribute, {0}> fields;
)";
os << llvm::formatv(fieldStart, fields.size());
const char *getFieldInfo = R"(
assert({0});
auto {0}_id = ::mlir::Identifier::get("{0}", context);
fields.emplace_back({0}_id, {0});
)";
const char *getFieldInfoOptional = R"(
if ({0}) {
auto {0}_id = ::mlir::Identifier::get("{0}", context);
fields.emplace_back({0}_id, {0});
}
)";
for (auto field : fields) {
if (field.getType().isOptional() || field.getType().hasDefaultValue())
os << llvm::formatv(getFieldInfoOptional, field.getName());
else
os << llvm::formatv(getFieldInfo, field.getName());
}
const char *getEndInfo = R"(
::mlir::Attribute dict = ::mlir::DictionaryAttr::get(fields, context);
return dict.dyn_cast<{0}>();
}
)";
os << llvm::formatv(getEndInfo, structName);
}
static void emitClassofDef(llvm::StringRef structName,
llvm::ArrayRef<mlir::tblgen::StructFieldAttr> fields,
raw_ostream &os) {
const char *classofInfo = R"(
bool {0}::classof(::mlir::Attribute attr))";
const char *classofInfoHeader = R"(
if (!attr)
return false;
auto derived = attr.dyn_cast<::mlir::DictionaryAttr>();
if (!derived)
return false;
int num_absent_attrs = 0;
)";
os << llvm::formatv(classofInfo, structName) << " {";
os << llvm::formatv(classofInfoHeader);
FmtContext fctx;
const char *classofArgInfo = R"(
auto {0} = derived.get("{0}");
if (!{0} || !({1}))
return false;
)";
const char *classofArgInfoOptional = R"(
auto {0} = derived.get("{0}");
if (!{0})
++num_absent_attrs;
else if (!({1}))
return false;
)";
for (auto field : fields) {
auto name = field.getName();
auto type = field.getType();
std::string condition =
std::string(tgfmt(type.getConditionTemplate(), &fctx.withSelf(name)));
if (type.isOptional() || type.hasDefaultValue())
os << llvm::formatv(classofArgInfoOptional, name, condition);
else
os << llvm::formatv(classofArgInfo, name, condition);
}
const char *classofEndInfo = R"(
return derived.size() + num_absent_attrs == {0};
}
)";
os << llvm::formatv(classofEndInfo, fields.size());
}
static void
emitAccessorDef(llvm::StringRef structName,
llvm::ArrayRef<mlir::tblgen::StructFieldAttr> fields,
raw_ostream &os) {
const char *fieldInfo = R"(
{0} {2}::{1}() const {
auto derived = this->cast<::mlir::DictionaryAttr>();
auto {1} = derived.get("{1}");
assert({1} && "attribute not found.");
assert({1}.isa<{0}>() && "incorrect Attribute type found.");
return {1}.cast<{0}>();
}
)";
const char *fieldInfoOptional = R"(
{0} {2}::{1}() const {
auto derived = this->cast<::mlir::DictionaryAttr>();
auto {1} = derived.get("{1}");
if (!{1})
return nullptr;
assert({1}.isa<{0}>() && "incorrect Attribute type found.");
return {1}.cast<{0}>();
}
)";
const char *fieldInfoDefaultValued = R"(
{0} {2}::{1}() const {
auto derived = this->cast<::mlir::DictionaryAttr>();
auto {1} = derived.get("{1}");
if (!{1}) {
::mlir::Builder builder(getContext());
return {3};
}
assert({1}.isa<{0}>() && "incorrect Attribute type found.");
return {1}.cast<{0}>();
}
)";
FmtContext fmtCtx;
fmtCtx.withBuilder("builder");
for (auto field : fields) {
auto name = field.getName();
auto type = field.getType();
auto storage = type.getStorageType();
if (type.isOptional()) {
os << llvm::formatv(fieldInfoOptional, storage, name, structName);
} else if (type.hasDefaultValue()) {
std::string defaultValue = tgfmt(type.getConstBuilderTemplate(), &fmtCtx,
type.getDefaultValue());
os << llvm::formatv(fieldInfoDefaultValued, storage, name, structName,
defaultValue);
} else {
os << llvm::formatv(fieldInfo, storage, name, structName);
}
}
}
static void emitStructDef(const Record &structDef, raw_ostream &os) {
StructAttr structAttr(&structDef);
StringRef cppNamespace = structAttr.getCppNamespace();
StringRef structName = structAttr.getStructClassName();
mlir::tblgen::FmtContext ctx;
auto fields = structAttr.getAllFields();
llvm::SmallVector<StringRef, 2> namespaces;
llvm::SplitString(cppNamespace, namespaces, "::");
for (auto ns : namespaces)
os << "namespace " << ns << " {\n";
emitFactoryDef(structName, fields, os);
emitClassofDef(structName, fields, os);
emitAccessorDef(structName, fields, os);
for (auto ns : llvm::reverse(namespaces))
os << "} // namespace " << ns << "\n";
}
static bool emitStructDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
llvm::emitSourceFileHeader("Struct Utility Definitions", os);
auto defs = recordKeeper.getAllDerivedDefinitions("StructAttr");
for (const auto *def : defs)
emitStructDef(*def, os);
return false;
}
// Registers the struct utility generator to mlir-tblgen.
static mlir::GenRegistration
genStructDecls("gen-struct-attr-decls",
"Generate struct utility declarations",
[](const RecordKeeper &records, raw_ostream &os) {
return emitStructDecls(records, os);
});
// Registers the struct utility generator to mlir-tblgen.
static mlir::GenRegistration
genStructDefs("gen-struct-attr-defs", "Generate struct utility definitions",
[](const RecordKeeper &records, raw_ostream &os) {
return emitStructDefs(records, os);
});