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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
|
//===- EmitC.cpp - EmitC Dialect ------------------------------------------===//
//
// 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/Dialect/EmitC/IR/EmitC.h"
#include "mlir/Dialect/EmitC/IR/EmitCInterfaces.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/Types.h"
#include "mlir/Interfaces/FunctionImplementation.h"
#include "mlir/Support/LLVM.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/Casting.h"
using namespace mlir;
using namespace mlir::emitc;
#include "mlir/Dialect/EmitC/IR/EmitCDialect.cpp.inc"
//===----------------------------------------------------------------------===//
// EmitCDialect
//===----------------------------------------------------------------------===//
void EmitCDialect::initialize() {
addOperations<
#define GET_OP_LIST
#include "mlir/Dialect/EmitC/IR/EmitC.cpp.inc"
>();
addTypes<
#define GET_TYPEDEF_LIST
#include "mlir/Dialect/EmitC/IR/EmitCTypes.cpp.inc"
>();
addAttributes<
#define GET_ATTRDEF_LIST
#include "mlir/Dialect/EmitC/IR/EmitCAttributes.cpp.inc"
>();
}
/// Materialize a single constant operation from a given attribute value with
/// the desired resultant type.
Operation *EmitCDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
return emitc::ConstantOp::create(builder, loc, type, value);
}
/// Default callback for builders of ops carrying a region. Inserts a yield
/// without arguments.
void mlir::emitc::buildTerminatedBody(OpBuilder &builder, Location loc) {
emitc::YieldOp::create(builder, loc);
}
bool mlir::emitc::isSupportedEmitCType(Type type) {
if (llvm::isa<emitc::OpaqueType>(type))
return true;
if (auto ptrType = llvm::dyn_cast<emitc::PointerType>(type))
return isSupportedEmitCType(ptrType.getPointee());
if (auto arrayType = llvm::dyn_cast<emitc::ArrayType>(type)) {
auto elemType = arrayType.getElementType();
return !llvm::isa<emitc::ArrayType>(elemType) &&
isSupportedEmitCType(elemType);
}
if (type.isIndex() || emitc::isPointerWideType(type))
return true;
if (llvm::isa<IntegerType>(type))
return isSupportedIntegerType(type);
if (llvm::isa<FloatType>(type))
return isSupportedFloatType(type);
if (auto tensorType = llvm::dyn_cast<TensorType>(type)) {
if (!tensorType.hasStaticShape()) {
return false;
}
auto elemType = tensorType.getElementType();
if (llvm::isa<emitc::ArrayType>(elemType)) {
return false;
}
return isSupportedEmitCType(elemType);
}
if (auto tupleType = llvm::dyn_cast<TupleType>(type)) {
return llvm::all_of(tupleType.getTypes(), [](Type type) {
return !llvm::isa<emitc::ArrayType>(type) && isSupportedEmitCType(type);
});
}
return false;
}
bool mlir::emitc::isSupportedIntegerType(Type type) {
if (auto intType = llvm::dyn_cast<IntegerType>(type)) {
switch (intType.getWidth()) {
case 1:
case 8:
case 16:
case 32:
case 64:
return true;
default:
return false;
}
}
return false;
}
bool mlir::emitc::isIntegerIndexOrOpaqueType(Type type) {
return llvm::isa<IndexType, emitc::OpaqueType>(type) ||
isSupportedIntegerType(type) || isPointerWideType(type);
}
bool mlir::emitc::isSupportedFloatType(Type type) {
if (auto floatType = llvm::dyn_cast<FloatType>(type)) {
switch (floatType.getWidth()) {
case 16:
return llvm::isa<Float16Type, BFloat16Type>(type);
case 32:
case 64:
return true;
default:
return false;
}
}
return false;
}
bool mlir::emitc::isPointerWideType(Type type) {
return isa<emitc::SignedSizeTType, emitc::SizeTType, emitc::PtrDiffTType>(
type);
}
bool mlir::emitc::isFundamentalType(Type type) {
return llvm::isa<IndexType>(type) || isPointerWideType(type) ||
isSupportedIntegerType(type) || isSupportedFloatType(type) ||
isa<emitc::PointerType>(type);
}
/// Check that the type of the initial value is compatible with the operations
/// result type.
static LogicalResult verifyInitializationAttribute(Operation *op,
Attribute value) {
assert(op->getNumResults() == 1 && "operation must have 1 result");
if (llvm::isa<emitc::OpaqueAttr>(value))
return success();
if (llvm::isa<StringAttr>(value))
return op->emitOpError()
<< "string attributes are not supported, use #emitc.opaque instead";
Type resultType = op->getResult(0).getType();
if (auto lType = dyn_cast<LValueType>(resultType))
resultType = lType.getValueType();
Type attrType = cast<TypedAttr>(value).getType();
if (isPointerWideType(resultType) && attrType.isIndex())
return success();
if (resultType != attrType)
return op->emitOpError()
<< "requires attribute to either be an #emitc.opaque attribute or "
"it's type ("
<< attrType << ") to match the op's result type (" << resultType
<< ")";
return success();
}
/// Parse a format string and return a list of its parts.
/// A part is either a StringRef that has to be printed as-is, or
/// a Placeholder which requires printing the next operand of the VerbatimOp.
/// In the format string, all `{}` are replaced by Placeholders, except if the
/// `{` is escaped by `{{` - then it doesn't start a placeholder.
template <class ArgType>
FailureOr<SmallVector<ReplacementItem>> parseFormatString(
StringRef toParse, ArgType fmtArgs,
llvm::function_ref<mlir::InFlightDiagnostic()> emitError = {}) {
SmallVector<ReplacementItem> items;
// If there are not operands, the format string is not interpreted.
if (fmtArgs.empty()) {
items.push_back(toParse);
return items;
}
while (!toParse.empty()) {
size_t idx = toParse.find('{');
if (idx == StringRef::npos) {
// No '{'
items.push_back(toParse);
break;
}
if (idx > 0) {
// Take all chars excluding the '{'.
items.push_back(toParse.take_front(idx));
toParse = toParse.drop_front(idx);
continue;
}
if (toParse.size() < 2) {
return emitError() << "expected '}' after unescaped '{' at end of string";
}
// toParse contains at least two characters and starts with `{`.
char nextChar = toParse[1];
if (nextChar == '{') {
// Double '{{' -> '{' (escaping).
items.push_back(toParse.take_front(1));
toParse = toParse.drop_front(2);
continue;
}
if (nextChar == '}') {
items.push_back(Placeholder{});
toParse = toParse.drop_front(2);
continue;
}
if (emitError) {
return emitError() << "expected '}' after unescaped '{'";
}
return failure();
}
return items;
}
//===----------------------------------------------------------------------===//
// AddOp
//===----------------------------------------------------------------------===//
LogicalResult AddOp::verify() {
Type lhsType = getLhs().getType();
Type rhsType = getRhs().getType();
if (isa<emitc::PointerType>(lhsType) && isa<emitc::PointerType>(rhsType))
return emitOpError("requires that at most one operand is a pointer");
if ((isa<emitc::PointerType>(lhsType) &&
!isa<IntegerType, emitc::OpaqueType>(rhsType)) ||
(isa<emitc::PointerType>(rhsType) &&
!isa<IntegerType, emitc::OpaqueType>(lhsType)))
return emitOpError("requires that one operand is an integer or of opaque "
"type if the other is a pointer");
return success();
}
//===----------------------------------------------------------------------===//
// ApplyOp
//===----------------------------------------------------------------------===//
LogicalResult ApplyOp::verify() {
StringRef applicableOperatorStr = getApplicableOperator();
// Applicable operator must not be empty.
if (applicableOperatorStr.empty())
return emitOpError("applicable operator must not be empty");
// Only `*` and `&` are supported.
if (applicableOperatorStr != "&" && applicableOperatorStr != "*")
return emitOpError("applicable operator is illegal");
Type operandType = getOperand().getType();
Type resultType = getResult().getType();
if (applicableOperatorStr == "&") {
if (!llvm::isa<emitc::LValueType>(operandType))
return emitOpError("operand type must be an lvalue when applying `&`");
if (!llvm::isa<emitc::PointerType>(resultType))
return emitOpError("result type must be a pointer when applying `&`");
} else {
if (!llvm::isa<emitc::PointerType>(operandType))
return emitOpError("operand type must be a pointer when applying `*`");
}
return success();
}
//===----------------------------------------------------------------------===//
// AssignOp
//===----------------------------------------------------------------------===//
/// The assign op requires that the assigned value's type matches the
/// assigned-to variable type.
LogicalResult emitc::AssignOp::verify() {
TypedValue<emitc::LValueType> variable = getVar();
if (!variable.getDefiningOp())
return emitOpError() << "cannot assign to block argument";
Type valueType = getValue().getType();
Type variableType = variable.getType().getValueType();
if (variableType != valueType)
return emitOpError() << "requires value's type (" << valueType
<< ") to match variable's type (" << variableType
<< ")\n variable: " << variable
<< "\n value: " << getValue() << "\n";
return success();
}
//===----------------------------------------------------------------------===//
// CastOp
//===----------------------------------------------------------------------===//
bool CastOp::areCastCompatible(TypeRange inputs, TypeRange outputs) {
Type input = inputs.front(), output = outputs.front();
if (auto arrayType = dyn_cast<emitc::ArrayType>(input)) {
if (auto pointerType = dyn_cast<emitc::PointerType>(output)) {
return (arrayType.getElementType() == pointerType.getPointee()) &&
arrayType.getShape().size() == 1 && arrayType.getShape()[0] >= 1;
}
return false;
}
return (
(emitc::isIntegerIndexOrOpaqueType(input) ||
emitc::isSupportedFloatType(input) || isa<emitc::PointerType>(input)) &&
(emitc::isIntegerIndexOrOpaqueType(output) ||
emitc::isSupportedFloatType(output) || isa<emitc::PointerType>(output)));
}
//===----------------------------------------------------------------------===//
// CallOpaqueOp
//===----------------------------------------------------------------------===//
LogicalResult emitc::CallOpaqueOp::verify() {
// Callee must not be empty.
if (getCallee().empty())
return emitOpError("callee must not be empty");
if (std::optional<ArrayAttr> argsAttr = getArgs()) {
for (Attribute arg : *argsAttr) {
auto intAttr = llvm::dyn_cast<IntegerAttr>(arg);
if (intAttr && llvm::isa<IndexType>(intAttr.getType())) {
int64_t index = intAttr.getInt();
// Args with elements of type index must be in range
// [0..operands.size).
if ((index < 0) || (index >= static_cast<int64_t>(getNumOperands())))
return emitOpError("index argument is out of range");
// Args with elements of type ArrayAttr must have a type.
} else if (llvm::isa<ArrayAttr>(
arg) /*&& llvm::isa<NoneType>(arg.getType())*/) {
// FIXME: Array attributes never have types
return emitOpError("array argument has no type");
}
}
}
if (std::optional<ArrayAttr> templateArgsAttr = getTemplateArgs()) {
for (Attribute tArg : *templateArgsAttr) {
if (!llvm::isa<TypeAttr, IntegerAttr, FloatAttr, emitc::OpaqueAttr>(tArg))
return emitOpError("template argument has invalid type");
}
}
if (llvm::any_of(getResultTypes(), llvm::IsaPred<ArrayType>)) {
return emitOpError() << "cannot return array type";
}
return success();
}
//===----------------------------------------------------------------------===//
// ConstantOp
//===----------------------------------------------------------------------===//
LogicalResult emitc::ConstantOp::verify() {
Attribute value = getValueAttr();
if (failed(verifyInitializationAttribute(getOperation(), value)))
return failure();
if (auto opaqueValue = llvm::dyn_cast<emitc::OpaqueAttr>(value)) {
if (opaqueValue.getValue().empty())
return emitOpError() << "value must not be empty";
}
return success();
}
OpFoldResult emitc::ConstantOp::fold(FoldAdaptor adaptor) { return getValue(); }
//===----------------------------------------------------------------------===//
// ExpressionOp
//===----------------------------------------------------------------------===//
ParseResult ExpressionOp::parse(OpAsmParser &parser, OperationState &result) {
SmallVector<OpAsmParser::UnresolvedOperand> operands;
if (parser.parseOperandList(operands))
return parser.emitError(parser.getCurrentLocation()) << "expected operands";
if (succeeded(parser.parseOptionalKeyword("noinline")))
result.addAttribute(ExpressionOp::getDoNotInlineAttrName(result.name),
parser.getBuilder().getUnitAttr());
Type type;
if (parser.parseColonType(type))
return parser.emitError(parser.getCurrentLocation(),
"expected function type");
auto fnType = llvm::dyn_cast<FunctionType>(type);
if (!fnType)
return parser.emitError(parser.getCurrentLocation(),
"expected function type");
if (parser.resolveOperands(operands, fnType.getInputs(),
parser.getCurrentLocation(), result.operands))
return failure();
if (fnType.getNumResults() != 1)
return parser.emitError(parser.getCurrentLocation(),
"expected single return type");
result.addTypes(fnType.getResults());
Region *body = result.addRegion();
SmallVector<OpAsmParser::Argument> argsInfo;
for (auto [unresolvedOperand, operandType] :
llvm::zip(operands, fnType.getInputs())) {
OpAsmParser::Argument argInfo;
argInfo.ssaName = unresolvedOperand;
argInfo.type = operandType;
argsInfo.push_back(argInfo);
}
if (parser.parseRegion(*body, argsInfo, /*enableNameShadowing=*/true))
return failure();
return success();
}
void emitc::ExpressionOp::print(OpAsmPrinter &p) {
p << ' ';
p.printOperands(getDefs());
p << " : ";
p.printFunctionalType(getOperation());
p.shadowRegionArgs(getRegion(), getDefs());
p << ' ';
p.printRegion(getRegion(), /*printEntryBlockArgs=*/false);
}
Operation *ExpressionOp::getRootOp() {
auto yieldOp = cast<YieldOp>(getBody()->getTerminator());
Value yieldedValue = yieldOp.getResult();
return yieldedValue.getDefiningOp();
}
LogicalResult ExpressionOp::verify() {
Type resultType = getResult().getType();
Region ®ion = getRegion();
Block &body = region.front();
if (!body.mightHaveTerminator())
return emitOpError("must yield a value at termination");
auto yield = cast<YieldOp>(body.getTerminator());
Value yieldResult = yield.getResult();
if (!yieldResult)
return emitOpError("must yield a value at termination");
Operation *rootOp = yieldResult.getDefiningOp();
if (!rootOp)
return emitOpError("yielded value has no defining op");
if (rootOp->getParentOp() != getOperation())
return emitOpError("yielded value not defined within expression");
Type yieldType = yieldResult.getType();
if (resultType != yieldType)
return emitOpError("requires yielded type to match return type");
for (Operation &op : region.front().without_terminator()) {
auto expressionInterface = dyn_cast<emitc::CExpressionInterface>(op);
if (!expressionInterface)
return emitOpError("contains an unsupported operation");
if (op.getNumResults() != 1)
return emitOpError("requires exactly one result for each operation");
Value result = op.getResult(0);
if (result.use_empty())
return emitOpError("contains an unused operation");
}
// Make sure any operation with side effect is only reachable once from
// the root op, otherwise emission will be replicating side effects.
SmallPtrSet<Operation *, 16> visited;
SmallVector<Operation *> worklist;
worklist.push_back(rootOp);
while (!worklist.empty()) {
Operation *op = worklist.back();
worklist.pop_back();
if (visited.contains(op)) {
if (cast<CExpressionInterface>(op).hasSideEffects())
return emitOpError(
"requires exactly one use for operations with side effects");
}
visited.insert(op);
for (Value operand : op->getOperands())
if (Operation *def = operand.getDefiningOp()) {
worklist.push_back(def);
}
}
return success();
}
//===----------------------------------------------------------------------===//
// ForOp
//===----------------------------------------------------------------------===//
void ForOp::build(OpBuilder &builder, OperationState &result, Value lb,
Value ub, Value step, BodyBuilderFn bodyBuilder) {
OpBuilder::InsertionGuard g(builder);
result.addOperands({lb, ub, step});
Type t = lb.getType();
Region *bodyRegion = result.addRegion();
Block *bodyBlock = builder.createBlock(bodyRegion);
bodyBlock->addArgument(t, result.location);
// Create the default terminator if the builder is not provided.
if (!bodyBuilder) {
ForOp::ensureTerminator(*bodyRegion, builder, result.location);
} else {
OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToStart(bodyBlock);
bodyBuilder(builder, result.location, bodyBlock->getArgument(0));
}
}
void ForOp::getCanonicalizationPatterns(RewritePatternSet &, MLIRContext *) {}
ParseResult ForOp::parse(OpAsmParser &parser, OperationState &result) {
Builder &builder = parser.getBuilder();
Type type;
OpAsmParser::Argument inductionVariable;
OpAsmParser::UnresolvedOperand lb, ub, step;
// Parse the induction variable followed by '='.
if (parser.parseOperand(inductionVariable.ssaName) || parser.parseEqual() ||
// Parse loop bounds.
parser.parseOperand(lb) || parser.parseKeyword("to") ||
parser.parseOperand(ub) || parser.parseKeyword("step") ||
parser.parseOperand(step))
return failure();
// Parse the optional initial iteration arguments.
SmallVector<OpAsmParser::Argument, 4> regionArgs;
regionArgs.push_back(inductionVariable);
// Parse optional type, else assume Index.
if (parser.parseOptionalColon())
type = builder.getIndexType();
else if (parser.parseType(type))
return failure();
// Resolve input operands.
regionArgs.front().type = type;
if (parser.resolveOperand(lb, type, result.operands) ||
parser.resolveOperand(ub, type, result.operands) ||
parser.resolveOperand(step, type, result.operands))
return failure();
// Parse the body region.
Region *body = result.addRegion();
if (parser.parseRegion(*body, regionArgs))
return failure();
ForOp::ensureTerminator(*body, builder, result.location);
// Parse the optional attribute list.
if (parser.parseOptionalAttrDict(result.attributes))
return failure();
return success();
}
void ForOp::print(OpAsmPrinter &p) {
p << " " << getInductionVar() << " = " << getLowerBound() << " to "
<< getUpperBound() << " step " << getStep();
p << ' ';
if (Type t = getInductionVar().getType(); !t.isIndex())
p << " : " << t << ' ';
p.printRegion(getRegion(),
/*printEntryBlockArgs=*/false,
/*printBlockTerminators=*/false);
p.printOptionalAttrDict((*this)->getAttrs());
}
LogicalResult ForOp::verifyRegions() {
// Check that the body defines as single block argument for the induction
// variable.
if (getInductionVar().getType() != getLowerBound().getType())
return emitOpError(
"expected induction variable to be same type as bounds and step");
return success();
}
//===----------------------------------------------------------------------===//
// CallOp
//===----------------------------------------------------------------------===//
LogicalResult CallOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
// Check that the callee attribute was specified.
auto fnAttr = (*this)->getAttrOfType<FlatSymbolRefAttr>("callee");
if (!fnAttr)
return emitOpError("requires a 'callee' symbol reference attribute");
FuncOp fn = symbolTable.lookupNearestSymbolFrom<FuncOp>(*this, fnAttr);
if (!fn)
return emitOpError() << "'" << fnAttr.getValue()
<< "' does not reference a valid function";
// Verify that the operand and result types match the callee.
auto fnType = fn.getFunctionType();
if (fnType.getNumInputs() != getNumOperands())
return emitOpError("incorrect number of operands for callee");
for (unsigned i = 0, e = fnType.getNumInputs(); i != e; ++i)
if (getOperand(i).getType() != fnType.getInput(i))
return emitOpError("operand type mismatch: expected operand type ")
<< fnType.getInput(i) << ", but provided "
<< getOperand(i).getType() << " for operand number " << i;
if (fnType.getNumResults() != getNumResults())
return emitOpError("incorrect number of results for callee");
for (unsigned i = 0, e = fnType.getNumResults(); i != e; ++i)
if (getResult(i).getType() != fnType.getResult(i)) {
auto diag = emitOpError("result type mismatch at index ") << i;
diag.attachNote() << " op result types: " << getResultTypes();
diag.attachNote() << "function result types: " << fnType.getResults();
return diag;
}
return success();
}
FunctionType CallOp::getCalleeType() {
return FunctionType::get(getContext(), getOperandTypes(), getResultTypes());
}
//===----------------------------------------------------------------------===//
// DeclareFuncOp
//===----------------------------------------------------------------------===//
LogicalResult
DeclareFuncOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
// Check that the sym_name attribute was specified.
auto fnAttr = getSymNameAttr();
if (!fnAttr)
return emitOpError("requires a 'sym_name' symbol reference attribute");
FuncOp fn = symbolTable.lookupNearestSymbolFrom<FuncOp>(*this, fnAttr);
if (!fn)
return emitOpError() << "'" << fnAttr.getValue()
<< "' does not reference a valid function";
return success();
}
//===----------------------------------------------------------------------===//
// FuncOp
//===----------------------------------------------------------------------===//
void FuncOp::build(OpBuilder &builder, OperationState &state, StringRef name,
FunctionType type, ArrayRef<NamedAttribute> attrs,
ArrayRef<DictionaryAttr> argAttrs) {
state.addAttribute(SymbolTable::getSymbolAttrName(),
builder.getStringAttr(name));
state.addAttribute(getFunctionTypeAttrName(state.name), TypeAttr::get(type));
state.attributes.append(attrs.begin(), attrs.end());
state.addRegion();
if (argAttrs.empty())
return;
assert(type.getNumInputs() == argAttrs.size());
call_interface_impl::addArgAndResultAttrs(
builder, state, argAttrs, /*resultAttrs=*/{},
getArgAttrsAttrName(state.name), getResAttrsAttrName(state.name));
}
ParseResult FuncOp::parse(OpAsmParser &parser, OperationState &result) {
auto buildFuncType =
[](Builder &builder, ArrayRef<Type> argTypes, ArrayRef<Type> results,
function_interface_impl::VariadicFlag,
std::string &) { return builder.getFunctionType(argTypes, results); };
return function_interface_impl::parseFunctionOp(
parser, result, /*allowVariadic=*/false,
getFunctionTypeAttrName(result.name), buildFuncType,
getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
}
void FuncOp::print(OpAsmPrinter &p) {
function_interface_impl::printFunctionOp(
p, *this, /*isVariadic=*/false, getFunctionTypeAttrName(),
getArgAttrsAttrName(), getResAttrsAttrName());
}
LogicalResult FuncOp::verify() {
if (llvm::any_of(getArgumentTypes(), llvm::IsaPred<LValueType>)) {
return emitOpError("cannot have lvalue type as argument");
}
if (getNumResults() > 1)
return emitOpError("requires zero or exactly one result, but has ")
<< getNumResults();
if (getNumResults() == 1 && isa<ArrayType>(getResultTypes()[0]))
return emitOpError("cannot return array type");
return success();
}
//===----------------------------------------------------------------------===//
// ReturnOp
//===----------------------------------------------------------------------===//
LogicalResult ReturnOp::verify() {
auto function = cast<FuncOp>((*this)->getParentOp());
// The operand number and types must match the function signature.
if (getNumOperands() != function.getNumResults())
return emitOpError("has ")
<< getNumOperands() << " operands, but enclosing function (@"
<< function.getName() << ") returns " << function.getNumResults();
if (function.getNumResults() == 1)
if (getOperand().getType() != function.getResultTypes()[0])
return emitError() << "type of the return operand ("
<< getOperand().getType()
<< ") doesn't match function result type ("
<< function.getResultTypes()[0] << ")"
<< " in function @" << function.getName();
return success();
}
//===----------------------------------------------------------------------===//
// IfOp
//===----------------------------------------------------------------------===//
void IfOp::build(OpBuilder &builder, OperationState &result, Value cond,
bool addThenBlock, bool addElseBlock) {
assert((!addElseBlock || addThenBlock) &&
"must not create else block w/o then block");
result.addOperands(cond);
// Add regions and blocks.
OpBuilder::InsertionGuard guard(builder);
Region *thenRegion = result.addRegion();
if (addThenBlock)
builder.createBlock(thenRegion);
Region *elseRegion = result.addRegion();
if (addElseBlock)
builder.createBlock(elseRegion);
}
void IfOp::build(OpBuilder &builder, OperationState &result, Value cond,
bool withElseRegion) {
result.addOperands(cond);
// Build then region.
OpBuilder::InsertionGuard guard(builder);
Region *thenRegion = result.addRegion();
builder.createBlock(thenRegion);
// Build else region.
Region *elseRegion = result.addRegion();
if (withElseRegion) {
builder.createBlock(elseRegion);
}
}
void IfOp::build(OpBuilder &builder, OperationState &result, Value cond,
function_ref<void(OpBuilder &, Location)> thenBuilder,
function_ref<void(OpBuilder &, Location)> elseBuilder) {
assert(thenBuilder && "the builder callback for 'then' must be present");
result.addOperands(cond);
// Build then region.
OpBuilder::InsertionGuard guard(builder);
Region *thenRegion = result.addRegion();
builder.createBlock(thenRegion);
thenBuilder(builder, result.location);
// Build else region.
Region *elseRegion = result.addRegion();
if (elseBuilder) {
builder.createBlock(elseRegion);
elseBuilder(builder, result.location);
}
}
ParseResult IfOp::parse(OpAsmParser &parser, OperationState &result) {
// Create the regions for 'then'.
result.regions.reserve(2);
Region *thenRegion = result.addRegion();
Region *elseRegion = result.addRegion();
Builder &builder = parser.getBuilder();
OpAsmParser::UnresolvedOperand cond;
Type i1Type = builder.getIntegerType(1);
if (parser.parseOperand(cond) ||
parser.resolveOperand(cond, i1Type, result.operands))
return failure();
// Parse the 'then' region.
if (parser.parseRegion(*thenRegion, /*arguments=*/{}, /*argTypes=*/{}))
return failure();
IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location);
// If we find an 'else' keyword then parse the 'else' region.
if (!parser.parseOptionalKeyword("else")) {
if (parser.parseRegion(*elseRegion, /*arguments=*/{}, /*argTypes=*/{}))
return failure();
IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location);
}
// Parse the optional attribute list.
if (parser.parseOptionalAttrDict(result.attributes))
return failure();
return success();
}
void IfOp::print(OpAsmPrinter &p) {
bool printBlockTerminators = false;
p << " " << getCondition();
p << ' ';
p.printRegion(getThenRegion(),
/*printEntryBlockArgs=*/false,
/*printBlockTerminators=*/printBlockTerminators);
// Print the 'else' regions if it exists and has a block.
Region &elseRegion = getElseRegion();
if (!elseRegion.empty()) {
p << " else ";
p.printRegion(elseRegion,
/*printEntryBlockArgs=*/false,
/*printBlockTerminators=*/printBlockTerminators);
}
p.printOptionalAttrDict((*this)->getAttrs());
}
/// Given the region at `index`, or the parent operation if `index` is None,
/// return the successor regions. These are the regions that may be selected
/// during the flow of control. `operands` is a set of optional attributes
/// that correspond to a constant value for each operand, or null if that
/// operand is not a constant.
void IfOp::getSuccessorRegions(RegionBranchPoint point,
SmallVectorImpl<RegionSuccessor> ®ions) {
// The `then` and the `else` region branch back to the parent operation.
if (!point.isParent()) {
regions.push_back(RegionSuccessor());
return;
}
regions.push_back(RegionSuccessor(&getThenRegion()));
// Don't consider the else region if it is empty.
Region *elseRegion = &this->getElseRegion();
if (elseRegion->empty())
regions.push_back(RegionSuccessor());
else
regions.push_back(RegionSuccessor(elseRegion));
}
void IfOp::getEntrySuccessorRegions(ArrayRef<Attribute> operands,
SmallVectorImpl<RegionSuccessor> ®ions) {
FoldAdaptor adaptor(operands, *this);
auto boolAttr = dyn_cast_or_null<BoolAttr>(adaptor.getCondition());
if (!boolAttr || boolAttr.getValue())
regions.emplace_back(&getThenRegion());
// If the else region is empty, execution continues after the parent op.
if (!boolAttr || !boolAttr.getValue()) {
if (!getElseRegion().empty())
regions.emplace_back(&getElseRegion());
else
regions.emplace_back();
}
}
void IfOp::getRegionInvocationBounds(
ArrayRef<Attribute> operands,
SmallVectorImpl<InvocationBounds> &invocationBounds) {
if (auto cond = llvm::dyn_cast_or_null<BoolAttr>(operands[0])) {
// If the condition is known, then one region is known to be executed once
// and the other zero times.
invocationBounds.emplace_back(0, cond.getValue() ? 1 : 0);
invocationBounds.emplace_back(0, cond.getValue() ? 0 : 1);
} else {
// Non-constant condition. Each region may be executed 0 or 1 times.
invocationBounds.assign(2, {0, 1});
}
}
//===----------------------------------------------------------------------===//
// IncludeOp
//===----------------------------------------------------------------------===//
void IncludeOp::print(OpAsmPrinter &p) {
bool standardInclude = getIsStandardInclude();
p << " ";
if (standardInclude)
p << "<";
p << "\"" << getInclude() << "\"";
if (standardInclude)
p << ">";
}
ParseResult IncludeOp::parse(OpAsmParser &parser, OperationState &result) {
bool standardInclude = !parser.parseOptionalLess();
StringAttr include;
OptionalParseResult includeParseResult =
parser.parseOptionalAttribute(include, "include", result.attributes);
if (!includeParseResult.has_value())
return parser.emitError(parser.getNameLoc()) << "expected string attribute";
if (standardInclude && parser.parseOptionalGreater())
return parser.emitError(parser.getNameLoc())
<< "expected trailing '>' for standard include";
if (standardInclude)
result.addAttribute("is_standard_include",
UnitAttr::get(parser.getContext()));
return success();
}
//===----------------------------------------------------------------------===//
// LiteralOp
//===----------------------------------------------------------------------===//
/// The literal op requires a non-empty value.
LogicalResult emitc::LiteralOp::verify() {
if (getValue().empty())
return emitOpError() << "value must not be empty";
return success();
}
//===----------------------------------------------------------------------===//
// SubOp
//===----------------------------------------------------------------------===//
LogicalResult SubOp::verify() {
Type lhsType = getLhs().getType();
Type rhsType = getRhs().getType();
Type resultType = getResult().getType();
if (isa<emitc::PointerType>(rhsType) && !isa<emitc::PointerType>(lhsType))
return emitOpError("rhs can only be a pointer if lhs is a pointer");
if (isa<emitc::PointerType>(lhsType) &&
!isa<IntegerType, emitc::OpaqueType, emitc::PointerType>(rhsType))
return emitOpError("requires that rhs is an integer, pointer or of opaque "
"type if lhs is a pointer");
if (isa<emitc::PointerType>(lhsType) && isa<emitc::PointerType>(rhsType) &&
!isa<IntegerType, emitc::PtrDiffTType, emitc::OpaqueType>(resultType))
return emitOpError("requires that the result is an integer, ptrdiff_t or "
"of opaque type if lhs and rhs are pointers");
return success();
}
//===----------------------------------------------------------------------===//
// VariableOp
//===----------------------------------------------------------------------===//
LogicalResult emitc::VariableOp::verify() {
return verifyInitializationAttribute(getOperation(), getValueAttr());
}
//===----------------------------------------------------------------------===//
// YieldOp
//===----------------------------------------------------------------------===//
LogicalResult emitc::YieldOp::verify() {
Value result = getResult();
Operation *containingOp = getOperation()->getParentOp();
if (!isa<DoOp>(containingOp) && result && containingOp->getNumResults() != 1)
return emitOpError() << "yields a value not returned by parent";
if (!isa<DoOp>(containingOp) && !result && containingOp->getNumResults() != 0)
return emitOpError() << "does not yield a value to be returned by parent";
return success();
}
//===----------------------------------------------------------------------===//
// SubscriptOp
//===----------------------------------------------------------------------===//
LogicalResult emitc::SubscriptOp::verify() {
// Checks for array operand.
if (auto arrayType = llvm::dyn_cast<emitc::ArrayType>(getValue().getType())) {
// Check number of indices.
if (getIndices().size() != (size_t)arrayType.getRank()) {
return emitOpError() << "on array operand requires number of indices ("
<< getIndices().size()
<< ") to match the rank of the array type ("
<< arrayType.getRank() << ")";
}
// Check types of index operands.
for (unsigned i = 0, e = getIndices().size(); i != e; ++i) {
Type type = getIndices()[i].getType();
if (!isIntegerIndexOrOpaqueType(type)) {
return emitOpError() << "on array operand requires index operand " << i
<< " to be integer-like, but got " << type;
}
}
// Check element type.
Type elementType = arrayType.getElementType();
Type resultType = getType().getValueType();
if (elementType != resultType) {
return emitOpError() << "on array operand requires element type ("
<< elementType << ") and result type (" << resultType
<< ") to match";
}
return success();
}
// Checks for pointer operand.
if (auto pointerType =
llvm::dyn_cast<emitc::PointerType>(getValue().getType())) {
// Check number of indices.
if (getIndices().size() != 1) {
return emitOpError()
<< "on pointer operand requires one index operand, but got "
<< getIndices().size();
}
// Check types of index operand.
Type type = getIndices()[0].getType();
if (!isIntegerIndexOrOpaqueType(type)) {
return emitOpError() << "on pointer operand requires index operand to be "
"integer-like, but got "
<< type;
}
// Check pointee type.
Type pointeeType = pointerType.getPointee();
Type resultType = getType().getValueType();
if (pointeeType != resultType) {
return emitOpError() << "on pointer operand requires pointee type ("
<< pointeeType << ") and result type (" << resultType
<< ") to match";
}
return success();
}
// The operand has opaque type, so we can't assume anything about the number
// or types of index operands.
return success();
}
//===----------------------------------------------------------------------===//
// VerbatimOp
//===----------------------------------------------------------------------===//
LogicalResult emitc::VerbatimOp::verify() {
auto errorCallback = [&]() -> InFlightDiagnostic {
return this->emitOpError();
};
FailureOr<SmallVector<ReplacementItem>> fmt =
::parseFormatString(getValue(), getFmtArgs(), errorCallback);
if (failed(fmt))
return failure();
size_t numPlaceholders = llvm::count_if(*fmt, [](ReplacementItem &item) {
return std::holds_alternative<Placeholder>(item);
});
if (numPlaceholders != getFmtArgs().size()) {
return emitOpError()
<< "requires operands for each placeholder in the format string";
}
return success();
}
FailureOr<SmallVector<ReplacementItem>> emitc::VerbatimOp::parseFormatString() {
// Error checking is done in verify.
return ::parseFormatString(getValue(), getFmtArgs());
}
//===----------------------------------------------------------------------===//
// EmitC Enums
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/EmitC/IR/EmitCEnums.cpp.inc"
//===----------------------------------------------------------------------===//
// EmitC Attributes
//===----------------------------------------------------------------------===//
#define GET_ATTRDEF_CLASSES
#include "mlir/Dialect/EmitC/IR/EmitCAttributes.cpp.inc"
//===----------------------------------------------------------------------===//
// EmitC Types
//===----------------------------------------------------------------------===//
#define GET_TYPEDEF_CLASSES
#include "mlir/Dialect/EmitC/IR/EmitCTypes.cpp.inc"
//===----------------------------------------------------------------------===//
// ArrayType
//===----------------------------------------------------------------------===//
Type emitc::ArrayType::parse(AsmParser &parser) {
if (parser.parseLess())
return Type();
SmallVector<int64_t, 4> dimensions;
if (parser.parseDimensionList(dimensions, /*allowDynamic=*/false,
/*withTrailingX=*/true))
return Type();
// Parse the element type.
auto typeLoc = parser.getCurrentLocation();
Type elementType;
if (parser.parseType(elementType))
return Type();
// Check that array is formed from allowed types.
if (!isValidElementType(elementType))
return parser.emitError(typeLoc, "invalid array element type '")
<< elementType << "'",
Type();
if (parser.parseGreater())
return Type();
return parser.getChecked<ArrayType>(dimensions, elementType);
}
void emitc::ArrayType::print(AsmPrinter &printer) const {
printer << "<";
for (int64_t dim : getShape()) {
printer << dim << 'x';
}
printer.printType(getElementType());
printer << ">";
}
LogicalResult emitc::ArrayType::verify(
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
::llvm::ArrayRef<int64_t> shape, Type elementType) {
if (shape.empty())
return emitError() << "shape must not be empty";
for (int64_t dim : shape) {
if (dim < 0)
return emitError() << "dimensions must have non-negative size";
}
if (!elementType)
return emitError() << "element type must not be none";
if (!isValidElementType(elementType))
return emitError() << "invalid array element type";
return success();
}
emitc::ArrayType
emitc::ArrayType::cloneWith(std::optional<ArrayRef<int64_t>> shape,
Type elementType) const {
if (!shape)
return emitc::ArrayType::get(getShape(), elementType);
return emitc::ArrayType::get(*shape, elementType);
}
//===----------------------------------------------------------------------===//
// LValueType
//===----------------------------------------------------------------------===//
LogicalResult mlir::emitc::LValueType::verify(
llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
mlir::Type value) {
// Check that the wrapped type is valid. This especially forbids nested
// lvalue types.
if (!isSupportedEmitCType(value))
return emitError()
<< "!emitc.lvalue must wrap supported emitc type, but got " << value;
if (llvm::isa<emitc::ArrayType>(value))
return emitError() << "!emitc.lvalue cannot wrap !emitc.array type";
return success();
}
//===----------------------------------------------------------------------===//
// OpaqueType
//===----------------------------------------------------------------------===//
LogicalResult mlir::emitc::OpaqueType::verify(
llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
llvm::StringRef value) {
if (value.empty()) {
return emitError() << "expected non empty string in !emitc.opaque type";
}
if (value.back() == '*') {
return emitError() << "pointer not allowed as outer type with "
"!emitc.opaque, use !emitc.ptr instead";
}
return success();
}
//===----------------------------------------------------------------------===//
// PointerType
//===----------------------------------------------------------------------===//
LogicalResult mlir::emitc::PointerType::verify(
llvm::function_ref<mlir::InFlightDiagnostic()> emitError, Type value) {
if (llvm::isa<emitc::LValueType>(value))
return emitError() << "pointers to lvalues are not allowed";
return success();
}
//===----------------------------------------------------------------------===//
// GlobalOp
//===----------------------------------------------------------------------===//
static void printEmitCGlobalOpTypeAndInitialValue(OpAsmPrinter &p, GlobalOp op,
TypeAttr type,
Attribute initialValue) {
p << type;
if (initialValue) {
p << " = ";
p.printAttributeWithoutType(initialValue);
}
}
static Type getInitializerTypeForGlobal(Type type) {
if (auto array = llvm::dyn_cast<ArrayType>(type))
return RankedTensorType::get(array.getShape(), array.getElementType());
return type;
}
static ParseResult
parseEmitCGlobalOpTypeAndInitialValue(OpAsmParser &parser, TypeAttr &typeAttr,
Attribute &initialValue) {
Type type;
if (parser.parseType(type))
return failure();
typeAttr = TypeAttr::get(type);
if (parser.parseOptionalEqual())
return success();
if (parser.parseAttribute(initialValue, getInitializerTypeForGlobal(type)))
return failure();
if (!llvm::isa<ElementsAttr, IntegerAttr, FloatAttr, emitc::OpaqueAttr>(
initialValue))
return parser.emitError(parser.getNameLoc())
<< "initial value should be a integer, float, elements or opaque "
"attribute";
return success();
}
LogicalResult GlobalOp::verify() {
if (!isSupportedEmitCType(getType())) {
return emitOpError("expected valid emitc type");
}
if (getInitialValue().has_value()) {
Attribute initValue = getInitialValue().value();
// Check that the type of the initial value is compatible with the type of
// the global variable.
if (auto elementsAttr = llvm::dyn_cast<ElementsAttr>(initValue)) {
auto arrayType = llvm::dyn_cast<ArrayType>(getType());
if (!arrayType)
return emitOpError("expected array type, but got ") << getType();
Type initType = elementsAttr.getType();
Type tensorType = getInitializerTypeForGlobal(getType());
if (initType != tensorType) {
return emitOpError("initial value expected to be of type ")
<< getType() << ", but was of type " << initType;
}
} else if (auto intAttr = dyn_cast<IntegerAttr>(initValue)) {
if (intAttr.getType() != getType()) {
return emitOpError("initial value expected to be of type ")
<< getType() << ", but was of type " << intAttr.getType();
}
} else if (auto floatAttr = dyn_cast<FloatAttr>(initValue)) {
if (floatAttr.getType() != getType()) {
return emitOpError("initial value expected to be of type ")
<< getType() << ", but was of type " << floatAttr.getType();
}
} else if (!isa<emitc::OpaqueAttr>(initValue)) {
return emitOpError("initial value should be a integer, float, elements "
"or opaque attribute, but got ")
<< initValue;
}
}
if (getStaticSpecifier() && getExternSpecifier()) {
return emitOpError("cannot have both static and extern specifiers");
}
return success();
}
//===----------------------------------------------------------------------===//
// GetGlobalOp
//===----------------------------------------------------------------------===//
LogicalResult
GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
// Verify that the type matches the type of the global variable.
auto global =
symbolTable.lookupNearestSymbolFrom<GlobalOp>(*this, getNameAttr());
if (!global)
return emitOpError("'")
<< getName() << "' does not reference a valid emitc.global";
Type resultType = getResult().getType();
Type globalType = global.getType();
// global has array type
if (llvm::isa<ArrayType>(globalType)) {
if (globalType != resultType)
return emitOpError("on array type expects result type ")
<< resultType << " to match type " << globalType
<< " of the global @" << getName();
return success();
}
// global has non-array type
auto lvalueType = dyn_cast<LValueType>(resultType);
if (!lvalueType)
return emitOpError("on non-array type expects result type to be an "
"lvalue type for the global @")
<< getName();
if (lvalueType.getValueType() != globalType)
return emitOpError("on non-array type expects result inner type ")
<< lvalueType.getValueType() << " to match type " << globalType
<< " of the global @" << getName();
return success();
}
//===----------------------------------------------------------------------===//
// SwitchOp
//===----------------------------------------------------------------------===//
/// Parse the case regions and values.
static ParseResult
parseSwitchCases(OpAsmParser &parser, DenseI64ArrayAttr &cases,
SmallVectorImpl<std::unique_ptr<Region>> &caseRegions) {
SmallVector<int64_t> caseValues;
while (succeeded(parser.parseOptionalKeyword("case"))) {
int64_t value;
Region ®ion = *caseRegions.emplace_back(std::make_unique<Region>());
if (parser.parseInteger(value) ||
parser.parseRegion(region, /*arguments=*/{}))
return failure();
caseValues.push_back(value);
}
cases = parser.getBuilder().getDenseI64ArrayAttr(caseValues);
return success();
}
/// Print the case regions and values.
static void printSwitchCases(OpAsmPrinter &p, Operation *op,
DenseI64ArrayAttr cases, RegionRange caseRegions) {
for (auto [value, region] : llvm::zip(cases.asArrayRef(), caseRegions)) {
p.printNewline();
p << "case " << value << ' ';
p.printRegion(*region, /*printEntryBlockArgs=*/false);
}
}
static LogicalResult verifyRegion(emitc::SwitchOp op, Region ®ion,
const Twine &name) {
auto yield = dyn_cast<emitc::YieldOp>(region.front().back());
if (!yield)
return op.emitOpError("expected region to end with emitc.yield, but got ")
<< region.front().back().getName();
if (yield.getNumOperands() != 0) {
return (op.emitOpError("expected each region to return ")
<< "0 values, but " << name << " returns "
<< yield.getNumOperands())
.attachNote(yield.getLoc())
<< "see yield operation here";
}
return success();
}
LogicalResult emitc::SwitchOp::verify() {
if (!isIntegerIndexOrOpaqueType(getArg().getType()))
return emitOpError("unsupported type ") << getArg().getType();
if (getCases().size() != getCaseRegions().size()) {
return emitOpError("has ")
<< getCaseRegions().size() << " case regions but "
<< getCases().size() << " case values";
}
DenseSet<int64_t> valueSet;
for (int64_t value : getCases())
if (!valueSet.insert(value).second)
return emitOpError("has duplicate case value: ") << value;
if (failed(verifyRegion(*this, getDefaultRegion(), "default region")))
return failure();
for (auto [idx, caseRegion] : llvm::enumerate(getCaseRegions()))
if (failed(verifyRegion(*this, caseRegion, "case region #" + Twine(idx))))
return failure();
return success();
}
unsigned emitc::SwitchOp::getNumCases() { return getCases().size(); }
Block &emitc::SwitchOp::getDefaultBlock() { return getDefaultRegion().front(); }
Block &emitc::SwitchOp::getCaseBlock(unsigned idx) {
assert(idx < getNumCases() && "case index out-of-bounds");
return getCaseRegions()[idx].front();
}
void SwitchOp::getSuccessorRegions(
RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> &successors) {
llvm::append_range(successors, getRegions());
}
void SwitchOp::getEntrySuccessorRegions(
ArrayRef<Attribute> operands,
SmallVectorImpl<RegionSuccessor> &successors) {
FoldAdaptor adaptor(operands, *this);
// If a constant was not provided, all regions are possible successors.
auto arg = dyn_cast_or_null<IntegerAttr>(adaptor.getArg());
if (!arg) {
llvm::append_range(successors, getRegions());
return;
}
// Otherwise, try to find a case with a matching value. If not, the
// default region is the only successor.
for (auto [caseValue, caseRegion] : llvm::zip(getCases(), getCaseRegions())) {
if (caseValue == arg.getInt()) {
successors.emplace_back(&caseRegion);
return;
}
}
successors.emplace_back(&getDefaultRegion());
}
void SwitchOp::getRegionInvocationBounds(
ArrayRef<Attribute> operands, SmallVectorImpl<InvocationBounds> &bounds) {
auto operandValue = llvm::dyn_cast_or_null<IntegerAttr>(operands.front());
if (!operandValue) {
// All regions are invoked at most once.
bounds.append(getNumRegions(), InvocationBounds(/*lb=*/0, /*ub=*/1));
return;
}
unsigned liveIndex = getNumRegions() - 1;
const auto *iteratorToInt = llvm::find(getCases(), operandValue.getInt());
liveIndex = iteratorToInt != getCases().end()
? std::distance(getCases().begin(), iteratorToInt)
: liveIndex;
for (unsigned regIndex = 0, regNum = getNumRegions(); regIndex < regNum;
++regIndex)
bounds.emplace_back(/*lb=*/0, /*ub=*/regIndex == liveIndex);
}
//===----------------------------------------------------------------------===//
// FileOp
//===----------------------------------------------------------------------===//
void FileOp::build(OpBuilder &builder, OperationState &state, StringRef id) {
state.addRegion()->emplaceBlock();
state.attributes.push_back(
builder.getNamedAttr("id", builder.getStringAttr(id)));
}
//===----------------------------------------------------------------------===//
// FieldOp
//===----------------------------------------------------------------------===//
static void printEmitCFieldOpTypeAndInitialValue(OpAsmPrinter &p, FieldOp op,
TypeAttr type,
Attribute initialValue) {
p << type;
if (initialValue) {
p << " = ";
p.printAttributeWithoutType(initialValue);
}
}
static Type getInitializerTypeForField(Type type) {
if (auto array = llvm::dyn_cast<ArrayType>(type))
return RankedTensorType::get(array.getShape(), array.getElementType());
return type;
}
static ParseResult
parseEmitCFieldOpTypeAndInitialValue(OpAsmParser &parser, TypeAttr &typeAttr,
Attribute &initialValue) {
Type type;
if (parser.parseType(type))
return failure();
typeAttr = TypeAttr::get(type);
if (parser.parseOptionalEqual())
return success();
if (parser.parseAttribute(initialValue, getInitializerTypeForField(type)))
return failure();
if (!llvm::isa<ElementsAttr, IntegerAttr, FloatAttr, emitc::OpaqueAttr>(
initialValue))
return parser.emitError(parser.getNameLoc())
<< "initial value should be a integer, float, elements or opaque "
"attribute";
return success();
}
LogicalResult FieldOp::verify() {
if (!isSupportedEmitCType(getType()))
return emitOpError("expected valid emitc type");
Operation *parentOp = getOperation()->getParentOp();
if (!parentOp || !isa<emitc::ClassOp>(parentOp))
return emitOpError("field must be nested within an emitc.class operation");
StringAttr symName = getSymNameAttr();
if (!symName || symName.getValue().empty())
return emitOpError("field must have a non-empty symbol name");
return success();
}
//===----------------------------------------------------------------------===//
// GetFieldOp
//===----------------------------------------------------------------------===//
LogicalResult GetFieldOp::verify() {
auto parentClassOp = getOperation()->getParentOfType<emitc::ClassOp>();
if (!parentClassOp.getOperation())
return emitOpError(" must be nested within an emitc.class operation");
return success();
}
LogicalResult GetFieldOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
mlir::FlatSymbolRefAttr fieldNameAttr = getFieldNameAttr();
FieldOp fieldOp =
symbolTable.lookupNearestSymbolFrom<FieldOp>(*this, fieldNameAttr);
if (!fieldOp)
return emitOpError("field '")
<< fieldNameAttr << "' not found in the class";
Type getFieldResultType = getResult().getType();
Type fieldType = fieldOp.getType();
if (fieldType != getFieldResultType)
return emitOpError("result type ")
<< getFieldResultType << " does not match field '" << fieldNameAttr
<< "' type " << fieldType;
return success();
}
//===----------------------------------------------------------------------===//
// DoOp
//===----------------------------------------------------------------------===//
void DoOp::print(OpAsmPrinter &p) {
p << ' ';
p.printRegion(getBodyRegion(), /*printEntryBlockArgs=*/false);
p << " while ";
p.printRegion(getConditionRegion());
p.printOptionalAttrDictWithKeyword(getOperation()->getAttrs());
}
LogicalResult emitc::DoOp::verify() {
Block &condBlock = getConditionRegion().front();
if (condBlock.getOperations().size() != 2)
return emitOpError(
"condition region must contain exactly two operations: "
"'emitc.expression' followed by 'emitc.yield', but found ")
<< condBlock.getOperations().size() << " operations";
Operation &first = condBlock.front();
auto exprOp = dyn_cast<emitc::ExpressionOp>(first);
if (!exprOp)
return emitOpError("expected first op in condition region to be "
"'emitc.expression', but got ")
<< first.getName();
if (!exprOp.getResult().getType().isInteger(1))
return emitOpError("emitc.expression in condition region must return "
"'i1', but returns ")
<< exprOp.getResult().getType();
Operation &last = condBlock.back();
auto condYield = dyn_cast<emitc::YieldOp>(last);
if (!condYield)
return emitOpError("expected last op in condition region to be "
"'emitc.yield', but got ")
<< last.getName();
if (condYield.getNumOperands() != 1)
return emitOpError("expected condition region to return 1 value, but "
"it returns ")
<< condYield.getNumOperands() << " values";
if (condYield.getOperand(0) != exprOp.getResult())
return emitError("'emitc.yield' must return result of "
"'emitc.expression' from this condition region");
Block &bodyBlock = getBodyRegion().front();
if (bodyBlock.mightHaveTerminator())
return emitOpError("body region must not contain terminator");
return success();
}
ParseResult DoOp::parse(OpAsmParser &parser, OperationState &result) {
Region *bodyRegion = result.addRegion();
Region *condRegion = result.addRegion();
if (parser.parseRegion(*bodyRegion) || parser.parseKeyword("while") ||
parser.parseRegion(*condRegion))
return failure();
if (bodyRegion->empty())
bodyRegion->emplaceBlock();
return parser.parseOptionalAttrDictWithKeyword(result.attributes);
}
//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/EmitC/IR/EmitCInterfaces.cpp.inc"
#define GET_OP_CLASSES
#include "mlir/Dialect/EmitC/IR/EmitC.cpp.inc"
|