MLIR 24.0.0git
XeGPUToXeVM.cpp
Go to the documentation of this file.
1//===-- XeGPUToXeVM.cpp - XeGPU to XeVM dialect conversion ------*- C++ -*-===//
2//
3// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
12
32#include "mlir/IR/IRMapping.h"
33#include "mlir/Pass/Pass.h"
34#include "mlir/Support/LLVM.h"
36#include "llvm/ADT/STLExtras.h"
37#include "llvm/ADT/SmallPtrSet.h"
38#include "llvm/Support/FormatVariadic.h"
39
41#include "mlir/IR/Types.h"
42
43#include "llvm/ADT/TypeSwitch.h"
44
45#include <string>
46
47namespace mlir {
48#define GEN_PASS_DEF_CONVERTXEGPUTOXEVMPASS
49#include "mlir/Conversion/Passes.h.inc"
50} // namespace mlir
51
52using namespace mlir;
53
54namespace {
55// TODO: Below are uArch dependent values, should move away from hardcoding
56static constexpr int32_t systolicDepth{8};
57static constexpr int32_t executionSize{16};
58
59// Offsets to individual fields of the 8xi32 layout nd tensor descriptor.
60enum class NdTdescOffset : uint32_t {
61 BasePtr = 0, // Base pointer (i64)
62 BaseShapeW = 2, // Base shape width (i32)
63 BaseShapeH = 3, // Base shape height (i32)
64 BasePitch = 4, // Base pitch/stride of dim rank-2 (i32)
65 LeadingStride0 = 5, // Row strides of the leading (batch) dims of a >2D
66 LeadingStride1 = 6, // descriptor (i32); added into offset_h by the load/store
67 LeadingStride2 = 7, // lowering. Left at 0 for 2D descriptors.
68};
69
70// Spare payload slots above, and the resulting max lowerable descriptor rank.
71static constexpr int64_t maxNdTdescLeadingDims{3};
72static constexpr int64_t maxNdTdescRank{2 + maxNdTdescLeadingDims};
73
74static int32_t getNumericXeVMAddrSpace(xegpu::MemorySpace xeGpuMemspace) {
75 switch (xeGpuMemspace) {
76 case xegpu::MemorySpace::Global:
77 return static_cast<int>(xevm::AddrSpace::GLOBAL);
78 case xegpu::MemorySpace::SLM:
79 return static_cast<int>(xevm::AddrSpace::SHARED);
80 }
81 llvm_unreachable("Unknown XeGPU memory space");
82}
83
84/// Translates a memref memory space attribute into XeVM's numeric address
85/// space, which follows the OpenCL/SPIR-V convention (0 = private, 1 =
86/// global, 2 = constant, 3 = shared/local, 4 = generic). A null attribute,
87/// meaning the memory space was left unspecified, maps to the default space
88/// 0. Returns failure if `memSpace` is a representation this pass does not
89/// know how to translate (e.g. a SPIR-V storage class or an arbitrary string
90/// attribute), rather than assuming it is an `IntegerAttr` and asserting.
91static FailureOr<unsigned> getNumericMemorySpace(Attribute memSpace) {
92 if (!memSpace)
93 return 0u;
94 if (auto intAttr = llvm::dyn_cast<IntegerAttr>(memSpace))
95 return static_cast<unsigned>(intAttr.getInt());
96 if (auto xevmSpace = llvm::dyn_cast<xevm::AddrSpaceAttr>(memSpace))
97 return static_cast<unsigned>(xevmSpace.getValue());
98 if (auto gpuSpace = llvm::dyn_cast<gpu::AddressSpaceAttr>(memSpace)) {
99 switch (gpuSpace.getValue()) {
100 case gpu::AddressSpace::Global:
101 return static_cast<unsigned>(xevm::AddrSpace::GLOBAL);
102 case gpu::AddressSpace::Workgroup:
103 return static_cast<unsigned>(xevm::AddrSpace::SHARED);
104 case gpu::AddressSpace::Private:
105 return static_cast<unsigned>(xevm::AddrSpace::PRIVATE);
106 case gpu::AddressSpace::Constant:
107 return static_cast<unsigned>(xevm::AddrSpace::CONSTANT);
108 }
109 llvm_unreachable("Unknown GPU address space");
110 }
111 return failure();
112}
113
114/// Checks if the given MemRefType refers to shared memory.
115static bool isSharedMemRef(const MemRefType &memrefTy) {
116 FailureOr<unsigned> addrSpace =
117 getNumericMemorySpace(memrefTy.getMemorySpace());
118 return succeeded(addrSpace) &&
119 *addrSpace == static_cast<unsigned>(xevm::AddrSpace::SHARED);
120}
121
122// Get same bitwidth flat vector type of new element type.
123static VectorType encodeVectorTypeTo(VectorType currentVecType,
124 Type toElemType) {
125 auto elemType = currentVecType.getElementType();
126 auto currentBitWidth = elemType.getIntOrFloatBitWidth();
127 auto newBitWidth = toElemType.getIntOrFloatBitWidth();
128 const int size =
129 currentVecType.getNumElements() * currentBitWidth / newBitWidth;
130 return VectorType::get(size, toElemType);
131}
132
133static xevm::LoadCacheControl
134translateLoadXeGPUCacheHint(std::optional<xegpu::CachePolicy> L1hint,
135 std::optional<xegpu::CachePolicy> L3hint) {
136 // If no hints are provided, use the default cache control.
137 if (!L1hint && !L3hint)
138 return xevm::LoadCacheControl::USE_DEFAULT;
139 // If only one of the hints is provided, use the default for the other level.
140 auto L1hintVal = L1hint.value_or(xegpu::CachePolicy::CACHED);
141 auto L3hintVal = L3hint.value_or(xegpu::CachePolicy::CACHED);
142 switch (L1hintVal) {
143 case xegpu::CachePolicy::CACHED:
144 if (L3hintVal == xegpu::CachePolicy::CACHED)
145 return xevm::LoadCacheControl::L1C_L2UC_L3C;
146 else if (L3hintVal == xegpu::CachePolicy::UNCACHED)
147 return xevm::LoadCacheControl::L1C_L2UC_L3UC;
148 else
149 llvm_unreachable("Unsupported cache control.");
150 case xegpu::CachePolicy::UNCACHED:
151 if (L3hintVal == xegpu::CachePolicy::CACHED)
152 return xevm::LoadCacheControl::L1UC_L2UC_L3C;
153 else if (L3hintVal == xegpu::CachePolicy::UNCACHED)
154 return xevm::LoadCacheControl::L1UC_L2UC_L3UC;
155 else
156 llvm_unreachable("Unsupported cache control.");
157 case xegpu::CachePolicy::STREAMING:
158 if (L3hintVal == xegpu::CachePolicy::CACHED)
159 return xevm::LoadCacheControl::L1S_L2UC_L3C;
160 else if (L3hintVal == xegpu::CachePolicy::UNCACHED)
161 return xevm::LoadCacheControl::L1S_L2UC_L3UC;
162 else
163 llvm_unreachable("Unsupported cache control.");
164 case xegpu::CachePolicy::READ_INVALIDATE:
165 return xevm::LoadCacheControl::INVALIDATE_READ;
166 default:
167 llvm_unreachable("Unsupported cache control.");
168 }
169}
170
171static xevm::StoreCacheControl
172translateStoreXeGPUCacheHint(std::optional<xegpu::CachePolicy> L1hint,
173 std::optional<xegpu::CachePolicy> L3hint) {
174 // If no hints are provided, use the default cache control.
175 if (!L1hint && !L3hint)
176 return xevm::StoreCacheControl::USE_DEFAULT;
177 // If only one of the hints is provided, use the default for the other level.
178 auto L1hintVal = L1hint.value_or(xegpu::CachePolicy::UNCACHED);
179 auto L3hintVal = L3hint.value_or(xegpu::CachePolicy::WRITE_BACK);
180 switch (L1hintVal) {
181 case xegpu::CachePolicy::UNCACHED:
182 if (L3hintVal == xegpu::CachePolicy::UNCACHED)
183 return xevm::StoreCacheControl::L1UC_L2UC_L3UC;
184 else if (L3hintVal == xegpu::CachePolicy::WRITE_BACK)
185 return xevm::StoreCacheControl::L1UC_L2UC_L3WB;
186 else
187 llvm_unreachable("Unsupported cache control.");
188 case xegpu::CachePolicy::STREAMING:
189 if (L3hintVal == xegpu::CachePolicy::UNCACHED)
190 return xevm::StoreCacheControl::L1S_L2UC_L3UC;
191 else if (L3hintVal == xegpu::CachePolicy::WRITE_BACK)
192 return xevm::StoreCacheControl::L1S_L2UC_L3WB;
193 else
194 llvm_unreachable("Unsupported cache control.");
195 case xegpu::CachePolicy::WRITE_BACK:
196 if (L3hintVal == xegpu::CachePolicy::UNCACHED)
197 return xevm::StoreCacheControl::L1WB_L2UC_L3UC;
198 else if (L3hintVal == xegpu::CachePolicy::WRITE_BACK)
199 return xevm::StoreCacheControl::L1WB_L2UC_L3WB;
200 else
201 llvm_unreachable("Unsupported cache control.");
202 case xegpu::CachePolicy::WRITE_THROUGH:
203 if (L3hintVal == xegpu::CachePolicy::UNCACHED)
204 return xevm::StoreCacheControl::L1WT_L2UC_L3UC;
205 else if (L3hintVal == xegpu::CachePolicy::WRITE_BACK)
206 return xevm::StoreCacheControl::L1WT_L2UC_L3WB;
207 else
208 llvm_unreachable("Unsupported cache control.");
209 default:
210 llvm_unreachable("Unsupported cache control.");
211 }
212}
213
214//
215// Note:
216// Block operations for tile of sub byte element types are handled by
217// emulating with larger element types.
218// Tensor descriptor are keep intact and only ops consuming them are
219// emulated
220//
221
222//
223// High-D (>2D) nd descriptors are lowered by viewing the source as a single
224// flattened 2D plane, so the 2D-block surface covers every leading (batch)
225// plane at once and a batch position becomes a row offset into it. The surface
226// height is the row extent the leading dims reach:
227// base_height = size[R-2] + sum_d (size[d] - 1) * (stride[d] / stride[R-2])
228//
229// Leaving `base_ptr` at the true base means an out-of-range batch index lands
230// past `base_height`, where the HW boundary check handles it, instead of aiming
231// the surface at unmapped memory. Encoding the leading strides as row counts
232// (`stride[d] / stride[R-2]`) also makes them dimensionless, so they survive
233// element-type repacking (e.g. the f16 -> i32 transpose repack) without a unit
234// conversion.
235//
236// Limitations of the flattened-plane view:
237// 1. Each leading stride must be a whole number of rows. A source with gaps
238// between planes (`stride[d] % stride[R-2] != 0`) is not lowered. This can
239// only be checked when the strides are static; for dynamic strides the
240// divisibility is assumed.
241// 2. `base_height` grows with the leading extent, so a source with a large
242// batch x head x sequence extent can exceed the HW 2D-block surface height.
243// 3. Plane boundaries are invisible to the boundary check, which only knows
244// the surface: a tile whose rows run past `size[R-2]` spills into the
245// following rows of the surface -- the next plane, or the padding between
246// planes -- rather than being clipped there as it would be on a per-plane
247// surface. A load reads those rows instead of returning zeros, and a store
248// overwrites them, so it can corrupt the next plane. This only matters
249// when `size[R-2]` is not a multiple of the tile height.
250//
251
252class CreateNdDescToXeVMPattern
253 : public OpConversionPattern<xegpu::CreateNdDescOp> {
254 using Base::Base;
255
256 // Checks the surface width and pitch against the target's 2D block memory
257 // restrictions. Only these two are enforced: both are backed by the source's
258 // static shape and strides, whereas the base address alignment is a property
259 // of the allocation that is not visible here. The width and the pitch are
260 // checked independently, each only when it is static; a value known only at
261 // runtime is left alone, as guarding it inside the kernel would cost far more
262 // than it is worth.
263 //
264 // A violation is a dead end rather than a pattern that does not apply: the op
265 // has no other legalization path, so it is reported with an error that names
266 // the limit it misses.
267 LogicalResult checkMemoryRestrictions(xegpu::CreateNdDescOp op,
268 int64_t rank) const {
269 const auto *uArch =
271 // Without a known target chip there is no uArch to check against, so the
272 // descriptor must be accepted as-is.
273 if (!uArch)
274 return success();
275 const xegpu::uArch::BlockIOMemoryRestrictions &limits =
276 uArch->getBlockIOMemoryRestrictions();
277
278 // Recover the width and the pitch as a number of elements. A memref source
279 // carries both in its type, and either one may be dynamic independently of
280 // the other, so they are queried separately instead of being dropped
281 // together when the source is not fully static. `getMixedSizes` /
282 // `getMixedStrides` are only consulted for an integer source, where the op
283 // itself holds the extents.
284 std::optional<int64_t> widthElems;
285 std::optional<int64_t> pitchElems;
286 if (auto memrefTy = dyn_cast<MemRefType>(op.getSource().getType())) {
287 if (!ShapedType::isDynamic(memrefTy.getDimSize(rank - 1)))
288 widthElems = memrefTy.getDimSize(rank - 1);
289 // The source is known to be strided; the caller checked that already.
290 SmallVector<int64_t> staticStrides;
291 int64_t staticOffset;
292 if (succeeded(
293 memrefTy.getStridesAndOffset(staticStrides, staticOffset)) &&
294 !ShapedType::isDynamic(staticStrides[rank - 2]))
295 pitchElems = staticStrides[rank - 2];
296 } else {
297 widthElems = getConstantIntValue(op.getMixedSizes()[rank - 1]);
298 pitchElems = getConstantIntValue(op.getMixedStrides()[rank - 2]);
299 }
300
301 unsigned elemBitSize =
302 op.getType().getElementType().getIntOrFloatBitWidth();
303 // The restrictions are expressed in bytes, but a sub-byte element type can
304 // leave the width or the pitch on a fraction of a byte, so the comparisons
305 // are done in bits. Such a size meets no byte alignment at all and is
306 // reported as a violation rather than waved through.
307 auto describe = [](int64_t bits) -> std::string {
308 if (bits % 8 == 0)
309 return llvm::formatv("{0} bytes", bits / 8).str();
310 return llvm::formatv("{0} bits", bits).str();
311 };
312 auto check = [&](std::optional<int64_t> elems, int64_t minBytes,
313 int64_t alignBytes,
314 llvm::StringRef what) -> LogicalResult {
315 if (!elems)
316 return success();
317 int64_t bits = *elems * elemBitSize;
318 if (bits < minBytes * 8)
319 return op.emitError(llvm::formatv(
320 "2D block surface {0} must be at least {1} bytes on this "
321 "target, got {2}.",
322 what, minBytes, describe(bits)));
323 if (alignBytes > 0 && bits % (alignBytes * 8) != 0)
324 return op.emitError(llvm::formatv(
325 "2D block surface {0} must be a multiple of {1} bytes on this "
326 "target, got {2}.",
327 what, alignBytes, describe(bits)));
328 return success();
329 };
330
331 if (failed(check(widthElems, limits.minBaseWidthBytes,
332 limits.baseWidthAlignmentBytes, "width")))
333 return failure();
334 // The pitch is the row stride of the innermost 2D tile.
335 if (failed(check(pitchElems, limits.minBasePitchBytes,
336 limits.basePitchAlignmentBytes, "pitch")))
337 return failure();
338 // Consecutive rows may not overlap, so the pitch has to span the full
339 // width.
340 if (widthElems && pitchElems && *pitchElems < *widthElems)
341 return op.emitError(
342 llvm::formatv("2D block surface pitch must be at least the surface "
343 "width, got a pitch of {0} for a surface {1} wide.",
344 describe(*pitchElems * elemBitSize),
345 describe(*widthElems * elemBitSize)));
346 return success();
347 }
348
349 LogicalResult
350 matchAndRewrite(xegpu::CreateNdDescOp op,
351 xegpu::CreateNdDescOp::Adaptor adaptor,
352 ConversionPatternRewriter &rewriter) const override {
353 auto loc = op.getLoc();
354 auto source = op.getSource();
355
356 // Check all failure conditions before generating any IR, so nothing has to
357 // be rolled back.
358 int64_t rank = op.getType().getRank();
359 int64_t sourceRank;
360 auto memrefTy = dyn_cast<MemRefType>(source.getType());
361 if (memrefTy) {
362 if (!memrefTy.isStrided())
363 return rewriter.notifyMatchFailure(op, "Expected strided Memref.");
364 sourceRank = memrefTy.getRank();
365 } else if (isa<IntegerType>(source.getType())) {
366 sourceRank = op.getMixedSizes().size();
367 } else {
368 return rewriter.notifyMatchFailure(op,
369 "Expected ranked Memref or integer.");
370 }
371 if (sourceRank != rank)
372 return rewriter.notifyMatchFailure(
373 op, "Expected descriptor rank to match source rank; subview the "
374 "source down to the descriptor rank.");
375 if (rank > maxNdTdescRank)
376 return rewriter.notifyMatchFailure(
377 op, "Batched nd descriptor supports at most " +
378 std::to_string(maxNdTdescLeadingDims) +
379 " leading dims (rank <= " + std::to_string(maxNdTdescRank) +
380 ").");
381 // Limitation 1 above; dynamic strides are assumed to divide evenly.
382 if (rank > 2) {
383 SmallVector<std::optional<int64_t>> constStrides(rank, std::nullopt);
384 if (memrefTy) {
385 SmallVector<int64_t> staticStrides;
386 int64_t staticOffset;
387 if (succeeded(
388 memrefTy.getStridesAndOffset(staticStrides, staticOffset)))
389 for (int64_t d = 0; d < rank; ++d)
390 if (!ShapedType::isDynamic(staticStrides[d]))
391 constStrides[d] = staticStrides[d];
392 } else {
393 SmallVector<OpFoldResult> mixed = op.getMixedStrides();
394 for (int64_t d = 0; d < rank; ++d)
395 constStrides[d] = getConstantIntValue(mixed[d]);
396 }
397 if (std::optional<int64_t> pitch = constStrides[rank - 2]) {
398 for (int64_t d = 0; d < rank - 2; ++d) {
399 std::optional<int64_t> leading = constStrides[d];
400 if (leading && (*pitch == 0 || *leading % *pitch != 0))
401 return rewriter.notifyMatchFailure(
402 op, "Expected each leading (batch) stride to be a multiple of "
403 "the row stride; the source has gaps between planes.");
404 }
405 }
406 }
407 // Enforce the target's 2D block memory restrictions. A rank-1 descriptor is
408 // just a base address and is not consumed by a 2D block op, so it is
409 // exempt.
410 if (rank >= 2 && failed(checkMemoryRestrictions(op, rank)))
411 return failure();
412
413 Type payloadElemTy = rewriter.getI32Type();
414 Type i64Ty = rewriter.getI64Type();
415
416 // Access the adaptor only after the failure checks, so a bail-out leaves no
417 // materialization cast behind.
418 Value baseAddr = adaptor.getSource();
419 if (isa<IntegerType>(source.getType()) && baseAddr.getType() != i64Ty) {
420 // Pointer type may be i32. Cast to i64 if needed.
421 baseAddr = arith::ExtUIOp::create(rewriter, loc, i64Ty, baseAddr);
422 }
423 // 1D tensor descriptor is just the base address.
424 if (rank == 1) {
425 rewriter.replaceOp(op, baseAddr);
426 return success();
427 }
428
429 SmallVector<OpFoldResult> mixedSizes;
430 SmallVector<OpFoldResult> mixedStrides;
431 if (memrefTy && !xegpu::hasStaticShapeAndStrides(memrefTy)) {
432 auto meta =
433 memref::ExtractStridedMetadataOp::create(rewriter, loc, source);
434 mixedSizes = meta.getConstifiedMixedSizes();
435 mixedStrides = meta.getConstifiedMixedStrides();
436 } else {
437 mixedSizes = op.getMixedSizes();
438 mixedStrides = op.getMixedStrides();
439 }
440
441 // Op is lowered to a code sequence that populates payload.
442 // Payload is a 8xi32 vector. Offset to individual fields are defined in
443 // NdTdescOffset enum.
444 VectorType payloadTy = VectorType::get(8, payloadElemTy);
445 // 4xi64 view is used for inserting the base pointer.
446 VectorType payloadI64Ty = VectorType::get(4, i64Ty);
447 // Initialize payload to zero.
448 Value payload = arith::ConstantOp::create(
449 rewriter, loc,
450 DenseElementsAttr::get(payloadTy, IntegerAttr::get(payloadElemTy, 0)));
451
452 // Utility for creating offset values from op fold result.
453 auto createOffset = [&](SmallVector<OpFoldResult> &ofrVec,
454 unsigned idx) -> Value {
455 Value val = getValueOrCreateConstantIntOp(rewriter, loc, ofrVec[idx]);
456 val = getValueOrCreateCastToIndexLike(rewriter, loc, payloadElemTy, val);
457 return val;
458 };
459 // The descriptor's innermost 2 dims are the 2D tile (H, W).
460 Value baseShapeW = createOffset(mixedSizes, rank - 1);
461 // Pitch is the stride of dim rank-2 (the row stride of the 2D tile).
462 Value basePitch = createOffset(mixedStrides, rank - 2);
463 // Leading (batch) strides as a number of rows (`stride[d] / pitch`).
464 SmallVector<Value> leadingRowStrides;
465 for (int64_t d = 0; d < rank - 2; ++d) {
466 std::optional<int64_t> leading = getConstantIntValue(mixedStrides[d]);
467 std::optional<int64_t> pitch =
468 getConstantIntValue(mixedStrides[rank - 2]);
469 if (leading && pitch && *pitch != 0)
470 leadingRowStrides.push_back(arith::ConstantIntOp::create(
471 rewriter, loc, payloadElemTy, *leading / *pitch));
472 else
473 leadingRowStrides.push_back(arith::DivUIOp::create(
474 rewriter, loc, createOffset(mixedStrides, d), basePitch));
475 }
476 // Height of the flattened plane: every leading (batch) plane is stacked
477 // into the surface, so the boundary check covers an out-of-range batch.
478 // base_height = size[R-2] + sum_d (size[d] - 1) * leadingRowStride[d]
479 // For rank 2 this is just size[0].
480 Value baseShapeH = createOffset(mixedSizes, rank - 2);
481 if (rank > 2) {
482 Value one = arith::ConstantIntOp::create(rewriter, loc, payloadElemTy, 1);
483 for (int64_t d = 0; d < rank - 2; ++d) {
484 Value planesBelow = rewriter.createOrFold<arith::SubIOp>(
485 loc, createOffset(mixedSizes, d), one);
486 Value rows = rewriter.createOrFold<arith::MulIOp>(loc, planesBelow,
487 leadingRowStrides[d]);
488 baseShapeH =
489 rewriter.createOrFold<arith::AddIOp>(loc, baseShapeH, rows);
490 }
491 }
492 // Populate payload.
493 Value payLoadAsI64 =
494 vector::BitCastOp::create(rewriter, loc, payloadI64Ty, payload);
495 payLoadAsI64 =
496 vector::InsertOp::create(rewriter, loc, baseAddr, payLoadAsI64,
497 static_cast<int>(NdTdescOffset::BasePtr));
498 payload = vector::BitCastOp::create(rewriter, loc, payloadTy, payLoadAsI64);
499 payload =
500 vector::InsertOp::create(rewriter, loc, baseShapeW, payload,
501 static_cast<int>(NdTdescOffset::BaseShapeW));
502 payload =
503 vector::InsertOp::create(rewriter, loc, baseShapeH, payload,
504 static_cast<int>(NdTdescOffset::BaseShapeH));
505 payload =
506 vector::InsertOp::create(rewriter, loc, basePitch, payload,
507 static_cast<int>(NdTdescOffset::BasePitch));
508 // The leading (batch) row strides go into the spare payload slots; the
509 // load/store/prefetch lowering turns the batch offsets into a row offset
510 // with them.
511 for (int64_t d = 0; d < rank - 2; ++d)
512 payload = vector::InsertOp::create(
513 rewriter, loc, leadingRowStrides[d], payload,
514 static_cast<int>(NdTdescOffset::LeadingStride0) + d);
515 rewriter.replaceOp(op, payload);
516 return success();
517 }
518};
519
520template <
521 typename OpType,
522 typename = std::enable_if_t<llvm::is_one_of<
523 OpType, xegpu::LoadNdOp, xegpu::StoreNdOp, xegpu::PrefetchNdOp>::value>>
524class LoadStorePrefetchNdToXeVMPattern : public OpConversionPattern<OpType> {
525 using OpConversionPattern<OpType>::OpConversionPattern;
526 LogicalResult
527 matchAndRewrite(OpType op, typename OpType::Adaptor adaptor,
528 ConversionPatternRewriter &rewriter) const override {
529 auto mixedOffsets = op.getMixedOffsets();
530 int64_t opOffsetsSize = mixedOffsets.size();
531 auto loc = op.getLoc();
532 auto ctxt = rewriter.getContext();
533
534 auto tdesc = adaptor.getTensorDesc();
535 auto tdescTy = op.getTensorDescType();
536 auto tileRank = tdescTy.getRank();
537 if (opOffsetsSize != tileRank)
538 return rewriter.notifyMatchFailure(
539 op, "Expected offset rank to match descriptor rank.");
540 if (tileRank > 2 && llvm::any_of(tdescTy.getShape().drop_back(2),
541 [](int64_t d) { return d != 1; }))
542 return rewriter.notifyMatchFailure(
543 op, "Expected leading (batch) descriptor dims to be unit.");
544 if (tileRank > maxNdTdescRank)
545 return rewriter.notifyMatchFailure(
546 op, "Expected descriptor rank <= " + std::to_string(maxNdTdescRank) +
547 ".");
548 auto elemType = tdescTy.getElementType();
549 auto elemBitSize = elemType.getIntOrFloatBitWidth();
550 bool isSubByte = elemBitSize < 8;
551 uint64_t wScaleFactor = 1;
552
553 if (!isSubByte && (elemBitSize % 8 != 0))
554 return rewriter.notifyMatchFailure(
555 op, "Expected element type bit width to be multiple of 8.");
556 auto tileW = tdescTy.getDimSize(tileRank - 1);
557 // For sub byte types, only 4bits are currently supported.
558 if (isSubByte) {
559 if (elemBitSize != 4)
560 return rewriter.notifyMatchFailure(
561 op, "Only sub byte types of 4bits are supported.");
562 if (tileRank != 2)
563 return rewriter.notifyMatchFailure(
564 op, "Sub byte types are only supported for 2D tensor descriptors.");
565 auto subByteFactor = 8 / elemBitSize;
566 auto tileH = tdescTy.getDimSize(0);
567 // Handle special case for packed load.
568 if constexpr (std::is_same_v<OpType, xegpu::LoadNdOp>) {
569 if (op.getPacked().value_or(false)) {
570 // packed load is implemented as packed loads of 8bit elements.
571 if (tileH == systolicDepth * 4 &&
572 tileW == executionSize * subByteFactor) {
573 // Usage case for loading as Matrix B with pack request.
574 // source is assumed to pre-packed into 8bit elements
575 // Emulate with 8bit loads with pack request.
576 // scaled_tileW = executionSize
577 elemType = rewriter.getIntegerType(8);
578 tileW = executionSize;
579 wScaleFactor = subByteFactor;
580 }
581 }
582 }
583 // If not handled by packed load case above, handle other cases.
584 if (wScaleFactor == 1) {
585 auto sub16BitFactor = subByteFactor * 2;
586 if (tileW == executionSize * sub16BitFactor) {
587 // Usage case for loading as Matrix A operand
588 // Emulate with 16bit loads/stores.
589 // scaled_tileW = executionSize
590 elemType = rewriter.getIntegerType(16);
591 tileW = executionSize;
592 wScaleFactor = sub16BitFactor;
593 } else {
594 return rewriter.notifyMatchFailure(
595 op, "Unsupported tile shape for sub byte types.");
596 }
597 }
598 // recompute element bit size for emulation.
599 elemBitSize = elemType.getIntOrFloatBitWidth();
600 }
601
602 // Get address space from tensor descriptor memory space.
603 auto ptrTypeLLVM = LLVM::LLVMPointerType::get(
604 ctxt, getNumericXeVMAddrSpace(tdescTy.getMemorySpace()));
605 if (tileRank >= 2) {
606 // Compute element byte size.
607 Value elemByteSize = arith::ConstantIntOp::create(
608 rewriter, loc, rewriter.getI32Type(), elemBitSize / 8);
609 VectorType payloadI64Ty = VectorType::get(4, rewriter.getI64Type());
610 Value payLoadAsI64 =
611 vector::BitCastOp::create(rewriter, loc, payloadI64Ty, tdesc);
612 Value basePtr =
613 vector::ExtractOp::create(rewriter, loc, payLoadAsI64,
614 static_cast<int>(NdTdescOffset::BasePtr));
615 Value baseShapeW = vector::ExtractOp::create(
616 rewriter, loc, tdesc, static_cast<int>(NdTdescOffset::BaseShapeW));
617 Value baseShapeH = vector::ExtractOp::create(
618 rewriter, loc, tdesc, static_cast<int>(NdTdescOffset::BaseShapeH));
619 Value basePitch = vector::ExtractOp::create(
620 rewriter, loc, tdesc, static_cast<int>(NdTdescOffset::BasePitch));
621
622 Value offsetW = getValueOrCreateConstantIntOp(rewriter, loc,
623 mixedOffsets[tileRank - 1]);
624 offsetW = getValueOrCreateCastToIndexLike(rewriter, loc,
625 rewriter.getI32Type(), offsetW);
626 Value offsetH = getValueOrCreateConstantIntOp(rewriter, loc,
627 mixedOffsets[tileRank - 2]);
628 offsetH = getValueOrCreateCastToIndexLike(rewriter, loc,
629 rewriter.getI32Type(), offsetH);
630 // Turn the leading (batch) offsets into a row offset into the flattened
631 // plane, using the row-unit batch strides encoded at create time:
632 // offsetH += sum_d offset[d] * leadingRowStride[d]
633 // The base pointer stays at the true base, so an out-of-range batch index
634 // is caught by the HW boundary check instead of moving the surface to
635 // unmapped memory.
636 for (int64_t d = 0; d < tileRank - 2; ++d) {
637 Value off =
638 getValueOrCreateConstantIntOp(rewriter, loc, mixedOffsets[d]);
639 off = getValueOrCreateCastToIndexLike(rewriter, loc,
640 rewriter.getI32Type(), off);
641 Value rowStride = vector::ExtractOp::create(
642 rewriter, loc, tdesc,
643 static_cast<int>(NdTdescOffset::LeadingStride0) + d);
644 Value term = arith::MulIOp::create(rewriter, loc, off, rowStride);
645 offsetH = arith::AddIOp::create(rewriter, loc, offsetH, term);
646 }
647 // Convert base pointer (i64) to LLVM pointer type.
648 Value basePtrLLVM =
649 LLVM::IntToPtrOp::create(rewriter, loc, ptrTypeLLVM, basePtr);
650 // FIXME: width or pitch is not the same as baseShapeW it should be the
651 // stride of the second to last dimension in row major layout.
652 // Compute width in bytes.
653 Value baseShapeWInBytes =
654 arith::MulIOp::create(rewriter, loc, baseShapeW, elemByteSize);
655 // Compute pitch in bytes.
656 Value basePitchBytes =
657 arith::MulIOp::create(rewriter, loc, basePitch, elemByteSize);
658
659 if (wScaleFactor > 1) {
660 // Scale offsetW, baseShapeWInBytes for sub byte emulation.
661 // Note: tileW is already scaled above.
662 Value wScaleFactorValLog2 = arith::ConstantIntOp::create(
663 rewriter, loc, rewriter.getI32Type(), llvm::Log2_64(wScaleFactor));
664 baseShapeWInBytes = arith::ShRSIOp::create(
665 rewriter, loc, baseShapeWInBytes, wScaleFactorValLog2);
666 basePitchBytes = arith::ShRSIOp::create(rewriter, loc, basePitchBytes,
667 wScaleFactorValLog2);
668 offsetW =
669 arith::ShRSIOp::create(rewriter, loc, offsetW, wScaleFactorValLog2);
670 }
671 // Get tile height from the tensor descriptor type (second-to-last dim).
672 auto tileH = tdescTy.getDimSize(tileRank - 2);
673 // Get vblocks from the tensor descriptor type.
674 int32_t vblocks = tdescTy.getArrayLength();
675 if constexpr (std::is_same_v<OpType, xegpu::StoreNdOp>) {
676 Value src = adaptor.getValue();
677 // If store value is a scalar, get value from op instead of adaptor.
678 // Adaptor might have optimized away single element vector
679 if (src.getType().isIntOrFloat()) {
680 src = op.getValue();
681 }
682 VectorType srcVecTy = dyn_cast<VectorType>(src.getType());
683 if (!srcVecTy)
684 return rewriter.notifyMatchFailure(
685 op, "Expected store value to be a vector type.");
686 // Get flat vector type of integer type with matching element bit size.
687 VectorType newSrcVecTy =
688 encodeVectorTypeTo(srcVecTy, rewriter.getIntegerType(elemBitSize));
689 if (srcVecTy != newSrcVecTy)
690 src = vector::BitCastOp::create(rewriter, loc, newSrcVecTy, src);
691 auto storeCacheControl =
692 translateStoreXeGPUCacheHint(op.getL1Hint(), op.getL3Hint());
693 xevm::BlockStore2dOp::create(
694 rewriter, loc, basePtrLLVM, baseShapeWInBytes, baseShapeH,
695 basePitchBytes, offsetW, offsetH, elemBitSize, tileW, tileH, src,
696 xevm::StoreCacheControlAttr::get(ctxt, storeCacheControl));
697 rewriter.eraseOp(op);
698 } else {
699 auto loadCacheControl =
700 translateLoadXeGPUCacheHint(op.getL1Hint(), op.getL3Hint());
701 if constexpr (std::is_same_v<OpType, xegpu::PrefetchNdOp>) {
702 xevm::BlockPrefetch2dOp::create(
703 rewriter, loc, basePtrLLVM, baseShapeWInBytes, baseShapeH,
704 basePitchBytes, offsetW, offsetH, elemBitSize, tileW, tileH,
705 vblocks, xevm::LoadCacheControlAttr::get(ctxt, loadCacheControl));
706 rewriter.eraseOp(op);
707 } else {
708 VectorType dstVecTy = cast<VectorType>(op.getValue().getType());
709 bool vnni = op.getPacked().value_or(false);
710 auto transposeValue = op.getTranspose();
711 bool transpose =
712 transposeValue.has_value() && transposeValue.value()[0] == 1;
713 // Handle special case of 32x16 and 8bit element load
714 // with no vnni, no transpose, no vblocks.
715 // For this special case, vnni and non vnni yields the same output
716 // and only the vnni variant is supported by HW.
717 // Check and set vnni of the special case.
718 if (elemBitSize == 8 && tileW == 16 && tileH == 32 && !vnni &&
719 !transpose) {
720 vnni = true;
721 }
722 // Handle tranpose request on small element size
723 // Transpose needs to be requested on 32bit element type.
724 // offsetW and tileW needs to be adjusted to account for element type
725 // change.
726 if (transpose && elemBitSize < 32) {
727 int32_t scale = 32 / elemBitSize;
728 Value scaleLog2 = arith::ConstantIntOp::create(
729 rewriter, loc, rewriter.getI32Type(), llvm::Log2_64(scale));
730 offsetW = arith::ShRSIOp::create(rewriter, loc, offsetW, scaleLog2);
731 tileW = tileW * elemBitSize / 32;
732 elemBitSize = 32;
733 }
734 VectorType loadedTy = encodeVectorTypeTo(
735 dstVecTy, vnni ? rewriter.getI32Type()
736 : rewriter.getIntegerType(elemBitSize));
737
738 Value resultFlatVec = xevm::BlockLoad2dOp::create(
739 rewriter, loc, loadedTy, basePtrLLVM, baseShapeWInBytes,
740 baseShapeH, basePitchBytes, offsetW, offsetH, elemBitSize, tileW,
741 tileH, vblocks, transpose, vnni,
742 xevm::LoadCacheControlAttr::get(ctxt, loadCacheControl));
743 resultFlatVec = vector::BitCastOp::create(
744 rewriter, loc,
745 encodeVectorTypeTo(loadedTy, dstVecTy.getElementType()),
746 resultFlatVec);
747 rewriter.replaceOp(op, resultFlatVec);
748 }
749 }
750 } else {
751 // 1D tensor descriptor.
752 // `tdesc` represents base address as i64
753 // Offset in number of elements, need to multiply by element byte size.
754 // Compute byte offset.
755 // byteOffset = offset * elementByteSize
756 Value offset =
757 getValueOrCreateConstantIntOp(rewriter, loc, mixedOffsets[0]);
758 offset = getValueOrCreateCastToIndexLike(rewriter, loc,
759 rewriter.getI64Type(), offset);
760 // Compute element byte size.
761 Value elemByteSize = arith::ConstantIntOp::create(
762 rewriter, loc, rewriter.getI64Type(), elemBitSize / 8);
763 Value byteOffset =
764 rewriter.createOrFold<arith::MulIOp>(loc, offset, elemByteSize);
765 // Final address = basePtr + byteOffset
766 Value finalAddrI64 = rewriter.createOrFold<arith::AddIOp>(
767 loc, tdesc,
768 getValueOrCreateCastToIndexLike(rewriter, loc, rewriter.getI64Type(),
769 byteOffset));
770 // Convert base pointer (i64) to LLVM pointer type.
771 Value finalPtrLLVM =
772 LLVM::IntToPtrOp::create(rewriter, loc, ptrTypeLLVM, finalAddrI64);
773 if constexpr (std::is_same_v<OpType, xegpu::StoreNdOp>) {
774 Value src = adaptor.getValue();
775 // If store value is a scalar, get value from op instead of adaptor.
776 // Adaptor might have optimized away single element vector
777 if (src.getType().isIntOrFloat()) {
778 src = op.getValue();
779 }
780 VectorType srcVecTy = dyn_cast<VectorType>(src.getType());
781 if (!srcVecTy)
782 return rewriter.notifyMatchFailure(
783 op, "Expected store value to be a vector type.");
784 // Get flat vector type of integer type with matching element bit size.
785 VectorType newSrcVecTy =
786 encodeVectorTypeTo(srcVecTy, rewriter.getIntegerType(elemBitSize));
787 if (srcVecTy != newSrcVecTy)
788 src = vector::BitCastOp::create(rewriter, loc, newSrcVecTy, src);
789 auto storeCacheControl =
790 translateStoreXeGPUCacheHint(op.getL1Hint(), op.getL3Hint());
791 rewriter.replaceOpWithNewOp<xevm::BlockStoreOp>(
792 op, finalPtrLLVM, src,
793 xevm::StoreCacheControlAttr::get(ctxt, storeCacheControl));
794 } else if constexpr (std::is_same_v<OpType, xegpu::LoadNdOp>) {
795 auto loadCacheControl =
796 translateLoadXeGPUCacheHint(op.getL1Hint(), op.getL3Hint());
797 VectorType resTy = cast<VectorType>(op.getValue().getType());
798 VectorType loadedTy =
799 encodeVectorTypeTo(resTy, rewriter.getIntegerType(elemBitSize));
800 Value load = xevm::BlockLoadOp::create(
801 rewriter, loc, loadedTy, finalPtrLLVM,
802 xevm::LoadCacheControlAttr::get(ctxt, loadCacheControl));
803 if (loadedTy != resTy)
804 load = vector::BitCastOp::create(rewriter, loc, resTy, load);
805 rewriter.replaceOp(op, load);
806 } else {
807 return rewriter.notifyMatchFailure(
808 op, "Unsupported operation: xegpu.prefetch_nd with tensor "
809 "descriptor rank == 1");
810 }
811 }
812 return success();
813 }
814};
815
816// Add a builder that creates
817// offset * elemByteSize + baseAddr
818static Value addOffsetToBaseAddr(ConversionPatternRewriter &rewriter,
819 Location loc, Value baseAddr, Value offset,
820 int64_t elemByteSize) {
822 rewriter, loc, baseAddr.getType(), elemByteSize);
823 Value byteOffset = arith::MulIOp::create(rewriter, loc, offset, byteSize);
824 Value newAddr = arith::AddIOp::create(rewriter, loc, baseAddr, byteOffset);
825 return newAddr;
826}
827
828// Returns true when every element of `mask` carries the same bit, so gating a
829// whole contiguous block on element 0 is equivalent. Splat constants, an
830// all-ones or all-zeros `vector.constant_mask`, broadcasts of a scalar, and a
831// `vector.from_elements` of one repeated value qualify.
832//
833// TODO: this recognizes a fixed list of producers. A general uniformity query
834// on the vector dialect would cover more forms and would not need updating
835// every time a new producer shows up here.
836static bool isUniformMask(Value mask) {
837 if (!isa<VectorType>(mask.getType()))
838 return true;
839 DenseElementsAttr splat;
840 if (matchPattern(mask, m_Constant(&splat)) && splat.isSplat())
841 return true;
842 if (auto constantMask = mask.getDefiningOp<vector::ConstantMaskOp>()) {
843 // All-ones and all-zeros are uniform. A zero dim size is only legal when
844 // every dim is zero, so those are the only two uniform cases.
845 return constantMask.isAllOnesMask() ||
846 llvm::all_of(constantMask.getMaskDimSizes(),
847 [](int64_t size) { return size == 0; });
848 }
849 if (auto broadcast = mask.getDefiningOp<vector::BroadcastOp>())
850 return !isa<VectorType>(broadcast.getSource().getType());
851 if (auto fromElements = mask.getDefiningOp<vector::FromElementsOp>())
852 return llvm::all_equal(fromElements.getElements());
853 // Flattening a rank > 1 operand to rank 1 inserts a shape cast. It keeps the
854 // same elements, so it keeps uniformity.
855 if (auto shapeCast = mask.getDefiningOp<vector::ShapeCastOp>())
856 return isUniformMask(shapeCast.getSource());
857 return false;
858}
859
860template <typename OpType,
861 typename = std::enable_if_t<llvm::is_one_of<
862 OpType, xegpu::LoadGatherOp, xegpu::StoreScatterOp>::value>>
863class LoadStoreToXeVMPattern : public OpConversionPattern<OpType> {
864 using OpConversionPattern<OpType>::OpConversionPattern;
865 LogicalResult
866 matchAndRewrite(OpType op, typename OpType::Adaptor adaptor,
867 ConversionPatternRewriter &rewriter) const override {
868 Value offset = adaptor.getOffsets();
869 if (!offset)
870 return rewriter.notifyMatchFailure(op, "Expected offset to be provided.");
871 auto loc = op.getLoc();
872 auto ctxt = rewriter.getContext();
873 Value basePtrI64;
874 // Load result or Store valye Type can be vector or scalar.
875 Type valOrResTy;
876 if constexpr (std::is_same_v<OpType, xegpu::LoadGatherOp>)
877 valOrResTy =
878 this->getTypeConverter()->convertType(op.getResult().getType());
879 else
880 valOrResTy = adaptor.getValue().getType();
881 VectorType valOrResVecTy = dyn_cast<VectorType>(valOrResTy);
882 bool hasScalarVal = !valOrResVecTy;
883 int64_t elemBitWidth =
884 hasScalarVal ? valOrResTy.getIntOrFloatBitWidth()
885 : valOrResVecTy.getElementType().getIntOrFloatBitWidth();
886 // Element type must be multiple of 8 bits.
887 if (elemBitWidth % 8 != 0)
888 return rewriter.notifyMatchFailure(
889 op, "Expected element type bit width to be multiple of 8.");
890 int64_t elemByteSize = elemBitWidth / 8;
891 // Default memory space is global.
892 LLVM::LLVMPointerType ptrTypeLLVM = LLVM::LLVMPointerType::get(
893 ctxt, getNumericXeVMAddrSpace(xegpu::MemorySpace::Global));
894 // Base pointer can come from source (load) or dest (store).
895 // If they are memrefs, we use their memory space.
896 if constexpr (std::is_same_v<OpType, xegpu::LoadGatherOp>) {
897 basePtrI64 = adaptor.getSource();
898 if (auto memRefTy = dyn_cast<MemRefType>(op.getSource().getType())) {
899 FailureOr<unsigned> addrSpace =
900 getNumericMemorySpace(memRefTy.getMemorySpace());
901 if (failed(addrSpace))
902 return rewriter.notifyMatchFailure(
903 op, "Unsupported memref memory space attribute.");
904 if (*addrSpace != 0)
905 ptrTypeLLVM = LLVM::LLVMPointerType::get(ctxt, *addrSpace);
906 }
907 } else {
908 basePtrI64 = adaptor.getDest();
909 if (auto memRefTy = dyn_cast<MemRefType>(op.getDest().getType())) {
910 FailureOr<unsigned> addrSpace =
911 getNumericMemorySpace(memRefTy.getMemorySpace());
912 if (failed(addrSpace))
913 return rewriter.notifyMatchFailure(
914 op, "Unsupported memref memory space attribute.");
915 if (*addrSpace != 0)
916 ptrTypeLLVM = LLVM::LLVMPointerType::get(ctxt, *addrSpace);
917 }
918 }
919 // Base pointer is passed as i32 or i64 by adaptor, cast to i64 if needed.
920 if (basePtrI64.getType() != rewriter.getI64Type()) {
921 basePtrI64 = arith::ExtUIOp::create(rewriter, loc, rewriter.getI64Type(),
922 basePtrI64);
923 }
924 Value mask = adaptor.getMask();
925
926 // Coalesce a lane's multi-element access into one block access.
927 //
928 // Distribution gives every element its own offset and mask bit, so a lane
929 // that takes D neighbouring elements arrives with `vector<D>` offsets and
930 // mask. One block access needs one base offset and one mask bit, so take
931 // both from element 0. A non-uniform mask cannot be reduced to one bit, so
932 // it stays a vector and fails to match below.
933 auto origOffsetsTy = dyn_cast<VectorType>(op.getOffsets().getType());
934 if (isa<VectorType>(offset.getType()) && origOffsetsTy && valOrResVecTy &&
935 origOffsetsTy.getNumElements() == valOrResVecTy.getNumElements() &&
936 isUniformMask(op.getMask())) {
937 offset = vector::ExtractOp::create(rewriter, loc, offset,
938 ArrayRef<int64_t>{0});
939 if (isa<VectorType>(mask.getType()))
940 mask = vector::ExtractOp::create(rewriter, loc, mask,
941 ArrayRef<int64_t>{0});
942 }
943
944 if (dyn_cast<VectorType>(offset.getType())) {
945 // Offset needs be scalar. Single element vector is converted to scalar
946 // by type converter.
947 return rewriter.notifyMatchFailure(op, "Expected offset to be a scalar.");
948 } else {
949 // If offset is provided, we add them to the base pointer.
950 // Offset is in number of elements, we need to multiply by
951 // element byte size.
952 basePtrI64 =
953 addOffsetToBaseAddr(rewriter, loc, basePtrI64, offset, elemByteSize);
954 }
955 // Convert base pointer (i64) to LLVM pointer type.
956 Value basePtrLLVM =
957 LLVM::IntToPtrOp::create(rewriter, loc, ptrTypeLLVM, basePtrI64);
958
959 Value maskForLane;
960 VectorType maskVecTy = dyn_cast<VectorType>(mask.getType());
961 if (maskVecTy) {
962 // Mask needs be scalar. Single element vector is converted to scalar by
963 // type converter.
964 return rewriter.notifyMatchFailure(op, "Expected mask to be a scalar.");
965 } else
966 maskForLane = mask;
967 if constexpr (std::is_same_v<OpType, xegpu::LoadGatherOp>) {
968 scf::IfOp ifOp = scf::IfOp::create(rewriter, loc, {valOrResTy},
969 maskForLane, true, true);
970 // If mask is true,- then clause - load from memory and yield.
971 rewriter.setInsertionPointToStart(&ifOp.getThenRegion().front());
972 if (!hasScalarVal)
973 valOrResTy = VectorType::get({valOrResVecTy.getNumElements()},
974 valOrResVecTy.getElementType());
975 Value loaded =
976 LLVM::LoadOp::create(rewriter, loc, valOrResTy, basePtrLLVM);
977 // Set cache control attribute on the load operation.
979 "cache_control", xevm::LoadCacheControlAttr::get(
980 ctxt, translateLoadXeGPUCacheHint(
981 op.getL1Hint(), op.getL3Hint())));
982 scf::YieldOp::create(rewriter, loc, ValueRange{loaded});
983 rewriter.setInsertionPointToStart(&ifOp.getElseRegion().front());
984 // If mask is false - else clause -yield a vector of zeros.
985 auto eTy = hasScalarVal ? valOrResTy : valOrResVecTy.getElementType();
986 TypedAttr eVal;
987 if (eTy.isFloat())
988 eVal = FloatAttr::get(eTy, 0.0);
989 else
990 eVal = IntegerAttr::get(eTy, 0);
991 if (hasScalarVal)
992 loaded = arith::ConstantOp::create(rewriter, loc, eVal);
993 else
994 loaded = arith::ConstantOp::create(
995 rewriter, loc, DenseElementsAttr::get(valOrResVecTy, eVal));
996 scf::YieldOp::create(rewriter, loc, ValueRange{loaded});
997 rewriter.replaceOp(op, ifOp.getResult(0));
998 } else {
999 // If mask is true, perform the store.
1000 scf::IfOp ifOp = scf::IfOp::create(rewriter, loc, maskForLane, false);
1001 auto body = ifOp.getBody();
1002 rewriter.setInsertionPointToStart(body);
1003 auto storeOp =
1004 LLVM::StoreOp::create(rewriter, loc, adaptor.getValue(), basePtrLLVM);
1005 // Set cache control attribute on the store operation.
1006 storeOp.getOperation()->setDiscardableAttr(
1007 "cache_control", xevm::StoreCacheControlAttr::get(
1008 ctxt, translateStoreXeGPUCacheHint(
1009 op.getL1Hint(), op.getL3Hint())));
1010 rewriter.eraseOp(op);
1011 }
1012 return success();
1013 }
1014};
1015
1016class CreateMemDescOpPattern final
1017 : public OpConversionPattern<xegpu::CreateMemDescOp> {
1018public:
1019 using OpConversionPattern<xegpu::CreateMemDescOp>::OpConversionPattern;
1020 LogicalResult
1021 matchAndRewrite(xegpu::CreateMemDescOp op, OpAdaptor adaptor,
1022 ConversionPatternRewriter &rewriter) const override {
1023
1024 rewriter.replaceOp(op, adaptor.getSource());
1025 return success();
1026 }
1027};
1028
1029/// Returns the shape of the largest piece of a matrix op payload that one LLVM
1030/// access can move, as an access only moves consecutive elements.
1031/// Examples:
1032/// mem_desc<16x2xf32> (stride [2, 1], row-major)
1033/// payload [1, 2] -> [1, 2] one access of 2 elements
1034/// payload [2, 1] -> [1, 1] 2 accesses of 1 element
1035/// mem_desc<16x2xf32, stride = [1, 16]> (column-major)
1036/// payload [4, 1] -> [4, 1] one access of 4 elements
1037/// payload [2, 2] -> [1, 1] 4 accesses of 1 element; each column is
1038/// consecutive in memory but not in the
1039/// row-major flattened vector
1040///
1041/// TODO: On a blocked layout memory is only consecutive inside a block, so a
1042/// piece that crosses a block boundary along the stride-1 dim is not
1043/// contiguous. Blocks are not taken into account yet.
1045getContiguousPayloadShape(ArrayRef<int64_t> payloadShape, size_t numOffsets,
1046 xegpu::MemDescType mdescTy) {
1047 SmallVector<int64_t> shape(payloadShape);
1048 int64_t rank = shape.size();
1049 if (rank != static_cast<int64_t>(numOffsets))
1050 return shape;
1051
1052 int64_t fastestDim = 0;
1053 for (auto [dim, strideAttr] :
1054 llvm::enumerate(mdescTy.getStrideAttr().getValue())) {
1055 if (cast<IntegerAttr>(strideAttr).getInt() == 1) {
1056 fastestDim = dim;
1057 break;
1058 }
1059 }
1060
1061 // A slice that runs along fastestDim is only consecutive in the vector if
1062 // every dim to its right has size 1.
1063 bool spansFastestDim =
1064 llvm::all_of(llvm::seq<int64_t>(fastestDim + 1, rank),
1065 [&](int64_t d) { return payloadShape[d] == 1; });
1066 for (int64_t d = 0; d < rank; ++d)
1067 shape[d] = spansFastestDim && d == fastestDim ? payloadShape[d] : 1;
1068
1069 return shape;
1070}
1071
1072template <typename OpType,
1073 typename = std::enable_if_t<llvm::is_one_of<
1074 OpType, xegpu::LoadMatrixOp, xegpu::StoreMatrixOp>::value>>
1075class LoadStoreMatrixToXeVMPattern : public OpConversionPattern<OpType> {
1076 using OpConversionPattern<OpType>::OpConversionPattern;
1077
1078 /// Lowers `op` to one LLVM access per `sliceShape`-sized piece of its
1079 /// payload, each at its own address. `basePtrLLVM` already addresses the
1080 /// first piece. A payload that is one contiguous piece, which is the common
1081 /// case, gives a single access of the whole payload.
1082 LogicalResult emitContiguousAccesses(
1083 OpType op, typename OpType::Adaptor adaptor,
1084 ConversionPatternRewriter &rewriter, xegpu::MemDescType mdescTy,
1085 ArrayRef<OpFoldResult> offsets, ArrayRef<int64_t> payloadShape,
1086 ArrayRef<int64_t> sliceShape, Value baseAddr32, Value basePtrLLVM,
1087 LLVM::LLVMPointerType ptrTypeLLVM, int64_t elemByteSize) const {
1088 Location loc = op.getLoc();
1089 int64_t rank = payloadShape.size();
1090 SmallVector<int64_t> sliceGrid(rank);
1091 for (int64_t d = 0; d < rank; ++d)
1092 sliceGrid[d] = payloadShape[d] / sliceShape[d];
1093 int64_t numSlices = computeProduct(sliceGrid);
1094 int64_t sliceLen = computeProduct(sliceShape);
1095 SmallVector<int64_t> payloadStrides = computeStrides(payloadShape);
1096 SmallVector<int64_t> gridStrides = computeStrides(sliceGrid);
1097
1098 // The payload type LLVM load/store accepts: f8E8M0FNU and friends become
1099 // same-width integers, vector<1> becomes a scalar. Operands arrive
1100 // converted in the adaptor, results do not.
1101 Type payloadTy;
1102 if constexpr (std::is_same_v<OpType, xegpu::LoadMatrixOp>)
1103 payloadTy =
1104 this->getTypeConverter()->convertType(op.getResult().getType());
1105 else
1106 payloadTy = adaptor.getData().getType();
1107 auto payloadVecTy = dyn_cast<VectorType>(payloadTy);
1108
1109 Type sliceTy = payloadTy;
1110 if (numSlices != 1) {
1111 if (!payloadVecTy)
1112 return rewriter.notifyMatchFailure(
1113 op, "Expected a vector payload to slice.");
1114 sliceTy = sliceLen == 1 ? payloadVecTy.getElementType()
1115 : VectorType::get({sliceLen},
1116 payloadVecTy.getElementType());
1117 }
1118
1119 // Addresses the slice `delta` elements into the payload.
1120 auto slicePtr = [&](ArrayRef<int64_t> delta) -> Value {
1121 if (llvm::all_of(delta, [](int64_t d) { return d == 0; }))
1122 return basePtrLLVM;
1123 SmallVector<OpFoldResult> sliceOffsets;
1124 for (auto [base, d] : llvm::zip_equal(offsets, delta)) {
1125 if (d == 0)
1126 sliceOffsets.push_back(base);
1127 else if (auto attr = dyn_cast<Attribute>(base))
1128 sliceOffsets.push_back(
1129 rewriter.getIndexAttr(cast<IntegerAttr>(attr).getInt() + d));
1130 else
1131 sliceOffsets.push_back(
1132 OpFoldResult(arith::AddIOp::create(
1133 rewriter, loc, cast<Value>(base),
1134 arith::ConstantIndexOp::create(rewriter, loc, d))
1135 .getResult()));
1136 }
1137 Value sliceOffset = mdescTy.getLinearOffsets(rewriter, loc, sliceOffsets);
1138 sliceOffset = arith::IndexCastUIOp::create(
1139 rewriter, loc, rewriter.getI32Type(), sliceOffset);
1140 Value slicePtrI32 = addOffsetToBaseAddr(rewriter, loc, baseAddr32,
1141 sliceOffset, elemByteSize);
1142 return LLVM::IntToPtrOp::create(rewriter, loc, ptrTypeLLVM, slicePtrI32);
1143 };
1144
1145 Value result;
1146 if constexpr (std::is_same_v<OpType, xegpu::LoadMatrixOp>)
1147 if (numSlices != 1)
1148 result = arith::ConstantOp::create(rewriter, loc, payloadVecTy,
1149 rewriter.getZeroAttr(payloadVecTy));
1150
1151 for (int64_t slice = 0; slice < numSlices; ++slice) {
1152 SmallVector<int64_t> gridCoords = delinearize(slice, gridStrides);
1153 SmallVector<int64_t> delta(rank);
1154 for (int64_t d = 0; d < rank; ++d)
1155 delta[d] = gridCoords[d] * sliceShape[d];
1156 int64_t payloadOffset = linearize(delta, payloadStrides);
1157 Value ptr = slicePtr(delta);
1158
1159 if constexpr (std::is_same_v<OpType, xegpu::LoadMatrixOp>) {
1160 Value loaded = LLVM::LoadOp::create(rewriter, loc, sliceTy, ptr);
1161 if (numSlices == 1)
1162 result = loaded;
1163 else if (sliceLen == 1)
1164 result = vector::InsertOp::create(rewriter, loc, loaded, result,
1165 ArrayRef<int64_t>{payloadOffset});
1166 else
1167 result = vector::InsertStridedSliceOp::create(
1168 rewriter, loc, loaded, result, ArrayRef<int64_t>{payloadOffset},
1169 ArrayRef<int64_t>{1});
1170 } else {
1171 Value data = adaptor.getData();
1172 if (numSlices != 1) {
1173 if (sliceLen == 1)
1174 data = vector::ExtractOp::create(rewriter, loc, data,
1175 ArrayRef<int64_t>{payloadOffset});
1176 else
1177 data = vector::ExtractStridedSliceOp::create(
1178 rewriter, loc, data, ArrayRef<int64_t>{payloadOffset},
1179 ArrayRef<int64_t>{sliceLen}, ArrayRef<int64_t>{1});
1180 }
1181 LLVM::StoreOp::create(rewriter, loc, data, ptr);
1182 }
1183 }
1184
1185 if constexpr (std::is_same_v<OpType, xegpu::LoadMatrixOp>)
1186 rewriter.replaceOp(op, result);
1187 else
1188 rewriter.eraseOp(op);
1189 return success();
1190 }
1191 LogicalResult
1192 matchAndRewrite(OpType op, typename OpType::Adaptor adaptor,
1193 ConversionPatternRewriter &rewriter) const override {
1194
1195 SmallVector<OpFoldResult> offsets = op.getMixedOffsets();
1196 if (offsets.empty())
1197 return rewriter.notifyMatchFailure(op, "Expected offset to be provided.");
1198
1199 auto loc = op.getLoc();
1200 auto ctxt = rewriter.getContext();
1201 Value baseAddr32 = adaptor.getMemDesc();
1202 Value mdescVal = op.getMemDesc();
1203 // Load result or Store value Type can be vector or scalar.
1204 Type dataTy;
1205 if constexpr (std::is_same_v<OpType, xegpu::LoadMatrixOp>) {
1206 Type resType = op.getResult().getType();
1207 // Some transforms may leave unit dimension in the 2D vector, adaptors do
1208 // not catch it for results.
1209 if (auto vecType = dyn_cast<VectorType>(resType))
1210 resType = VectorType::get({vecType.getNumElements()},
1211 vecType.getElementType());
1212 dataTy = resType;
1213 } else
1214 dataTy = adaptor.getData().getType();
1215 VectorType valOrResVecTy = dyn_cast<VectorType>(dataTy);
1216 if (!valOrResVecTy)
1217 valOrResVecTy = VectorType::get(1, dataTy);
1218
1219 int64_t elemBitWidth =
1220 valOrResVecTy.getElementType().getIntOrFloatBitWidth();
1221 // Element type must be multiple of 8 bits.
1222 if (elemBitWidth % 8 != 0)
1223 return rewriter.notifyMatchFailure(
1224 op, "Expected element type bit width to be multiple of 8.");
1225 int64_t elemByteSize = elemBitWidth / 8;
1226
1227 // Default memory space is SLM.
1228 LLVM::LLVMPointerType ptrTypeLLVM = LLVM::LLVMPointerType::get(
1229 ctxt, getNumericXeVMAddrSpace(xegpu::MemorySpace::SLM));
1230
1231 auto mdescTy = cast<xegpu::MemDescType>(mdescVal.getType());
1232
1233 Value linearOffset = mdescTy.getLinearOffsets(rewriter, loc, offsets);
1234 linearOffset = arith::IndexCastUIOp::create(
1235 rewriter, loc, rewriter.getI32Type(), linearOffset);
1236 Value basePtrI32 = addOffsetToBaseAddr(rewriter, loc, baseAddr32,
1237 linearOffset, elemByteSize);
1238
1239 // convert base pointer (i32) to LLVM pointer type
1240 Value basePtrLLVM =
1241 LLVM::IntToPtrOp::create(rewriter, loc, ptrTypeLLVM, basePtrI32);
1242
1243 if (op.getSubgroupBlockIoAttr()) {
1244 // if the attribute 'subgroup_block_io' is set to true, it lowers to
1245 // xevm.blockload
1246
1247 Type intElemTy = rewriter.getIntegerType(elemBitWidth);
1248 VectorType intVecTy =
1249 VectorType::get(valOrResVecTy.getShape(), intElemTy);
1250
1251 if constexpr (std::is_same_v<OpType, xegpu::LoadMatrixOp>) {
1252 Value loadOp = xevm::BlockLoadOp::create(
1253 rewriter, loc, intVecTy, basePtrLLVM, /*cache_control=*/nullptr);
1254 if (intVecTy != valOrResVecTy) {
1255 loadOp =
1256 vector::BitCastOp::create(rewriter, loc, valOrResVecTy, loadOp);
1257 }
1258 rewriter.replaceOp(op, loadOp);
1259 } else {
1260 Value dataToStore = adaptor.getData();
1261 if (valOrResVecTy != intVecTy) {
1262 dataToStore =
1263 vector::BitCastOp::create(rewriter, loc, intVecTy, dataToStore);
1264 }
1265 xevm::BlockStoreOp::create(rewriter, loc, basePtrLLVM, dataToStore,
1266 nullptr);
1267 rewriter.eraseOp(op);
1268 }
1269 return success();
1270 }
1271
1272 if (valOrResVecTy.getNumElements() >= 1) {
1273 auto chipOpt = xegpu::getChipStr(op);
1274 if (!chipOpt ||
1275 (*chipOpt != "pvc" && *chipOpt != "bmg" && *chipOpt != "cri")) {
1276 // the lowering for chunk load only works for pvc, bmg or cri
1277 return rewriter.notifyMatchFailure(
1278 op, "The lowering is specific to pvc, bmg or cri.");
1279 }
1280 }
1281
1282 // The payload shape says where in the matrix each element lives, which the
1283 // adaptor has already linearized away.
1284 ArrayRef<int64_t> payloadShape;
1285 Type opPayloadTy;
1286 if constexpr (std::is_same_v<OpType, xegpu::LoadMatrixOp>)
1287 opPayloadTy = op.getResult().getType();
1288 else
1289 opPayloadTy = op.getData().getType();
1290 if (auto opPayloadVecTy = dyn_cast<VectorType>(opPayloadTy))
1291 payloadShape = opPayloadVecTy.getShape();
1292
1293 return emitContiguousAccesses(
1294 op, adaptor, rewriter, mdescTy, offsets, payloadShape,
1295 getContiguousPayloadShape(payloadShape, offsets.size(), mdescTy),
1296 baseAddr32, basePtrLLVM, ptrTypeLLVM, elemByteSize);
1297 }
1298};
1299
1300class PrefetchToXeVMPattern : public OpConversionPattern<xegpu::PrefetchOp> {
1301 using Base::Base;
1302 LogicalResult
1303 matchAndRewrite(xegpu::PrefetchOp op, xegpu::PrefetchOp::Adaptor adaptor,
1304 ConversionPatternRewriter &rewriter) const override {
1305 auto loc = op.getLoc();
1306 auto ctxt = rewriter.getContext();
1307 Value basePtrI64 = adaptor.getSource();
1308 // Base pointer is passed as i32 or i64 by adaptor, cast to i64 if needed.
1309 if (basePtrI64.getType() != rewriter.getI64Type())
1310 basePtrI64 = arith::ExtUIOp::create(rewriter, loc, rewriter.getI64Type(),
1311 basePtrI64);
1312 Value offsets = adaptor.getOffsets();
1313 if (offsets) {
1314 VectorType offsetsVecTy = dyn_cast<VectorType>(offsets.getType());
1315 if (offsetsVecTy) {
1316 // Offset needs be scalar.
1317 return rewriter.notifyMatchFailure(op,
1318 "Expected offsets to be a scalar.");
1319 } else {
1320 int64_t elemBitWidth{0};
1321 int64_t elemByteSize;
1322 // Element byte size can come from two sources:
1323 if (auto memRefTy = dyn_cast<MemRefType>(op.getSourceType())) {
1324 // If memref is available, we use its element type to
1325 // determine element byte size.
1326 elemBitWidth = memRefTy.getElementType().getIntOrFloatBitWidth();
1327 } else {
1328 // Otherwise, we use the provided offset byte alignment.
1329 elemByteSize = *op.getOffsetAlignByte();
1330 }
1331 if (elemBitWidth != 0) {
1332 if (elemBitWidth % 8 != 0)
1333 return rewriter.notifyMatchFailure(
1334 op, "Expected element type bit width to be multiple of 8.");
1335 elemByteSize = elemBitWidth / 8;
1336 }
1337 basePtrI64 = addOffsetToBaseAddr(rewriter, loc, basePtrI64, offsets,
1338 elemByteSize);
1339 }
1340 }
1341 // Default memory space is global.
1342 LLVM::LLVMPointerType ptrTypeLLVM = LLVM::LLVMPointerType::get(
1343 ctxt, getNumericXeVMAddrSpace(xegpu::MemorySpace::Global));
1344 // If source is a memref, we use its memory space.
1345 if (auto memRefTy = dyn_cast<MemRefType>(op.getSource().getType())) {
1346 FailureOr<unsigned> addrSpace =
1347 getNumericMemorySpace(memRefTy.getMemorySpace());
1348 if (failed(addrSpace))
1349 return rewriter.notifyMatchFailure(
1350 op, "Unsupported memref memory space attribute.");
1351 if (*addrSpace != 0)
1352 ptrTypeLLVM = LLVM::LLVMPointerType::get(ctxt, *addrSpace);
1353 }
1354 // Convert base pointer (i64) to LLVM pointer type.
1355 Value ptrLLVM =
1356 LLVM::IntToPtrOp::create(rewriter, loc, ptrTypeLLVM, basePtrI64);
1357 // Create the prefetch op with cache control attribute.
1358 xevm::PrefetchOp::create(
1359 rewriter, loc, ptrLLVM,
1360 xevm::LoadCacheControlAttr::get(
1361 ctxt, translateLoadXeGPUCacheHint(op.getL1Hint(), op.getL3Hint())));
1362 rewriter.eraseOp(op);
1363 return success();
1364 }
1365};
1366
1367class FenceToXeVMPattern : public OpConversionPattern<xegpu::FenceOp> {
1368 using Base::Base;
1369 LogicalResult
1370 matchAndRewrite(xegpu::FenceOp op, xegpu::FenceOp::Adaptor adaptor,
1371 ConversionPatternRewriter &rewriter) const override {
1372 auto loc = op.getLoc();
1373 xevm::MemScope memScope{xevm::MemScope::WORKGROUP};
1374 switch (op.getFenceScope()) {
1375 case xegpu::FenceScope::Workgroup:
1376 memScope = xevm::MemScope::WORKGROUP;
1377 break;
1378 case xegpu::FenceScope::GPU:
1379 memScope = xevm::MemScope::DEVICE;
1380 break;
1381 }
1382 xevm::AddrSpace addrSpace{xevm::AddrSpace::GLOBAL};
1383 switch (op.getMemoryKind()) {
1384 case xegpu::MemorySpace::Global:
1385 addrSpace = xevm::AddrSpace::GLOBAL;
1386 break;
1387 case xegpu::MemorySpace::SLM:
1388 addrSpace = xevm::AddrSpace::SHARED;
1389 break;
1390 }
1391 xevm::MemfenceOp::create(rewriter, loc, memScope, addrSpace);
1392 rewriter.eraseOp(op);
1393 return success();
1394 }
1395};
1396
1397static auto encodePrecision = [](Type type) -> xevm::ElemType {
1398 if (type.isBF16())
1399 return xevm::ElemType::BF16;
1400 else if (type.isF16())
1401 return xevm::ElemType::F16;
1402 else if (type.isTF32())
1403 return xevm::ElemType::TF32;
1404 else if (type.isInteger(8)) {
1405 if (type.isUnsignedInteger())
1406 return xevm::ElemType::U8;
1407 return xevm::ElemType::S8;
1408 } else if (type.isF32())
1409 return xevm::ElemType::F32;
1410 else if (type.isInteger(32))
1411 return xevm::ElemType::S32;
1412 else if (type.isF8E5M2())
1413 return xevm::ElemType::BF8;
1414 else if (type.isF8E4M3FN())
1415 return xevm::ElemType::F8;
1416 else if (mlir::isa<Float4E2M1FNType>(type))
1417 return xevm::ElemType::E2M1;
1418 llvm_unreachable("add more support for ElemType");
1419};
1420
1421static unsigned getNumOperandsPerDword(xevm::ElemType pTy) {
1422 switch (pTy) {
1423 case xevm::ElemType::TF32:
1424 return 1;
1425 case xevm::ElemType::BF16:
1426 case xevm::ElemType::F16:
1427 return 2;
1428 case xevm::ElemType::U8:
1429 case xevm::ElemType::S8:
1430 case xevm::ElemType::F8:
1431 case xevm::ElemType::BF8:
1432 return 4;
1433 case xevm::ElemType::E2M1:
1434 return 8;
1435 default:
1436 llvm_unreachable("unsupported xevm::ElemType");
1437 }
1438}
1439
1440class DpasToXeVMPattern : public OpConversionPattern<xegpu::DpasOp> {
1441 using Base::Base;
1442 LogicalResult
1443 matchAndRewrite(xegpu::DpasOp op, xegpu::DpasOp::Adaptor adaptor,
1444 ConversionPatternRewriter &rewriter) const override {
1445 auto loc = op.getLoc();
1446 auto ctxt = rewriter.getContext();
1447 auto aTy = cast<VectorType>(op.getLhs().getType());
1448 auto bTy = cast<VectorType>(op.getRhs().getType());
1449 auto resultType = cast<VectorType>(op.getResultType());
1450
1451 // get the correct dpasInst by getting info from chip
1452 auto chipStr = xegpu::getChipStr(op);
1453 if (!chipStr)
1454 return rewriter.notifyMatchFailure(op, "cannot determine target chip");
1455
1456 const auto *uArch = mlir::xegpu::uArch::getUArch(*chipStr);
1457 if (!uArch)
1458 return rewriter.notifyMatchFailure(op, "unsupported target uArch");
1459
1460 auto *dpasInst = const_cast<xegpu::uArch::SubgroupMatrixMultiplyAcc *>(
1461 llvm::dyn_cast_or_null<xegpu::uArch::SubgroupMatrixMultiplyAcc>(
1462 uArch->getInstruction(
1463 xegpu::uArch::InstructionKind::SubgroupMatrixMultiplyAcc)));
1464 if (!dpasInst)
1465 return rewriter.notifyMatchFailure(op,
1466 "DPAS not supported by target uArch");
1467
1468 auto checkSupportedTypes = [&](VectorType vecTy,
1469 xegpu::uArch::MMAOpndKind kind) -> bool {
1470 auto supported = dpasInst->getSupportedTypes(*ctxt, kind);
1471 return llvm::find(supported, vecTy.getElementType()) != supported.end();
1472 };
1473
1474 if (!checkSupportedTypes(aTy, xegpu::uArch::MMAOpndKind::MatrixA))
1475 return rewriter.notifyMatchFailure(
1476 op, "A-matrix element type not supported by target uArch");
1477 if (!checkSupportedTypes(bTy, xegpu::uArch::MMAOpndKind::MatrixB))
1478 return rewriter.notifyMatchFailure(
1479 op, "B-matrix element type not supported by target uArch");
1480 // NOTE: Supported types for MatrixC and MatrixD are identical
1481 if (!checkSupportedTypes(resultType, xegpu::uArch::MMAOpndKind::MatrixD))
1482 return rewriter.notifyMatchFailure(
1483 op, "result/accumulator element type not supported by target uArch");
1484
1485 xevm::ElemType precATy = encodePrecision(aTy.getElementType());
1486 xevm::ElemType precBTy = encodePrecision(bTy.getElementType());
1487 Value c = op.getAcc();
1488 if (!c) {
1489 auto elementTy = resultType.getElementType();
1490 Attribute initValueAttr;
1491 if (isa<FloatType>(elementTy))
1492 initValueAttr = FloatAttr::get(elementTy, 0.0);
1493 else
1494 initValueAttr = IntegerAttr::get(elementTy, 0);
1495 c = arith::ConstantOp::create(
1496 rewriter, loc, DenseElementsAttr::get(resultType, initValueAttr));
1497 }
1498
1499 Value aVec = op.getLhs();
1500 Value bVec = op.getRhs();
1501 auto cvecty = cast<VectorType>(c.getType());
1502 xevm::ElemType precCTy = encodePrecision(cvecty.getElementType());
1503 xevm::ElemType precDTy = encodePrecision(resultType.getElementType());
1504 VectorType cNty =
1505 VectorType::get(cvecty.getNumElements(), cvecty.getElementType());
1506 if (cvecty != cNty)
1507 c = vector::ShapeCastOp::create(rewriter, loc, cNty, c);
1508 Value dpasRes = xevm::MMAOp::create(
1509 rewriter, loc, cNty, aVec, bVec, c,
1510 xevm::MMAShapeAttr::get(ctxt, cvecty.getNumElements(), executionSize,
1511 systolicDepth *
1512 getNumOperandsPerDword(precATy)),
1513 xevm::MMATypesAttr::get(ctxt, precDTy, precATy, precBTy, precCTy));
1514 if (cvecty != cNty)
1515 dpasRes = vector::ShapeCastOp::create(rewriter, loc, resultType, dpasRes);
1516 rewriter.replaceOp(op, dpasRes);
1517 return success();
1518 }
1519};
1520
1521static std::optional<LLVM::AtomicBinOp>
1522matchSimpleAtomicOp(arith::AtomicRMWKind arithKind) {
1523 switch (arithKind) {
1524 case arith::AtomicRMWKind::addf:
1525 return LLVM::AtomicBinOp::fadd;
1526 case arith::AtomicRMWKind::addi:
1527 return LLVM::AtomicBinOp::add;
1528 case arith::AtomicRMWKind::assign:
1529 return LLVM::AtomicBinOp::xchg;
1530 case arith::AtomicRMWKind::maximumf:
1531 return LLVM::AtomicBinOp::fmax;
1532 case arith::AtomicRMWKind::maxs:
1533 return LLVM::AtomicBinOp::max;
1534 case arith::AtomicRMWKind::maxu:
1535 return LLVM::AtomicBinOp::umax;
1536 case arith::AtomicRMWKind::minimumf:
1537 return LLVM::AtomicBinOp::fmin;
1538 case arith::AtomicRMWKind::mins:
1539 return LLVM::AtomicBinOp::min;
1540 case arith::AtomicRMWKind::minu:
1541 return LLVM::AtomicBinOp::umin;
1542 case arith::AtomicRMWKind::ori:
1543 return LLVM::AtomicBinOp::_or;
1544 case arith::AtomicRMWKind::andi:
1545 return LLVM::AtomicBinOp::_and;
1546 default:
1547 return std::nullopt;
1548 }
1549}
1550
1551class AtomicRMWToXeVMPattern : public OpConversionPattern<xegpu::AtomicRMWOp> {
1552 using Base::Base;
1553 LogicalResult
1554 matchAndRewrite(xegpu::AtomicRMWOp op, xegpu::AtomicRMWOp::Adaptor adaptor,
1555 ConversionPatternRewriter &rewriter) const override {
1556 auto loc = op.getLoc();
1557 auto ctxt = rewriter.getContext();
1558 auto tdesc = op.getTensorDesc().getType();
1559 auto ptrTypeLLVM = LLVM::LLVMPointerType::get(
1560 ctxt, getNumericXeVMAddrSpace(tdesc.getMemorySpace()));
1561 Value basePtrI64 = arith::IndexCastOp::create(
1562 rewriter, loc, rewriter.getI64Type(), adaptor.getTensorDesc());
1563 Value basePtrLLVM =
1564 LLVM::IntToPtrOp::create(rewriter, loc, ptrTypeLLVM, basePtrI64);
1565 VectorType srcOrDstVecTy = cast<VectorType>(op.getValue().getType());
1566 VectorType srcOrDstFlatVecTy = VectorType::get(
1567 srcOrDstVecTy.getNumElements(), srcOrDstVecTy.getElementType());
1568 Value srcFlatVec = vector::ShapeCastOp::create(
1569 rewriter, loc, srcOrDstFlatVecTy, op.getValue());
1570 auto atomicKind = matchSimpleAtomicOp(op.getKind());
1571 assert(atomicKind.has_value());
1572 Value resVec = srcFlatVec;
1573 for (int i = 0; i < srcOrDstVecTy.getNumElements(); i++) {
1574 auto val = vector::ExtractOp::create(rewriter, loc, resVec, i);
1575 Value idx = LLVM::ConstantOp::create(rewriter, loc, rewriter.getI64Type(),
1576 rewriter.getI64IntegerAttr(i));
1577 Value currPtr =
1578 LLVM::GEPOp::create(rewriter, loc, ptrTypeLLVM,
1579 srcOrDstVecTy.getElementType(), basePtrLLVM, idx);
1580 Value newVal =
1581 LLVM::AtomicRMWOp::create(rewriter, loc, atomicKind.value(), currPtr,
1582 val, LLVM::AtomicOrdering::seq_cst);
1583 resVec = vector::InsertOp::create(rewriter, loc, newVal, resVec, i);
1584 }
1585 rewriter.replaceOp(op, resVec);
1586 return success();
1587 }
1588};
1589
1590class DpasMxToXeVMPattern : public OpConversionPattern<xegpu::DpasMxOp> {
1591 using Base::Base;
1592 LogicalResult
1593 matchAndRewrite(xegpu::DpasMxOp op, xegpu::DpasMxOp::Adaptor adaptor,
1594 ConversionPatternRewriter &rewriter) const override {
1595 auto loc = op.getLoc();
1596 auto ctxt = rewriter.getContext();
1597 auto aTy = op.getA().getType();
1598 auto bTy = op.getB().getType();
1599 auto resVecTy =
1600 cast<VectorType>(getTypeConverter()->convertType(op.getType()));
1601
1602 auto chipStr = xegpu::getChipStr(op);
1603 if (!chipStr)
1604 return rewriter.notifyMatchFailure(op, "cannot determine target chip");
1605
1606 const auto *uArch = xegpu::uArch::getUArch(*chipStr);
1607 if (!uArch)
1608 return rewriter.notifyMatchFailure(op, "unsupported target uArch");
1609
1610 // TODO: Add supported shape check
1611
1612 xevm::ElemType precATy = encodePrecision(aTy.getElementType());
1613 xevm::ElemType precBTy = encodePrecision(bTy.getElementType());
1614 Value c = adaptor.getAcc();
1615 if (!c) {
1616 auto elementTy = resVecTy.getElementType();
1617 Attribute initValueAttr;
1618 if (isa<FloatType>(elementTy))
1619 initValueAttr = FloatAttr::get(elementTy, 0.0);
1620 else
1621 initValueAttr = IntegerAttr::get(elementTy, 0);
1622 c = arith::ConstantOp::create(
1623 rewriter, loc, DenseElementsAttr::get(resVecTy, initValueAttr));
1624 }
1625
1626 Value aVec = adaptor.getA();
1627 Value bVec = adaptor.getB();
1628 auto aVecTy = cast<VectorType>(aVec.getType());
1629 auto bVecTy = cast<VectorType>(bVec.getType());
1630 if (aVecTy.getElementTypeBitWidth() == 4)
1631 aVec = vector::BitCastOp::create(
1632 rewriter, loc,
1633 VectorType::get(aVecTy.getNumElements() / 2, rewriter.getI8Type()),
1634 aVec);
1635 if (bVecTy.getElementTypeBitWidth() == 4)
1636 bVec = vector::BitCastOp::create(
1637 rewriter, loc,
1638 VectorType::get(bVecTy.getNumElements() / 2, rewriter.getI8Type()),
1639 bVec);
1640 auto cVecTy = cast<VectorType>(c.getType());
1641 xevm::ElemType precCTy = encodePrecision(cVecTy.getElementType());
1642 xevm::ElemType precDTy = encodePrecision(resVecTy.getElementType());
1643 Value scaleA = adaptor.getScaleA();
1644 Value scaleB = adaptor.getScaleB();
1645 Value dpasMxRes = xevm::MMAMxOp::create(
1646 rewriter, loc, resVecTy, aVec, bVec, scaleA, scaleB, c,
1647 xevm::MMAShapeAttr::get(ctxt, cVecTy.getNumElements(), executionSize,
1648 systolicDepth *
1649 getNumOperandsPerDword(precATy)),
1650 xevm::MMATypesAttr::get(ctxt, precDTy, precATy, precBTy, precCTy));
1651 rewriter.replaceOp(op, dpasMxRes);
1652 return success();
1653 }
1654};
1655
1656//===----------------------------------------------------------------------===//
1657// arith.extf / arith.truncf to xevm.extf / xevm.truncf
1658//===----------------------------------------------------------------------===//
1659//
1660// Micro-scaling (MX) GEMM lowering breaks arith.scaling_extf/scaling_truncf
1661// into plain arith.extf/arith.truncf whose narrow side uses one of the MX float
1662// formats (f8E5M2, f8E4M3FN or f4E2M1FN). These narrow floats have no native
1663// LLVM support, so the conversions are mapped onto the dedicated xevm.extf /
1664// xevm.truncf ops which lower to hardware builtins. The f8E8M0FNU scale type is
1665// intentionally not handled here: it is expanded into integer arithmetic by
1666// arith-expand before this pass runs.
1667
1668// xevm.extf / xevm.truncf only convert between the MX narrow floats and
1669// f16/bf16, and the underlying builtins operate on exactly 16 f16/bf16 values.
1670static constexpr int64_t kXeVMExtfTruncfNumElems = 16;
1671
1672// Maps a narrow MX float element type to the matching xevm.extf source enum.
1673static std::optional<xevm::ExtfSrcElemTypes> getExtfNarrowType(Type etype) {
1674 if (isa<Float8E5M2Type>(etype))
1675 return xevm::ExtfSrcElemTypes::BF8;
1676 if (isa<Float8E4M3FNType>(etype))
1677 return xevm::ExtfSrcElemTypes::F8;
1678 if (isa<Float4E2M1FNType>(etype))
1679 return xevm::ExtfSrcElemTypes::E2M1;
1680 return std::nullopt;
1681}
1682
1683// Maps a narrow MX float element type to the matching xevm.truncf dest enum.
1684static std::optional<xevm::TruncfDstElemTypes> getTruncfNarrowType(Type etype) {
1685 if (isa<Float8E5M2Type>(etype))
1686 return xevm::TruncfDstElemTypes::BF8;
1687 if (isa<Float8E4M3FNType>(etype))
1688 return xevm::TruncfDstElemTypes::F8;
1689 if (isa<Float4E2M1FNType>(etype))
1690 return xevm::TruncfDstElemTypes::E2M1;
1691 return std::nullopt;
1692}
1693
1694// Returns true if `op` is an arith.extf that can be lowered to xevm.extf, i.e.
1695// a rank-1 widening from an MX narrow float to a 16-element f16/bf16 vector.
1696static bool isXeVMExtf(arith::ExtFOp op) {
1697 auto srcTy = dyn_cast<VectorType>(op.getIn().getType());
1698 auto dstTy = dyn_cast<VectorType>(op.getType());
1699 if (!srcTy || !dstTy || srcTy.getRank() != 1 || dstTy.getRank() != 1)
1700 return false;
1701 if (dstTy.getNumElements() != kXeVMExtfTruncfNumElems)
1702 return false;
1703 Type dstETy = dstTy.getElementType();
1704 if (!dstETy.isF16() && !dstETy.isBF16())
1705 return false;
1706 return getExtfNarrowType(srcTy.getElementType()).has_value();
1707}
1708
1709// Returns true if `op` is an arith.truncf that can be lowered to xevm.truncf,
1710// i.e. a rank-1 truncation from an f16/bf16 vector to an MX narrow float. The
1711// source has to hold a whole number of the fixed-size groups xevm.truncf
1712// converts at a time; wider vectors are converted in several steps.
1713static bool isXeVMTruncf(arith::TruncFOp op) {
1714 auto srcTy = dyn_cast<VectorType>(op.getIn().getType());
1715 auto dstTy = dyn_cast<VectorType>(op.getType());
1716 if (!srcTy || !dstTy || srcTy.getRank() != 1 || dstTy.getRank() != 1)
1717 return false;
1718 int64_t numElems = srcTy.getNumElements();
1719 if (numElems == 0 || numElems % kXeVMExtfTruncfNumElems != 0)
1720 return false;
1721 Type srcETy = srcTy.getElementType();
1722 if (!srcETy.isF16() && !srcETy.isBF16())
1723 return false;
1724 return getTruncfNarrowType(dstTy.getElementType()).has_value();
1725}
1726
1727class ExtfToXeVMPattern : public OpConversionPattern<arith::ExtFOp> {
1728 using Base::Base;
1729 LogicalResult
1730 matchAndRewrite(arith::ExtFOp op, OpAdaptor adaptor,
1731 ConversionPatternRewriter &rewriter) const override {
1732 if (!isXeVMExtf(op))
1733 return rewriter.notifyMatchFailure(op, "not a xevm.extf compatible extf");
1734 Location loc = op.getLoc();
1735 MLIRContext *ctx = op.getContext();
1736 auto srcVecTy = cast<VectorType>(op.getIn().getType());
1737 auto dstVecTy = cast<VectorType>(op.getType());
1738 xevm::ExtfSrcElemTypes srcEnum =
1739 *getExtfNarrowType(srcVecTy.getElementType());
1740 xevm::ExtfDstElemTypes dstEnum = dstVecTy.getElementType().isF16()
1741 ? xevm::ExtfDstElemTypes::F16
1742 : xevm::ExtfDstElemTypes::BF16;
1743 // The narrow float operand has already been type-converted to an integer
1744 // vector of the same bit width (i4 for fp4, i8 for fp8). xevm.extf takes
1745 // the values packed into an i8 vector, so re-pack fp4 (i4) operands.
1746 Value src = adaptor.getIn();
1747 auto convSrcTy = cast<VectorType>(src.getType());
1748 if (convSrcTy.getElementTypeBitWidth() == 4)
1749 src = vector::BitCastOp::create(
1750 rewriter, loc,
1751 VectorType::get(convSrcTy.getNumElements() / 2, rewriter.getI8Type()),
1752 src);
1753 Type resTy = getTypeConverter()->convertType(dstVecTy);
1754 Value res = xevm::ExtfOp::create(
1755 rewriter, loc, resTy, src, xevm::ExtfSrcElemTypeAttr::get(ctx, srcEnum),
1756 xevm::ExtfDstElemTypeAttr::get(ctx, dstEnum));
1757 rewriter.replaceOp(op, res);
1758 return success();
1759 }
1760};
1761
1762class TruncfToXeVMPattern : public OpConversionPattern<arith::TruncFOp> {
1763 using Base::Base;
1764 LogicalResult
1765 matchAndRewrite(arith::TruncFOp op, OpAdaptor adaptor,
1766 ConversionPatternRewriter &rewriter) const override {
1767 if (!isXeVMTruncf(op))
1768 return rewriter.notifyMatchFailure(op,
1769 "not a xevm.truncf compatible truncf");
1770 Location loc = op.getLoc();
1771 MLIRContext *ctx = op.getContext();
1772 auto srcVecTy = cast<VectorType>(op.getIn().getType());
1773 auto dstVecTy = cast<VectorType>(op.getType());
1774 xevm::TruncfSrcElemTypes srcEnum = srcVecTy.getElementType().isF16()
1775 ? xevm::TruncfSrcElemTypes::F16
1776 : xevm::TruncfSrcElemTypes::BF16;
1777 xevm::TruncfDstElemTypes dstEnum =
1778 *getTruncfNarrowType(dstVecTy.getElementType());
1779 auto srcEnumAttr = xevm::TruncfSrcElemTypeAttr::get(ctx, srcEnum);
1780 auto dstEnumAttr = xevm::TruncfDstElemTypeAttr::get(ctx, dstEnum);
1781
1782 // xevm.truncf lowers to instructions that convert a fixed number of
1783 // elements at a time, so a wider source is converted one group at a time
1784 // and the packed results are concatenated. Each group produces the narrow
1785 // floats packed into an i8 vector.
1786 int64_t numGroups = srcVecTy.getNumElements() / kXeVMExtfTruncfNumElems;
1787 int64_t groupBytes =
1788 kXeVMExtfTruncfNumElems * dstVecTy.getElementTypeBitWidth() / 8;
1789 Type groupTy = VectorType::get(groupBytes, rewriter.getI8Type());
1790
1791 Value src = adaptor.getIn();
1792 Value packed;
1793 if (numGroups == 1) {
1794 packed = xevm::TruncfOp::create(rewriter, loc, groupTy, src, srcEnumAttr,
1795 dstEnumAttr);
1796 } else {
1797 auto packedTy =
1798 VectorType::get(groupBytes * numGroups, rewriter.getI8Type());
1799 packed = arith::ConstantOp::create(rewriter, loc, packedTy,
1800 rewriter.getZeroAttr(packedTy));
1801 for (int64_t group = 0; group < numGroups; group++) {
1802 Value slice = vector::ExtractStridedSliceOp::create(
1803 rewriter, loc, src, group * kXeVMExtfTruncfNumElems,
1804 kXeVMExtfTruncfNumElems, /*strides=*/1);
1805 Value converted = xevm::TruncfOp::create(rewriter, loc, groupTy, slice,
1806 srcEnumAttr, dstEnumAttr);
1807 packed = vector::InsertStridedSliceOp::create(
1808 rewriter, loc, converted, packed, group * groupBytes,
1809 /*strides=*/1);
1810 }
1811 }
1812 // Re-shape to the type-converted result type (i4 vector for fp4).
1813 Type resTy = getTypeConverter()->convertType(dstVecTy);
1814 if (packed.getType() != resTy)
1815 packed = vector::BitCastOp::create(rewriter, loc, resTy, packed);
1816 rewriter.replaceOp(op, packed);
1817 return success();
1818 }
1819};
1820
1821// Hoist the i4-to-i8 part of compatible extensions through a closed
1822// data-movement graph and lane shuffle. Widening all the way to the extension's
1823// result type could exceed the bitcast-shuffle lowering's 64-bit fragment
1824// limit. Signed widening preserves the eventual signed interpretation of each
1825// nibble; unsigned widening preserves its unsigned interpretation.
1826template <typename ExtOp>
1827class HoistI4ExtensionAboveLaneShuffle final : public OpRewritePattern<ExtOp> {
1828public:
1829 using OpRewritePattern<ExtOp>::OpRewritePattern;
1830
1831 LogicalResult matchAndRewrite(ExtOp op,
1832 PatternRewriter &rewriter) const override {
1833 auto srcTy = dyn_cast<VectorType>(op.getIn().getType());
1834 auto dstTy = dyn_cast<VectorType>(op.getType());
1835 if (!srcTy || !srcTy.getElementType().isInteger(4) || !dstTy ||
1836 dstTy.getElementTypeBitWidth() < 8)
1837 return failure();
1838
1839 auto isMover = [](Operation *def) {
1840 return isa<vector::ShapeCastOp, vector::ExtractStridedSliceOp,
1841 vector::ExtractOp, vector::BroadcastOp, vector::TransposeOp>(
1842 def);
1843 };
1844 Value source = op.getIn();
1845 while (Operation *def = source.getDefiningOp()) {
1846 if (isa<xegpu::LaneShuffleOp>(def))
1847 break;
1848 if (!isMover(def))
1849 return failure();
1850 source = def->getOperand(0);
1851 }
1852 auto shuffle = source.getDefiningOp<xegpu::LaneShuffleOp>();
1853 if (!shuffle)
1854 return failure();
1855 VectorType fragmentTy = shuffle.getSourceType();
1856 int64_t numElements = fragmentTy.getNumElements();
1857 if (!fragmentTy.getElementType().isInteger(4) || numElements < 2 ||
1858 numElements > 8 || !llvm::isPowerOf2_64(numElements))
1859 return failure();
1860
1861 // Validate every use before changing IR. Widening a shared fragment is
1862 // safe only when all paths end in extensions with the same signedness.
1863 SmallVector<Operation *> movers;
1864 SmallVector<ExtOp> extensions;
1865 SmallPtrSet<Operation *, 16> visited;
1866 auto visit = [&](auto &&visit, Value value) -> LogicalResult {
1867 for (Operation *user : value.getUsers()) {
1868 if (!visited.insert(user).second)
1869 continue;
1870 if (auto ext = dyn_cast<ExtOp>(user)) {
1871 auto type = dyn_cast<VectorType>(ext.getType());
1872 if (!type || type.getElementTypeBitWidth() < 8)
1873 return failure();
1874 extensions.push_back(ext);
1875 continue;
1876 }
1877 if (!isMover(user) || user->getOperand(0) != value ||
1878 user->getNumResults() != 1 ||
1879 failed(visit(visit, user->getResult(0))))
1880 return failure();
1881 movers.push_back(user);
1882 }
1883 return success();
1884 };
1885 if (failed(visit(visit, shuffle.getResult())))
1886 return failure();
1887
1888 rewriter.setInsertionPoint(shuffle);
1889 VectorType wideTy = fragmentTy.clone(rewriter.getI8Type());
1890 Value wide =
1891 ExtOp::create(rewriter, op.getLoc(), wideTy, shuffle.getSource());
1892 Value result = xegpu::LaneShuffleOp::create(
1893 rewriter, shuffle.getLoc(), wideTy, wide, shuffle.getMode());
1894 IRMapping mapping;
1895 mapping.map(shuffle.getResult(), result);
1896 for (Operation *mover : llvm::reverse(movers)) {
1897 rewriter.setInsertionPoint(mover);
1898 Operation *clone = rewriter.clone(*mover, mapping);
1899 Type type = rewriter.getI8Type();
1900 if (auto vectorTy = dyn_cast<VectorType>(mover->getResult(0).getType()))
1901 type = vectorTy.clone(type);
1902 clone->getResult(0).setType(type);
1903 }
1904 for (ExtOp ext : extensions) {
1905 Value input = mapping.lookup(ext.getIn());
1906 if (input.getType() == ext.getType())
1907 rewriter.replaceOp(ext, input);
1908 else
1909 rewriter.modifyOpInPlace(ext,
1910 [&] { ext.getInMutable().assign(input); });
1911 }
1912 for (Operation *mover : movers)
1913 rewriter.eraseOp(mover);
1914 rewriter.eraseOp(shuffle);
1915 return success();
1916 }
1917};
1918
1919// Moves an `xegpu.lane_shuffle` above the integer extension that feeds it, so
1920// that the shuffle moves the narrow value. The shuffle only moves elements
1921// between lanes, and the extension acts on each element, so
1922// shuffle(ext(x)) == ext(shuffle(x)). A narrow fragment (the vector that one
1923// lane holds) has fewer bits. A wide one can be over the 64-bit limit of the
1924// lowering below: four i4 values extended to i32 are 128 bits.
1925//
1926// - The lowering takes only 8, 16, 32 or 64-bit elements. So the narrow
1927// width is the source width rounded up to a power of two, and at least 8.
1928// If that is not narrower than the result, the pattern does nothing. If the
1929// source is narrower than it, the source is first extended to it.
1930// - Data-movement ops between the extension and the shuffle are rebuilt on
1931// the narrow value.
1932// - The extension and each of those ops must have one use.
1933//
1934// HoistI4ExtensionAboveLaneShuffle handles the other order: an i4 extension
1935// after a shuffle. It moves only the i4-to-i8 step above the shuffle.
1936template <typename ExtOp>
1937class ShuffleBeforeExtension final : public OpRewritePattern<ExtOp> {
1938public:
1939 using OpRewritePattern<ExtOp>::OpRewritePattern;
1940
1941 LogicalResult matchAndRewrite(ExtOp ext,
1942 PatternRewriter &rewriter) const override {
1943 auto srcTy = dyn_cast<VectorType>(ext.getIn().getType());
1944 auto dstTy = dyn_cast<VectorType>(ext.getType());
1945 if (!srcTy || !dstTy || !srcTy.getElementType().isInteger())
1946 return failure();
1947 unsigned narrowBits = std::max<unsigned>(
1948 llvm::PowerOf2Ceil(srcTy.getElementTypeBitWidth()), 8);
1949 if (narrowBits >= dstTy.getElementTypeBitWidth())
1950 return failure();
1951
1952 auto isMover = [](Operation *op) {
1953 return isa<vector::ShapeCastOp, vector::ExtractStridedSliceOp,
1954 vector::ExtractOp, vector::BroadcastOp, vector::TransposeOp>(
1955 op);
1956 };
1957 // Walk forward from the extension through single-use movers to a shuffle.
1958 SmallVector<Operation *> movers;
1959 Value value = ext.getResult();
1960 xegpu::LaneShuffleOp shuffle;
1961 while (value.hasOneUse()) {
1962 Operation *user = *value.getUsers().begin();
1963 if ((shuffle = dyn_cast<xegpu::LaneShuffleOp>(user)))
1964 break;
1965 if (!isMover(user) || user->getNumResults() != 1 ||
1966 user->getOperand(0) != value)
1967 return failure();
1968 movers.push_back(user);
1969 value = user->getResult(0);
1970 }
1971 if (!shuffle)
1972 return failure();
1973
1974 Type narrowElemTy = rewriter.getIntegerType(narrowBits);
1975 Value narrow = ext.getIn();
1976 rewriter.setInsertionPoint(ext);
1977 if (srcTy.getElementTypeBitWidth() < narrowBits)
1978 narrow = ExtOp::create(rewriter, ext.getLoc(), srcTy.clone(narrowElemTy),
1979 narrow);
1980 IRMapping mapping;
1981 mapping.map(ext.getResult(), narrow);
1982 for (Operation *mover : movers) {
1983 rewriter.setInsertionPoint(mover);
1984 Operation *clone = rewriter.clone(*mover, mapping);
1985 auto vectorTy = dyn_cast<VectorType>(mover->getResult(0).getType());
1986 clone->getResult(0).setType(vectorTy ? Type(vectorTy.clone(narrowElemTy))
1987 : narrowElemTy);
1988 }
1989 Value narrowSrc = mapping.lookup(shuffle.getSource());
1990 rewriter.setInsertionPoint(shuffle);
1991 Value moved = xegpu::LaneShuffleOp::create(rewriter, shuffle.getLoc(),
1992 narrowSrc.getType(), narrowSrc,
1993 shuffle.getMode());
1994 rewriter.replaceOpWithNewOp<ExtOp>(shuffle, shuffle.getType(), moved);
1995 for (Operation *mover : llvm::reverse(movers))
1996 rewriter.eraseOp(mover);
1997 rewriter.eraseOp(ext);
1998 return success();
1999 }
2000};
2001
2002// Lowers `xegpu.lane_shuffle` to `xevm.bitcast_shuffle`.
2003//
2004// `xevm.bitcast_shuffle` concatenates the components of its operand across the
2005// subgroup, the first component of every lane first, and then hands chunks the
2006// size of a result component back out to the lanes in order. Numbering the
2007// elements of a `vector<NxT>` fragment held by lane `i` of a subgroup of size
2008// `S` by their logical position, that concatenation is exactly the `pack` mode
2009// input numbering `j * S + i`. Taking the result as a single `N * width(T)` bit
2010// scalar then hands lane `i` the logical positions `i * N .. i * N + N - 1`,
2011// which is the `pack` mode output numbering.
2012//
2013// So `pack` is a vector-to-scalar `xevm.bitcast_shuffle` followed by a bitcast
2014// back to the fragment type, and `unpack`, being its inverse, is a bitcast to
2015// the scalar followed by a scalar-to-vector `xevm.bitcast_shuffle`.
2016//
2017// `xevm.bitcast_shuffle` only accepts the integer types `i8`, `i16`, `i32` and
2018// `i64`, since it is bit-preserving and so does not depend on how the bits are
2019// interpreted. A fragment of a floating point type is therefore bitcast to a
2020// same-width integer vector on the way in and back on the way out.
2021class LaneShuffleToXeVMPattern
2022 : public OpConversionPattern<xegpu::LaneShuffleOp> {
2023 using Base::Base;
2024 LogicalResult
2025 matchAndRewrite(xegpu::LaneShuffleOp op, OpAdaptor adaptor,
2026 ConversionPatternRewriter &rewriter) const override {
2027 auto vecTy = dyn_cast<VectorType>(adaptor.getSource().getType());
2028 if (!vecTy)
2029 return rewriter.notifyMatchFailure(op, "Expected a vector fragment.");
2030 // The shuffle redistributes whole bytes between the lanes, so sub-byte
2031 // element types, fp4 in particular, cannot be shuffled. Widths without a
2032 // matching integer type the op accepts are rejected for the same reason.
2033 unsigned elemBits = vecTy.getElementTypeBitWidth();
2034 if (elemBits != 8 && elemBits != 16 && elemBits != 32 && elemBits != 64)
2035 return rewriter.notifyMatchFailure(
2036 op, "Expected an element type of 8, 16, 32 or 64 bits.");
2037 int64_t fragmentBits = vecTy.getNumElements() * elemBits;
2038 if (fragmentBits > 64 || !llvm::isPowerOf2_64(fragmentBits))
2039 return rewriter.notifyMatchFailure(
2040 op, "Expected a fragment of 8, 16, 32 or 64 bits.");
2041
2042 Location loc = op.getLoc();
2043 Type packedTy = rewriter.getIntegerType(fragmentBits);
2044 // The integer vector type the shuffle actually operates on. Equal to the
2045 // fragment type when that is already an integer vector.
2046 VectorType shuffleTy =
2047 VectorType::get(vecTy.getShape(), rewriter.getIntegerType(elemBits));
2048
2049 Value res;
2050 if (op.getMode() == xegpu::LaneShuffleMode::Pack) {
2051 Value src = adaptor.getSource();
2052 if (shuffleTy != vecTy)
2053 src = LLVM::BitcastOp::create(rewriter, loc, shuffleTy, src);
2054 res = xevm::BitcastShuffleOp::create(rewriter, loc, packedTy, src);
2055 res = LLVM::BitcastOp::create(rewriter, loc, vecTy, res);
2056 } else {
2057 Value packed =
2058 LLVM::BitcastOp::create(rewriter, loc, packedTy, adaptor.getSource());
2059 res = xevm::BitcastShuffleOp::create(rewriter, loc, shuffleTy, packed);
2060 if (shuffleTy != vecTy)
2061 res = LLVM::BitcastOp::create(rewriter, loc, vecTy, res);
2062 }
2063 rewriter.replaceOp(op, res);
2064 return success();
2065 }
2066};
2067
2068//===----------------------------------------------------------------------===//
2069// Pass Definition
2070//===----------------------------------------------------------------------===//
2071
2072struct ConvertXeGPUToXeVMPass
2073 : public impl::ConvertXeGPUToXeVMPassBase<ConvertXeGPUToXeVMPass> {
2074 using Base::Base;
2075
2076 void runOnOperation() override {
2077 MLIRContext *context = &getContext();
2078
2079 // Restrict greedy rewriting to casts and newly generated operations so
2080 // unrelated dead XeGPU ops still receive conversion diagnostics.
2081 GreedyRewriteConfig config;
2082 config.setStrictness(GreedyRewriteStrictness::ExistingAndNewOps);
2083 // Preserve extension consumers until hoisting has finished: narrow-type
2084 // legalization would otherwise obscure the chains this peephole matches.
2085 {
2086 RewritePatternSet patterns(context);
2087 patterns.add<HoistI4ExtensionAboveLaneShuffle<arith::ExtUIOp>,
2088 HoistI4ExtensionAboveLaneShuffle<arith::ExtSIOp>,
2089 ShuffleBeforeExtension<arith::ExtUIOp>,
2090 ShuffleBeforeExtension<arith::ExtSIOp>>(context);
2091 SmallVector<Operation *> extensions;
2092 getOperation()->walk([&](Operation *op) {
2093 if (isa<arith::ExtUIOp, arith::ExtSIOp>(op))
2094 extensions.push_back(op);
2095 });
2096 if (failed(
2097 applyOpPatternsGreedily(extensions, std::move(patterns), config)))
2098 return signalPassFailure();
2099 }
2100 // Expand the new i4-to-i8 casts into byte-level masks and shifts before
2101 // lowering. LLVM extensions on i4 vectors are not legal in the backend.
2102 {
2103 RewritePatternSet patterns(context);
2104 vector::populateVectorNarrowTypeRewritePatterns(patterns);
2107 SmallVector<Operation *> casts;
2108 getOperation()->walk([&](Operation *op) {
2109 if (isa<arith::ExtUIOp, arith::ExtSIOp, arith::TruncIOp,
2110 vector::BitCastOp>(op))
2111 casts.push_back(op);
2112 });
2113 if (failed(applyOpPatternsGreedily(casts, std::move(patterns), config)))
2114 return signalPassFailure();
2115 }
2116
2117 // XeVM type converter is based on LLVM type converter with the
2118 // following customizations.
2119 // First, type conversion rules are added for xegpu custom types,
2120 // TensorDescType and MemDescType.
2121 // Second, MemRefType is lowered to single integer type
2122 // Third, VectorType of single element or 0D is converted to vector
2123 // element type. Otherwise, vector type is flatten to 1D.
2124 LowerToLLVMOptions options(context);
2125 options.overrideIndexBitwidth(this->use64bitIndex ? 64 : 32);
2126 LLVMTypeConverter typeConverter(context, options);
2127
2128 Type xevmIndexType = typeConverter.convertType(IndexType::get(context));
2129 Type i32Type = IntegerType::get(context, 32);
2130 typeConverter.addConversion([&](VectorType type) -> Type {
2131 auto elemType = typeConverter.convertType(type.getElementType());
2132 // If the vector rank is 0 or has a single element, return the element
2133 unsigned rank = type.getRank();
2134 if (rank == 0 || type.getNumElements() == 1)
2135 return elemType;
2136 // Otherwise, convert the vector to a flat vector type.
2137 int64_t sum = llvm::product_of(type.getShape());
2138 return VectorType::get(sum, elemType);
2139 });
2140 typeConverter.addConversion([&](xegpu::TensorDescType type) -> Type {
2141 if (type.getRank() == 1)
2142 return xevmIndexType;
2143 return VectorType::get(8, i32Type);
2144 });
2145 // SLM access related type conversions.
2146 // TODO: LLVM DLTI provides clean way of representing different pointer size
2147 // based on address space. Currently pointer size of SLM access is hard
2148 // coded to 32bit. Update to use DLTI when switching overall XeGPU lowering
2149 // to use DLTI instead of use64bitIndex option used above.
2150
2151 // Convert MemDescType into i32 for SLM
2152 typeConverter.addConversion(
2153 [&](xegpu::MemDescType type) -> Type { return i32Type; });
2154
2155 typeConverter.addConversion([&](MemRefType type) -> Type {
2156 return isSharedMemRef(type) ? i32Type : xevmIndexType;
2157 });
2158
2159 // LLVM type converter puts unrealized casts for the following cases:
2160 // add materialization casts to handle them.
2161
2162 // Materialization to convert memref to i64 or i32 depending on global/SLM
2163 // Applies only to target materialization.
2164 // Note: int type to memref materialization is not required as xegpu ops
2165 // currently do not produce memrefs as result.
2166 auto memrefToIntMaterializationCast = [](OpBuilder &builder, Type type,
2167 ValueRange inputs,
2168 Location loc) -> Value {
2169 if (inputs.size() != 1)
2170 return {};
2171 auto input = inputs.front();
2172 if (auto memrefTy = dyn_cast<MemRefType>(input.getType())) {
2173 unsigned rank = memrefTy.getRank();
2174 Type indexType = builder.getIndexType();
2175
2176 int64_t intOffsets;
2177 SmallVector<int64_t> intStrides;
2178 Value addr;
2179 Value offset;
2180 if (succeeded(memrefTy.getStridesAndOffset(intStrides, intOffsets)) &&
2181 ShapedType::isStatic(intOffsets)) {
2182 addr = memref::ExtractAlignedPointerAsIndexOp::create(builder, loc,
2183 input);
2184 offset = arith::ConstantOp::create(builder, loc,
2185 builder.getIndexAttr(intOffsets));
2186 } else {
2187
2188 // Result types: [base_memref, offset, stride0, stride1, ...,
2189 // strideN-1, size0, size1, ..., sizeN-1]
2190 SmallVector<Type> resultTypes{
2191 MemRefType::get({}, memrefTy.getElementType(),
2192 MemRefLayoutAttrInterface(),
2193 memrefTy.getMemorySpace()),
2194 indexType};
2195 // strides + sizes
2196 resultTypes.append(2 * rank, indexType);
2197
2198 auto meta = memref::ExtractStridedMetadataOp::create(
2199 builder, loc, resultTypes, input);
2200
2201 addr = memref::ExtractAlignedPointerAsIndexOp::create(
2202 builder, loc, meta.getBaseBuffer());
2203 offset = meta.getOffset();
2204 }
2205
2206 auto addrCasted =
2207 arith::IndexCastUIOp::create(builder, loc, type, addr);
2208 auto offsetCasted =
2209 arith::IndexCastUIOp::create(builder, loc, type, offset);
2210
2211 // Compute the final address: base address + byte offset.
2212 return xegpu::addElemOffsetInBytes(builder, loc, addrCasted,
2213 offsetCasted, memrefTy);
2214 }
2215 return {};
2216 };
2217
2218 // Materialization to convert ui64 to i64
2219 // Applies only to target materialization.
2220 // Note: i64 to ui64 materialization is not required as xegpu ops
2221 // currently do not produce ui64 as result.
2222 auto ui64ToI64MaterializationCast = [](OpBuilder &builder, Type type,
2223 ValueRange inputs,
2224 Location loc) -> Value {
2225 if (inputs.size() != 1)
2226 return {};
2227 auto input = inputs.front();
2228 if (input.getType() == builder.getIntegerType(64, false)) {
2229 Value cast =
2230 index::CastUOp::create(builder, loc, builder.getIndexType(), input)
2231 .getResult();
2232 return arith::IndexCastUIOp::create(builder, loc, type, cast)
2233 .getResult();
2234 }
2235 return {};
2236 };
2237
2238 // Materialization to convert ui32 to i32
2239 // Applies only to target materialization.
2240 // Note: i32 to ui32 materialization is not required as xegpu ops
2241 // currently do not produce ui32 as result.
2242 auto ui32ToI32MaterializationCast = [](OpBuilder &builder, Type type,
2243 ValueRange inputs,
2244 Location loc) -> Value {
2245 if (inputs.size() != 1)
2246 return {};
2247 auto input = inputs.front();
2248 if (input.getType() == builder.getIntegerType(32, false)) {
2249 Value cast =
2250 index::CastUOp::create(builder, loc, builder.getIndexType(), input)
2251 .getResult();
2252 return arith::IndexCastUIOp::create(builder, loc, type, cast)
2253 .getResult();
2254 }
2255 return {};
2256 };
2257
2258 // Materialization to convert between vector types
2259 // - Add shape cast for different shapes
2260 // - Add bitcast for different element types
2261 // Applies to both source and target materialization.
2262 auto vectorToVectorMaterializationCast = [](OpBuilder &builder, Type type,
2263 ValueRange inputs,
2264 Location loc) -> Value {
2265 if (inputs.size() != 1)
2266 return {};
2267 auto input = inputs.front();
2268 if (auto vecTy = dyn_cast<VectorType>(input.getType())) {
2269 if (auto targetVecTy = dyn_cast<VectorType>(type)) {
2270 Value cast = input;
2271 // If the target type has a different shape, add a shape cast
2272 // If the target type has a different element type, add a bitcast
2273 if (targetVecTy.getShape() != vecTy.getShape()) {
2274 cast = vector::ShapeCastOp::create(
2275 builder, loc,
2276 VectorType::get(targetVecTy.getShape(),
2277 vecTy.getElementType()),
2278 cast)
2279 .getResult();
2280 }
2281 if (targetVecTy.getElementType() != vecTy.getElementType()) {
2282 cast = vector::BitCastOp::create(builder, loc, targetVecTy, cast)
2283 .getResult();
2284 }
2285 return cast;
2286 }
2287 }
2288 return {};
2289 };
2290
2291 // Materialization to convert
2292 // - single element vector to single element of vector element type
2293 // Applies only to target materialization.
2294 auto vectorToSingleElementMaterializationCast =
2295 [](OpBuilder &builder, Type type, ValueRange inputs,
2296 Location loc) -> Value {
2297 if (inputs.size() != 1)
2298 return {};
2299 auto input = inputs.front();
2300 if (auto vecTy = dyn_cast<VectorType>(input.getType())) {
2301 // Source needs to be single element vector
2302 auto rank = vecTy.getRank();
2303 if (rank != 0 && vecTy.getNumElements() != 1)
2304 return {};
2305 auto inElemTy = vecTy.getElementType();
2306 // extract scalar
2307 Value cast = input;
2308 if (rank == 0) {
2309 cast = vector::ExtractOp::create(builder, loc, cast, {}).getResult();
2310 } else {
2311 cast = vector::ExtractOp::create(builder, loc, cast,
2312 SmallVector<int64_t>(rank, 0))
2313 .getResult();
2314 }
2315 // Extracted element type may need conversion
2316 // Two cases
2317 // 1. Index type to integer type
2318 // 2. Other element type mismatch
2319 if (inElemTy.isIndex()) {
2320 cast = arith::IndexCastUIOp::create(builder, loc, type, cast)
2321 .getResult();
2322 } else if (inElemTy != type) {
2323 cast = arith::BitcastOp::create(builder, loc, type, cast).getResult();
2324 }
2325 return cast;
2326 }
2327 return {};
2328 };
2329
2330 // Materialization to convert
2331 // - single element of vector element type to single element vector
2332 // If result type of original op is single element vector and lowered type
2333 // is scalar. This materialization cast creates a single element vector by
2334 // First convert element type if needed and then broadcast to single
2335 // element vector.
2336 // Applies only to source materialization.
2337 auto singleElementToVectorMaterializationCast =
2338 [](OpBuilder &builder, Type type, ValueRange inputs,
2339 Location loc) -> Value {
2340 if (inputs.size() != 1)
2341 return {};
2342 auto input = inputs.front();
2343 auto inTy = input.getType();
2344 if (!inTy.isIntOrFloat())
2345 return {};
2346 // If the target type is a vector of rank 0 or single element vector
2347 // of element type matching input type, broadcast input to target type.
2348 if (auto vecTy = dyn_cast<VectorType>(type)) {
2349 if (vecTy.getRank() != 0 && vecTy.getNumElements() != 1)
2350 return {};
2351 auto outElemTy = vecTy.getElementType();
2352 Value cast = input;
2353 if (outElemTy.isIndex()) {
2354 cast = arith::IndexCastUIOp::create(builder, loc,
2355 builder.getIndexType(), cast)
2356 .getResult();
2357 } else if (inTy != outElemTy) {
2358 cast = arith::BitcastOp::create(builder, loc, outElemTy, cast)
2359 .getResult();
2360 }
2361 return vector::BroadcastOp::create(builder, loc, vecTy, cast)
2362 .getResult();
2363 }
2364 return {};
2365 };
2366 typeConverter.addSourceMaterialization(
2367 singleElementToVectorMaterializationCast);
2368 typeConverter.addSourceMaterialization(vectorToVectorMaterializationCast);
2369 typeConverter.addTargetMaterialization(memrefToIntMaterializationCast);
2370 typeConverter.addTargetMaterialization(ui32ToI32MaterializationCast);
2371 typeConverter.addTargetMaterialization(ui64ToI64MaterializationCast);
2372 typeConverter.addTargetMaterialization(
2373 vectorToSingleElementMaterializationCast);
2374 typeConverter.addTargetMaterialization(vectorToVectorMaterializationCast);
2375 ConversionTarget target(*context);
2376 target.addLegalDialect<xevm::XeVMDialect, LLVM::LLVMDialect,
2377 vector::VectorDialect, arith::ArithDialect,
2378 memref::MemRefDialect, gpu::GPUDialect,
2379 index::IndexDialect>();
2380 target.addIllegalDialect<xegpu::XeGPUDialect>();
2381 // arith.extf/arith.truncf between MX narrow floats and f16/bf16 are routed
2382 // to xevm.extf/xevm.truncf; all other arith float casts stay legal.
2383 target.addDynamicallyLegalOp<arith::ExtFOp>(
2384 [](arith::ExtFOp op) { return !isXeVMExtf(op); });
2385 target.addDynamicallyLegalOp<arith::TruncFOp>(
2386 [](arith::TruncFOp op) { return !isXeVMTruncf(op); });
2387
2388 RewritePatternSet patterns(context);
2389 populateXeGPUToXeVMConversionPatterns(typeConverter, patterns);
2391 patterns, target);
2392 if (failed(applyPartialConversion(getOperation(), target,
2393 std::move(patterns))))
2394 signalPassFailure();
2395 }
2396};
2397} // namespace
2398
2399//===----------------------------------------------------------------------===//
2400// Pattern Population
2401//===----------------------------------------------------------------------===//
2403 const LLVMTypeConverter &typeConverter, RewritePatternSet &patterns) {
2404 patterns.add<CreateNdDescToXeVMPattern,
2405 LoadStorePrefetchNdToXeVMPattern<xegpu::LoadNdOp>,
2406 LoadStorePrefetchNdToXeVMPattern<xegpu::StoreNdOp>,
2407 LoadStorePrefetchNdToXeVMPattern<xegpu::PrefetchNdOp>>(
2408 typeConverter, patterns.getContext());
2409 patterns.add<AtomicRMWToXeVMPattern, PrefetchToXeVMPattern,
2410 LoadStoreToXeVMPattern<xegpu::LoadGatherOp>,
2411 LoadStoreToXeVMPattern<xegpu::StoreScatterOp>>(
2412 typeConverter, patterns.getContext());
2413 patterns.add<LoadStoreMatrixToXeVMPattern<xegpu::LoadMatrixOp>,
2414 LoadStoreMatrixToXeVMPattern<xegpu::StoreMatrixOp>,
2415 CreateMemDescOpPattern>(typeConverter, patterns.getContext());
2416 patterns.add<FenceToXeVMPattern, DpasToXeVMPattern>(typeConverter,
2417 patterns.getContext());
2418 patterns.add<DpasMxToXeVMPattern>(typeConverter, patterns.getContext());
2419 patterns.add<ExtfToXeVMPattern, TruncfToXeVMPattern>(typeConverter,
2420 patterns.getContext());
2421 patterns.add<LaneShuffleToXeVMPattern>(typeConverter, patterns.getContext());
2422}
return success()
static void visit(Operation *op, DenseSet< Operation * > &visited)
Visits all the pdl.operand(s), pdl.result(s), and pdl.operation(s) connected to the given operation.
Definition PDL.cpp:49
b getContext())
auto load
static llvm::ManagedStatic< PassManagerOptions > options
static Value broadcast(Location loc, Value toBroadcast, unsigned numElements, const TypeConverter &typeConverter, ConversionPatternRewriter &rewriter)
Broadcasts the value to vector with numElements number of elements.
static SmallVector< Value > slice(OpBuilder &b, Location loc, Value value, VectorType nativeType)
Attributes are known-constant values of operations.
Definition Attributes.h:25
IntegerAttr getIndexAttr(int64_t value)
Definition Builders.cpp:116
IntegerType getIntegerType(unsigned width)
Definition Builders.cpp:75
IndexType getIndexType()
Definition Builders.cpp:59
IntegerType getI8Type()
Definition Builders.cpp:67
An attribute that represents a reference to a dense vector or tensor object.
bool isSplat() const
Returns true if this attribute corresponds to a splat, i.e.
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
GreedyRewriteConfig & setStrictness(GreedyRewriteStrictness mode)
auto lookup(T from) const
Lookup a mapped value within the map.
Definition IRMapping.h:72
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
Definition IRMapping.h:30
Conversion from types to the LLVM IR dialect.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext * getContext() const
Return the context this location is uniqued in.
Definition Location.h:86
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
Definition Builders.cpp:581
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:401
Value getOperand(unsigned idx)
Definition Operation.h:377
void setDiscardableAttr(StringAttr name, Attribute value)
Set a discardable attribute by name.
Definition Operation.h:514
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:434
unsigned getNumResults()
Return the number of results held by this operation.
Definition Operation.h:431
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition Types.cpp:118
bool isF16() const
Definition Types.cpp:38
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:124
bool isBF16() const
Definition Types.cpp:37
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
void setType(Type newType)
Mutate the type of this Value to be of the specified type.
Definition Value.h:116
Type getType() const
Return the type of this value.
Definition Value.h:105
user_range getUsers() const
Definition Value.h:218
bool hasOneUse() const
Returns true if this value has exactly one use.
Definition Value.h:197
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Definition ArithOps.cpp:453
static ConstantIntOp create(OpBuilder &builder, Location location, int64_t value, unsigned width)
Definition ArithOps.cpp:352
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
void populateSCFStructuralTypeConversionsAndLegality(const TypeConverter &typeConverter, RewritePatternSet &patterns, ConversionTarget &target, PatternBenefit benefit=1)
Populates patterns for SCF structural type conversions and sets up the provided ConversionTarget with...
void populateVectorInterleaveToShufflePatterns(RewritePatternSet &patterns, PatternBenefit benefit=1)
void populateVectorDeinterleaveToShufflePatterns(RewritePatternSet &patterns, PatternBenefit benefit=1)
const uArch * getUArch(llvm::StringRef archName)
Definition uArchCommon.h:24
bool hasStaticShapeAndStrides(MemRefType type)
Returns true if type has a static shape and static strides.
Value addElemOffsetInBytes(OpBuilder &builder, Location loc, Value baseAddr, Value elemOffset, MemRefType memrefTy)
Returns baseAddr + elemOffset * bitWidth / 8, i.e.
std::optional< std::string > getChipStr(Operation *op)
Retrieves the chip string from the XeVM target attribute of the parent GPU module operation.
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
Definition Matchers.h:490
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
SmallVector< int64_t > computeStrides(ArrayRef< int64_t > sizes)
SmallVector< int64_t > delinearize(int64_t linearIndex, ArrayRef< int64_t > strides)
Given the strides together with a linear index in the dimension space, return the vector-space offset...
Value getValueOrCreateConstantIntOp(OpBuilder &b, Location loc, OpFoldResult ofr)
Converts an OpFoldResult to a Value.
Definition Utils.cpp:105
LogicalResult applyOpPatternsGreedily(ArrayRef< Operation * > ops, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr, bool *allErased=nullptr)
Rewrite the specified ops by repeatedly applying the highest benefit patterns in a greedy worklist dr...
int64_t computeProduct(ArrayRef< int64_t > basis)
Self-explicit.
Value getValueOrCreateCastToIndexLike(OpBuilder &b, Location loc, Type targetType, Value value)
Create a cast from an index-like value (index or integer) to another index-like value.
Definition Utils.cpp:122
void populateXeGPUToXeVMConversionPatterns(const LLVMTypeConverter &typeConverter, RewritePatternSet &patterns)
Operation * clone(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
int64_t linearize(ArrayRef< int64_t > offsets, ArrayRef< int64_t > basis)
Return the linearized index of 'offsets' w.r.t.
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
Definition Matchers.h:369
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...