-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathLinalgToXeGPU.cpp
1547 lines (1334 loc) · 57.8 KB
/
LinalgToXeGPU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===- LinalgToXeGPU.cpp - Linalg To XeGPU Lowering -------------*- C++ -*-===//
//
// This file is licensed 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 "gc/Transforms/Passes.h"
#include "gc/Transforms/Utils/MatcherUtils.h"
#include "gc/Transforms/Utils/StructuredOpMatcher.h"
#include "gc/Transforms/Utils/ValueUtils.h"
#include "mlir/Conversion/Passes.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/GPU/TransformOps/Utils.h"
#include "mlir/Dialect/GPU/Transforms/Passes.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Linalg/Passes.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Vector/IR/VectorOps.h"
#include "mlir/Dialect/XeGPU/IR/XeGPU.h"
#include "mlir/IR/Dialect.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/ADT/TypeSwitch.h"
#include <numeric>
#include <optional>
using namespace mlir;
using namespace mlir::gc;
using namespace mlir::xegpu;
namespace mlir {
namespace gc {
#define GEN_PASS_DEF_LINALGTOXEGPU
#include "gc/Transforms/Passes.h.inc"
} // namespace gc
} // namespace mlir
namespace {
// Represents VNNI configuration for an operand.
struct VnniConfig {
int vnniFactor;
int vnniAxis;
};
// Helper struct to keep track of tiles' position with respect to whole matrix.
struct TilesArray {
TilesArray() = delete;
TilesArray(int numRows, int numCols) {
assert(((numRows > 0) && (numCols > 0)) && "Expected 2D array shape");
for (int i = 0; i < numRows; i++) {
tileMatrix.push_back(SmallVector<Value>{});
for (int j = 0; j < numCols; j++)
tileMatrix[i].push_back(Value{});
}
}
~TilesArray() = default;
Value getTile(int row, int col) { return tileMatrix[row][col]; }
void setTile(int row, int col, Value val) { tileMatrix[row][col] = val; }
SmallVector<Value> toFlatVector() {
SmallVector<Value> flatVector;
// NOLINTBEGIN
for (auto row : tileMatrix)
flatVector.append(row);
// NOLINTEND
return flatVector;
}
SmallVector<SmallVector<Value>> tileMatrix;
};
static xegpu::TensorDescType getTensorDescType(llvm::ArrayRef<int64_t> shape,
mlir::Type elementType) {
return xegpu::TensorDescType::get(shape, elementType, /*array_length*/ 1,
/*boundary_check*/ true);
}
// Return DPAS tile sizes if the gemm-like operation fits DPAS hardware.
static bool isDPASCompatible(linalg::LinalgOp linalgOp, int kTile,
ArrayRef<int64_t> dpasTile) {
if (!(isa<linalg::MatmulOp>(linalgOp) ||
isa<linalg::BatchReduceMatmulOp>(linalgOp) ||
isa<linalg::GenericOp>(linalgOp))) {
return false;
}
// Expect MxNxK DPAS register block sizes.
if (dpasTile.size() != 3)
return false;
// Only static shapes are supported.
if (linalgOp.hasDynamicShape())
return false;
auto aType = cast<ShapedType>(linalgOp.getDpsInputs()[0].getType());
auto bType = cast<ShapedType>(linalgOp.getDpsInputs()[1].getType());
auto cType = cast<ShapedType>(linalgOp.getDpsInits()[0].getType());
auto elemTypeA = aType.getElementType();
auto elemTypeB = bType.getElementType();
auto elemTypeC = cType.getElementType();
// TODO: Add more DPAS combinations.
bool isSupportedPrecision =
(elemTypeA.isF16() && elemTypeB.isF16() && elemTypeC.isF16()) ||
(elemTypeA.isF16() && elemTypeB.isF16() && elemTypeC.isF32());
if (!isSupportedPrecision)
return false;
auto mDim = cType.getShape()[0];
auto nDim = cType.getShape()[1];
auto kDim = aType.getShape().back();
// Validate workload sizes.
// The computation dimensions must fit into the tiles.
// Reduction dimension tile size has to be compatible
// with the warp tile.
int dpasTileM = dpasTile[0];
int dpasTileN = dpasTile[1];
int dpasTileK = dpasTile[2];
// NOLINTBEGIN
if ((mDim % dpasTileM != 0) || (nDim % dpasTileN != 0) ||
(kDim % dpasTileK != 0) || (kTile % dpasTileK != 0)) {
return false;
}
// NOLINTEND
return true;
}
// Verify if linalg operands fulfill lowering constraints.
static LogicalResult isValidMemrefOperand(linalg::LinalgOp linalgOp,
Value operand,
PatternRewriter &rewriter,
unsigned maxDims = 2) {
auto type = dyn_cast<MemRefType>(operand.getType());
if (!type) {
return rewriter.notifyMatchFailure(
linalgOp, "Expect memref operand for XeGPU lowering");
}
if (type.getShape().size() > maxDims) {
return rewriter.notifyMatchFailure(
linalgOp, "Too high dimensionality for XeGPU operations");
}
auto strides = utils::getStaticStrides(operand);
if (failed(strides)) {
return rewriter.notifyMatchFailure(
linalgOp, "Expect static strides for XeGPU lowering");
}
if (strides->back() != 1) {
return rewriter.notifyMatchFailure(linalgOp,
"Expect unit stride in the innermost "
"dimension for XeGPU operations");
}
return success();
}
// Match and, if possible, lower a generic operation to an XeGPU compatible op.
// Returns the result of the lowered op or nullopt, otherwise.
static std::optional<Value> lowerGenericOp(linalg::GenericOp genericOp,
ArrayRef<Value> operands,
VectorType resType,
PatternRewriter &rewriter) {
Location loc = genericOp.getLoc();
// Expect operands to be already loaded vectors.
for (auto operand : operands) {
if (!isa<VectorType>(operand.getType()))
return std::nullopt;
}
if (structured_match::utils::isTwoDReluOp(genericOp,
/*operands=*/nullptr)) { // NOLINT
assert(operands.size() == 1 &&
"Invalid number of operands for generic 2D ReLU");
auto eltType = resType.getElementType();
Value zeroConst;
if (isa<FloatType>(eltType)) {
auto floatType = cast<FloatType>(eltType);
zeroConst = rewriter.create<arith::ConstantFloatOp>(
loc, APFloat::getZero(floatType.getFloatSemantics()), floatType);
} else if (isa<IntegerType>(eltType)) {
zeroConst = rewriter.create<arith::ConstantIntOp>(loc, 0, eltType);
} else {
// Unhandled type. Bail out.
return std::nullopt;
}
auto zeroVec =
rewriter.create<vector::BroadcastOp>(loc, resType, zeroConst);
return rewriter
.create<arith::MaximumFOp>(loc, resType, operands[0], zeroVec)
.getResult();
}
if (structured_match::utils::isTwoDAddOp(genericOp,
/*operands=*/nullptr)) { // NOLINT
assert(operands.size() == 2 &&
"Invalid number of operands for generic 2D add");
return rewriter
.create<arith::AddFOp>(loc, resType, operands[0], operands[1])
.getResult();
}
return std::nullopt;
}
// Lower an elementwise operation to an XeGPU compatible op.
// Returns the result of the lowered op or nullopt, otherwise.
static std::optional<Value> lowerEltwiseOp(linalg::LinalgOp linalgOp,
ArrayRef<Value> operands,
PatternRewriter &rewriter) {
Location loc = linalgOp.getLoc();
assert(llvm::all_of(operands,
[&](Value tile) {
return tile.getType() == operands[0].getType();
}) &&
"All eltwise operands must have the same type.");
// Expect operands to be already loaded vectors.
for (auto operand : operands) {
if (!isa<VectorType>(operand.getType()))
return std::nullopt;
}
auto operandType = cast<ShapedType>(operands[0].getType());
auto resType =
VectorType::get(operandType.getShape(), operandType.getElementType());
auto eltType = resType.getElementType();
return llvm::TypeSwitch<Operation *, std::optional<Value>>(linalgOp)
.Case([&](linalg::AbsOp absOp) -> std::optional<Value> {
assert(operands.size() == 1 && "Invalid number of operands for abs");
if (isa<FloatType>(eltType)) {
return rewriter.create<math::AbsFOp>(loc, resType, operands[0])
.getResult();
}
if (isa<IntegerType>(eltType)) {
return rewriter.create<math::AbsIOp>(loc, resType, operands[0])
.getResult();
}
// Unhandled type. Bail out.
return std::nullopt;
})
.Case([&](linalg::AddOp addOp) -> std::optional<Value> {
assert(operands.size() == 2 && "Invalid number of operands for add");
if (isa<FloatType>(eltType)) {
return rewriter
.create<arith::AddFOp>(loc, resType, operands[0], operands[1])
.getResult();
}
if (isa<IntegerType>(eltType)) {
return rewriter
.create<arith::AddIOp>(loc, resType, operands[0], operands[1])
.getResult();
}
// Unhandled type. Bail out.
return std::nullopt;
})
.Case([&](linalg::CeilOp ceilOp) -> std::optional<Value> {
assert(operands.size() == 1 && "Invalid number of operands for ceil");
return rewriter.create<math::CeilOp>(loc, resType, operands[0])
.getResult();
})
.Case([&](linalg::DivOp divOp) -> std::optional<Value> {
assert(operands.size() == 2 && "Invalid number of operands for div");
if (isa<FloatType>(eltType)) {
return rewriter
.create<arith::DivFOp>(loc, resType, operands[0], operands[1])
.getResult();
}
if (isa<IntegerType>(eltType)) {
return rewriter
.create<arith::DivSIOp>(loc, resType, operands[0], operands[1])
.getResult();
}
// Unhandled type. Bail out.
return std::nullopt;
})
.Case([&](linalg::DivUnsignedOp divUnsignedOp) -> std::optional<Value> {
assert(operands.size() == 2 &&
"Invalid number of operands for unsigned div");
if (isa<IntegerType>(eltType)) {
return rewriter
.create<arith::DivUIOp>(loc, resType, operands[0], operands[1])
.getResult();
}
// Unhandled type. Bail out.
return std::nullopt;
})
.Case([&](linalg::ExpOp expOp) -> std::optional<Value> {
assert(operands.size() == 1 && "Invalid number of operands for exp");
return rewriter.create<math::ExpOp>(loc, resType, operands[0])
.getResult();
})
.Case([&](linalg::FloorOp floorOp) -> std::optional<Value> {
assert(operands.size() == 1 && "Invalid number of operands for floor");
return rewriter.create<math::FloorOp>(loc, resType, operands[0])
.getResult();
})
.Case([&](linalg::MaxOp maxOp) -> std::optional<Value> {
assert(operands.size() == 2 && "Invalid number of operands for max");
if (isa<FloatType>(eltType)) {
return rewriter
.create<arith::MaximumFOp>(loc, resType, operands[0], operands[1])
.getResult();
}
if (isa<IntegerType>(eltType)) {
if (eltType.isUnsignedInteger()) {
return rewriter
.create<arith::MaxUIOp>(loc, resType, operands[0], operands[1])
.getResult();
} else {
return rewriter
.create<arith::MaxSIOp>(loc, resType, operands[0], operands[1])
.getResult();
}
}
// Unhandled type. Bail out.
return std::nullopt;
})
.Case([&](linalg::MulOp mulOp) -> std::optional<Value> {
assert(operands.size() == 2 && "Invalid number of operands for mul");
if (isa<FloatType>(eltType)) {
return rewriter
.create<arith::MulFOp>(loc, resType, operands[0], operands[1])
.getResult();
}
if (isa<IntegerType>(eltType)) {
return rewriter
.create<arith::MulIOp>(loc, resType, operands[0], operands[1])
.getResult();
}
// Unhandled type. Bail out.
return std::nullopt;
})
.Case([&](linalg::NegFOp negFOp) -> std::optional<Value> {
assert(operands.size() == 1 && "Invalid number of operands for negf");
return rewriter.create<arith::NegFOp>(loc, resType, operands[0])
.getResult();
})
.Case([&](linalg::SubOp subOp) -> std::optional<Value> {
assert(operands.size() == 2 && "Invalid number of operands for sub");
if (isa<FloatType>(eltType)) {
return rewriter
.create<arith::SubFOp>(loc, resType, operands[0], operands[1])
.getResult();
}
if (isa<IntegerType>(eltType)) {
return rewriter
.create<arith::SubIOp>(loc, resType, operands[0], operands[1])
.getResult();
}
// Unhandled type. Bail out.
return std::nullopt;
})
.Case([&](linalg::GenericOp genericOp) -> std::optional<Value> {
return lowerGenericOp(genericOp, operands, resType, rewriter);
})
.Default(
[&](Operation *op) -> std::optional<Value> { return std::nullopt; });
}
// Get static GPU block sizes represented by a surrounding operation
// like a kernel launch or parallel loop.
// Returns known block sizes if they are all static or failure, otherwise.
static FailureOr<SmallVector<int64_t>> getStaticBlockSizes(Operation *op) {
if (!op)
return failure();
auto getConstVal = [&](Value val) -> std::optional<int64_t> {
if (auto constOp = val.getDefiningOp<arith::ConstantIndexOp>()) {
return constOp.value();
}
return std::nullopt;
};
if (auto launchOp = dyn_cast<gpu::LaunchOp>(op)) {
auto sizeX = getConstVal(launchOp.getBlockSizeX());
auto sizeY = getConstVal(launchOp.getBlockSizeY());
auto sizeZ = getConstVal(launchOp.getBlockSizeZ());
if (!sizeX || !sizeY || !sizeZ)
return failure();
return SmallVector<int64_t>{*sizeX, *sizeY, *sizeZ};
}
// TODO: Remove when the lowering only occurs within a gpu.launch op.
// Manually computing this is brittle and duplicated parallel
// loops to gpu conversion.
if (auto blockLoop = dyn_cast<scf::ParallelOp>(op)) {
auto gridLoop = blockLoop->getParentOfType<scf::ParallelOp>();
// Blocks or number of threads are represented by the first parallel loop
// nested within another parallel loop.
//
// Fail if there is no outer parallel loop or current loop is nested more
// than once.
if (!gridLoop || (gridLoop->getParentOfType<scf::ParallelOp>())) {
return failure();
}
SmallVector<int64_t> blockSizes;
for (auto [lb, ub, step] :
llvm::zip_equal(blockLoop.getLowerBound(), blockLoop.getUpperBound(),
blockLoop.getStep())) {
auto lbVal = getConstVal(lb);
auto ubVal = getConstVal(ub);
auto stepVal = getConstVal(step);
if (!lbVal || !ubVal || !stepVal)
return failure();
int64_t blockSize = (*ubVal - *lbVal) / *stepVal;
// There must be at least one subgroup created for each dimension.
// Otherwise, bail out and let kernel outlining fail later.
if (blockSize <= 0)
return failure();
blockSizes.push_back(blockSize);
}
// Too many dimensions, something went wrong. Bail out.
if (blockSizes.size() > 3)
return failure();
return blockSizes;
}
return failure();
}
// Get linearized GPU thread ID.
static Value getGpuLinearThreadId(PatternRewriter &rewriter, Location loc) {
SmallVector<Value, 3> threadIds;
SmallVector<Value, 3> blockDims;
for (auto dim : {gpu::Dimension::x, gpu::Dimension::y, gpu::Dimension::z}) {
threadIds.push_back(rewriter.create<gpu::ThreadIdOp>(loc, dim));
blockDims.push_back(rewriter.create<gpu::BlockDimOp>(loc, dim));
}
// The default GPU indexing is modeled after CUDA:
// linear index = (z * sizeY + y) * sizeX + x
Value threadId =
rewriter.create<arith::MulIOp>(loc, threadIds[2], blockDims[1]);
threadId = rewriter.create<arith::AddIOp>(loc, threadId, threadIds[1]);
threadId = rewriter.create<arith::MulIOp>(loc, threadId, blockDims[0]);
threadId = rewriter.create<arith::AddIOp>(loc, threadId, threadIds[0]);
return threadId;
}
// Create a GEMM input tile to be loaded by each subgroup in
// cooperative fashion.
// Optionally accepts batch IV for batched GEMM input loading.
// Returns failure if it is unable to split block/workgroup for
// prefetching.
static FailureOr<xegpu::CreateNdDescOp>
createGemmCoopPrefetchTile(PatternRewriter &rewriter, linalg::LinalgOp linalgOp,
unsigned inputPos, int64_t numThreads,
ArrayRef<int> blockTile, ArrayRef<int> threadTile,
int tileStep) {
assert(inputPos <= 1 && "Can handle only GEMM inputs: mat A or mat B");
Location loc = linalgOp.getLoc();
Value src = linalgOp.getDpsInputs()[inputPos];
// Get a top level view into the whole matrix not only the thread slice.
if (auto subview = dyn_cast_or_null<memref::SubViewOp>(src.getDefiningOp())) {
src = subview.getSource();
}
const int tileRows = inputPos == 0 ? blockTile[0] : tileStep;
const int tileCols = inputPos == 0 ? tileStep : blockTile[1];
const int numElements = tileRows * tileCols;
const int elementsPerThread = numElements / numThreads;
// Limit the maximum prefetching row length to avoid very wide tiles.
//
// Currently, the max row size is capped by the hardware max load width.
//
// TODO: Expose as a tunable parameter or add some heuristics.
const int maxRowLength = 32;
// Prioritize first loading contiguous elements (row lenght/number of
// columns) only then gather any remaining elements to be loaded from
// further rows.
// Also, ensure that the prefetch tile stays within the tile bounds.
//
// Ideally, prefetch tile sizes should be derived from total number of
// elements to be loaded, number of threads/workitems, and hardware load
// size limits. Large prefetch tiles might need to be split into sub-tiles.
const int numCols =
std::min(std::min(elementsPerThread, tileCols), maxRowLength);
const int numRows = elementsPerThread / numCols;
// Bail on invalid prefetching tiles config.
if (numRows == 0 ||
((numRows * numCols * numThreads) > (tileRows * tileCols)))
return failure();
auto srcType = cast<ShapedType>(src.getType());
auto prefetchType =
getTensorDescType({numRows, numCols}, srcType.getElementType());
Value threadId = getGpuLinearThreadId(rewriter, loc);
// TODO: Simplify block offsets.
// Prefetching tile should be derived from the matmul op operands and
// exposed as a subview.
//
// Add offset if there are multiple blocks in the current tile's non-reduction
// dimension.
Value blockOffset = rewriter.create<arith::ConstantIndexOp>(loc, 0);
if (blockTile[inputPos] / threadTile[inputPos] > 1) {
Value blockSize =
rewriter.create<arith::ConstantIndexOp>(loc, blockTile[inputPos]);
// For matrix B, pick correct block dimension.
// Block min X has to be used if there is no thread tiling in the rows
// (dim X) but only in columns (dim Y).
gpu::Dimension gpuDim = gpu::Dimension::x;
if ((inputPos == 1) && (blockTile[0] / threadTile[0] > 1)) {
gpuDim = gpu::Dimension::y;
}
Value blockId = rewriter.create<gpu::BlockIdOp>(loc, gpuDim);
blockOffset = rewriter.create<arith::MulIOp>(loc, blockId, blockSize);
}
Value numColTiles =
rewriter.create<arith::ConstantIndexOp>(loc, tileStep / numCols);
if (inputPos == 1) {
numColTiles =
rewriter.create<arith::ConstantIndexOp>(loc, blockTile[1] / numCols);
}
Value tileRowOffset =
rewriter.create<arith::DivUIOp>(loc, threadId, numColTiles);
Value tileColOffset =
rewriter.create<arith::RemUIOp>(loc, threadId, numColTiles);
Value tileRowSize = rewriter.create<arith::ConstantIndexOp>(loc, numRows);
Value tileColSize = rewriter.create<arith::ConstantIndexOp>(loc, numCols);
Value eltRowOffset =
rewriter.create<arith::MulIOp>(loc, tileRowOffset, tileRowSize);
Value eltColOffset =
rewriter.create<arith::MulIOp>(loc, tileColOffset, tileColSize);
if (inputPos == 0) {
eltRowOffset =
rewriter.create<arith::AddIOp>(loc, eltRowOffset, blockOffset);
} else {
eltColOffset =
rewriter.create<arith::AddIOp>(loc, eltColOffset, blockOffset);
}
SmallVector<mlir::OpFoldResult> prefetchOffsets{eltRowOffset, eltColOffset};
return rewriter.create<xegpu::CreateNdDescOp>(
loc, prefetchType, dyn_cast<TypedValue<MemRefType>>(src),
prefetchOffsets);
}
// Insert prefetches for the given tensor descriptors.
static void prefetchTiles(PatternRewriter &rewriter, Location loc,
ValueRange prefetchTiles,
xegpu::CachePolicyAttr readCacheHint) {
// Prefetch the next set of input tiles.
for (auto tile : prefetchTiles) {
rewriter.create<xegpu::PrefetchNdOp>(loc, tile,
/*l1_hint=*/readCacheHint,
/*l2_hint=*/readCacheHint,
/*l3_hint=*/readCacheHint);
}
}
// Update all tensor descriptors offsets with the fixed offsets.
static SmallVector<Value> updateTilesOffsets(PatternRewriter &rewriter,
Location loc, ValueRange tiles,
ArrayRef<int64_t> offsets) {
SmallVector<Value> updatedTiles;
// convert static offsets to dynamic because of this IMEX bug:
// https://github.com/intel/mlir-extensions/issues/815
std::vector<Value> dynOffsets;
for (auto &x : offsets) {
Value offset = rewriter.create<arith::ConstantIndexOp>(loc, x);
dynOffsets.push_back(offset);
}
ValueRange newOffsets{dynOffsets};
for (auto tile : tiles) {
auto updatedTile = rewriter
.create<xegpu::UpdateNdOffsetOp>(
loc, tile.getType(), tile,
/*offsets=*/newOffsets,
SmallVector<int64_t>{ShapedType::kDynamic,
ShapedType::kDynamic})
.getResult();
updatedTiles.push_back(updatedTile);
}
return updatedTiles;
}
// Split a source into a series of descriptor tiles.
//
// The descriptors collectively load a 2D shape at the specified offsets from
// the given source.
// The offsets and the load shape must stay within the source boundaries.
//
// The descriptor sub-tiles are ordered in row-major fashion with respect to the
// whole load tile.
static SmallVector<Value> createDescriptorTiles(PatternRewriter &rewriter,
Location loc, Value src,
ArrayRef<int64_t> loadShape,
ArrayRef<int64_t> loadOffsets,
ArrayRef<int64_t> descTile,
int arrayLength = 1) {
assert(arrayLength == 1 && "Array descriptors are not supported");
auto type = cast<ShapedType>(src.getType());
auto descType = getTensorDescType(descTile, type.getElementType());
// Create the root descriptor.
//
// It is more efficient to create remainig descriptors by only updating its
// offsets compared to creating separate descriptors.
// The original tile is split into contiguous sub-tiles so, the first tile
// can be used as an anchor.
Value rootOffsetRow =
rewriter.create<arith::ConstantIndexOp>(loc, loadOffsets[0]);
Value rootOffsetCol =
rewriter.create<arith::ConstantIndexOp>(loc, loadOffsets[1]);
mlir::SmallVector<mlir::OpFoldResult> offsets{rootOffsetRow, rootOffsetCol};
auto rootTile =
rewriter
.create<xegpu::CreateNdDescOp>(
loc, descType, dyn_cast<TypedValue<MemRefType>>(src), offsets)
.getResult();
SmallVector<Value> tiles;
for (int i = 0; i < loadShape[0]; i += descTile[0]) {
// convert static offsets to dynamic because of this IMEX bug:
// https://github.com/intel/mlir-extensions/issues/815
Value newRowOffs = rewriter.create<arith::ConstantIndexOp>(loc, i);
for (int j = 0; j < loadShape[1]; j += descTile[1] * arrayLength) {
Value newColOffs = rewriter.create<arith::ConstantIndexOp>(loc, j);
auto tile = rewriter
.create<xegpu::UpdateNdOffsetOp>(
loc, descType, rootTile,
/*offsets=*/ValueRange{newRowOffs, newColOffs},
SmallVector<int64_t>{ShapedType::kDynamic,
ShapedType::kDynamic})
.getResult();
tiles.push_back(tile);
}
}
return tiles;
}
// Create coarse sub-tiles to be loaded by the current subgroup.
//
// The shape to be loaded is split into the largest 2D loads supported
// by the hardware.
//
// The load subgroup tiles are ordered in row-major fashion with respect to the
// source shape.
static SmallVector<Value> createCoarseDscTiles(PatternRewriter &rewriter,
Location loc, Value src,
ArrayRef<int64_t> sgTile,
bool isVnni) {
assert(sgTile.size() <= 2 &&
"Require at most 2D tile size for eltwise lowering");
// Ensure that load is 2D.
// TODO: Add support for 1D loads.
SmallVector<int64_t, 2> sgTile2D{sgTile};
if (sgTile.size() == 1)
sgTile2D.push_back(1);
auto type = cast<ShapedType>(src.getType());
auto elemByteWidth = type.getElementType().getIntOrFloatBitWidth() / 8;
// TODO: Fetch actual list of supported load configs.
int64_t maxHeight = 32;
int64_t maxWidth = 64 / elemByteWidth;
// Assumes VNNI-factor 2.
// TODO: Make the VNNI-factor flexible.
if (isVnni)
maxWidth /= 2;
int64_t maxArrayLength = 4;
int64_t sgLoadRows = std::min(sgTile2D[0], maxHeight);
int64_t sgLoadCols = std::min(sgTile2D[1], maxWidth);
int64_t arrayLength = std::min(maxWidth / sgLoadCols, maxArrayLength);
// In case of partial fit, load only single tile.
// NOLINTBEGIN
if (maxWidth % sgLoadCols != 0 || arrayLength != 4 || arrayLength != 2)
arrayLength = 1;
// TODO: Add variable array_length support.
arrayLength = 1;
// NOLINTEND
return createDescriptorTiles(rewriter, loc, src, sgTile2D, {0, 0},
{sgLoadRows, sgLoadCols}, arrayLength);
}
// Return vector type with specified VNNI shape.
static VectorType getVnniVector(ArrayRef<int64_t> shape, Type elementType,
VnniConfig vnniConf) {
assert(shape.size() == 2 && "Expected plain 2D shape");
SmallVector<int64_t> vecShape{shape};
vecShape[vnniConf.vnniAxis] /= vnniConf.vnniFactor;
vecShape.push_back(vnniConf.vnniFactor);
return VectorType::get(vecShape, elementType);
}
// Loads n-D tiles from memory to registers.
static SmallVector<Value>
loadNdDescTiles(PatternRewriter &rewriter, Location loc, ValueRange loadTiles,
xegpu::CachePolicyAttr hint,
std::optional<VnniConfig> vnniConf = std::nullopt,
DenseI64ArrayAttr transpose = nullptr) {
// Assume all tiles have the same shape.
auto tileType = cast<xegpu::TensorDescType>(loadTiles[0].getType());
assert(llvm::all_of(loadTiles,
[&](Value tile) { return tile.getType() == tileType; }) &&
"All load tiles must have the same type.");
VectorType vecLoadType =
VectorType::get(tileType.getShape(), tileType.getElementType());
mlir::UnitAttr packedAttr = nullptr;
if (vnniConf) {
vecLoadType = getVnniVector(tileType.getShape(), tileType.getElementType(),
*vnniConf);
packedAttr = mlir::UnitAttr::get(rewriter.getContext());
}
IntegerAttr transpose_bit = nullptr;
SmallVector<Value> loadVec;
for (auto tile : loadTiles) {
auto loadOp = rewriter.create<xegpu::LoadNdOp>(
loc, vecLoadType, tile, packedAttr, transpose, transpose_bit,
/*l1_hint=*/hint,
/*l2_hint=*/hint, /*l3_hint=*/hint);
loadVec.push_back(loadOp);
}
// TODO: Add split over the array_length > 1.
// The split must preserve row-major ordering of the load tiles.
return loadVec;
}
// Splits loaded tiles of a larger 2D tile into individual subtiles and places
// them in their corresponding positions with respect to the original large
// tile.
//
// The loaded tiles must be perfectly divisible by the specified subtiles.
// Assumes row-major ordering for both the loaded tiles and the original tile.
//
// If the loaded tiles use VNNI layout, corresponding VNNI configuration must be
// provided.
static TilesArray
extractVecSubTiles(PatternRewriter &rewriter, Location loc,
ValueRange loadVecTiles, ArrayRef<int64_t> sgTotalTile,
ArrayRef<int64_t> loadTile, ArrayRef<int64_t> subTile,
std::optional<VnniConfig> vnniConf = std::nullopt) {
auto vecLoadType = cast<VectorType>(loadVecTiles[0].getType());
assert(llvm::all_of(loadVecTiles,
[&](Value tile) {
return cast<VectorType>(tile.getType()) == vecLoadType;
}) &&
"All loaded vectors must have the same type.");
assert(vecLoadType.getShape().size() == 2 ||
(vnniConf && "Requires VNNI config for non 2D loaded tiles"));
// Accumulate all dimensions as the vector might have extra VNNI
// dimensions.
int loadVecSize = std::accumulate(vecLoadType.getShape().begin(),
vecLoadType.getShape().end(), 1,
std::multiplies<int64_t>());
auto loadVecFlat = VectorType::get(loadVecSize, vecLoadType.getElementType());
VectorType vecSubTileType =
VectorType::get(subTile, vecLoadType.getElementType());
if (vnniConf) {
vecSubTileType =
getVnniVector(subTile, vecLoadType.getElementType(), *vnniConf);
}
const int totalTileRows = sgTotalTile[0] / loadTile[0];
const int totalTileCols = sgTotalTile[1] / loadTile[1];
const int subTilesPerLoadRow = loadTile[0] / subTile[0];
const int subTilePerLoadCol = loadTile[1] / subTile[1];
const int subTileRows = sgTotalTile[0] / subTile[0];
const int subTileCols = sgTotalTile[1] / subTile[1];
TilesArray subTiles(subTileRows, subTileCols);
// Iterate over the total tile.
for (int m = 0; m < totalTileRows; m++) {
for (int k = 0; k < totalTileCols; k++) {
// Load tiles are ordered in row-major fashion.
int loadIdx = m * totalTileCols + k;
auto sgTotalTile = loadVecTiles[loadIdx];
auto castFlat =
rewriter.create<vector::ShapeCastOp>(loc, loadVecFlat, sgTotalTile);
// Iterate over load tiles.
// Each load tile contains one or more sub-tiles.
for (int i = 0; i < subTilesPerLoadRow; i++) {
for (int j = 0; j < subTilePerLoadCol; j++) {
const int subTileSize = subTile[0] * subTile[1];
int dpasIdx = i * subTilePerLoadCol + j;
int offset = dpasIdx * subTileSize;
auto slice = rewriter.create<vector::ExtractStridedSliceOp>(
loc, castFlat, /*offsets=*/ArrayRef<int64_t>{offset},
/*sizes=*/ArrayRef<int64_t>{subTileSize},
/*strides=*/ArrayRef<int64_t>{1});
auto castTile =
rewriter.create<vector::ShapeCastOp>(loc, vecSubTileType, slice);
// Insert the sub-tiles in their position relative to the whole
// subgroup tile.
int rowIdx = m * subTilesPerLoadRow + i;
int colIdx = k * subTilePerLoadCol + j;
subTiles.setTile(rowIdx, colIdx, castTile);
}
}
}
}
return subTiles;
}
// Create XeGPU DPAS kernel out of GEMM-like operation.
static LogicalResult createDPASKernel(linalg::LinalgOp linalgOp,
ArrayRef<int64_t> dpasTile, int kTile,
int prefetchStages,
PatternRewriter &rewriter) {
assert((isa<linalg::MatmulOp>(linalgOp) ||
isa<linalg::BatchReduceMatmulOp>(linalgOp) ||
isa<linalg::GenericOp>(linalgOp)) &&
"Requires a GEMM-like op for DPAS lowering");
Location loc = linalgOp.getLoc();
auto ctx = linalgOp.getContext();
auto matA = linalgOp.getDpsInputs()[0];
auto matB = linalgOp.getDpsInputs()[1];
auto matC = linalgOp.getDpsInits()[0];
auto typeA = cast<ShapedType>(matA.getType());
auto typeC = cast<ShapedType>(matC.getType());
int64_t dpasTileM = dpasTile[0];
int64_t dpasTileN = dpasTile[1];
int64_t dpasTileK = dpasTile[2];
// Cache hints for loads and stores.
auto readCacheHint =
xegpu::CachePolicyAttr::get(ctx, xegpu::CachePolicy::CACHED);
auto writeCacheHint =
xegpu::CachePolicyAttr::get(ctx, xegpu::CachePolicy::WRITE_BACK);
bool isBrgemm = isa<linalg::BatchReduceMatmulOp>(linalgOp);
Value zero = rewriter.create<arith::ConstantIndexOp>(loc, 0);
int dimM = typeC.getShape()[0];
int dimN = typeC.getShape()[1];
int dimK = typeA.getShape().back();
// Create C sub-tiles.
auto dpasTypeC =
getTensorDescType({dpasTileM, dpasTileN}, typeC.getElementType());
SmallVector<Value> tilesC = createDescriptorTiles(
rewriter, loc, matC, typeC.getShape(), {0, 0}, dpasTypeC.getShape());
// Load C sub-tiles.
// Fetch the inital values of the output accumulator.
SmallVector<Value> loadVecC =
loadNdDescTiles(rewriter, loc, tilesC, readCacheHint);
// DPAS only works with F32 accumulators.
auto dpasResType =
VectorType::get(dpasTypeC.getShape(), FloatType::getF32(ctx));
// Extend the accumulation values if needed.
auto convOutPrecision = !typeC.getElementType().isF32();
if (convOutPrecision) {
for (size_t i = 0; i < loadVecC.size(); i++) {
auto extOp =
rewriter.create<arith::ExtFOp>(loc, dpasResType, loadVecC[i]);
loadVecC[i] = extOp.getOut();
}
}
// Create a loop and step into it.
auto startLoop = [&](int lb, int ub, int step,
ValueRange iterArgs) -> scf::ForOp {
Value lbCst = rewriter.create<arith::ConstantIndexOp>(loc, lb);
Value ubCst = rewriter.create<arith::ConstantIndexOp>(loc, ub);
Value stepCst = rewriter.create<arith::ConstantIndexOp>(loc, step);
scf::ForOp loopOp =
rewriter.create<scf::ForOp>(loc, lbCst, ubCst, stepCst, iterArgs);
rewriter.setInsertionPointToStart(loopOp.getBody());
return loopOp;
};
auto getLoopIterValues = [&](scf::ForOp loopOp) -> SmallVector<Value> {
SmallVector<Value> loopIterVals;
for (auto iterArg : loopOp.getRegionIterArgs())
loopIterVals.push_back(iterArg);
return loopIterVals;
};
OpBuilder::InsertionGuard guard(rewriter);
// Construct and move into batch reduction loop.
// Propagate output values as iter args.
scf::ForOp batchLoop;
Value batchIv;
if (isBrgemm) {
batchLoop = startLoop(0, typeA.getShape()[0], 1, loadVecC);
batchIv = batchLoop.getInductionVar();
loadVecC = getLoopIterValues(batchLoop);
// TODO: Replace input matrices A and B with subviews on the current
// batchIV as loads can only be performed on 2D memrefs.
}
// Create A sub-tiles.
SmallVector<Value> tilesA =
createCoarseDscTiles(rewriter, loc, matA, {dimM, kTile}, /*isVnni=*/true);
// Create B sub-tiles.
SmallVector<Value> tilesB =
createCoarseDscTiles(rewriter, loc, matB, {kTile, dimN}, /*isVnni=*/true);
// Create input prefetch tiles.
int64_t numThreads = 1;
auto blockDims =
getStaticBlockSizes(linalgOp->getParentOfType<scf::ParallelOp>());
if (succeeded(blockDims)) {
numThreads = std::accumulate(blockDims->begin(), blockDims->end(), 1,
std::multiplies<int64_t>());
}
// Disable prefetching when there is no block/workgroup parallelism.
bool isCoopPrefetch = numThreads > 1;
Value prefetchA;
Value prefetchB;
xegpu::TensorDescType prefetchTypeA;
xegpu::TensorDescType prefetchTypeB;
if (isCoopPrefetch) {
// Return dimension size on which the whole block/workgroup operates.
auto getBlockLevelSize = [&](Value val, int dim) -> int {
if (auto subview =
dyn_cast_or_null<memref::SubViewOp>(val.getDefiningOp())) {
val = subview.getSource();
}
return cast<ShapedType>(val.getType()).getShape()[dim];
};
int blockRows = getBlockLevelSize(matC, 0);
int blockCols = getBlockLevelSize(matC, 1);
auto prefetchDescA = createGemmCoopPrefetchTile(
rewriter, linalgOp, /*inputPos=*/0, numThreads, {blockRows, blockCols},
{dimM, dimN}, kTile);
auto prefetchDescB = createGemmCoopPrefetchTile(
rewriter, linalgOp, /*inputPos=*/1, numThreads, {blockRows, blockCols},
{dimM, dimN}, kTile);