forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttrOrTypeDefGen.cpp
1138 lines (1023 loc) · 42.9 KB
/
AttrOrTypeDefGen.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===- AttrOrTypeDefGen.cpp - MLIR AttrOrType definitions 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
//
//===----------------------------------------------------------------------===//
#include "AttrOrTypeFormatGen.h"
#include "mlir/TableGen/AttrOrTypeDef.h"
#include "mlir/TableGen/Class.h"
#include "mlir/TableGen/CodeGenHelpers.h"
#include "mlir/TableGen/Format.h"
#include "mlir/TableGen/GenInfo.h"
#include "mlir/TableGen/Interfaces.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/TableGenBackend.h"
#define DEBUG_TYPE "mlir-tblgen-attrortypedefgen"
using namespace mlir;
using namespace mlir::tblgen;
using llvm::Record;
using llvm::RecordKeeper;
//===----------------------------------------------------------------------===//
// Utility Functions
//===----------------------------------------------------------------------===//
/// Find all the AttrOrTypeDef for the specified dialect. If no dialect
/// specified and can only find one dialect's defs, use that.
static void collectAllDefs(StringRef selectedDialect,
ArrayRef<const Record *> records,
SmallVectorImpl<AttrOrTypeDef> &resultDefs) {
// Nothing to do if no defs were found.
if (records.empty())
return;
auto defs = llvm::map_range(
records, [&](const Record *rec) { return AttrOrTypeDef(rec); });
if (selectedDialect.empty()) {
// If a dialect was not specified, ensure that all found defs belong to the
// same dialect.
if (!llvm::all_equal(llvm::map_range(
defs, [](const auto &def) { return def.getDialect(); }))) {
llvm::PrintFatalError("defs belonging to more than one dialect. Must "
"select one via '--(attr|type)defs-dialect'");
}
resultDefs.assign(defs.begin(), defs.end());
} else {
// Otherwise, generate the defs that belong to the selected dialect.
auto dialectDefs = llvm::make_filter_range(defs, [&](const auto &def) {
return def.getDialect().getName() == selectedDialect;
});
resultDefs.assign(dialectDefs.begin(), dialectDefs.end());
}
}
//===----------------------------------------------------------------------===//
// DefGen
//===----------------------------------------------------------------------===//
namespace {
class DefGen {
public:
/// Create the attribute or type class.
DefGen(const AttrOrTypeDef &def);
void emitDecl(raw_ostream &os) const {
if (storageCls && def.genStorageClass()) {
NamespaceEmitter ns(os, def.getStorageNamespace());
os << "struct " << def.getStorageClassName() << ";\n";
}
defCls.writeDeclTo(os);
}
void emitDef(raw_ostream &os) const {
if (storageCls && def.genStorageClass()) {
NamespaceEmitter ns(os, def.getStorageNamespace());
storageCls->writeDeclTo(os); // everything is inline
}
defCls.writeDefTo(os);
}
private:
/// Add traits from the TableGen definition to the class.
void createParentWithTraits();
/// Emit top-level declarations: using declarations and any extra class
/// declarations.
void emitTopLevelDeclarations();
/// Emit the function that returns the type or attribute name.
void emitName();
/// Emit the dialect name as a static member variable.
void emitDialectName();
/// Emit attribute or type builders.
void emitBuilders();
/// Emit a verifier declaration for custom verification (impl. provided by
/// the users).
void emitVerifierDecl();
/// Emit a verifier that checks type constraints.
void emitInvariantsVerifierImpl();
/// Emit an entry poiunt for verification that calls the invariants and
/// custom verifier.
void emitInvariantsVerifier(bool hasImpl, bool hasCustomVerifier);
/// Emit parsers and printers.
void emitParserPrinter();
/// Emit parameter accessors, if required.
void emitAccessors();
/// Emit interface methods.
void emitInterfaceMethods();
//===--------------------------------------------------------------------===//
// Builder Emission
/// Emit the default builder `Attribute::get`
void emitDefaultBuilder();
/// Emit the checked builder `Attribute::getChecked`
void emitCheckedBuilder();
/// Emit a custom builder.
void emitCustomBuilder(const AttrOrTypeBuilder &builder);
/// Emit a checked custom builder.
void emitCheckedCustomBuilder(const AttrOrTypeBuilder &builder);
//===--------------------------------------------------------------------===//
// Interface Method Emission
/// Emit methods for a trait.
void emitTraitMethods(const InterfaceTrait &trait);
/// Emit a trait method.
void emitTraitMethod(const InterfaceMethod &method);
//===--------------------------------------------------------------------===//
// Storage Class Emission
void emitStorageClass();
/// Generate the storage class constructor.
void emitStorageConstructor();
/// Emit the key type `KeyTy`.
void emitKeyType();
/// Emit the equality comparison operator.
void emitEquals();
/// Emit the key hash function.
void emitHashKey();
/// Emit the function to construct the storage class.
void emitConstruct();
//===--------------------------------------------------------------------===//
// Utility Function Declarations
/// Get the method parameters for a def builder, where the first several
/// parameters may be different.
SmallVector<MethodParameter>
getBuilderParams(std::initializer_list<MethodParameter> prefix) const;
//===--------------------------------------------------------------------===//
// Class fields
/// The attribute or type definition.
const AttrOrTypeDef &def;
/// The list of attribute or type parameters.
ArrayRef<AttrOrTypeParameter> params;
/// The attribute or type class.
Class defCls;
/// An optional attribute or type storage class. The storage class will
/// exist if and only if the def has more than zero parameters.
std::optional<Class> storageCls;
/// The C++ base value of the def, either "Attribute" or "Type".
StringRef valueType;
/// The prefix/suffix of the TableGen def name, either "Attr" or "Type".
StringRef defType;
};
} // namespace
DefGen::DefGen(const AttrOrTypeDef &def)
: def(def), params(def.getParameters()), defCls(def.getCppClassName()),
valueType(isa<AttrDef>(def) ? "Attribute" : "Type"),
defType(isa<AttrDef>(def) ? "Attr" : "Type") {
// Check that all parameters have names.
for (const AttrOrTypeParameter ¶m : def.getParameters())
if (param.isAnonymous())
llvm::PrintFatalError("all parameters must have a name");
// If a storage class is needed, create one.
if (def.getNumParameters() > 0)
storageCls.emplace(def.getStorageClassName(), /*isStruct=*/true);
// Create the parent class with any indicated traits.
createParentWithTraits();
// Emit top-level declarations.
emitTopLevelDeclarations();
// Emit builders for defs with parameters
if (storageCls)
emitBuilders();
// Emit the type name.
emitName();
// Emit the dialect name.
emitDialectName();
// Emit verification of type constraints.
bool genVerifyInvariantsImpl = def.genVerifyInvariantsImpl();
if (storageCls && genVerifyInvariantsImpl)
emitInvariantsVerifierImpl();
// Emit the custom verifier (written by the user).
bool genVerifyDecl = def.genVerifyDecl();
if (storageCls && genVerifyDecl)
emitVerifierDecl();
// Emit the "verifyInvariants" function if there is any verification at all.
if (storageCls)
emitInvariantsVerifier(genVerifyInvariantsImpl, genVerifyDecl);
// Emit the mnemonic, if there is one, and any associated parser and printer.
if (def.getMnemonic())
emitParserPrinter();
// Emit accessors
if (def.genAccessors())
emitAccessors();
// Emit trait interface methods
emitInterfaceMethods();
defCls.finalize();
// Emit a storage class if one is needed
if (storageCls && def.genStorageClass())
emitStorageClass();
}
void DefGen::createParentWithTraits() {
ParentClass defParent(strfmt("::mlir::{0}::{1}Base", valueType, defType));
defParent.addTemplateParam(def.getCppClassName());
defParent.addTemplateParam(def.getCppBaseClassName());
defParent.addTemplateParam(storageCls
? strfmt("{0}::{1}", def.getStorageNamespace(),
def.getStorageClassName())
: strfmt("::mlir::{0}Storage", valueType));
for (auto &trait : def.getTraits()) {
defParent.addTemplateParam(
isa<NativeTrait>(&trait)
? cast<NativeTrait>(&trait)->getFullyQualifiedTraitName()
: cast<InterfaceTrait>(&trait)->getFullyQualifiedTraitName());
}
defCls.addParent(std::move(defParent));
}
/// Include declarations specified on NativeTrait
static std::string formatExtraDeclarations(const AttrOrTypeDef &def) {
SmallVector<StringRef> extraDeclarations;
// Include extra class declarations from NativeTrait
for (const auto &trait : def.getTraits()) {
if (auto *attrOrTypeTrait = dyn_cast<tblgen::NativeTrait>(&trait)) {
StringRef value = attrOrTypeTrait->getExtraConcreteClassDeclaration();
if (value.empty())
continue;
extraDeclarations.push_back(value);
}
}
if (std::optional<StringRef> extraDecl = def.getExtraDecls()) {
extraDeclarations.push_back(*extraDecl);
}
return llvm::join(extraDeclarations, "\n");
}
/// Extra class definitions have a `$cppClass` substitution that is to be
/// replaced by the C++ class name.
static std::string formatExtraDefinitions(const AttrOrTypeDef &def) {
SmallVector<StringRef> extraDefinitions;
// Include extra class definitions from NativeTrait
for (const auto &trait : def.getTraits()) {
if (auto *attrOrTypeTrait = dyn_cast<tblgen::NativeTrait>(&trait)) {
StringRef value = attrOrTypeTrait->getExtraConcreteClassDefinition();
if (value.empty())
continue;
extraDefinitions.push_back(value);
}
}
if (std::optional<StringRef> extraDef = def.getExtraDefs()) {
extraDefinitions.push_back(*extraDef);
}
FmtContext ctx = FmtContext().addSubst("cppClass", def.getCppClassName());
return tgfmt(llvm::join(extraDefinitions, "\n"), &ctx).str();
}
void DefGen::emitTopLevelDeclarations() {
// Inherit constructors from the attribute or type class.
defCls.declare<VisibilityDeclaration>(Visibility::Public);
defCls.declare<UsingDeclaration>("Base::Base");
// Emit the extra declarations first in case there's a definition in there.
std::string extraDecl = formatExtraDeclarations(def);
std::string extraDef = formatExtraDefinitions(def);
defCls.declare<ExtraClassDeclaration>(std::move(extraDecl),
std::move(extraDef));
}
void DefGen::emitName() {
StringRef name;
if (auto *attrDef = dyn_cast<AttrDef>(&def)) {
name = attrDef->getAttrName();
} else {
auto *typeDef = cast<TypeDef>(&def);
name = typeDef->getTypeName();
}
std::string nameDecl =
strfmt("static constexpr ::llvm::StringLiteral name = \"{0}\";\n", name);
defCls.declare<ExtraClassDeclaration>(std::move(nameDecl));
}
void DefGen::emitDialectName() {
std::string decl =
strfmt("static constexpr ::llvm::StringLiteral dialectName = \"{0}\";\n",
def.getDialect().getName());
defCls.declare<ExtraClassDeclaration>(std::move(decl));
}
void DefGen::emitBuilders() {
if (!def.skipDefaultBuilders()) {
emitDefaultBuilder();
if (def.genVerifyDecl() || def.genVerifyInvariantsImpl())
emitCheckedBuilder();
}
for (auto &builder : def.getBuilders()) {
emitCustomBuilder(builder);
if (def.genVerifyDecl() || def.genVerifyInvariantsImpl())
emitCheckedCustomBuilder(builder);
}
}
void DefGen::emitVerifierDecl() {
defCls.declareStaticMethod(
"::llvm::LogicalResult", "verify",
getBuilderParams({{"::llvm::function_ref<::mlir::InFlightDiagnostic()>",
"emitError"}}));
}
static const char *const patternParameterVerificationCode = R"(
if (!({0})) {
emitError() << "failed to verify '{1}': {2}";
return ::mlir::failure();
}
)";
void DefGen::emitInvariantsVerifierImpl() {
SmallVector<MethodParameter> builderParams = getBuilderParams(
{{"::llvm::function_ref<::mlir::InFlightDiagnostic()>", "emitError"}});
Method *verifier =
defCls.addMethod("::llvm::LogicalResult", "verifyInvariantsImpl",
Method::Static, builderParams);
verifier->body().indent();
// Generate verification for each parameter that is a type constraint.
for (auto it : llvm::enumerate(def.getParameters())) {
const AttrOrTypeParameter ¶m = it.value();
std::optional<Constraint> constraint = param.getConstraint();
// No verification needed for parameters that are not type constraints.
if (!constraint.has_value())
continue;
FmtContext ctx;
// Note: Skip over the first method parameter (`emitError`).
ctx.withSelf(builderParams[it.index() + 1].getName());
std::string condition = tgfmt(constraint->getConditionTemplate(), &ctx);
verifier->body() << formatv(patternParameterVerificationCode, condition,
param.getName(), constraint->getSummary())
<< "\n";
}
verifier->body() << "return ::mlir::success();";
}
void DefGen::emitInvariantsVerifier(bool hasImpl, bool hasCustomVerifier) {
if (!hasImpl && !hasCustomVerifier)
return;
defCls.declare<UsingDeclaration>("Base::getChecked");
SmallVector<MethodParameter> builderParams = getBuilderParams(
{{"::llvm::function_ref<::mlir::InFlightDiagnostic()>", "emitError"}});
Method *verifier =
defCls.addMethod("::llvm::LogicalResult", "verifyInvariants",
Method::Static, builderParams);
verifier->body().indent();
auto emitVerifierCall = [&](StringRef name) {
verifier->body() << strfmt("if (::mlir::failed({0}(", name);
llvm::interleaveComma(
llvm::map_range(builderParams,
[](auto ¶m) { return param.getName(); }),
verifier->body());
verifier->body() << ")))\n";
verifier->body() << " return ::mlir::failure();\n";
};
if (hasImpl) {
// Call the verifier that checks the type constraints.
emitVerifierCall("verifyInvariantsImpl");
}
if (hasCustomVerifier) {
// Call the custom verifier that is provided by the user.
emitVerifierCall("verify");
}
verifier->body() << "return ::mlir::success();";
}
void DefGen::emitParserPrinter() {
auto *mnemonic = defCls.addStaticMethod<Method::Constexpr>(
"::llvm::StringLiteral", "getMnemonic");
mnemonic->body().indent() << strfmt("return {\"{0}\"};", *def.getMnemonic());
// Declare the parser and printer, if needed.
bool hasAssemblyFormat = def.getAssemblyFormat().has_value();
if (!def.hasCustomAssemblyFormat() && !hasAssemblyFormat)
return;
// Declare the parser.
SmallVector<MethodParameter> parserParams;
parserParams.emplace_back("::mlir::AsmParser &", "odsParser");
if (isa<AttrDef>(&def))
parserParams.emplace_back("::mlir::Type", "odsType");
auto *parser = defCls.addMethod(strfmt("::mlir::{0}", valueType), "parse",
hasAssemblyFormat ? Method::Static
: Method::StaticDeclaration,
std::move(parserParams));
// Declare the printer.
auto props = hasAssemblyFormat ? Method::Const : Method::ConstDeclaration;
Method *printer =
defCls.addMethod("void", "print", props,
MethodParameter("::mlir::AsmPrinter &", "odsPrinter"));
// Emit the bodies if we are using the declarative format.
if (hasAssemblyFormat)
return generateAttrOrTypeFormat(def, parser->body(), printer->body());
}
void DefGen::emitAccessors() {
for (auto ¶m : params) {
Method *m = defCls.addMethod(
param.getCppAccessorType(), param.getAccessorName(),
def.genStorageClass() ? Method::Const : Method::ConstDeclaration);
// Generate accessor definitions only if we also generate the storage
// class. Otherwise, let the user define the exact accessor definition.
if (!def.genStorageClass())
continue;
m->body().indent() << "return getImpl()->" << param.getName() << ";";
}
}
void DefGen::emitInterfaceMethods() {
for (auto &traitDef : def.getTraits())
if (auto *trait = dyn_cast<InterfaceTrait>(&traitDef))
if (trait->shouldDeclareMethods())
emitTraitMethods(*trait);
}
//===----------------------------------------------------------------------===//
// Builder Emission
SmallVector<MethodParameter>
DefGen::getBuilderParams(std::initializer_list<MethodParameter> prefix) const {
SmallVector<MethodParameter> builderParams;
builderParams.append(prefix.begin(), prefix.end());
for (auto ¶m : params)
builderParams.emplace_back(param.getCppType(), param.getName());
return builderParams;
}
void DefGen::emitDefaultBuilder() {
Method *m = defCls.addStaticMethod(
def.getCppClassName(), "get",
getBuilderParams({{"::mlir::MLIRContext *", "context"}}));
MethodBody &body = m->body().indent();
auto scope = body.scope("return Base::get(context", ");");
for (const auto ¶m : params)
body << ", std::move(" << param.getName() << ")";
}
void DefGen::emitCheckedBuilder() {
Method *m = defCls.addStaticMethod(
def.getCppClassName(), "getChecked",
getBuilderParams(
{{"::llvm::function_ref<::mlir::InFlightDiagnostic()>", "emitError"},
{"::mlir::MLIRContext *", "context"}}));
MethodBody &body = m->body().indent();
auto scope = body.scope("return Base::getChecked(emitError, context", ");");
for (const auto ¶m : params)
body << ", " << param.getName();
}
static SmallVector<MethodParameter>
getCustomBuilderParams(std::initializer_list<MethodParameter> prefix,
const AttrOrTypeBuilder &builder) {
auto params = builder.getParameters();
SmallVector<MethodParameter> builderParams;
builderParams.append(prefix.begin(), prefix.end());
if (!builder.hasInferredContextParameter())
builderParams.emplace_back("::mlir::MLIRContext *", "context");
for (auto ¶m : params) {
builderParams.emplace_back(param.getCppType(), *param.getName(),
param.getDefaultValue());
}
return builderParams;
}
void DefGen::emitCustomBuilder(const AttrOrTypeBuilder &builder) {
// Don't emit a body if there isn't one.
auto props = builder.getBody() ? Method::Static : Method::StaticDeclaration;
StringRef returnType = def.getCppClassName();
if (std::optional<StringRef> builderReturnType = builder.getReturnType())
returnType = *builderReturnType;
Method *m = defCls.addMethod(returnType, "get", props,
getCustomBuilderParams({}, builder));
if (!builder.getBody())
return;
// Format the body and emit it.
FmtContext ctx;
ctx.addSubst("_get", "Base::get");
if (!builder.hasInferredContextParameter())
ctx.addSubst("_ctxt", "context");
std::string bodyStr = tgfmt(*builder.getBody(), &ctx);
m->body().indent().getStream().printReindented(bodyStr);
}
/// Replace all instances of 'from' to 'to' in `str` and return the new string.
static std::string replaceInStr(std::string str, StringRef from, StringRef to) {
size_t pos = 0;
while ((pos = str.find(from.data(), pos, from.size())) != std::string::npos)
str.replace(pos, from.size(), to.data(), to.size());
return str;
}
void DefGen::emitCheckedCustomBuilder(const AttrOrTypeBuilder &builder) {
// Don't emit a body if there isn't one.
auto props = builder.getBody() ? Method::Static : Method::StaticDeclaration;
StringRef returnType = def.getCppClassName();
if (std::optional<StringRef> builderReturnType = builder.getReturnType())
returnType = *builderReturnType;
Method *m = defCls.addMethod(
returnType, "getChecked", props,
getCustomBuilderParams(
{{"::llvm::function_ref<::mlir::InFlightDiagnostic()>", "emitError"}},
builder));
if (!builder.getBody())
return;
// Format the body and emit it. Replace $_get(...) with
// Base::getChecked(emitError, ...)
FmtContext ctx;
if (!builder.hasInferredContextParameter())
ctx.addSubst("_ctxt", "context");
std::string bodyStr = replaceInStr(builder.getBody()->str(), "$_get(",
"Base::getChecked(emitError, ");
bodyStr = tgfmt(bodyStr, &ctx);
m->body().indent().getStream().printReindented(bodyStr);
}
//===----------------------------------------------------------------------===//
// Interface Method Emission
void DefGen::emitTraitMethods(const InterfaceTrait &trait) {
// Get the set of methods that should always be declared.
auto alwaysDeclaredMethods = trait.getAlwaysDeclaredMethods();
StringSet<> alwaysDeclared;
alwaysDeclared.insert(alwaysDeclaredMethods.begin(),
alwaysDeclaredMethods.end());
Interface iface = trait.getInterface(); // causes strange bugs if elided
for (auto &method : iface.getMethods()) {
// Don't declare if the method has a body. Or if the method has a default
// implementation and the def didn't request that it always be declared.
if (method.getBody() || (method.getDefaultImplementation() &&
!alwaysDeclared.count(method.getName())))
continue;
emitTraitMethod(method);
}
}
void DefGen::emitTraitMethod(const InterfaceMethod &method) {
// All interface methods are declaration-only.
auto props =
method.isStatic() ? Method::StaticDeclaration : Method::ConstDeclaration;
SmallVector<MethodParameter> params;
for (auto ¶m : method.getArguments())
params.emplace_back(param.type, param.name);
defCls.addMethod(method.getReturnType(), method.getName(), props,
std::move(params));
}
//===----------------------------------------------------------------------===//
// Storage Class Emission
void DefGen::emitStorageConstructor() {
Constructor *ctor =
storageCls->addConstructor<Method::Inline>(getBuilderParams({}));
for (auto ¶m : params) {
std::string movedValue = ("std::move(" + param.getName() + ")").str();
ctor->addMemberInitializer(param.getName(), movedValue);
}
}
void DefGen::emitKeyType() {
std::string keyType("std::tuple<");
llvm::raw_string_ostream os(keyType);
llvm::interleaveComma(params, os,
[&](auto ¶m) { os << param.getCppType(); });
os << '>';
storageCls->declare<UsingDeclaration>("KeyTy", std::move(os.str()));
// Add a method to construct the key type from the storage.
Method *m = storageCls->addConstMethod<Method::Inline>("KeyTy", "getAsKey");
m->body().indent() << "return KeyTy(";
llvm::interleaveComma(params, m->body().indent(),
[&](auto ¶m) { m->body() << param.getName(); });
m->body() << ");";
}
void DefGen::emitEquals() {
Method *eq = storageCls->addConstMethod<Method::Inline>(
"bool", "operator==", MethodParameter("const KeyTy &", "tblgenKey"));
auto &body = eq->body().indent();
auto scope = body.scope("return (", ");");
const auto eachFn = [&](auto it) {
FmtContext ctx({{"_lhs", it.value().getName()},
{"_rhs", strfmt("std::get<{0}>(tblgenKey)", it.index())}});
body << tgfmt(it.value().getComparator(), &ctx);
};
llvm::interleave(llvm::enumerate(params), body, eachFn, ") && (");
}
void DefGen::emitHashKey() {
Method *hash = storageCls->addStaticInlineMethod(
"::llvm::hash_code", "hashKey",
MethodParameter("const KeyTy &", "tblgenKey"));
auto &body = hash->body().indent();
auto scope = body.scope("return ::llvm::hash_combine(", ");");
llvm::interleaveComma(llvm::enumerate(params), body, [&](auto it) {
body << llvm::formatv("std::get<{0}>(tblgenKey)", it.index());
});
}
void DefGen::emitConstruct() {
Method *construct = storageCls->addMethod<Method::Inline>(
strfmt("{0} *", def.getStorageClassName()), "construct",
def.hasStorageCustomConstructor() ? Method::StaticDeclaration
: Method::Static,
MethodParameter(strfmt("::mlir::{0}StorageAllocator &", valueType),
"allocator"),
MethodParameter("KeyTy &&", "tblgenKey"));
if (!def.hasStorageCustomConstructor()) {
auto &body = construct->body().indent();
for (const auto &it : llvm::enumerate(params)) {
body << formatv("auto {0} = std::move(std::get<{1}>(tblgenKey));\n",
it.value().getName(), it.index());
}
// Use the parameters' custom allocator code, if provided.
FmtContext ctx = FmtContext().addSubst("_allocator", "allocator");
for (auto ¶m : params) {
if (std::optional<StringRef> allocCode = param.getAllocator()) {
ctx.withSelf(param.getName()).addSubst("_dst", param.getName());
body << tgfmt(*allocCode, &ctx) << '\n';
}
}
auto scope =
body.scope(strfmt("return new (allocator.allocate<{0}>()) {0}(",
def.getStorageClassName()),
");");
llvm::interleaveComma(params, body, [&](auto ¶m) {
body << "std::move(" << param.getName() << ")";
});
}
}
void DefGen::emitStorageClass() {
// Add the appropriate parent class.
storageCls->addParent(strfmt("::mlir::{0}Storage", valueType));
// Add the constructor.
emitStorageConstructor();
// Declare the key type.
emitKeyType();
// Add the comparison method.
emitEquals();
// Emit the key hash method.
emitHashKey();
// Emit the storage constructor. Just declare it if the user wants to define
// it themself.
emitConstruct();
// Emit the storage class members as public, at the very end of the struct.
storageCls->finalize();
for (auto ¶m : params)
storageCls->declare<Field>(param.getCppType(), param.getName());
}
//===----------------------------------------------------------------------===//
// DefGenerator
//===----------------------------------------------------------------------===//
namespace {
/// This struct is the base generator used when processing tablegen interfaces.
class DefGenerator {
public:
bool emitDecls(StringRef selectedDialect);
bool emitDefs(StringRef selectedDialect);
protected:
DefGenerator(ArrayRef<const Record *> defs, raw_ostream &os,
StringRef defType, StringRef valueType, bool isAttrGenerator)
: defRecords(defs), os(os), defType(defType), valueType(valueType),
isAttrGenerator(isAttrGenerator) {
// Sort by occurrence in file.
llvm::sort(defRecords, [](const Record *lhs, const Record *rhs) {
return lhs->getID() < rhs->getID();
});
}
/// Emit the list of def type names.
void emitTypeDefList(ArrayRef<AttrOrTypeDef> defs);
/// Emit the code to dispatch between different defs during parsing/printing.
void emitParsePrintDispatch(ArrayRef<AttrOrTypeDef> defs);
/// The set of def records to emit.
std::vector<const Record *> defRecords;
/// The attribute or type class to emit.
/// The stream to emit to.
raw_ostream &os;
/// The prefix of the tablegen def name, e.g. Attr or Type.
StringRef defType;
/// The C++ base value type of the def, e.g. Attribute or Type.
StringRef valueType;
/// Flag indicating if this generator is for Attributes. False if the
/// generator is for types.
bool isAttrGenerator;
};
/// A specialized generator for AttrDefs.
struct AttrDefGenerator : public DefGenerator {
AttrDefGenerator(const RecordKeeper &records, raw_ostream &os)
: DefGenerator(records.getAllDerivedDefinitionsIfDefined("AttrDef"), os,
"Attr", "Attribute", /*isAttrGenerator=*/true) {}
};
/// A specialized generator for TypeDefs.
struct TypeDefGenerator : public DefGenerator {
TypeDefGenerator(const RecordKeeper &records, raw_ostream &os)
: DefGenerator(records.getAllDerivedDefinitionsIfDefined("TypeDef"), os,
"Type", "Type", /*isAttrGenerator=*/false) {}
};
} // namespace
//===----------------------------------------------------------------------===//
// GEN: Declarations
//===----------------------------------------------------------------------===//
/// Print this above all the other declarations. Contains type declarations used
/// later on.
static const char *const typeDefDeclHeader = R"(
namespace mlir {
class AsmParser;
class AsmPrinter;
} // namespace mlir
)";
bool DefGenerator::emitDecls(StringRef selectedDialect) {
emitSourceFileHeader((defType + "Def Declarations").str(), os);
IfDefScope scope("GET_" + defType.upper() + "DEF_CLASSES", os);
// Output the common "header".
os << typeDefDeclHeader;
SmallVector<AttrOrTypeDef, 16> defs;
collectAllDefs(selectedDialect, defRecords, defs);
if (defs.empty())
return false;
{
NamespaceEmitter nsEmitter(os, defs.front().getDialect());
// Declare all the def classes first (in case they reference each other).
for (const AttrOrTypeDef &def : defs)
os << "class " << def.getCppClassName() << ";\n";
// Emit the declarations.
for (const AttrOrTypeDef &def : defs)
DefGen(def).emitDecl(os);
}
// Emit the TypeID explicit specializations to have a single definition for
// each of these.
for (const AttrOrTypeDef &def : defs)
if (!def.getDialect().getCppNamespace().empty())
os << "MLIR_DECLARE_EXPLICIT_TYPE_ID("
<< def.getDialect().getCppNamespace() << "::" << def.getCppClassName()
<< ")\n";
return false;
}
//===----------------------------------------------------------------------===//
// GEN: Def List
//===----------------------------------------------------------------------===//
void DefGenerator::emitTypeDefList(ArrayRef<AttrOrTypeDef> defs) {
IfDefScope scope("GET_" + defType.upper() + "DEF_LIST", os);
auto interleaveFn = [&](const AttrOrTypeDef &def) {
os << def.getDialect().getCppNamespace() << "::" << def.getCppClassName();
};
llvm::interleave(defs, os, interleaveFn, ",\n");
os << "\n";
}
//===----------------------------------------------------------------------===//
// GEN: Definitions
//===----------------------------------------------------------------------===//
/// The code block for default attribute parser/printer dispatch boilerplate.
/// {0}: the dialect fully qualified class name.
/// {1}: the optional code for the dynamic attribute parser dispatch.
/// {2}: the optional code for the dynamic attribute printer dispatch.
static const char *const dialectDefaultAttrPrinterParserDispatch = R"(
/// Parse an attribute registered to this dialect.
::mlir::Attribute {0}::parseAttribute(::mlir::DialectAsmParser &parser,
::mlir::Type type) const {{
::llvm::SMLoc typeLoc = parser.getCurrentLocation();
::llvm::StringRef attrTag;
{{
::mlir::Attribute attr;
auto parseResult = generatedAttributeParser(parser, &attrTag, type, attr);
if (parseResult.has_value())
return attr;
}
{1}
parser.emitError(typeLoc) << "unknown attribute `"
<< attrTag << "` in dialect `" << getNamespace() << "`";
return {{};
}
/// Print an attribute registered to this dialect.
void {0}::printAttribute(::mlir::Attribute attr,
::mlir::DialectAsmPrinter &printer) const {{
if (::mlir::succeeded(generatedAttributePrinter(attr, printer)))
return;
{2}
}
)";
/// The code block for dynamic attribute parser dispatch boilerplate.
static const char *const dialectDynamicAttrParserDispatch = R"(
{
::mlir::Attribute genAttr;
auto parseResult = parseOptionalDynamicAttr(attrTag, parser, genAttr);
if (parseResult.has_value()) {
if (::mlir::succeeded(parseResult.value()))
return genAttr;
return Attribute();
}
}
)";
/// The code block for dynamic type printer dispatch boilerplate.
static const char *const dialectDynamicAttrPrinterDispatch = R"(
if (::mlir::succeeded(printIfDynamicAttr(attr, printer)))
return;
)";
/// The code block for default type parser/printer dispatch boilerplate.
/// {0}: the dialect fully qualified class name.
/// {1}: the optional code for the dynamic type parser dispatch.
/// {2}: the optional code for the dynamic type printer dispatch.
static const char *const dialectDefaultTypePrinterParserDispatch = R"(
/// Parse a type registered to this dialect.
::mlir::Type {0}::parseType(::mlir::DialectAsmParser &parser) const {{
::llvm::SMLoc typeLoc = parser.getCurrentLocation();
::llvm::StringRef mnemonic;
::mlir::Type genType;
auto parseResult = generatedTypeParser(parser, &mnemonic, genType);
if (parseResult.has_value())
return genType;
{1}
parser.emitError(typeLoc) << "unknown type `"
<< mnemonic << "` in dialect `" << getNamespace() << "`";
return {{};
}
/// Print a type registered to this dialect.
void {0}::printType(::mlir::Type type,
::mlir::DialectAsmPrinter &printer) const {{
if (::mlir::succeeded(generatedTypePrinter(type, printer)))
return;
{2}
}
)";
/// The code block for dynamic type parser dispatch boilerplate.
static const char *const dialectDynamicTypeParserDispatch = R"(
{
auto parseResult = parseOptionalDynamicType(mnemonic, parser, genType);
if (parseResult.has_value()) {
if (::mlir::succeeded(parseResult.value()))
return genType;
return ::mlir::Type();
}
}
)";
/// The code block for dynamic type printer dispatch boilerplate.
static const char *const dialectDynamicTypePrinterDispatch = R"(
if (::mlir::succeeded(printIfDynamicType(type, printer)))
return;
)";
/// Emit the dialect printer/parser dispatcher. User's code should call these
/// functions from their dialect's print/parse methods.
void DefGenerator::emitParsePrintDispatch(ArrayRef<AttrOrTypeDef> defs) {
if (llvm::none_of(defs, [](const AttrOrTypeDef &def) {
return def.getMnemonic().has_value();
})) {
return;
}
// Declare the parser.
SmallVector<MethodParameter> params = {{"::mlir::AsmParser &", "parser"},
{"::llvm::StringRef *", "mnemonic"}};
if (isAttrGenerator)
params.emplace_back("::mlir::Type", "type");
params.emplace_back(strfmt("::mlir::{0} &", valueType), "value");
Method parse("::mlir::OptionalParseResult",
strfmt("generated{0}Parser", valueType), Method::StaticInline,
std::move(params));
// Declare the printer.
Method printer("::llvm::LogicalResult",
strfmt("generated{0}Printer", valueType), Method::StaticInline,
{{strfmt("::mlir::{0}", valueType), "def"},
{"::mlir::AsmPrinter &", "printer"}});
// The parser dispatch uses a KeywordSwitch, matching on the mnemonic and
// calling the def's parse function.
parse.body() << " return "
"::mlir::AsmParser::KeywordSwitch<::mlir::"
"OptionalParseResult>(parser)\n";
const char *const getValueForMnemonic =
R"( .Case({0}::getMnemonic(), [&](llvm::StringRef, llvm::SMLoc) {{
value = {0}::{1};
return ::mlir::success(!!value);
})
)";
// The printer dispatch uses llvm::TypeSwitch to find and call the correct
// printer.
printer.body() << " return ::llvm::TypeSwitch<::mlir::" << valueType
<< ", ::llvm::LogicalResult>(def)";
const char *const printValue = R"( .Case<{0}>([&](auto t) {{
printer << {0}::getMnemonic();{1}
return ::mlir::success();
})
)";
for (auto &def : defs) {
if (!def.getMnemonic())
continue;
bool hasParserPrinterDecl =
def.hasCustomAssemblyFormat() || def.getAssemblyFormat();
std::string defClass = strfmt(
"{0}::{1}", def.getDialect().getCppNamespace(), def.getCppClassName());
// If the def has no parameters or parser code, invoke a normal `get`.
std::string parseOrGet =
hasParserPrinterDecl
? strfmt("parse(parser{0})", isAttrGenerator ? ", type" : "")
: "get(parser.getContext())";
parse.body() << llvm::formatv(getValueForMnemonic, defClass, parseOrGet);
// If the def has no parameters and no printer, just print the mnemonic.
StringRef printDef = "";
if (hasParserPrinterDecl)
printDef = "\nt.print(printer);";
printer.body() << llvm::formatv(printValue, defClass, printDef);
}
parse.body() << " .Default([&](llvm::StringRef keyword, llvm::SMLoc) {\n"
" *mnemonic = keyword;\n"
" return std::nullopt;\n"
" });";
printer.body() << " .Default([](auto) { return ::mlir::failure(); });";
raw_indented_ostream indentedOs(os);
parse.writeDeclTo(indentedOs);
printer.writeDeclTo(indentedOs);
}
bool DefGenerator::emitDefs(StringRef selectedDialect) {
emitSourceFileHeader((defType + "Def Definitions").str(), os);
SmallVector<AttrOrTypeDef, 16> defs;
collectAllDefs(selectedDialect, defRecords, defs);
if (defs.empty())
return false;
emitTypeDefList(defs);
IfDefScope scope("GET_" + defType.upper() + "DEF_CLASSES", os);
emitParsePrintDispatch(defs);
for (const AttrOrTypeDef &def : defs) {
{
NamespaceEmitter ns(os, def.getDialect());
DefGen gen(def);
gen.emitDef(os);
}
// Emit the TypeID explicit specializations to have a single symbol def.
if (!def.getDialect().getCppNamespace().empty())
os << "MLIR_DEFINE_EXPLICIT_TYPE_ID("
<< def.getDialect().getCppNamespace() << "::" << def.getCppClassName()
<< ")\n";
}
Dialect firstDialect = defs.front().getDialect();
// Emit the default parser/printer for Attributes if the dialect asked for it.
if (isAttrGenerator && firstDialect.useDefaultAttributePrinterParser()) {
NamespaceEmitter nsEmitter(os, firstDialect);
if (firstDialect.isExtensible()) {