-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathFlyOps.cpp
More file actions
1961 lines (1713 loc) · 83.5 KB
/
FlyOps.cpp
File metadata and controls
1961 lines (1713 loc) · 83.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 FlyDSL Project Contributors
#include "mlir/IR/Builders.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/Support/LogicalResult.h"
#include "flydsl/Dialect/Fly/IR/FlyDialect.h"
#include "flydsl/Dialect/Fly/Utils/IntTupleUtils.h"
#include "flydsl/Dialect/Fly/Utils/LayoutUtils.h"
#include "flydsl/Dialect/Fly/Utils/TiledOpUtils.h"
#include <mlir/IR/Attributes.h>
#include <mlir/IR/BuiltinAttributes.h>
#define GET_OP_CLASSES
#include "flydsl/Dialect/Fly/IR/FlyOps.cpp.inc"
#include <algorithm>
#include <tuple>
using namespace mlir;
using namespace mlir::fly;
namespace {
LayoutAttr GetLayoutAttrFromLayoutLikeType(Type type) {
Attribute layoutAttr;
if (auto memrefTy = dyn_cast<fly::MemRefType>(type)) {
layoutAttr = memrefTy.getLayout();
} else if (auto coordTensorTy = dyn_cast<CoordTensorType>(type)) {
layoutAttr = coordTensorTy.getLayout();
} else if (auto layoutTy = dyn_cast<LayoutType>(type)) {
layoutAttr = layoutTy.getAttr();
} else if (auto composedTy = dyn_cast<ComposedLayoutType>(type)) {
layoutAttr = composedTy.getAttr();
} else {
return nullptr;
}
if (auto layout = dyn_cast<LayoutAttr>(layoutAttr)) {
return layout;
} else if (auto composed = dyn_cast<ComposedLayoutAttr>(layoutAttr)) {
return composed.getOuter();
} else {
return nullptr;
}
}
Type RebuildLayoutLikeType(Type type, LayoutAttr newLayoutAttr) {
auto replaceOuter = [&](Attribute oldLayout) -> Attribute {
if (isa<LayoutAttr>(oldLayout))
return newLayoutAttr;
auto composed = cast<ComposedLayoutAttr>(oldLayout);
return ComposedLayoutAttr::get(composed.getInner(), composed.getOffset(), newLayoutAttr);
};
if (auto memrefTy = dyn_cast<fly::MemRefType>(type)) {
return fly::MemRefType::get(memrefTy.getElemTy(), memrefTy.getAddressSpace(),
replaceOuter(memrefTy.getLayout()), memrefTy.getAlignment(),
memrefTy.getSwizzle());
} else if (auto coordTensorTy = dyn_cast<fly::CoordTensorType>(type)) {
return CoordTensorType::get(coordTensorTy.getBase(), replaceOuter(coordTensorTy.getLayout()));
} else if (auto layoutTy = dyn_cast<fly::LayoutType>(type)) {
return LayoutType::get(layoutTy.getContext(), newLayoutAttr);
} else if (auto composedTy = dyn_cast<fly::ComposedLayoutType>(type)) {
return ComposedLayoutType::get(composedTy.getAttr().getInner(),
composedTy.getAttr().getOffset(), newLayoutAttr);
} else {
llvm_unreachable("Unsupported LayoutLike type");
}
}
Type applyIntTupleTransform(Type inputTy, function_ref<IntTupleAttr(IntTupleAttr)> fn) {
if (auto tupleTy = dyn_cast<IntTupleType>(inputTy))
return IntTupleType::get(fn(tupleTy.getAttr()));
LayoutAttr outerLayout = GetLayoutAttrFromLayoutLikeType(inputTy);
if (!outerLayout)
return {};
LayoutAttr transformed = LayoutAttr::get(fn(outerLayout.getShape()), fn(outerLayout.getStride()));
return RebuildLayoutLikeType(inputTy, transformed);
}
Type applyOffsetOnMemRef(LayoutBuilder<LayoutAttr> &builder, fly::MemRefType memrefTy,
IntTupleAttr offset, LayoutAttr layoutAttr) {
if (auto composed = dyn_cast<ComposedLayoutAttr>(memrefTy.getLayout())) {
IntTupleAttr newOffset = intTupleAdd(builder, composed.getOffset(), offset);
Attribute newLayout = ComposedLayoutAttr::get(composed.getInner(), newOffset, layoutAttr);
return fly::MemRefType::get(memrefTy.getElemTy(), memrefTy.getAddressSpace(), newLayout,
memrefTy.getAlignment(), memrefTy.getSwizzle());
} else {
int32_t valDiv = memrefTy.getValueDivisibility();
IntAttr offsetInt = offset.extractIntFromLeaf();
int32_t offsetDiv =
offsetInt.isStatic() ? std::abs(offsetInt.getValue()) : offsetInt.getDivisibility();
int32_t newValDiv = (offsetDiv == 0) ? valDiv : utils::divisibilityAdd(valDiv, offsetDiv);
return fly::MemRefType::get(memrefTy.getElemTy(), memrefTy.getAddressSpace(), layoutAttr,
AlignAttr::get(memrefTy.getElemTy(), newValDiv),
memrefTy.getSwizzle());
}
}
Type applyOffsetOnCoordTensor(LayoutBuilder<LayoutAttr> &builder, CoordTensorType coordTensorTy,
IntTupleAttr offset, LayoutAttr layoutAttr) {
if (auto composed = dyn_cast<ComposedLayoutAttr>(coordTensorTy.getLayout())) {
IntTupleAttr newOffset = intTupleAdd(builder, composed.getOffset(), offset);
Attribute newLayout = ComposedLayoutAttr::get(composed.getInner(), newOffset, layoutAttr);
return CoordTensorType::get(coordTensorTy.getBase(), newLayout);
} else {
IntTupleAttr newBase = intTupleAdd(builder, coordTensorTy.getBase(), offset);
return CoordTensorType::get(newBase, layoutAttr);
}
}
Type applyOffsetOnTensorLike(LayoutBuilder<LayoutAttr> &builder, Type tensorLikeTy,
IntTupleAttr offset, LayoutAttr layoutAttr) {
if (auto memrefTy = dyn_cast<fly::MemRefType>(tensorLikeTy))
return applyOffsetOnMemRef(builder, memrefTy, offset, layoutAttr);
if (auto coordTensorTy = dyn_cast<CoordTensorType>(tensorLikeTy)) {
return applyOffsetOnCoordTensor(builder, coordTensorTy, offset, layoutAttr);
}
llvm_unreachable("Unsupported tensor like type");
}
FailureOr<std::pair<int64_t, int64_t>> getCoalescedLeafCountAndStride(fly::MemRefType memRefTy) {
auto layoutAttr = dyn_cast<LayoutAttr>(memRefTy.getLayout());
if (!layoutAttr)
return failure();
LayoutBuilder<LayoutAttr> builder(memRefTy.getContext());
auto coalesced = layoutCoalesce(builder, layoutAttr);
if (!coalesced.isLeaf())
return failure();
auto shape = coalesced.getShape().getLeafAsInt();
auto stride = coalesced.getStride().getLeafAsInt();
if (!shape.isStatic() || !stride.isStatic())
return failure();
return std::make_pair<int64_t, int64_t>(shape.getValue(), stride.getValue());
}
LogicalResult verifyUniversalCopyOperand(Operation *op, StringRef operandName, CopyAtomType copyAtomTy,
fly::MemRefType memRefTy) {
auto universalCopy = dyn_cast<CopyOpUniversalCopyType>(copyAtomTy.getCopyOp());
if (!universalCopy)
return success();
auto countAndStride = getCoalescedLeafCountAndStride(memRefTy);
if (failed(countAndStride)) {
return op->emitOpError() << operandName
<< " memref layout must coalesce to a single static leaf for "
<< copyAtomTy;
}
auto [count, stride] = *countAndStride;
int64_t elemBits = memRefTy.getElemTy().getIntOrFloatBitWidth();
int64_t copyBits = universalCopy.getBitSize();
int64_t totalBits = count * elemBits;
if (totalBits != copyBits) {
return op->emitOpError() << operandName << " memref covers " << totalBits
<< " bits after coalescing, but " << copyAtomTy << " expects "
<< copyBits << " bits";
}
int64_t contiguousBits = (count <= 1 || stride == 1) ? totalBits : elemBits;
if (contiguousBits < copyBits) {
return op->emitOpError() << operandName << " memref contiguous bit count " << contiguousBits
<< " is smaller than copy granularity " << copyBits;
}
return success();
}
} // namespace
#define FLY_INFER_RETURN_TYPES(OP) \
llvm::LogicalResult OP::inferReturnTypes( \
mlir::MLIRContext *context, std::optional<::mlir::Location> location, \
mlir::ValueRange operands, mlir::DictionaryAttr attributes, \
mlir::OpaqueProperties properties, mlir::RegionRange regions, \
llvm::SmallVectorImpl<mlir::Type> &inferredReturnTypes)
LogicalResult CopyAtomCall::verify() {
auto copyAtomTy = dyn_cast<CopyAtomType>(getCopyAtom().getType());
if (!copyAtomTy)
return emitOpError("copyAtom is not CopyAtomType");
auto srcTy = cast<fly::MemRefType>(getSrc().getType());
auto dstTy = cast<fly::MemRefType>(getDst().getType());
if (srcTy.getElemTy() != dstTy.getElemTy())
return emitOpError("src/dst element types mismatch");
if (failed(verifyUniversalCopyOperand(getOperation(), "src", copyAtomTy, srcTy)))
return failure();
if (failed(verifyUniversalCopyOperand(getOperation(), "dst", copyAtomTy, dstTy)))
return failure();
return success();
}
LogicalResult CopyAtomCallSSA::verify() {
auto copyAtomTy = dyn_cast<CopyAtomType>(getCopyAtom().getType());
if (!copyAtomTy)
return emitOpError("copyAtom is not CopyAtomType");
auto srcTy = dyn_cast<fly::MemRefType>(getSrc().getType());
auto dstTy = getDst() ? dyn_cast<fly::MemRefType>(getDst().getType()) : fly::MemRefType();
if (srcTy && dstTy && srcTy.getElemTy() != dstTy.getElemTy())
return emitOpError("src/dst element types mismatch");
if (srcTy && failed(verifyUniversalCopyOperand(getOperation(), "src", copyAtomTy, srcTy)))
return failure();
if (dstTy && failed(verifyUniversalCopyOperand(getOperation(), "dst", copyAtomTy, dstTy)))
return failure();
return success();
}
//===----------------------------------------------------------------------===//
// Constructors
//===----------------------------------------------------------------------===//
FLY_INFER_RETURN_TYPES(MakeLayoutOp) {
auto shapeType = dyn_cast<IntTupleType>(operands[0].getType());
auto strideType = dyn_cast<IntTupleType>(operands[1].getType());
if (!shapeType)
return emitOptionalError(location, "MakeLayoutOp: expected IntTupleType for shape, got ",
operands[0].getType());
if (!strideType)
return emitOptionalError(location, "MakeLayoutOp: expected IntTupleType for stride, got ",
operands[1].getType());
auto layoutAttr = LayoutAttr::get(context, shapeType.getAttr(), strideType.getAttr());
inferredReturnTypes.assign({LayoutType::get(context, layoutAttr)});
return success();
}
FLY_INFER_RETURN_TYPES(MakeLayoutLikeOp) {
auto layoutTy = dyn_cast<LayoutType>(operands[0].getType());
if (!layoutTy)
return emitOptionalError(location, "MakeLayoutLikeOp: expected LayoutType, got ",
operands[0].getType());
LayoutBuilder<LayoutAttr> layoutBuilder(context);
LayoutAttr inferred = layoutMakeLayoutLike(layoutBuilder, layoutTy.getAttr());
inferredReturnTypes.assign({LayoutType::get(context, inferred)});
return success();
}
FLY_INFER_RETURN_TYPES(MakeOrderedLayoutOp) {
auto shapeTy = dyn_cast<IntTupleType>(operands[0].getType());
auto orderTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!shapeTy)
return emitOptionalError(location, "MakeOrderedLayoutOp: expected IntTupleType for shape, got ",
operands[0].getType());
if (!orderTy)
return emitOptionalError(location, "MakeOrderedLayoutOp: expected IntTupleType for order, got ",
operands[1].getType());
LayoutBuilder<LayoutAttr> layoutBuilder(context);
LayoutAttr layoutAttr =
layoutMakeOrderedLayout(layoutBuilder, shapeTy.getAttr(), orderTy.getAttr());
inferredReturnTypes.assign({LayoutType::get(context, layoutAttr)});
return success();
}
FLY_INFER_RETURN_TYPES(MakeComposedLayoutOp) {
auto offsetTy = dyn_cast<IntTupleType>(operands[1].getType());
auto outerTy = dyn_cast<LayoutType>(operands[2].getType());
if (!offsetTy)
return emitOptionalError(location,
"MakeComposedLayoutOp: expected IntTupleType for offset, got ",
operands[1].getType());
if (!outerTy)
return emitOptionalError(location, "MakeComposedLayoutOp: expected LayoutType for outer, got ",
operands[2].getType());
Attribute innerAttr = nullptr;
if (auto innerLayoutTy = dyn_cast<LayoutType>(operands[0].getType())) {
innerAttr = innerLayoutTy.getAttr();
} else if (auto innerComposedTy = dyn_cast<ComposedLayoutType>(operands[0].getType())) {
innerAttr = innerComposedTy.getAttr();
} else if (auto innerSwizzleTy = dyn_cast<SwizzleType>(operands[0].getType())) {
innerAttr = innerSwizzleTy.getAttr();
} else {
return emitOptionalError(
location, "MakeComposedLayoutOp: expected Layout/ComposedLayout/Swizzle for inner, got ",
operands[0].getType());
}
auto composedAttr =
ComposedLayoutAttr::get(context, innerAttr, offsetTy.getAttr(), outerTy.getAttr());
inferredReturnTypes.assign({ComposedLayoutType::get(context, composedAttr)});
return success();
}
FLY_INFER_RETURN_TYPES(MakeIdentityLayoutOp) {
auto shapeTy = dyn_cast<IntTupleType>(operands[0].getType());
if (!shapeTy)
return emitOptionalError(location,
"MakeIdentityLayoutOp: expected IntTupleType for shape, got ",
operands[0].getType());
IntTupleAttr shapeAttr = shapeTy.getAttr();
IntTupleAttr strideAttr = intTupleMakeBasisTupleLike(shapeAttr);
LayoutAttr layoutAttr = LayoutAttr::get(context, shapeAttr, strideAttr);
inferredReturnTypes.assign({LayoutType::get(context, layoutAttr)});
return success();
}
FLY_INFER_RETURN_TYPES(MakeViewOp) {
Type iterTy = operands[0].getType();
Type layoutArgTy = operands[1].getType();
Attribute layoutAttr;
if (auto layoutTy = dyn_cast<LayoutType>(layoutArgTy)) {
layoutAttr = layoutTy.getAttr();
} else if (auto composedTy = dyn_cast<ComposedLayoutType>(layoutArgTy)) {
layoutAttr = composedTy.getAttr();
} else {
return emitOptionalError(
location, "MakeViewOp: expected LayoutType or ComposedLayoutType for operand #1, got ",
layoutArgTy);
}
if (auto intTupleTy = dyn_cast<IntTupleType>(iterTy)) {
inferredReturnTypes.assign({CoordTensorType::get(intTupleTy.getAttr(), layoutAttr)});
return success();
} else if (auto ptrTy = dyn_cast<PointerType>(iterTy)) {
inferredReturnTypes.assign(
{MemRefType::get(ptrTy.getElemTy(), ptrTy.getAddressSpace(), layoutAttr,
ptrTy.getAlignment(), ptrTy.getSwizzle())});
return success();
} else {
return emitOptionalError(
location, "MakeViewOp: expected IntTupleType or PointerType for operand #0, got ", iterTy);
}
}
FLY_INFER_RETURN_TYPES(MakeFragmentLayoutLikeOp) {
auto srcLayout = GetLayoutAttrFromLayoutLikeType(operands[0].getType());
if (!srcLayout)
return emitOptionalError(location,
"MakeFragmentLayoutLikeOp: expected LayoutType or MemRefType, got ",
operands[0].getType());
if (!srcLayout.getShape().isStatic())
return emitOptionalError(
location, "MakeFragmentLayoutLikeOp: expected static shape layout, got ", srcLayout);
LayoutBuilder<LayoutAttr> layoutBuilder(context);
LayoutAttr fragmentLayout = layoutMakeFragmentLayout(layoutBuilder, srcLayout);
inferredReturnTypes.assign({LayoutType::get(context, fragmentLayout)});
return success();
}
FLY_INFER_RETURN_TYPES(MakeFragmentLikeOp) {
TypeAttr dtypeAttr;
if (properties)
dtypeAttr = properties.as<Properties *>()->dtype;
auto srcLayout = GetLayoutAttrFromLayoutLikeType(operands[0].getType());
if (!srcLayout)
return emitOptionalError(location, "MakeFragmentLikeOp: expected LayoutLikeType, got ",
operands[0].getType());
Type elemTy;
if (auto memrefTy = dyn_cast<MemRefType>(operands[0].getType())) {
elemTy = dtypeAttr ? dtypeAttr.getValue() : memrefTy.getElemTy();
} else {
if (!dtypeAttr)
return emitOptionalError(
location, "MakeFragmentLikeOp: dtype is required when input is not MemRefType");
elemTy = dtypeAttr.getValue();
}
if (!srcLayout.getShape().isStatic())
return emitOptionalError(location, "MakeFragmentLikeOp: expected static shape layout, got ",
srcLayout);
LayoutBuilder<LayoutAttr> layoutBuilder(context);
LayoutAttr fragmentLayout = layoutMakeFragmentLayout(layoutBuilder, srcLayout);
inferredReturnTypes.assign({MemRefType::get(
elemTy, AddressSpaceAttr::get(context, AddressSpace::Register), fragmentLayout)});
return success();
}
//===----------------------------------------------------------------------===//
// Extractors
//===----------------------------------------------------------------------===//
FLY_INFER_RETURN_TYPES(GetScalarOp) {
auto intTupleType = dyn_cast<IntTupleType>(operands[0].getType());
if (!intTupleType)
return emitOptionalError(location, "GetScalarOp: expected IntTupleType, got ",
operands[0].getType());
IntTupleAttr scalarAttr = intTupleType.getAttr();
while (!scalarAttr.isLeaf() && scalarAttr.rank() == 1)
scalarAttr = scalarAttr.at(0);
if (!scalarAttr.isLeaf())
return emitOptionalError(location, "GetScalarOp: expected a scalar IntTuple, got ",
intTupleType);
auto intAttr = scalarAttr.extractIntFromLeaf();
inferredReturnTypes.assign({IntegerType::get(context, intAttr.getWidth())});
return success();
}
FLY_INFER_RETURN_TYPES(GetLeavesOp) {
auto intTupleType = dyn_cast<IntTupleType>(operands[0].getType());
if (!intTupleType)
return emitOptionalError(location, "GetLeavesOp: expected IntTupleType, got ",
operands[0].getType());
bool dynamicOnly = false;
if (properties)
dynamicOnly = properties.as<Properties *>()->dynamicOnly.getValue();
IntTupleBuilder<IntTupleAttr> builder(context);
SmallVector<IntTupleAttr> flatLeaves;
intTupleFlattenToVector(builder, intTupleType.getAttr(), flatLeaves);
for (auto leaf : flatLeaves) {
auto intAttr = leaf.extractIntFromLeaf();
if (dynamicOnly && intAttr.isStatic())
continue;
inferredReturnTypes.push_back(IntegerType::get(context, std::max(32, intAttr.getWidth())));
}
return success();
}
FLY_INFER_RETURN_TYPES(GetShapeOp) {
auto layout = GetLayoutAttrFromLayoutLikeType(operands[0].getType());
if (!layout)
return emitOptionalError(location, "GetShapeOp: expected LayoutLikeType, got ",
operands[0].getType());
inferredReturnTypes.assign({IntTupleType::get(layout.getShape())});
return success();
}
FLY_INFER_RETURN_TYPES(GetStrideOp) {
Type inputTy = operands[0].getType();
if (isa<ComposedLayoutType>(inputTy))
return emitOptionalError(location, "GetStrideOp: unsupported ComposedLayoutType");
if (auto memrefTy = dyn_cast<fly::MemRefType>(inputTy);
memrefTy && isa<ComposedLayoutAttr>(memrefTy.getLayout()))
return emitOptionalError(location, "GetStrideOp: unsupported MemRefType with ComposedLayout");
if (auto coordTensorTy = dyn_cast<CoordTensorType>(inputTy);
coordTensorTy && isa<ComposedLayoutAttr>(coordTensorTy.getLayout()))
return emitOptionalError(location,
"GetStrideOp: unsupported CoordTensorType with ComposedLayout");
auto layout = GetLayoutAttrFromLayoutLikeType(inputTy);
if (!layout)
return emitOptionalError(location, "GetStrideOp: expected LayoutLikeType, got ", inputTy);
inferredReturnTypes.assign({IntTupleType::get(layout.getStride())});
return success();
}
FLY_INFER_RETURN_TYPES(GetLayoutOp) {
Attribute layoutAttr;
if (auto memrefTy = dyn_cast<MemRefType>(operands[0].getType())) {
layoutAttr = memrefTy.getLayout();
} else if (auto coordTensorTy = dyn_cast<CoordTensorType>(operands[0].getType())) {
layoutAttr = coordTensorTy.getLayout();
} else {
return emitOptionalError(location, "GetLayoutOp: expected TensorLikeType, got ",
operands[0].getType());
}
if (auto layout = dyn_cast<LayoutAttr>(layoutAttr)) {
inferredReturnTypes.assign({LayoutType::get(context, layout)});
return success();
} else if (auto composed = dyn_cast<ComposedLayoutAttr>(layoutAttr)) {
inferredReturnTypes.assign({ComposedLayoutType::get(context, composed)});
return success();
}
return emitOptionalError(location, "GetLayoutOp: unsupported layout attribute type ", layoutAttr);
}
FLY_INFER_RETURN_TYPES(GetIterOp) {
if (auto memrefTy = dyn_cast<MemRefType>(operands[0].getType())) {
inferredReturnTypes.assign({PointerType::get(memrefTy.getElemTy(), memrefTy.getAddressSpace(),
memrefTy.getAlignment(), memrefTy.getSwizzle())});
return success();
}
if (auto coordTensorTy = dyn_cast<CoordTensorType>(operands[0].getType())) {
inferredReturnTypes.assign({IntTupleType::get(coordTensorTy.getBase())});
return success();
}
return emitOptionalError(location, "GetIterOp: expected TensorLikeType, got ",
operands[0].getType());
}
FLY_INFER_RETURN_TYPES(ComposedGetInnerOp) {
auto inputTy = dyn_cast<ComposedLayoutType>(operands[0].getType());
if (!inputTy)
return emitOptionalError(location, "ComposedGetInnerOp: expected ComposedLayoutType, got ",
operands[0].getType());
auto innerAttr = inputTy.getAttr().getInner();
if (auto swizzleAttr = dyn_cast<SwizzleAttr>(innerAttr)) {
inferredReturnTypes.assign({SwizzleType::get(context, swizzleAttr)});
return success();
} else if (auto layoutAttr = dyn_cast<LayoutAttr>(innerAttr)) {
inferredReturnTypes.assign({LayoutType::get(context, layoutAttr)});
return success();
} else if (auto composedLayoutAttr = dyn_cast<ComposedLayoutAttr>(innerAttr)) {
inferredReturnTypes.assign({ComposedLayoutType::get(context, composedLayoutAttr)});
return success();
}
return emitOptionalError(location, "ComposedGetInnerOp: unrecognized inner attribute type");
}
FLY_INFER_RETURN_TYPES(ComposedGetOffsetOp) {
auto inputTy = dyn_cast<ComposedLayoutType>(operands[0].getType());
if (!inputTy)
return emitOptionalError(location, "ComposedGetOffsetOp: expected ComposedLayoutType, got ",
operands[0].getType());
inferredReturnTypes.assign({IntTupleType::get(inputTy.getAttr().getOffset())});
return success();
}
FLY_INFER_RETURN_TYPES(ComposedGetOuterOp) {
auto inputTy = dyn_cast<ComposedLayoutType>(operands[0].getType());
if (!inputTy)
return emitOptionalError(location, "ComposedGetOuterOp: expected ComposedLayoutType, got ",
operands[0].getType());
inferredReturnTypes.assign({LayoutType::get(context, inputTy.getAttr().getOuter())});
return success();
}
//===----------------------------------------------------------------------===//
// IntTuple operations
//===----------------------------------------------------------------------===//
FLY_INFER_RETURN_TYPES(IntTupleAddOp) {
auto lhsTy = dyn_cast<IntTupleType>(operands[0].getType());
auto rhsTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!lhsTy)
return emitOptionalError(location, "IntTupleAddOp: expected IntTupleType for lhs, got ",
operands[0].getType());
if (!rhsTy)
return emitOptionalError(location, "IntTupleAddOp: expected IntTupleType for rhs, got ",
operands[1].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign(
{IntTupleType::get(intTupleAdd(builder, lhsTy.getAttr(), rhsTy.getAttr()))});
return success();
}
FLY_INFER_RETURN_TYPES(IntTupleSubOp) {
auto lhsTy = dyn_cast<IntTupleType>(operands[0].getType());
auto rhsTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!lhsTy)
return emitOptionalError(location, "IntTupleSubOp: expected IntTupleType for lhs, got ",
operands[0].getType());
if (!rhsTy)
return emitOptionalError(location, "IntTupleSubOp: expected IntTupleType for rhs, got ",
operands[1].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign(
{IntTupleType::get(intTupleSub(builder, lhsTy.getAttr(), rhsTy.getAttr()))});
return success();
}
FLY_INFER_RETURN_TYPES(IntTupleMulOp) {
auto lhsTy = dyn_cast<IntTupleType>(operands[0].getType());
auto rhsTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!lhsTy)
return emitOptionalError(location, "IntTupleMulOp: expected IntTupleType for lhs, got ",
operands[0].getType());
if (!rhsTy)
return emitOptionalError(location, "IntTupleMulOp: expected IntTupleType for rhs, got ",
operands[1].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign(
{IntTupleType::get(intTupleMul(builder, lhsTy.getAttr(), rhsTy.getAttr()))});
return success();
}
FLY_INFER_RETURN_TYPES(IntTupleDivOp) {
auto lhsTy = dyn_cast<IntTupleType>(operands[0].getType());
auto rhsTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!lhsTy)
return emitOptionalError(location, "IntTupleDivOp: expected IntTupleType for lhs, got ",
operands[0].getType());
if (!rhsTy)
return emitOptionalError(location, "IntTupleDivOp: expected IntTupleType for rhs, got ",
operands[1].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign(
{IntTupleType::get(intTupleDiv(builder, lhsTy.getAttr(), rhsTy.getAttr()))});
return success();
}
FLY_INFER_RETURN_TYPES(IntTupleModOp) {
auto lhsTy = dyn_cast<IntTupleType>(operands[0].getType());
auto rhsTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!lhsTy)
return emitOptionalError(location, "IntTupleModOp: expected IntTupleType for lhs, got ",
operands[0].getType());
if (!rhsTy)
return emitOptionalError(location, "IntTupleModOp: expected IntTupleType for rhs, got ",
operands[1].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign(
{IntTupleType::get(intTupleMod(builder, lhsTy.getAttr(), rhsTy.getAttr()))});
return success();
}
FLY_INFER_RETURN_TYPES(IntTupleProductEachOp) {
auto inputTy = dyn_cast<IntTupleType>(operands[0].getType());
if (!inputTy)
return emitOptionalError(location, "IntTupleProductEachOp: expected IntTupleType, got ",
operands[0].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign({IntTupleType::get(intTupleProductEach(builder, inputTy.getAttr()))});
return success();
}
FLY_INFER_RETURN_TYPES(IntTupleProductOp) {
auto inputTy = dyn_cast<IntTupleType>(operands[0].getType());
if (!inputTy)
return emitOptionalError(location, "IntTupleProductOp: expected IntTupleType, got ",
operands[0].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign({IntTupleType::get(intTupleProduct(builder, inputTy.getAttr()))});
return success();
}
FLY_INFER_RETURN_TYPES(IntTupleProductLikeOp) {
auto tupleTy = dyn_cast<IntTupleType>(operands[0].getType());
auto guideTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!tupleTy)
return emitOptionalError(location, "IntTupleProductLikeOp: expected IntTupleType for lhs, got ",
operands[0].getType());
if (!guideTy)
return emitOptionalError(location, "IntTupleProductLikeOp: expected IntTupleType for rhs, got ",
operands[1].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign(
{IntTupleType::get(intTupleProductLike(builder, tupleTy.getAttr(), guideTy.getAttr()))});
return success();
}
FLY_INFER_RETURN_TYPES(ShapeDivOp) {
auto lhsTy = dyn_cast<IntTupleType>(operands[0].getType());
auto rhsTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!lhsTy)
return emitOptionalError(location, "ShapeDivOp: expected IntTupleType for lhs, got ",
operands[0].getType());
if (!rhsTy)
return emitOptionalError(location, "ShapeDivOp: expected IntTupleType for rhs, got ",
operands[1].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign(
{IntTupleType::get(intTupleShapeDiv(builder, lhsTy.getAttr(), rhsTy.getAttr()))});
return success();
}
FLY_INFER_RETURN_TYPES(CeilDivOp) {
auto lhsTy = dyn_cast<IntTupleType>(operands[0].getType());
auto rhsTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!lhsTy)
return emitOptionalError(location, "CeilDivOp: expected IntTupleType for lhs, got ",
operands[0].getType());
if (!rhsTy)
return emitOptionalError(location, "CeilDivOp: expected IntTupleType for rhs, got ",
operands[1].getType());
IntTupleBuilder<IntTupleAttr> builder(context);
inferredReturnTypes.assign(
{IntTupleType::get(intTupleCeilDiv(builder, lhsTy.getAttr(), rhsTy.getAttr()))});
return success();
}
//===----------------------------------------------------------------------===//
// IntTupleLike operations
//===----------------------------------------------------------------------===//
FLY_INFER_RETURN_TYPES(GetOp) {
Type inputTy = operands[0].getType();
ArrayRef<int32_t> mode = properties.as<Properties *>()->mode;
int32_t depth = -1;
if (auto nested = dyn_cast<NestedTypeInterface>(inputTy))
depth = nested.depth();
else if (auto memrefTy = dyn_cast<MemRefType>(inputTy))
depth = cast<NestedAttrInterface>(memrefTy.getLayout()).depth();
else if (auto coordTensorTy = dyn_cast<CoordTensorType>(inputTy))
depth = cast<NestedAttrInterface>(coordTensorTy.getLayout()).depth();
else
return emitOptionalError(location, "GetOp: unsupported input type ", inputTy);
if (depth < static_cast<int32_t>(mode.size()))
return emitOptionalError(location, "GetOp: mode length ", mode.size(), " exceeds input depth ",
depth);
Type resultTy;
if (auto intTupleTy = dyn_cast<IntTupleType>(inputTy))
resultTy = intTupleTy.at(mode);
else if (auto layoutTy = dyn_cast<LayoutType>(inputTy))
resultTy = layoutTy.at(mode);
else if (auto composedTy = dyn_cast<ComposedLayoutType>(inputTy))
resultTy = composedTy.at(mode);
else if (auto memrefTy = dyn_cast<MemRefType>(inputTy))
resultTy = memrefTy.at(mode);
else if (auto coordTensorTy = dyn_cast<CoordTensorType>(inputTy))
resultTy = coordTensorTy.at(mode);
else
return emitOptionalError(location, "GetOp: unsupported input type ", inputTy);
inferredReturnTypes.assign({resultTy});
return success();
}
FLY_INFER_RETURN_TYPES(TakeOp) {
int32_t begin = properties.as<Properties *>()->begin.getInt();
int32_t end = properties.as<Properties *>()->end.getInt();
IntTupleBuilder<IntTupleAttr> builder(context);
Type resultTy = applyIntTupleTransform(operands[0].getType(), [&](IntTupleAttr attr) {
return intTupleTake(builder, attr, begin, end);
});
if (!resultTy)
return emitOptionalError(location, "TakeOp: unsupported input type ", operands[0].getType());
inferredReturnTypes.assign({resultTy});
return success();
}
FLY_INFER_RETURN_TYPES(SelectOp) {
auto idxArr = properties.as<Properties *>()->indices.asArrayRef();
SmallVector<int32_t> indices(idxArr.begin(), idxArr.end());
IntTupleBuilder<IntTupleAttr> builder(context);
Type resultTy = applyIntTupleTransform(operands[0].getType(), [&](IntTupleAttr attr) {
return intTupleSelect(builder, attr, indices);
});
if (!resultTy)
return emitOptionalError(location, "SelectOp: unsupported input type ", operands[0].getType());
inferredReturnTypes.assign({resultTy});
return success();
}
FLY_INFER_RETURN_TYPES(GroupOp) {
int32_t begin = properties.as<Properties *>()->begin.getInt();
int32_t end = properties.as<Properties *>()->end.getInt();
IntTupleBuilder<IntTupleAttr> builder(context);
Type resultTy = applyIntTupleTransform(operands[0].getType(), [&](IntTupleAttr attr) {
return intTupleGroup(builder, attr, begin, end);
});
if (!resultTy)
return emitOptionalError(location, "GroupOp: unsupported input type ", operands[0].getType());
inferredReturnTypes.assign({resultTy});
return success();
}
FLY_INFER_RETURN_TYPES(AppendOp) {
Type tupleTy = operands[0].getType();
Type elemTy = operands[1].getType();
int32_t n = -1;
if (properties) {
auto nAttr = properties.as<Properties *>()->n;
if (nAttr)
n = static_cast<int32_t>(nAttr.getInt());
}
IntTupleBuilder<IntTupleAttr> builder(context);
if (auto tupleIT = dyn_cast<IntTupleType>(tupleTy)) {
auto elemIT = dyn_cast<IntTupleType>(elemTy);
if (!elemIT)
return emitOptionalError(location, "AppendOp: tuple and elem must be the same category");
IntTupleAttr result = intTupleAppend(builder, tupleIT.getAttr(), elemIT.getAttr(), n);
inferredReturnTypes.assign({IntTupleType::get(result)});
return success();
}
if (auto tupleLayout = dyn_cast<LayoutType>(tupleTy)) {
auto elemLayout = dyn_cast<LayoutType>(elemTy);
if (!elemLayout)
return emitOptionalError(location, "AppendOp: tuple and elem must be the same category");
LayoutAttr baseAttr = tupleLayout.getAttr();
LayoutAttr eAttr = elemLayout.getAttr();
IntTupleAttr newShape = intTupleAppend(builder, baseAttr.getShape(), eAttr.getShape(), n);
IntTupleAttr newStride = intTupleAppend(builder, baseAttr.getStride(), eAttr.getStride(), n);
inferredReturnTypes.assign(
{LayoutType::get(context, LayoutAttr::get(context, newShape, newStride))});
return success();
}
if (auto composedTy = dyn_cast<ComposedLayoutType>(tupleTy)) {
auto elemLayout = dyn_cast<LayoutType>(elemTy);
if (!elemLayout)
return emitOptionalError(location,
"AppendOp: elem must be LayoutType when tuple is ComposedLayout");
ComposedLayoutAttr ca = composedTy.getAttr();
LayoutAttr outer = ca.getOuter();
LayoutAttr eAttr = elemLayout.getAttr();
IntTupleAttr newShape = intTupleAppend(builder, outer.getShape(), eAttr.getShape(), n);
IntTupleAttr newStride = intTupleAppend(builder, outer.getStride(), eAttr.getStride(), n);
LayoutAttr newOuter = LayoutAttr::get(context, newShape, newStride);
inferredReturnTypes.assign({ComposedLayoutType::get(
context, ComposedLayoutAttr::get(context, ca.getInner(), ca.getOffset(), newOuter))});
return success();
}
return emitOptionalError(location, "AppendOp: unsupported input type ", tupleTy);
}
FLY_INFER_RETURN_TYPES(PrependOp) {
Type tupleTy = operands[0].getType();
Type elemTy = operands[1].getType();
int32_t n = -1;
if (properties) {
auto nAttr = properties.as<Properties *>()->n;
if (nAttr)
n = static_cast<int32_t>(nAttr.getInt());
}
IntTupleBuilder<IntTupleAttr> builder(context);
if (auto tupleIT = dyn_cast<IntTupleType>(tupleTy)) {
auto elemIT = dyn_cast<IntTupleType>(elemTy);
if (!elemIT)
return emitOptionalError(location, "PrependOp: tuple and elem must be the same category");
IntTupleAttr result = intTuplePrepend(builder, tupleIT.getAttr(), elemIT.getAttr(), n);
inferredReturnTypes.assign({IntTupleType::get(result)});
return success();
}
if (auto tupleLayout = dyn_cast<LayoutType>(tupleTy)) {
auto elemLayout = dyn_cast<LayoutType>(elemTy);
if (!elemLayout)
return emitOptionalError(location, "PrependOp: tuple and elem must be the same category");
LayoutAttr baseAttr = tupleLayout.getAttr();
LayoutAttr eAttr = elemLayout.getAttr();
IntTupleAttr newShape = intTuplePrepend(builder, baseAttr.getShape(), eAttr.getShape(), n);
IntTupleAttr newStride = intTuplePrepend(builder, baseAttr.getStride(), eAttr.getStride(), n);
inferredReturnTypes.assign({LayoutType::get(LayoutAttr::get(newShape, newStride))});
return success();
}
if (auto composedTy = dyn_cast<ComposedLayoutType>(tupleTy)) {
auto elemLayout = dyn_cast<LayoutType>(elemTy);
if (!elemLayout)
return emitOptionalError(location,
"PrependOp: elem must be LayoutType when tuple is ComposedLayout");
ComposedLayoutAttr ca = composedTy.getAttr();
LayoutAttr outer = ca.getOuter();
LayoutAttr eAttr = elemLayout.getAttr();
IntTupleAttr newShape = intTuplePrepend(builder, outer.getShape(), eAttr.getShape(), n);
IntTupleAttr newStride = intTuplePrepend(builder, outer.getStride(), eAttr.getStride(), n);
LayoutAttr newOuter = LayoutAttr::get(newShape, newStride);
inferredReturnTypes.assign({ComposedLayoutType::get(ca.getInner(), ca.getOffset(), newOuter)});
return success();
}
return emitOptionalError(location, "PrependOp: unsupported input type ", tupleTy);
}
FLY_INFER_RETURN_TYPES(SliceOp) {
Type srcTy = operands[0].getType();
auto coordTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!coordTy)
return emitOptionalError(location, "SliceOp: expected IntTupleType for coord, got ",
operands[1].getType());
IntTupleAttr coordAttr = coordTy.getAttr();
IntTupleBuilder<IntTupleAttr> builder(context);
auto sliceLayout = [&](LayoutAttr layout) -> LayoutAttr {
IntTupleAttr newShape = intTupleSlice(builder, layout.getShape(), coordAttr);
IntTupleAttr newStride = intTupleSlice(builder, layout.getStride(), coordAttr);
return LayoutAttr::get(context, newShape, newStride);
};
auto sliceComposed = [&](ComposedLayoutAttr composed) -> ComposedLayoutAttr {
LayoutAttr outer = composed.getOuter();
IntTupleAttr offset = layoutCrd2Idx(builder, coordAttr, outer.getShape(), outer.getStride());
IntTupleAttr newOffset = intTupleAdd(builder, composed.getOffset(), offset);
return ComposedLayoutAttr::get(composed.getInner(), newOffset, sliceLayout(outer));
};
if (auto srcTupleTy = dyn_cast<IntTupleType>(srcTy)) {
IntTupleAttr result = intTupleSlice(builder, srcTupleTy.getAttr(), coordAttr);
inferredReturnTypes.assign({IntTupleType::get(result)});
return success();
}
if (auto srcLayoutTy = dyn_cast<LayoutType>(srcTy)) {
inferredReturnTypes.assign({LayoutType::get(sliceLayout(srcLayoutTy.getAttr()))});
return success();
}
if (auto srcComposedTy = dyn_cast<ComposedLayoutType>(srcTy)) {
inferredReturnTypes.assign({ComposedLayoutType::get(sliceComposed(srcComposedTy.getAttr()))});
return success();
}
if (auto srcMemRefTy = dyn_cast<fly::MemRefType>(srcTy)) {
Attribute layout = srcMemRefTy.getLayout();
Attribute newLayout;
int32_t valDiv = srcMemRefTy.getValueDivisibility();
int32_t newValDiv;
if (auto la = dyn_cast<LayoutAttr>(layout)) {
newLayout = sliceLayout(la);
IntTupleAttr offsetAttr = layoutCrd2Idx(builder, coordAttr, la.getShape(), la.getStride());
IntAttr offsetInt = offsetAttr.extractIntFromLeaf();
int32_t offsetDiv =
offsetInt.isStatic() ? std::abs(offsetInt.getValue()) : offsetInt.getDivisibility();
newValDiv = (offsetDiv == 0) ? valDiv : utils::divisibilityAdd(valDiv, offsetDiv);
} else {
newLayout = sliceComposed(cast<ComposedLayoutAttr>(layout));
newValDiv = valDiv;
}
inferredReturnTypes.assign({fly::MemRefType::get(
srcMemRefTy.getElemTy(), srcMemRefTy.getAddressSpace(), newLayout,
AlignAttr::get(srcMemRefTy.getElemTy(), newValDiv), srcMemRefTy.getSwizzle())});
return success();
}
if (auto srcCoordTensorTy = dyn_cast<CoordTensorType>(srcTy)) {
Attribute layout = srcCoordTensorTy.getLayout();
Attribute newLayout;
if (auto la = dyn_cast<LayoutAttr>(layout))
newLayout = sliceLayout(la);
else
newLayout = sliceComposed(cast<ComposedLayoutAttr>(layout));
inferredReturnTypes.assign({CoordTensorType::get(srcCoordTensorTy.getBase(), newLayout)});
return success();
}
return emitOptionalError(location, "SliceOp: unsupported input type ", srcTy);
}
FLY_INFER_RETURN_TYPES(DiceOp) {
Type srcTy = operands[0].getType();
auto coordTy = dyn_cast<IntTupleType>(operands[1].getType());
if (!coordTy)
return emitOptionalError(location, "DiceOp: expected IntTupleType for coord, got ",
operands[1].getType());
IntTupleAttr coordAttr = coordTy.getAttr();
IntTupleBuilder<IntTupleAttr> builder(context);
if (auto srcTupleTy = dyn_cast<IntTupleType>(srcTy)) {
IntTupleAttr result = intTupleDice(builder, srcTupleTy.getAttr(), coordAttr);
inferredReturnTypes.assign({IntTupleType::get(result)});
return success();
}
if (auto srcLayoutTy = dyn_cast<LayoutType>(srcTy)) {
LayoutAttr profile = srcLayoutTy.getAttr();
IntTupleAttr newShape = intTupleDice(builder, profile.getShape(), coordAttr);
IntTupleAttr newStride = intTupleDice(builder, profile.getStride(), coordAttr);
inferredReturnTypes.assign(
{LayoutType::get(context, LayoutAttr::get(context, newShape, newStride))});
return success();
}
return emitOptionalError(location, "DiceOp: expected IntTupleType or LayoutType, got ", srcTy);
}
//===----------------------------------------------------------------------===//
// LayoutLike operations
//===----------------------------------------------------------------------===//
FLY_INFER_RETURN_TYPES(SizeOp) {
if (auto intTupleTy = dyn_cast<IntTupleType>(operands[0].getType())) {
IntTupleBuilder<IntTupleAttr> builder(context);
IntTupleAttr size = intTupleProduct(builder, intTupleTy.getAttr());
inferredReturnTypes.assign({IntTupleType::get(size)});
return success();
}
auto layout = GetLayoutAttrFromLayoutLikeType(operands[0].getType());
if (!layout)
return emitOptionalError(location, "SizeOp: expected LayoutLikeType, got ",
operands[0].getType());
LayoutBuilder<LayoutAttr> layoutBuilder(context);
inferredReturnTypes.assign({IntTupleType::get(layoutSize(layoutBuilder, layout))});
return success();
}
FLY_INFER_RETURN_TYPES(CoprofileOp) {
auto layout = GetLayoutAttrFromLayoutLikeType(operands[0].getType());
if (!layout)
return emitOptionalError(location, "CoprofileOp: expected LayoutLikeType, got ",
operands[0].getType());
LayoutBuilder<LayoutAttr> layoutBuilder(context);
inferredReturnTypes.assign({IntTupleType::get(layoutCoprofile(layoutBuilder, layout))});
return success();
}
FLY_INFER_RETURN_TYPES(CoshapeOp) {
auto layout = GetLayoutAttrFromLayoutLikeType(operands[0].getType());
if (!layout)
return emitOptionalError(location, "CoshapeOp: expected LayoutLikeType, got ",
operands[0].getType());
LayoutBuilder<LayoutAttr> layoutBuilder(context);
inferredReturnTypes.assign({IntTupleType::get(layoutCoshape(layoutBuilder, layout))});
return success();
}
FLY_INFER_RETURN_TYPES(CosizeOp) {
auto layout = GetLayoutAttrFromLayoutLikeType(operands[0].getType());
if (!layout)
return emitOptionalError(location, "CosizeOp: expected LayoutLikeType, got ",
operands[0].getType());
LayoutBuilder<LayoutAttr> layoutBuilder(context);
inferredReturnTypes.assign({IntTupleType::get(layoutCosize(layoutBuilder, layout))});