-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathXeGPUOps.cpp
696 lines (577 loc) · 26 KB
/
XeGPUOps.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
//===- XeGPUOps.cpp - MLIR XeGPU ops implementation -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Arith/Utils/Utils.h"
#include "mlir/Dialect/Utils/StaticValueUtils.h"
#include "mlir/Dialect/XeGPU/IR/XeGPU.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/TypeUtilities.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "xegpu"
namespace mlir {
namespace xegpu {
static void transpose(llvm::ArrayRef<int64_t> trans,
SmallVector<int64_t> &shape) {
SmallVector<int64_t> old = shape;
for (size_t i = 0; i < trans.size(); i++)
shape[i] = old[trans[i]];
}
template <typename T>
static std::string makeString(T array, bool breakline = false) {
std::string buf;
buf.clear();
llvm::raw_string_ostream os(buf);
os << "[";
for (size_t i = 1; i < array.size(); i++) {
os << array[i - 1] << ", ";
if (breakline)
os << "\n\t\t";
}
os << array.back() << "]";
return buf;
}
static SmallVector<int64_t> getShapeOf(Type type) {
SmallVector<int64_t> shape;
if (auto ty = llvm::dyn_cast<ShapedType>(type))
shape = SmallVector<int64_t>(ty.getShape());
else
shape.push_back(1);
return shape;
}
static int64_t getRankOf(Value val) {
auto type = val.getType();
if (auto ty = llvm::dyn_cast<ShapedType>(type))
return ty.getRank();
return 0;
}
static bool isReadHintOrNone(const CachePolicyAttr &attr) {
if (!attr)
return true;
auto kind = attr.getValue();
return kind == CachePolicy::CACHED || kind == CachePolicy::UNCACHED ||
kind == CachePolicy::STREAMING || kind == CachePolicy::READ_INVALIDATE;
}
static bool isWriteHintOrNone(const CachePolicyAttr &attr) {
if (!attr)
return true;
auto kind = attr.getValue();
return kind == CachePolicy::CACHED || kind == CachePolicy::UNCACHED ||
kind == CachePolicy::WRITE_BACK || kind == CachePolicy::WRITE_THROUGH;
}
// Helper to validate value shape of LoadNd and StoreNd ops.
static LogicalResult
isArgShapesValid(TensorDescType tdescTy, VectorType valueTy,
ArrayRef<int64_t> adjustedTdescShape,
function_ref<InFlightDiagnostic()> emitError) {
auto layout = tdescTy.getLayoutAttr();
auto valueShape = valueTy.getShape();
// layout not present means IR is in SIMD mode. In this case value shape must
// match adjusted tensor descriptor shape.
if (!layout)
return valueShape == adjustedTdescShape
? success()
: emitError()
<< "Value shape " << makeString(valueShape)
<< " is not consistent with tensor descriptor " << tdescTy;
// layout present means IR is in SIMT mode. In this case layout determines the
// value shape.
auto expectedValueShapeOrFailure = tdescTy.getDistributedVectorType();
assert(succeeded(expectedValueShapeOrFailure) &&
"Failed to compute distributed vector shape for "
"tensor descriptor ");
return valueTy == expectedValueShapeOrFailure.value()
? success()
: emitError()
<< "Result shape " << makeString(valueShape)
<< " is not consistent with distributed vector shape "
<< makeString(expectedValueShapeOrFailure.value().getShape())
<< " for tensor descriptor " << tdescTy;
}
static bool isEvenDistributed(llvm::ArrayRef<int64_t> shape,
xegpu::LayoutAttr attr) {
assert(attr && "Layout attribute is missing.");
llvm::SmallVector<int32_t> defaults(shape.size(), 1);
llvm::ArrayRef<int32_t> layout, data;
if (auto sg_layout = attr.getSgLayout()) {
layout = sg_layout.asArrayRef();
auto sg_data = attr.getSgData();
data = sg_data? sg_data.asArrayRef(): defaults;
} else {
layout = attr.getLaneLayout().asArrayRef();
auto lane_data = attr.getLaneData();
data = lane_data? lane_data.asArrayRef(): defaults;
}
for (auto [s, d, l] : llvm::zip_equal(shape, data, layout)) {
// check s % (d * l) != 0
if (s % d != 0 || (s / d) % l != 0)
return false;
}
return true;
}
//===----------------------------------------------------------------------===//
// XeGPU_CreateNdDescOp
//===----------------------------------------------------------------------===//
void CreateNdDescOp::build(OpBuilder &builder, OperationState &state,
Type tdesc, TypedValue<MemRefType> source,
llvm::ArrayRef<OpFoldResult> offsets) {
[[maybe_unused]] auto ty = source.getType();
assert(ty.hasStaticShape() && offsets.size() == (size_t)ty.getRank());
llvm::SmallVector<int64_t> staticOffsets;
llvm::SmallVector<Value> dynamicOffsets;
dispatchIndexOpFoldResults(offsets, dynamicOffsets, staticOffsets);
build(builder, state, tdesc, source, dynamicOffsets /* dynamic offsets */,
ValueRange({}) /* empty dynamic shape */,
ValueRange({}) /* empty dynamic strides */,
staticOffsets /* const offsets */, {} /* empty const shape*/,
{} /* empty const strides*/);
}
void CreateNdDescOp::build(OpBuilder &builder, OperationState &state,
Type tdesc, TypedValue<MemRefType> source,
llvm::ArrayRef<OpFoldResult> offsets,
llvm::ArrayRef<OpFoldResult> shape,
llvm::ArrayRef<OpFoldResult> strides) {
assert(shape.size() && offsets.size() && strides.size() &&
shape.size() == strides.size() && shape.size() == offsets.size());
llvm::SmallVector<int64_t> staticOffsets;
llvm::SmallVector<int64_t> staticShape;
llvm::SmallVector<int64_t> staticStrides;
llvm::SmallVector<Value> dynamicOffsets;
llvm::SmallVector<Value> dynamicShape;
llvm::SmallVector<Value> dynamicStrides;
dispatchIndexOpFoldResults(offsets, dynamicOffsets, staticOffsets);
dispatchIndexOpFoldResults(shape, dynamicShape, staticShape);
dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides);
auto staticOffsetsAttr = builder.getDenseI64ArrayAttr(staticOffsets);
auto staticShapeAttr = builder.getDenseI64ArrayAttr(staticShape);
auto staticStridesAttr = builder.getDenseI64ArrayAttr(staticStrides);
build(builder, state, tdesc, source, dynamicOffsets, dynamicShape,
dynamicStrides, staticOffsetsAttr, staticShapeAttr, staticStridesAttr);
}
void CreateNdDescOp::build(OpBuilder &builder, OperationState &state,
Type tdesc, TypedValue<IntegerType> source,
llvm::ArrayRef<OpFoldResult> offsets,
llvm::ArrayRef<OpFoldResult> shape,
llvm::ArrayRef<OpFoldResult> strides) {
assert(shape.size() && offsets.size() && strides.size() &&
shape.size() == strides.size() && shape.size() == offsets.size());
llvm::SmallVector<int64_t> staticOffsets;
llvm::SmallVector<int64_t> staticShape;
llvm::SmallVector<int64_t> staticStrides;
llvm::SmallVector<Value> dynamicOffsets;
llvm::SmallVector<Value> dynamicShape;
llvm::SmallVector<Value> dynamicStrides;
dispatchIndexOpFoldResults(offsets, dynamicOffsets, staticOffsets);
dispatchIndexOpFoldResults(shape, dynamicShape, staticShape);
dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides);
auto staticOffsetsAttr = builder.getDenseI64ArrayAttr(staticOffsets);
auto staticShapeAttr = builder.getDenseI64ArrayAttr(staticShape);
auto staticStridesAttr = builder.getDenseI64ArrayAttr(staticStrides);
build(builder, state, tdesc, source, dynamicOffsets, dynamicShape,
dynamicStrides, staticOffsetsAttr, staticShapeAttr, staticStridesAttr);
}
LogicalResult CreateNdDescOp::verify() {
auto rank = (int64_t)getMixedOffsets().size();
bool invalidRank = false;
bool invalidElemTy = false;
// Memory space of created TensorDesc should match with the source.
// Both source and TensorDesc are considered for global memory by default,
// if the memory scope attr is not specified. If source is an integer,
// it is considered as ptr to global memory.
auto srcMemorySpace = getSourceMemorySpace();
auto tdescMemorySpace = static_cast<unsigned>(getType().getMemorySpace());
if (srcMemorySpace != tdescMemorySpace)
return emitOpError("Memory space mismatch.")
<< " Source: " << srcMemorySpace
<< ", TensorDesc: " << tdescMemorySpace;
// check source type matches the rank if it is a memref.
// It also should have the same ElementType as TensorDesc.
auto memrefTy = dyn_cast<MemRefType>(getSourceType());
if (memrefTy) {
invalidRank |= (memrefTy.getRank() != rank);
invalidElemTy |= memrefTy.getElementType() != getElementType();
}
// mismatches among shape, strides, and offsets are
// already handeled by OffsetSizeAndStrideOpInterface.
// So they are not check here.
if (invalidRank)
return emitOpError(
"Expecting the rank of shape, strides, offsets, and source (if source "
"is a memref) should match with each other.");
// check result TensorDesc rank
invalidRank = (getType().getRank() > 2 || getType().getRank() > rank);
if (invalidRank)
return emitOpError(
"Expecting the TensorDesc rank is up to 2 and not greater than the "
"ranks of shape, strides, offsets or the memref source.");
if (invalidElemTy)
return emitOpError("TensorDesc should have the same element "
"type with the source if it is a memref.\n");
if (getType().isScattered())
return emitOpError("Expects a non-scattered TensorDesc.\n");
return success();
}
//===----------------------------------------------------------------------===//
// XeGPU_PrefetchNdOp
//===----------------------------------------------------------------------===//
LogicalResult PrefetchNdOp::verify() {
auto tdescTy = getTensorDescType();
if (tdescTy.isScattered())
return emitOpError("Expects a non-scattered TensorDesc.\n");
if (!isReadHintOrNone(getL1HintAttr()))
return emitOpError("invalid l1_hint: ") << getL1HintAttr();
if (!isReadHintOrNone(getL2HintAttr()))
return emitOpError("invalid l2_hint: ") << getL2HintAttr();
if (!isReadHintOrNone(getL3HintAttr()))
return emitOpError("invalid l3_hint: ") << getL3HintAttr();
return success();
}
//===----------------------------------------------------------------------===//
// XeGPU_LoadNdOp
//===----------------------------------------------------------------------===//
LogicalResult LoadNdOp::verify() {
auto tdescTy = getTensorDescType();
auto valueTy = getType();
if (tdescTy.getRank() > 2)
return emitOpError("Expecting a 1D/2D TensorDesc.\n");
if (tdescTy.isScattered())
return emitOpError("Expects a non-scattered TensorDesc.\n");
if (!valueTy)
return emitOpError("Invalid result, it should be a VectorType.\n");
if (!isReadHintOrNone(getL1HintAttr()))
return emitOpError("invalid l1_hint: ") << getL1HintAttr();
if (!isReadHintOrNone(getL2HintAttr()))
return emitOpError("invalid l2_hint: ") << getL2HintAttr();
if (!isReadHintOrNone(getL3HintAttr()))
return emitOpError("invalid l3_hint: ") << getL3HintAttr();
auto array_len = tdescTy.getArrayLength();
// adjusted tensor descriptor shape tracks the expected shape of the result.
auto adjustedTdescShape = getShapeOf(tdescTy);
auto valueShape = getShapeOf(valueTy);
if (getTranspose()) {
auto trans = getTranspose().value();
// Make sure the transpose value is valid.
bool valid = std::all_of(trans.begin(), trans.end(), [&](int t) {
return t >= 0 && t < tdescTy.getRank();
});
if (valid)
transpose(trans, adjustedTdescShape);
else
mlir::emitWarning(getLoc()) << "Invalid transpose attr. It is ignored.";
}
if (getPacked()) {
if (tdescTy.getRank() == 2) {
const int axis = 0;
auto vnni_factor = valueShape.back();
adjustedTdescShape[axis] /= vnni_factor;
adjustedTdescShape.push_back(vnni_factor);
} else {
mlir::emitWarning(getLoc())
<< "Invalid Packed Attr. It is ignored (available for 2D "
"TensorDesc only).";
}
}
if (array_len > 1) {
auto it = adjustedTdescShape.begin();
adjustedTdescShape.insert(it, array_len);
}
return isArgShapesValid(tdescTy, valueTy, adjustedTdescShape,
[&]() { return emitOpError(); });
}
//===----------------------------------------------------------------------===//
// XeGPU_StoreNdOp
//===----------------------------------------------------------------------===//
LogicalResult StoreNdOp::verify() {
auto dstTy = getTensorDescType(); // Tile
auto valTy = getValueType(); // Vector
if (dstTy.getRank() > 2)
return emitOpError("Expecting a 1D/2D TensorDesc.\n");
if (dstTy.isScattered())
return emitOpError("Expects a non-scattered TensorDesc.\n");
if (!valTy)
return emitOpError("Expecting a VectorType result.\n");
if (!isWriteHintOrNone(getL1HintAttr()))
return emitOpError("invalid l1_hint: ") << getL1HintAttr();
if (!isWriteHintOrNone(getL2HintAttr()))
return emitOpError("invalid l2_hint: ") << getL2HintAttr();
if (!isWriteHintOrNone(getL3HintAttr()))
return emitOpError("invalid l3_hint: ") << getL3HintAttr();
auto tdescShape = getShapeOf(dstTy);
auto valueShape = getShapeOf(valTy);
return isArgShapesValid(dstTy, valTy, tdescShape,
[&]() { return emitOpError(); });
}
//===----------------------------------------------------------------------===//
// XeGPU_UpdateNDOffsetOp
//===----------------------------------------------------------------------===//
LogicalResult UpdateNdOffsetOp::verify() {
auto ty = getTensorDescType();
if (ty.isScattered())
return emitOpError("Expects a non-scattered TensorDesc.\n");
// number of offsets specified must match the rank of the tensor descriptor
if (ty.getRank() != (int64_t)getNumOffsets()) {
return emitOpError("Invalid number of offsets.");
}
return success();
}
//===----------------------------------------------------------------------===//
// XeGPU_CreateDescOp
//===----------------------------------------------------------------------===//
void CreateDescOp::build(OpBuilder &builder, OperationState &state,
TensorDescType TensorDesc, Value source,
llvm::ArrayRef<OpFoldResult> offsets) {
auto loc = source.getLoc();
int64_t size = static_cast<int64_t>(offsets.size());
auto type = VectorType::get(size, builder.getIndexType());
auto values = getValueOrCreateConstantIndexOp(builder, loc, offsets);
auto offset = builder.create<vector::FromElementsOp>(loc, type, values);
build(builder, state, TensorDesc, source, offset);
}
void CreateDescOp::build(OpBuilder &builder, OperationState &state,
TensorDescType TensorDesc, Value source,
llvm::ArrayRef<int64_t> offsets) {
auto ofrs = getAsIndexOpFoldResult(builder.getContext(), offsets);
build(builder, state, TensorDesc, source, ofrs);
}
LogicalResult CreateDescOp::verify() {
auto tdescTy = getTensorDescType();
if (getRankOf(getSource()) > 1)
return emitOpError(
"Expecting the source is a 1D memref or pointer (uint64_t).");
if (!tdescTy.isScattered())
return emitOpError("Expects a scattered TensorDesc.\n");
// Memory space of created TensorDesc should match with the source.
// Both source and TensorDesc are considered for global memory by default,
// if the memory scope attr is not specified. If source is an integer,
// it is considered as ptr to global memory.
auto srcMemorySpace = getSourceMemorySpace();
auto tdescMemorySpace = static_cast<unsigned>(tdescTy.getMemorySpace());
if (srcMemorySpace != tdescMemorySpace)
return emitOpError("Memory space mismatch.")
<< " Source: " << srcMemorySpace
<< ", TensorDesc: " << tdescMemorySpace;
// check total size
auto chunkSize = tdescTy.getChunkSize();
auto elemBits = tdescTy.getElementType().getIntOrFloatBitWidth();
auto bitsPerLane = elemBits * chunkSize;
if (chunkSize > 1 && bitsPerLane % 32) {
// For 8-bit and 16-bit data, the hardware only supports chunk size of 1.
// For 32-bit data, the hardware can support larger larger chunk size. So
// we can bitcast 8-bit/16-bit data to 32-bit data for better performance.
// But this requires the total size is 32 bit aligned to make the
// optimization work.
return emitOpError(
"access size (chunk_size * sizeof(elemTy)) should be 32-bit aligned.");
}
auto lscConstraints = 512 * 8; // each access is upto 512 bytes.
if (elemBits * tdescTy.getNumElements() > lscConstraints)
return emitOpError("total access size (simd_lanes * chunk_size * "
"sizeof(elemTy)) is upto 512 bytes.");
SmallVector<int64_t> shape({(int64_t)getNumOffsets()});
if (chunkSize != 1)
shape.push_back(chunkSize);
auto tdescShape = getShapeOf(tdescTy);
if (shape != tdescShape)
return emitOpError("Incorrect TensorDesc shape. ")
<< "Expected is " << makeString(shape) << "\n";
return success();
}
//===----------------------------------------------------------------------===//
// XeGPU_PrefetchOp
//===----------------------------------------------------------------------===//
LogicalResult PrefetchOp::verify() {
auto tdescTy = getTensorDescType();
if (!tdescTy.isScattered())
return emitOpError("Expects a scattered TensorDesc.\n");
if (!isReadHintOrNone(getL1HintAttr()))
return emitOpError("invalid l1_hint: ") << getL1HintAttr();
if (!isReadHintOrNone(getL2HintAttr()))
return emitOpError("invalid l2_hint: ") << getL2HintAttr();
if (!isReadHintOrNone(getL3HintAttr()))
return emitOpError("invalid l3_hint: ") << getL3HintAttr();
return success();
}
//===----------------------------------------------------------------------===//
// XeGPU_LoadGatherOp
//===----------------------------------------------------------------------===//
LogicalResult LoadGatherOp::verify() {
auto tdescTy = getTensorDescType();
auto maskTy = getMaskType();
auto valueTy = getValueType();
if (!valueTy)
return emitOpError("Expecting a vector type result.\n");
if (!tdescTy.isScattered())
return emitOpError("Expects a scattered TensorDesc.\n");
if (!isReadHintOrNone(getL1HintAttr()))
return emitOpError("invalid l1_hint: ") << getL1HintAttr();
if (!isReadHintOrNone(getL2HintAttr()))
return emitOpError("invalid l2_hint: ") << getL2HintAttr();
if (!isReadHintOrNone(getL3HintAttr()))
return emitOpError("invalid l3_hint: ") << getL3HintAttr();
auto tdescElemTy = tdescTy.getElementType();
auto valueElemTy = getElementType();
if (tdescElemTy != valueElemTy)
return emitOpError(
"Value should have the same element type as TensorDesc.");
auto maskShape = getShapeOf(maskTy);
auto valueShape = getShapeOf(valueTy);
auto tdescShape = getShapeOf(tdescTy);
if (tdescShape[0] != maskShape[0])
return emitOpError("dim-0 of the Mask and TensorDesc should be the same.");
if (tdescTy.getRank() == 2) {
if (!getTransposeAttr())
return emitOpError("load of rank-2 tensor has to be transposed.");
transpose({1, 0}, tdescShape);
}
return isArgShapesValid(tdescTy, valueTy, tdescShape,
[&]() { return emitOpError(); });
}
//===----------------------------------------------------------------------===//
// XeGPU_StoreScatterOp
//===----------------------------------------------------------------------===//
LogicalResult StoreScatterOp::verify() {
auto tdescTy = getTensorDescType();
if (!tdescTy.isScattered())
return emitOpError("Expects a scattered TensorDesc.\n");
if (!isWriteHintOrNone(getL1HintAttr()))
return emitOpError("invalid l1_hint: ") << getL1HintAttr();
if (!isWriteHintOrNone(getL2HintAttr()))
return emitOpError("invalid l2_hint: ") << getL2HintAttr();
if (!isWriteHintOrNone(getL3HintAttr()))
return emitOpError("invalid l3_hint: ") << getL3HintAttr();
auto maskTy = getMaskType();
auto valueTy = getValueType();
if (!valueTy)
return emitOpError("Expecting a vector type for the value.\n");
auto maskShape = getShapeOf(maskTy);
auto tdescShape = getShapeOf(tdescTy);
auto valueShape = getShapeOf(valueTy);
if (tdescShape[0] != maskShape[0])
return emitOpError("dim-0 of the Mask and TensorDesc should be the same.");
if (tdescTy.getRank() == 2) {
if (!getTransposeAttr())
return emitOpError("Store of a rank-2 tensor has to be transposed.");
transpose({1, 0}, tdescShape);
}
return isArgShapesValid(tdescTy, valueTy, tdescShape,
[&]() { return emitOpError(); });
}
//===---------------------------------------------------------------------===//
// XeGPU_UpdateOffsetOp
//===----------------------------------------------------------------------===//
void UpdateOffsetOp::build(OpBuilder &builder, OperationState &state,
mlir::Value tensorDesc,
llvm::ArrayRef<OpFoldResult> offsets) {
auto tdescTy = mlir::dyn_cast<TensorDescType>(tensorDesc.getType());
assert(tdescTy && "Expecting the source is a TensorDescType value.");
auto loc = tensorDesc.getLoc();
int64_t size = static_cast<int64_t>(offsets.size());
auto type = VectorType::get({size}, builder.getIndexType());
auto values = getValueOrCreateConstantIndexOp(builder, loc, offsets);
auto offset = builder.create<vector::FromElementsOp>(loc, type, values);
build(builder, state, tdescTy, tensorDesc, offset);
}
void UpdateOffsetOp::build(OpBuilder &builder, OperationState &state,
Value tensorDesc, llvm::ArrayRef<int64_t> offsets) {
auto ofrs = getAsIndexOpFoldResult(builder.getContext(), offsets);
build(builder, state, tensorDesc, ofrs);
}
//===----------------------------------------------------------------------===//
// XeGPU_DpasOp
//===----------------------------------------------------------------------===//
LogicalResult DpasOp::verify() {
int64_t lhsRank = getLhsType().getRank();
int64_t rhsRank = getRhsType().getRank();
int64_t resRank = getResultType().getRank();
auto lhsShape = getLhsType().getShape();
auto rhsShape = getRhsType().getShape();
auto resShape = getResultType().getShape();
auto aLayout = getALayoutAttr();
auto bLayout = getBLayoutAttr();
auto cLayout = getCLayoutAttr();
// make sure the layout attribute is either set for every available
// operand or simply not set at all. C is special, since ACC is optional.
// If they are all set, they also should be in the same scope.
auto isValidSet = [&]() {
bool result = (aLayout != nullptr) ^ (bLayout != nullptr);
if (hasAcc()) {
result |= (aLayout != nullptr) ^ (cLayout != nullptr);
}
result = !result;
return result;
};
if (!isValidSet())
return emitOpError(
"layout attributes should be either set for all operands (for SIMT "
"code) or not set at all (for SIMD code).");
// query the scope from aLayout (a valid setting).
if (aLayout) {
// In SIMT mode, All data fragments must be 2D
if (lhsRank != 2 || rhsRank != 2 || resRank != 2)
return emitOpError("expecting lhs, rhs, and result to be a 2D vector.");
auto laneLayoutA = aLayout.getLaneLayout();
auto laneLayoutB = bLayout.getLaneLayout();
auto laneLayoutC = cLayout.getLaneLayout();
// Obtain the expanded shapes of the operands and result using lane_layout.
// NOTE: For B, get rid of the packed dimension for the expanded shape.
SmallVector<int64_t> expandedShapeA = {lhsShape[0] * laneLayoutA[0],
lhsShape[1] * laneLayoutA[1]};
SmallVector<int64_t> expandedShapeB = {
rhsShape[0] * rhsShape[1] * laneLayoutB[0], 1 * laneLayoutB[1]};
SmallVector<int64_t> expandedShapeC = {resShape[0] * laneLayoutC[0],
resShape[1] * laneLayoutC[1]};
auto bK = expandedShapeB[0];
if (bK != expandedShapeA[1])
return emitOpError("K-dimension mismatch.");
if (expandedShapeA[0] != expandedShapeC[0])
return emitOpError("M-dimension mismatch.");
if (expandedShapeB[1] != expandedShapeC[1])
return emitOpError("N-dimension mismatch.");
} else { // For other scopes, operands' shape should match the mxkxn
// semantics.
if (lhsRank != 2 || (rhsRank != 2 && rhsRank != 3) || resRank != 2)
return emitOpError(
"expecting lhs and result to be a 2D vector, and rhs to be either "
"2D or 3D (packed) vector.");
auto bK = rhsRank == 3 ? rhsShape[0] * rhsShape[2] : rhsShape[0];
if (bK != lhsShape[1])
return emitOpError("K-dimension mismatch.");
if (lhsShape[0] != resShape[0])
return emitOpError("M-dimension mismatch.");
if (rhsShape[1] != resShape[1])
return emitOpError("N-dimension mismatch.");
}
return success();
}
//===----------------------------------------------------------------------===//
// XeGPU_ConvertLayoutOp
//===----------------------------------------------------------------------===//
LogicalResult ConvertLayoutOp::verify() {
auto srcMap = getSrcMapAttr();
auto resMap = getResMapAttr();
if (!srcMap)
return emitOpError("expected srcMap.");
if (!resMap)
return emitOpError("expected resMap.");
if (srcMap == resMap)
return emitOpError("expected different srcMap and resMap.");
// both srcMap and resMap should be WgLayout or SgLayout at the same time.
if ((!srcMap.isWgLayout() || !resMap.isWgLayout()) &&
(!srcMap.isSgLayout() || !resMap.isSgLayout()))
return emitOpError(
"expected srcMap and resMap be WgLayout or SgLayout at the same time.");
auto shape = getSource().getType().getShape();
if (!isEvenDistributed(shape, srcMap))
return emitOpError("invalid srcMap, data cannot be evenly distributed.");
if (!isEvenDistributed(shape, resMap))
return emitOpError("invalid resMap, data cannot be evenly distributed.");
return mlir::success();
}
} // namespace xegpu
} // namespace mlir
#include <mlir/Dialect/XeGPU/IR/XeGPUEnums.cpp.inc>
#define GET_OP_CLASSES
#include <mlir/Dialect/XeGPU/IR/XeGPU.cpp.inc>