-
Notifications
You must be signed in to change notification settings - Fork 10
/
sair_ops.cc
2118 lines (1874 loc) · 82 KB
/
sair_ops.cc
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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sair_ops.h"
#include <algorithm>
#include <map>
#include <optional>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/raw_ostream.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/OperationSupport.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/Types.h"
#include "mlir/IR/Value.h"
#include "mlir/IR/Visitors.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
#include "loop_nest.h"
#include "sair_attributes.h"
#include "sair_dialect.h"
#include "sair_op_interfaces.h"
#include "sair_types.h"
#include "sequence.h"
#include "storage.h"
#include "util.h"
namespace sair {
namespace {
// Parses a Sair value access if present. Returns std::nullopt if no Sair value
// access is present, and a ParseResult indicating the parsing status otherwise.
// Populates "value" and "mapping" with an operand placeholder and a
// mapping attribute on success.
OptionalParseResult ParseOptionalValueAccess(
int num_dimensions, mlir::OpAsmParser &parser,
mlir::OpAsmParser::UnresolvedOperand &value, MappingAttr &mapping) {
OptionalParseResult has_operand = parser.parseOptionalOperand(value);
if (!has_operand.has_value() || mlir::failed(has_operand.value()))
return has_operand;
llvm::SMLoc loc = parser.getCurrentLocation();
if (!(mapping = ParseOptionalMapping(parser, num_dimensions))) {
return mlir::failure();
}
if (mapping.HasNoneExprs() || mapping.HasUnknownExprs()) {
return parser.emitError(loc)
<< "expected mapping to a concrete element, got 'none' or '?'";
}
return mlir::success(mapping != nullptr);
}
// Parses a potentially empty list of Sair value operands with corresponding
// mappings.
//
// value-list ::= epsilon
// | ssa-value mapping (`,` ssa-value mapping)*
ParseResult ParseOperandList(
int num_dimensions, mlir::OpAsmParser &parser,
llvm::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand> &operands,
llvm::SmallVectorImpl<MappingAttr> &mappings) {
// Try parsing a value access. If there is no operand in the parsing stream,
// interpret it as having parsed an empty operand list and succeed.
mlir::OpAsmParser::UnresolvedOperand first_operand;
MappingAttr first_mapping;
OptionalParseResult has_first_operand = ParseOptionalValueAccess(
num_dimensions, parser, first_operand, first_mapping);
if (!has_first_operand.has_value()) {
return mlir::success();
}
if (mlir::failed(has_first_operand.value())) {
return mlir::failure();
}
operands.emplace_back(first_operand);
mappings.emplace_back(first_mapping);
// If there was an operand, attempt parsing succeeding operands that are
// separated by commas.
while (mlir::succeeded(parser.parseOptionalComma())) {
if (mlir::failed(ParseValueAccess(num_dimensions, parser,
operands.emplace_back(),
mappings.emplace_back()))) {
return mlir::failure();
}
}
return mlir::success();
}
// Checks the type of an operand and the shape of its mapping. Registers the
// operand in result.
ParseResult ResolveOperand(const mlir::OpAsmParser::UnresolvedOperand &operand,
MappingAttr mapping, DomainShapeAttr shape,
mlir::Type element_type, mlir::OpAsmParser &parser,
mlir::OperationState &result) {
AttrLocation loc(parser.getEncodedSourceLoc(operand.location),
"operand mapping");
if (mlir::failed(VerifyMappingShape(loc, mapping, shape))) {
return mlir::failure();
}
auto type = ValueType::get(shape, element_type).AccessedType(mapping);
return parser.resolveOperand(operand, type, result.operands);
}
} // namespace
// Parses the range operator. This operation has an iteration domain and
// accesses a single Sair value with index elements. The syntax for the range
// operator is the following.
//
// range-op ::= `sair.dyn_range` domain ssa-value mapping
//
ParseResult SairDynRangeOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
mlir::Builder &builder = parser.getBuilder();
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain;
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 2> operands;
llvm::SmallVector<MappingAttr, 2> mappings;
DynRangeType type;
if (ParseDomain(parser, domain) ||
ParseOperandList(domain.size(), parser, operands, mappings)) {
return failure();
}
if (succeeded(parser.parseOptionalKeyword(RangeOp::kStepAttrName))) {
auto index_type = parser.getBuilder().getIndexType();
mlir::Attribute step;
if (mlir::failed(parser.parseAttribute(
step, index_type, RangeOp::kStepAttrName, result.attributes))) {
return failure();
}
}
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType<DynRangeType>(type) ||
parser.addTypeToList(type, result.types) ||
ResolveDomain(parser, type.Shape(), domain, result)) {
return failure();
}
llvm::ArrayRef<mlir::Attribute> mapping_attrs(mappings.begin(),
mappings.size());
result.addAttribute(
SairOp::kMappingAttrName,
ArrayAttr::get(parser.getBuilder().getContext(), mapping_attrs));
result.addAttribute(
SairDynRangeOp::getOperandSegmentSizeAttr(),
builder.getDenseI32ArrayAttr({static_cast<int32_t>(domain.size()),
static_cast<int32_t>(operands.size() - 1), 1}));
for (auto [operand, mapping] : llvm::zip(operands, mappings)) {
if (mlir::failed(ResolveOperand(operand, mapping, type.Shape(),
builder.getIndexType(), parser, result))) {
return mlir::failure();
}
}
return mlir::success();
}
// Parses the static_range operation. This operation takes a single integer
// attribute as argument and returns a Sair range. The syntax is the following.
//
// static-range-op ::= `sair.static_range` : !sair.static_range< size, step >
//
ParseResult SairStaticRangeOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
StaticRangeType type;
return failure(parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType<StaticRangeType>(type) ||
parser.addTypeToList(type, result.types));
}
// Parses the placeholder dimension. This operation has an iteration domain and
// returns a range value. The syntax is the following.
//
// placeholder-op ::= `sair.placeholder` domain : range-type
//
ParseResult SairPlaceholderOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> domain;
DimensionType type;
return mlir::failure(ParseDomain(parser, domain) ||
parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType<DimensionType>(type) ||
parser.addTypeToList(type, result.types) ||
ResolveDomain(parser, type.Shape(), domain, result));
}
// Parses the copy operation. This operation has an iteration domain and
// accesses a single Sair value. The syntax for the operation is the following.
//
// copy-op ::= `sair.copy` domain ssa-value mapping attributes
//
ParseResult SairCopyOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain;
mlir::OpAsmParser::UnresolvedOperand value;
MappingAttr mapping;
ValueType type;
if (ParseDomain(parser, domain) ||
ParseValueAccess(domain.size(), parser, value, mapping) ||
parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType<ValueType>(type) ||
parser.addTypeToList(type, result.types) ||
ResolveDomain(parser, type.Shape(), domain, result)) {
return failure();
}
result.addAttribute(SairOp::kMappingAttrName,
parser.getBuilder().getArrayAttr({mapping}));
return ResolveOperand(value, mapping, type.Shape(), type.ElementType(),
parser, result);
}
// Parses the sair.from_scalar operation, that takes a single argument and
// returns 0D sair value that encapsulates the argument type. The syntax of the
// operation is the following.
//
// from-scalar-op ::= `sair.from_memref` ssa-value attribute-dict
// `:` sair-value-type
//
ParseResult SairFromScalarOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
mlir::OpAsmParser::UnresolvedOperand operand;
ValueType result_type;
if (parser.parseOperand(operand) ||
parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType<ValueType>(result_type) ||
parser.addTypeToList(result_type, result.types)) {
return failure();
}
return parser.resolveOperand(operand, result_type.ElementType(),
result.operands);
}
// Parses the FromMemRef operation. This operations takes an iteration domain
// and a memref as argument and returns a Sair value. This syntax is the
// following.
//
// from-memref-op ::= 'sair.from_memref' parallel-domain memref-operand
// 'memref' memref-domain attr-dict : shape, memref-type
//
ParseResult SairFromMemRefOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain;
mlir::OpAsmParser::UnresolvedOperand memref;
MappingAttr mapping;
mlir::MemRefType memref_type;
DomainShapeAttr shape;
if (mlir::failed(ParseDomain(parser, domain))) return mlir::failure();
int parallel_domain_size = domain.size();
if (ParseValueAccess(domain.size(), parser, memref, mapping) ||
parser.parseKeyword("memref") || ParseDomain(parser, domain) ||
parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
parser.parseAttribute(shape) || parser.parseComma() ||
parser.parseType(memref_type) ||
ResolveDomain(parser, shape, domain, result)) {
return mlir::failure();
}
// It is irrelevant which Op class we use to get the attribute name because it
// comes from a trait. However, we cannot call a trait method directly.
result.addAttribute(
SairFromMemRefOp::getOperandSegmentSizeAttr(),
parser.getBuilder().getDenseI32ArrayAttr(
{static_cast<int32_t>(parallel_domain_size),
static_cast<int32_t>(domain.size() - parallel_domain_size),
static_cast<int32_t>(1)}));
mapping = mapping.ResizeUseDomain(domain.size());
result.addAttribute(SairOp::kMappingAttrName,
parser.getBuilder().getArrayAttr({mapping}));
auto result_type = ValueType::get(shape, memref_type.getElementType());
return mlir::failure(
ResolveOperand(memref, mapping, shape, memref_type, parser, result) ||
parser.addTypeToList(result_type, result.types));
}
// Parses the LoadFromMemRef operation. This operations takes an iteration
// domain and a memref as argument and returns a Sair value. The syntax is the
// following.
//
// from-memref-op ::= 'sair.load_from_memref' domain memref-operand
// attr-dict : memref-type -> value_type
//
ParseResult SairLoadFromMemRefOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain;
mlir::OpAsmParser::UnresolvedOperand memref;
MappingAttr mapping;
mlir::MemRefType memref_type;
ValueType value_type;
if (ParseDomain(parser, domain) ||
ParseValueAccess(domain.size(), parser, memref, mapping) ||
parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType(memref_type) || parser.parseArrow() ||
parser.parseType(value_type) ||
parser.addTypeToList(value_type, result.types) ||
ResolveDomain(parser, value_type.Shape(), domain, result)) {
return failure();
}
result.addAttribute(SairOp::kMappingAttrName,
parser.getBuilder().getArrayAttr({mapping}));
return ResolveOperand(memref, mapping, value_type.Shape(), memref_type,
parser, result);
}
// Parses the ToMemRef operation. This operation takes an iteration domain, a
// Sair value and a memref as argument and returns nothing. Its syntax is the
// following.
//
// to-memref-op ::= 'sair.from_memref' parallel-domain memref-operand
// 'memref' memref-domain value-operand attr-dict : shape, memref-type
//
ParseResult SairToMemRefOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain;
mlir::OpAsmParser::UnresolvedOperand memref, value;
MappingAttr memref_mapping, value_mapping;
DomainShapeAttr shape;
mlir::MemRefType memref_type;
if (mlir::failed(ParseDomain(parser, domain))) return mlir::failure();
int parallel_domain_size = domain.size();
if (ParseValueAccess(domain.size(), parser, memref, memref_mapping) ||
parser.parseKeyword("memref") || ParseDomain(parser, domain) ||
ParseValueAccess(domain.size(), parser, value, value_mapping) ||
parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
parser.parseAttribute(shape, SairDialect::kShapeAttrName,
result.attributes) ||
parser.parseComma() || parser.parseType(memref_type) ||
ResolveDomain(parser, shape, domain, result)) {
return mlir::failure();
}
memref_mapping = memref_mapping.ResizeUseDomain(domain.size());
result.addAttribute(
SairOp::kMappingAttrName,
parser.getBuilder().getArrayAttr({memref_mapping, value_mapping}));
// It is irrelevant which Op class we use to get the attribute name because it
// comes from a trait. However, we cannot call a trait method directly.
result.addAttribute(
SairFromMemRefOp::getOperandSegmentSizeAttr(),
parser.getBuilder().getDenseI32ArrayAttr(
{static_cast<int32_t>(parallel_domain_size),
static_cast<int32_t>(domain.size() - parallel_domain_size),
static_cast<int32_t>(1), static_cast<int32_t>(1)}));
return failure(ResolveOperand(memref, memref_mapping, shape, memref_type,
parser, result) ||
ResolveOperand(value, value_mapping, shape,
memref_type.getElementType(), parser, result));
}
// Parses the StoreToMemRef operation. The syntax is the following.
//
// store-to-memref-op ::= 'sair.store_to_memref' domain memref-operand ','
// value-operand attr-dict : shape, memref_type
//
ParseResult SairStoreToMemRefOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain;
mlir::OpAsmParser::UnresolvedOperand memref, value;
MappingAttr memref_mapping, value_mapping;
mlir::MemRefType memref_type;
DomainShapeAttr shape;
if (ParseDomain(parser, domain) ||
ParseValueAccess(domain.size(), parser, memref, memref_mapping) ||
parser.parseComma() ||
ParseValueAccess(domain.size(), parser, value, value_mapping) ||
parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
parser.parseAttribute(shape, SairDialect::kShapeAttrName,
result.attributes) ||
parser.parseComma() || parser.parseType(memref_type) ||
ResolveDomain(parser, shape, domain, result)) {
return failure();
}
result.addAttribute(
SairOp::kMappingAttrName,
parser.getBuilder().getArrayAttr({memref_mapping, value_mapping}));
return failure(ResolveOperand(memref, memref_mapping, shape, memref_type,
parser, result) ||
ResolveOperand(value, value_mapping, shape,
memref_type.getElementType(), parser, result));
}
namespace {
constexpr llvm::StringRef kOfKeyword = "of";
// Parses an operation of the form:
//
// proj ::= dialect-namespace '.' op-name domain 'of' domain operand
// attr-dict? ':' shape ',' element-type
//
mlir::ParseResult parseProjectionOp(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain;
mlir::OpAsmParser::UnresolvedOperand value;
MappingAttr mapping;
DomainShapeAttr shape;
mlir::Type element_type;
if (failed(ParseDomain(parser, domain))) return mlir::failure();
int num_parallel_dimensions = domain.size();
if (parser.parseKeyword(kOfKeyword) || ParseDomain(parser, domain) ||
ParseValueAccess(domain.size(), parser, value, mapping) ||
parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
parser.parseAttribute(shape, SairDialect::kShapeAttrName,
result.attributes) ||
parser.parseComma() || parser.parseType(element_type) ||
ResolveDomain(parser, shape, domain, result)) {
return mlir::failure();
}
mapping = mapping.ResizeUseDomain(domain.size());
result.addAttribute(SairOp::kMappingAttrName,
parser.getBuilder().getArrayAttr({mapping}));
DomainShapeAttr result_shape = shape.Prefix(num_parallel_dimensions);
result.addTypes(ValueType::get(result_shape, element_type));
// Store the number of operands in each variadic segments as required by MLIR,
// it expects specifically int32_t.
int num_projection_dimensions = domain.size() - num_parallel_dimensions;
result.addAttribute(SairProjAnyOp::getOperandSegmentSizeAttr(),
parser.getBuilder().getDenseI32ArrayAttr(
{static_cast<int32_t>(num_parallel_dimensions),
static_cast<int32_t>(num_projection_dimensions),
static_cast<int32_t>(1)}));
return ResolveOperand(value, mapping, shape, element_type, parser, result);
}
} // namespace
mlir::ParseResult SairProjAnyOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
return parseProjectionOp(parser, result);
}
mlir::ParseResult SairProjLastOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
return parseProjectionOp(parser, result);
}
// Parses the sair.return operation, with the following syntax.
//
// return-op ::= `sair.return` operands attr-dict (`:` operands-types)?
//
mlir::ParseResult SairReturnOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> operands;
llvm::SmallVector<mlir::Type, 4> operand_types;
return failure(parser.parseOperandList(operands) ||
parser.parseOptionalAttrDict(result.attributes) ||
parser.parseOptionalColonTypeList(operand_types) ||
parser.resolveOperands(operands, operand_types,
parser.getNameLoc(), result.operands));
}
// Parses the sair.exit operation, with the follwing syntax.
//
// exit-op ::= `sair.exit` operands attr-dict? (':' element-types)?
//
mlir::ParseResult SairExitOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> operands;
llvm::SmallVector<mlir::Type, 4> element_types;
llvm::SmallVector<MappingAttr, 4> mappings;
llvm::SMLoc type_loc;
if (ParseOperandList(0, parser, operands, mappings) ||
parser.parseOptionalAttrDict(result.attributes) ||
parser.getCurrentLocation(&type_loc) ||
parser.parseOptionalColonTypeList(element_types)) {
return mlir::failure();
}
llvm::ArrayRef<mlir::Attribute> mapping_attrs(mappings.begin(),
mappings.end());
result.addAttribute(
SairOp::kMappingAttrName,
ArrayAttr::get(parser.getBuilder().getContext(), mapping_attrs));
assert(mappings.size() == operands.size());
if (element_types.size() != operands.size()) {
return parser.emitError(type_loc)
<< "expected " << operands.size() << " types";
}
mlir::Builder &builder = parser.getBuilder();
auto domain_shape = DomainShapeAttr::get(builder.getContext());
for (auto [operand, element_type, mapping] :
llvm::zip(operands, element_types, mappings)) {
if (failed(ResolveOperand(operand, mapping, domain_shape, element_type,
parser, result))) {
return mlir::failure();
}
}
return mlir::success();
}
// Parses the sair.alloc operation with the following syntax.
//
// alloc-op ::= `sair.alloc` value-list attr-dict? : type
//
mlir::ParseResult SairAllocOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain, values;
llvm::SmallVector<mlir::Attribute, 4> access_patterns;
mlir::OpAsmParser::UnresolvedOperand value;
MappingAttr pattern;
if (failed(ParseDomain(parser, domain))) return mlir::failure();
mlir::OptionalParseResult parse_result =
ParseOptionalValueAccess(domain.size(), parser, value, pattern);
if (parse_result.has_value() && mlir::failed(*parse_result)) {
return failure();
}
if (parse_result.has_value() && mlir::succeeded(*parse_result)) {
access_patterns.push_back(pattern);
values.push_back(value);
while (mlir::succeeded(parser.parseOptionalComma())) {
if (mlir::failed(ParseValueAccess(domain.size(), parser,
values.emplace_back(), pattern))) {
return mlir::failure();
}
access_patterns.push_back(pattern);
}
}
result.attributes.append(SairAllocOp::getOperandSegmentSizeAttr(),
parser.getBuilder().getDenseI32ArrayAttr(
{static_cast<int32_t>(domain.size()),
static_cast<int32_t>(values.size())}));
result.attributes.append(SairOp::kMappingAttrName,
parser.getBuilder().getArrayAttr(access_patterns));
ValueType resultType;
if (parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
parser.parseType(resultType) ||
parser.addTypeToList(resultType, result.types) ||
ResolveDomain(parser, resultType.Shape(), domain, result)) {
return mlir::failure();
}
auto index_type = parser.getBuilder().getIndexType();
for (auto [value, mapping_attr] : llvm::zip(values, access_patterns)) {
auto mapping = mapping_attr.cast<MappingAttr>();
if (mlir::failed(ResolveOperand(value, mapping, resultType.Shape(),
index_type, parser, result))) {
return failure();
}
}
return success();
}
// Parses the sair.free operation with the following syntax.
//
// free-op ::= `sair.free` domain value attr-dict : type
//
mlir::ParseResult SairFreeOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain;
mlir::OpAsmParser::UnresolvedOperand value;
MappingAttr mapping;
ValueType value_type;
if (ParseDomain(parser, domain) ||
ParseValueAccess(domain.size(), parser, value, mapping) ||
parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
parser.parseType(value_type) ||
ResolveDomain(parser, value_type.Shape(), domain, result) ||
ResolveOperand(value, mapping, value_type.Shape(),
value_type.ElementType(), parser, result)) {
return mlir::failure();
}
result.addAttribute(SairOp::kMappingAttrName,
parser.getBuilder().getArrayAttr(mapping));
return mlir::success();
}
// Parses the sair.fby operation, with the following syntax.
//
// fby-op ::= `sair.fby` domain init `then` domain value attr-dict : type
//
mlir::ParseResult SairFbyOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand, 4> domain;
mlir::OpAsmParser::UnresolvedOperand init, value;
MappingAttr init_mapping, value_mapping;
ValueType type;
if (failed(ParseDomain(parser, domain))) return mlir::failure();
int num_parallel_dimensions = domain.size();
if (ParseValueAccess(num_parallel_dimensions, parser, init, init_mapping) ||
parser.parseKeyword(SairFbyOp::kThenKeyword) ||
ParseDomain(parser, domain) ||
ParseValueAccess(domain.size(), parser, value, value_mapping) ||
parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType(type) ||
ResolveDomain(parser, type.Shape(), domain, result) ||
parser.addTypeToList(type, result.types)) {
return mlir::failure();
}
result.addAttribute(
SairOp::kMappingAttrName,
mlir::ArrayAttr::get(
type.getContext(),
{init_mapping.ResizeUseDomain(domain.size()), value_mapping}));
// Store the number of operands in each variadic segments as required by MLIR,
// it expects specifically int32_t.
result.addAttribute(
SairMapOp::getOperandSegmentSizeAttr(),
parser.getBuilder().getDenseI32ArrayAttr(
{static_cast<int32_t>(num_parallel_dimensions),
static_cast<int32_t>(domain.size() - num_parallel_dimensions), 1,
1}));
return failure(ResolveOperand(init, init_mapping, type.Shape(),
type.ElementType(), parser, result) ||
ResolveOperand(value, value_mapping, type.Shape(),
type.ElementType(), parser, result));
}
namespace {
// Prints a Sair value access list. Takes the list of values and respective
// mappings as arguments. Expects "values" and "mappings" to be ranges
// of equal length.
void PrintValueAccessList(const ValueOperandRange operands,
mlir::OpAsmPrinter &printer) {
llvm::interleaveComma(operands, printer, [&](ValueOperand operand) {
PrintValueAccess(operand, printer);
});
}
} // namespace
// Prints the range operation.
void SairDynRangeOp::print(OpAsmPrinter &printer) {
PrintDomain(getDomain(), printer);
printer << " ";
if (LowerBound().is_value()) {
PrintValueAccess(ValueOperands()[0], printer);
printer << ", ";
}
PrintValueAccess(ValueOperands().back(), printer);
if (getStep() != 1) {
printer << " " << RangeOp::kStepAttrName << " " << getStep();
}
printer.printOptionalAttrDict(
getOperation()->getAttrs(),
{RangeOp::kStepAttrName, SairOp::kMappingAttrName,
SairDynRangeOp::getOperandSegmentSizeAttr()});
printer << " : " << getType();
}
// Prints the sair.static_range operation.
void SairStaticRangeOp::print(OpAsmPrinter &printer) {
printer.printOptionalAttrDict(getOperation()->getAttrs());
printer << " : " << getType();
}
void SairPlaceholderOp::print(mlir::OpAsmPrinter &printer) {
PrintDomain(getDomain(), printer);
printer.printOptionalAttrDict(getOperation()->getAttrs());
printer << " : " << getRange().getType();
}
// Prints the copy operation.
void SairCopyOp::print(OpAsmPrinter &printer) {
PrintDomain(getDomain(), printer);
printer << " ";
PrintValueAccess(Value(), printer);
printer.printOptionalAttrDict(getOperation()->getAttrs(),
{SairOp::kMappingAttrName});
printer << " : " << getType();
}
// Prints the sair.from_scalar operation.
void SairFromScalarOp::print(OpAsmPrinter &printer) {
printer << " " << getValue();
printer.printOptionalAttrDict(getOperation()->getAttrs());
printer << " : " << getType();
}
// Prints the from_memref operation.
void SairFromMemRefOp::print(OpAsmPrinter &printer) {
PrintDomain(getParallelDomain(), printer);
printer << " ";
PrintValueAccess(MemRef(), printer);
printer << " memref";
PrintDomain(getMemrefDomain(), printer, getParallelDomain().size());
// It is irrelevant which Op class we use to get the attribute name because it
// comes from a trait. However, we cannot call a trait method directly.
printer.printOptionalAttrDict(getOperation()->getAttrs(),
{SairFromMemRefOp::getOperandSegmentSizeAttr(),
SairOp::kMappingAttrName});
printer << " : " << getShape() << ", " << MemRefType();
}
// Prints the load_from_memref operation.
void SairLoadFromMemRefOp::print(OpAsmPrinter &printer) {
PrintDomain(getDomain(), printer);
printer << " ";
PrintValueAccess(MemRef(), printer);
printer.printOptionalAttrDict(getOperation()->getAttrs(),
{SairOp::kMappingAttrName});
printer << " : " << MemRefType() << " -> " << getType();
}
// Prints the to_memref operation.
void SairToMemRefOp::print(OpAsmPrinter &printer) {
PrintDomain(getParallelDomain(), printer);
printer << " ";
PrintValueAccess(MemRef(), printer);
printer << " memref";
PrintDomain(getMemrefDomain(), printer, getParallelDomain().size());
printer << " ";
PrintValueAccess(Value(), printer);
// It is irrelevant which Op class we use to get the attribute name because it
// comes from a trait. However, we cannot call a trait method directly.
printer.printOptionalAttrDict(
getOperation()->getAttrs(),
{SairFromMemRefOp::getOperandSegmentSizeAttr(),
SairDialect::kShapeAttrName, SairOp::kMappingAttrName});
printer << " : " << getShape() << ", " << MemRefType();
}
// Prints the store_to_memref operation.
void SairStoreToMemRefOp::print(OpAsmPrinter &printer) {
PrintDomain(getDomain(), printer);
printer << " ";
PrintValueAccess(MemRef(), printer);
printer << ", ";
PrintValueAccess(Value(), printer);
printer.printOptionalAttrDict(
getOperation()->getAttrs(),
{SairOp::kMappingAttrName, SairDialect::kShapeAttrName});
printer << " : " << getShape() << ", " << MemRefType();
}
namespace {
// Prints a projection operation.
template<typename Op>
void PrintProjectionOp(Op op, OpAsmPrinter &printer) {
PrintDomain(op.getParallelDomain(), printer);
printer << " " << kOfKeyword;
PrintDomain(op.getProjectionDomain(), printer, op.getParallelDomain().size());
printer << " ";
PrintValueAccess(op.Value(), printer);
printer.printOptionalAttrDict(
op->getAttrs(), {SairFromMemRefOp::getOperandSegmentSizeAttr(),
SairDialect::kShapeAttrName, SairOp::kMappingAttrName});
printer << " : " << op.getShape() << ", "
<< op.getResult().getType().template cast<ValueType>().ElementType();
}
} // namespace
// Prints the proj_any operation.
void SairProjAnyOp::print(OpAsmPrinter &printer) {
PrintProjectionOp(*this, printer);
}
// Prints the proj_last operation.
void SairProjLastOp::print(OpAsmPrinter &printer) {
PrintProjectionOp(*this, printer);
}
// Prints the sair.return operation.
void SairReturnOp::print(OpAsmPrinter &printer) {
printer << " ";
printer.printOperands(getOperands());
printer.printOptionalAttrDict(getOperation()->getAttrs());
if (getOperands().empty()) return;
printer << " : ";
llvm::interleaveComma(getOperands().getTypes(), printer,
[&](mlir::Type type) { printer.printType(type); });
}
// Prints the sair.exit operation.
void SairExitOp::print(OpAsmPrinter &printer) {
printer << " ";
PrintValueAccessList(ValueOperands(), printer);
printer.printOptionalAttrDict(getOperation()->getAttrs(),
{SairOp::kMappingAttrName});
if (getInputs().empty()) return;
printer << " : ";
llvm::interleaveComma(
getOperands().getTypes(), printer, [&](mlir::Type type) {
printer.printType(type.cast<ValueType>().ElementType());
});
}
// Prints the sair.fby operation.
void SairFbyOp::print(OpAsmPrinter &printer) {
PrintDomain(getParallelDomain(), printer);
printer << " ";
PrintValueAccess(Init(), printer);
printer << " " << SairFbyOp::kThenKeyword;
PrintDomain(getSequentialDomain(), printer, getParallelDomain().size());
printer << " ";
PrintValueAccess(Value(), printer);
printer.printOptionalAttrDict(getOperation()->getAttrs(),
{
SairMapOp::getOperandSegmentSizeAttr(),
SairOp::kMappingAttrName,
});
printer << " : ";
printer.printType(getType());
}
void SairAllocOp::print(mlir::OpAsmPrinter &printer) {
PrintDomain(getDomain(), printer);
if (!ValueOperands().empty()) printer << " ";
llvm::interleaveComma(ValueOperands(), printer, [&](ValueOperand value) {
PrintValueAccess(value, printer);
});
printer.printOptionalAttrDict(
getOperation()->getAttrs(),
{SairOp::kMappingAttrName, SairAllocOp::getOperandSegmentSizeAttr()});
printer << " : " << getResult().getType();
}
void SairFreeOp::print(mlir::OpAsmPrinter &printer) {
PrintDomain(getDomain(), printer);
printer << " ";
PrintValueAccess(Value(), printer);
printer.printOptionalAttrDict(getOperation()->getAttrs(),
{SairOp::kMappingAttrName});
mlir::Type element_type = Value().GetType().ElementType();
printer << " : " << ValueType::get(getShape(), element_type);
}
namespace {
mlir::LogicalResult VerifyLoadFromStoreToMemRef(mlir::Operation *op,
mlir::MemRefType memref_type,
ValueType value_type,
MappingAttr layout) {
if (memref_type.getElementType() != value_type.ElementType()) {
return op->emitError()
<< "memref and value type must have the same element type";
}
if (memref_type.getRank() != layout.size()) {
return op->emitError() << "memref and layout must have the same rank";
}
if (layout.HasNoneExprs()) {
return op->emitError() << "layout must be surjective";
}
return mlir::success();
}
static mlir::LogicalResult VerifyFromToMemRef(mlir::Operation *op,
int parallel_domain_size,
DomainShapeAttr shape,
mlir::Value memref,
mlir::Value value) {
auto memref_type =
memref.getType().cast<ValueType>().ElementType().cast<MemRefType>();
auto value_type = value.getType().cast<ValueType>();
if (memref_type.getElementType() != value_type.ElementType()) {
return op->emitError()
<< "memref and value must have the same element type";
}
int memref_domain_size = shape.NumDimensions() - parallel_domain_size;
if (memref_type.getRank() != memref_domain_size) {
return op->emitError() << "expected memref of rank " << memref_domain_size
<< ", got " << memref_type.getRank();
}
for (const DomainShapeDim shape_dim :
shape.Dimensions().drop_front(parallel_domain_size)) {
int max_dependency = shape_dim.DependencyMask().find_last();
if (max_dependency >= parallel_domain_size) {
return op->emitError()
<< "memref domain dimensions cannot depend on each other";
}
}
return mlir::success();
}
} // namespace
mlir::LogicalResult SairFromScalarOp::verify() {
SairFromScalarOp op = *this;
mlir::Type expected_type =
op.getResult().getType().cast<ValueType>().ElementType();
if (op.getValue().getType() != expected_type) {
return op.emitError() << "expects different type: '"
<< op.getValue().getType() << "' vs '"
<< expected_type << "'";
}
return mlir::success();
}
mlir::LogicalResult SairExitOp::verify() {
SairExitOp op = *this;
auto program_op = op->getParentOfType<SairProgramOp>();
assert(program_op);
if (op.getNumOperands() != program_op.getNumResults()) {
return op.emitError() << "expected " << program_op.getNumResults()
<< " operands, found " << op.getNumOperands();
}
for (auto p : llvm::zip(op.getOperandTypes(), program_op.getResultTypes())) {
mlir::Type given_type = std::get<0>(p).cast<ValueType>().ElementType();
mlir::Type expected_type = std::get<1>(p);
if (expected_type != given_type) {
return op.emitError()
<< "sair.exit operands must match the return type of the "
"sair.program: expected "
<< expected_type << ", found " << given_type;
}
}
if (op.getInstances().has_value() && op.getInstances().value().empty()) {
return op.emitOpError() << "must have an instance";
}
return mlir::success();
}
mlir::LogicalResult SairAllocOp::verify() {
SairAllocOp op = *this;
if (op.getDynamicSizes().size() != op.MemType().getNumDynamicDims()) {
return op.emitError() << "expected " << op.MemType().getNumDynamicDims()
<< " dynamic size operands";
}
return mlir::success();
}
mlir::LogicalResult SairLoadFromMemRefOp::verify() {
return VerifyLoadFromStoreToMemRef(*this, MemRefType(),
getType().cast<ValueType>(), getLayout());
}
mlir::LogicalResult SairStoreToMemRefOp::verify() {
return VerifyLoadFromStoreToMemRef(*this, MemRefType(), Value().GetType(),
getLayout());
}
mlir::LogicalResult SairFromMemRefOp::verify() {
return VerifyFromToMemRef(*this, getParallelDomain().size(), getShape(),
getMemref(), getResult());
}
mlir::LogicalResult SairToMemRefOp::verify() {
return VerifyFromToMemRef(*this, getParallelDomain().size(), getShape(),
getMemref(), getValue());