MLIR 23.0.0git
MemRefToSPIRV.cpp
Go to the documentation of this file.
1//===- MemRefToSPIRV.cpp - MemRef to SPIR-V Patterns ----------------------===//
2//
3// Part of the LLVM Project, 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//
9// This file implements patterns to convert MemRef dialect to SPIR-V dialect.
10//
11//===----------------------------------------------------------------------===//
12
22#include "mlir/IR/MLIRContext.h"
23#include "mlir/IR/Visitors.h"
24#include <cassert>
25#include <limits>
26#include <optional>
27
28#define DEBUG_TYPE "memref-to-spirv-pattern"
29
30using namespace mlir;
31
32//===----------------------------------------------------------------------===//
33// Utility functions
34//===----------------------------------------------------------------------===//
35
36/// Returns the offset of the value in `targetBits` representation.
37///
38/// `srcIdx` is an index into a 1-D array with each element having `sourceBits`.
39/// It's assumed to be non-negative.
40///
41/// When accessing an element in the array treating as having elements of
42/// `targetBits`, multiple values are loaded in the same time. The method
43/// returns the offset where the `srcIdx` locates in the value. For example, if
44/// `sourceBits` equals to 8 and `targetBits` equals to 32, the x-th element is
45/// located at (x % 4) * 8. Because there are four elements in one i32, and one
46/// element has 8 bits.
47static Value getOffsetForBitwidth(Location loc, Value srcIdx, int sourceBits,
48 int targetBits, OpBuilder &builder) {
49 assert(targetBits % sourceBits == 0);
50 Type type = srcIdx.getType();
51 IntegerAttr idxAttr = builder.getIntegerAttr(type, targetBits / sourceBits);
52 auto idx = builder.createOrFold<spirv::ConstantOp>(loc, type, idxAttr);
53 IntegerAttr srcBitsAttr = builder.getIntegerAttr(type, sourceBits);
54 auto srcBitsValue =
55 builder.createOrFold<spirv::ConstantOp>(loc, type, srcBitsAttr);
56 auto m = builder.createOrFold<spirv::UModOp>(loc, srcIdx, idx);
57 return builder.createOrFold<spirv::IMulOp>(loc, type, m, srcBitsValue);
58}
59
60/// Returns an adjusted spirv::AccessChainOp. Based on the
61/// extension/capabilities, certain integer bitwidths `sourceBits` might not be
62/// supported. During conversion if a memref of an unsupported type is used,
63/// load/stores to this memref need to be modified to use a supported higher
64/// bitwidth `targetBits` and extracting the required bits. For an accessing a
65/// 1D array (spirv.array or spirv.rtarray), the last index is modified to load
66/// the bits needed. The extraction of the actual bits needed are handled
67/// separately. Note that this only works for a 1-D tensor.
68static Value
70 spirv::AccessChainOp op, int sourceBits,
71 int targetBits, OpBuilder &builder) {
72 assert(targetBits % sourceBits == 0);
73 const auto loc = op.getLoc();
74 Value lastDim = op->getOperand(op.getNumOperands() - 1);
75 Type type = lastDim.getType();
76 IntegerAttr attr = builder.getIntegerAttr(type, targetBits / sourceBits);
77 auto idx = builder.createOrFold<spirv::ConstantOp>(loc, type, attr);
78 auto indices = llvm::to_vector<4>(op.getIndices());
79 // There are two elements if this is a 1-D tensor.
80 assert(indices.size() == 2);
81 indices.back() = builder.createOrFold<spirv::SDivOp>(loc, lastDim, idx);
82 Type t = typeConverter.convertType(op.getComponentPtr().getType());
83 return spirv::AccessChainOp::create(builder, loc, t, op.getBasePtr(),
84 indices);
85}
86
87/// Casts the given `srcBool` into an integer of `dstType`.
88static Value castBoolToIntN(Location loc, Value srcBool, Type dstType,
89 OpBuilder &builder) {
90 assert(srcBool.getType().isInteger(1));
91 if (dstType.isInteger(1))
92 return srcBool;
93 Value zero = spirv::ConstantOp::getZero(dstType, loc, builder);
94 Value one = spirv::ConstantOp::getOne(dstType, loc, builder);
95 return builder.createOrFold<spirv::SelectOp>(loc, dstType, srcBool, one,
96 zero);
97}
98
99/// Returns the `targetBits`-bit value shifted by the given `offset`, and cast
100/// to the type destination type, and masked.
101static Value shiftValue(Location loc, Value value, Value offset, Value mask,
102 OpBuilder &builder) {
103 IntegerType dstType = cast<IntegerType>(mask.getType());
104 int targetBits = static_cast<int>(dstType.getWidth());
105 int valueBits = value.getType().getIntOrFloatBitWidth();
106 assert(valueBits <= targetBits);
107
108 if (valueBits == 1) {
109 value = castBoolToIntN(loc, value, dstType, builder);
110 } else {
111 if (valueBits < targetBits) {
112 value = spirv::UConvertOp::create(
113 builder, loc, builder.getIntegerType(targetBits), value);
114 }
115
116 value = builder.createOrFold<spirv::BitwiseAndOp>(loc, value, mask);
117 }
118 return builder.createOrFold<spirv::ShiftLeftLogicalOp>(loc, value.getType(),
119 value, offset);
120}
121
122/// Returns true if the allocations of memref `type` generated from `allocOp`
123/// can be lowered to SPIR-V.
124static bool isAllocationSupported(Operation *allocOp, MemRefType type) {
125 if (isa<memref::AllocOp, memref::DeallocOp>(allocOp)) {
126 auto sc = dyn_cast_or_null<spirv::StorageClassAttr>(type.getMemorySpace());
127 if (!sc || sc.getValue() != spirv::StorageClass::Workgroup)
128 return false;
129 } else if (isa<memref::AllocaOp>(allocOp)) {
130 auto sc = dyn_cast_or_null<spirv::StorageClassAttr>(type.getMemorySpace());
131 if (!sc || sc.getValue() != spirv::StorageClass::Function)
132 return false;
133 } else {
134 return false;
135 }
136
137 // Currently only support static shape and int or float, complex of int or
138 // float, or vector of int or float element type.
139 if (!type.hasStaticShape())
140 return false;
141
142 Type elementType = type.getElementType();
143 if (auto vecType = dyn_cast<VectorType>(elementType))
144 elementType = vecType.getElementType();
145 if (auto compType = dyn_cast<ComplexType>(elementType))
146 elementType = compType.getElementType();
147 return elementType.isIntOrFloat();
148}
149
150/// Returns the scope to use for atomic operations use for emulating store
151/// operations of unsupported integer bitwidths, based on the memref
152/// type. Returns std::nullopt on failure.
153static std::optional<spirv::Scope> getAtomicOpScope(MemRefType type) {
154 auto sc = dyn_cast_or_null<spirv::StorageClassAttr>(type.getMemorySpace());
155 switch (sc.getValue()) {
156 case spirv::StorageClass::StorageBuffer:
157 return spirv::Scope::Device;
158 case spirv::StorageClass::Workgroup:
159 return spirv::Scope::Workgroup;
160 default:
161 break;
162 }
163 return {};
164}
165
166/// Extracts the element type from a SPIR-V pointer type pointing to storage.
167///
168/// For Kernel capability, the pointer points directly to the element type
169/// (possibly wrapped in an array). For Vulkan, the pointer points to a struct
170/// containing an array or runtime array, and we need to unwrap to get the
171/// element type.
172static Type
174 const SPIRVTypeConverter &typeConverter) {
175 if (typeConverter.allows(spirv::Capability::Kernel)) {
176 if (auto arrayType = dyn_cast<spirv::ArrayType>(pointeeType))
177 return arrayType.getElementType();
178 return pointeeType;
179 }
180 // For Vulkan we need to extract element from wrapping struct and array.
181 Type structElemType = cast<spirv::StructType>(pointeeType).getElementType(0);
182 if (auto arrayType = dyn_cast<spirv::ArrayType>(structElemType))
183 return arrayType.getElementType();
184 return cast<spirv::RuntimeArrayType>(structElemType).getElementType();
185}
186
187/// Casts the given `srcInt` into a boolean value.
188static Value castIntNToBool(Location loc, Value srcInt, OpBuilder &builder) {
189 if (srcInt.getType().isInteger(1))
190 return srcInt;
191
192 auto one = spirv::ConstantOp::getZero(srcInt.getType(), loc, builder);
193 return builder.createOrFold<spirv::INotEqualOp>(loc, srcInt, one);
194}
195
196//===----------------------------------------------------------------------===//
197// Operation conversion
198//===----------------------------------------------------------------------===//
199
200// Note that DRR cannot be used for the patterns in this file: we may need to
201// convert type along the way, which requires ConversionPattern. DRR generates
202// normal RewritePattern.
203
204namespace {
205
206/// Converts memref.alloca to SPIR-V Function variables.
207class AllocaOpPattern final : public OpConversionPattern<memref::AllocaOp> {
208public:
209 using Base::Base;
210
211 LogicalResult
212 matchAndRewrite(memref::AllocaOp allocaOp, OpAdaptor adaptor,
213 ConversionPatternRewriter &rewriter) const override;
214};
215
216/// Converts an allocation operation to SPIR-V. Currently only supports lowering
217/// to Workgroup memory when the size is constant. Note that this pattern needs
218/// to be applied in a pass that runs at least at spirv.module scope since it
219/// wil ladd global variables into the spirv.module.
220class AllocOpPattern final : public OpConversionPattern<memref::AllocOp> {
221public:
222 using Base::Base;
223
224 LogicalResult
225 matchAndRewrite(memref::AllocOp operation, OpAdaptor adaptor,
226 ConversionPatternRewriter &rewriter) const override;
227};
228
229/// Converts memref.automic_rmw operations to SPIR-V atomic operations.
230class AtomicRMWOpPattern final
231 : public OpConversionPattern<memref::AtomicRMWOp> {
232public:
233 using Base::Base;
234
235 LogicalResult
236 matchAndRewrite(memref::AtomicRMWOp atomicOp, OpAdaptor adaptor,
237 ConversionPatternRewriter &rewriter) const override;
238};
239
240/// Removed a deallocation if it is a supported allocation. Currently only
241/// removes deallocation if the memory space is workgroup memory.
242class DeallocOpPattern final : public OpConversionPattern<memref::DeallocOp> {
243public:
244 using Base::Base;
245
246 LogicalResult
247 matchAndRewrite(memref::DeallocOp operation, OpAdaptor adaptor,
248 ConversionPatternRewriter &rewriter) const override;
249};
250
251/// Converts memref.load to spirv.Load + spirv.AccessChain on integers.
252class IntLoadOpPattern final : public OpConversionPattern<memref::LoadOp> {
253public:
254 using Base::Base;
255
256 LogicalResult
257 matchAndRewrite(memref::LoadOp loadOp, OpAdaptor adaptor,
258 ConversionPatternRewriter &rewriter) const override;
259};
260
261/// Converts memref.load to spirv.Load + spirv.AccessChain.
262class LoadOpPattern final : public OpConversionPattern<memref::LoadOp> {
263public:
264 using Base::Base;
265
266 LogicalResult
267 matchAndRewrite(memref::LoadOp loadOp, OpAdaptor adaptor,
268 ConversionPatternRewriter &rewriter) const override;
269};
270
271/// Converts memref.load to spirv.Image + spirv.ImageFetch
272class ImageLoadOpPattern final : public OpConversionPattern<memref::LoadOp> {
273public:
274 using Base::Base;
275
276 LogicalResult
277 matchAndRewrite(memref::LoadOp loadOp, OpAdaptor adaptor,
278 ConversionPatternRewriter &rewriter) const override;
279};
280
281/// Converts memref.store to spirv.Store on integers.
282class IntStoreOpPattern final : public OpConversionPattern<memref::StoreOp> {
283public:
284 using Base::Base;
285
286 LogicalResult
287 matchAndRewrite(memref::StoreOp storeOp, OpAdaptor adaptor,
288 ConversionPatternRewriter &rewriter) const override;
289};
290
291/// Converts memref.memory_space_cast to the appropriate spirv cast operations.
292class MemorySpaceCastOpPattern final
293 : public OpConversionPattern<memref::MemorySpaceCastOp> {
294public:
295 using Base::Base;
296
297 LogicalResult
298 matchAndRewrite(memref::MemorySpaceCastOp addrCastOp, OpAdaptor adaptor,
299 ConversionPatternRewriter &rewriter) const override;
300};
301
302/// Converts memref.store to spirv.Store.
303class StoreOpPattern final : public OpConversionPattern<memref::StoreOp> {
304public:
305 using Base::Base;
306
307 LogicalResult
308 matchAndRewrite(memref::StoreOp storeOp, OpAdaptor adaptor,
309 ConversionPatternRewriter &rewriter) const override;
310};
311
312class ReinterpretCastPattern final
313 : public OpConversionPattern<memref::ReinterpretCastOp> {
314public:
315 using Base::Base;
316
317 LogicalResult
318 matchAndRewrite(memref::ReinterpretCastOp op, OpAdaptor adaptor,
319 ConversionPatternRewriter &rewriter) const override;
320};
321
322class CastPattern final : public OpConversionPattern<memref::CastOp> {
323public:
324 using Base::Base;
325
326 LogicalResult
327 matchAndRewrite(memref::CastOp op, OpAdaptor adaptor,
328 ConversionPatternRewriter &rewriter) const override {
329 Value src = adaptor.getSource();
330 Type srcType = src.getType();
331
332 const TypeConverter *converter = getTypeConverter();
333 Type dstType = converter->convertType(op.getType());
334 if (srcType != dstType)
335 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
336 diag << "types doesn't match: " << srcType << " and " << dstType;
337 });
338
339 rewriter.replaceOp(op, src);
340 return success();
341 }
342};
343
344/// Converts memref.extract_aligned_pointer_as_index to spirv.ConvertPtrToU.
345class ExtractAlignedPointerAsIndexOpPattern final
346 : public OpConversionPattern<memref::ExtractAlignedPointerAsIndexOp> {
347public:
348 using Base::Base;
349
350 LogicalResult
351 matchAndRewrite(memref::ExtractAlignedPointerAsIndexOp extractOp,
352 OpAdaptor adaptor,
353 ConversionPatternRewriter &rewriter) const override;
354};
355} // namespace
356
357//===----------------------------------------------------------------------===//
358// AllocaOp
359//===----------------------------------------------------------------------===//
360
361LogicalResult
362AllocaOpPattern::matchAndRewrite(memref::AllocaOp allocaOp, OpAdaptor adaptor,
363 ConversionPatternRewriter &rewriter) const {
364 MemRefType allocType = allocaOp.getType();
365 if (!isAllocationSupported(allocaOp, allocType))
366 return rewriter.notifyMatchFailure(allocaOp, "unhandled allocation type");
367
368 // Get the SPIR-V type for the allocation.
369 Type spirvType = getTypeConverter()->convertType(allocType);
370 if (!spirvType)
371 return rewriter.notifyMatchFailure(allocaOp, "type conversion failed");
372
373 rewriter.replaceOpWithNewOp<spirv::VariableOp>(allocaOp, spirvType,
374 spirv::StorageClass::Function,
375 /*initializer=*/nullptr);
376 return success();
377}
378
379//===----------------------------------------------------------------------===//
380// AllocOp
381//===----------------------------------------------------------------------===//
382
383LogicalResult
384AllocOpPattern::matchAndRewrite(memref::AllocOp operation, OpAdaptor adaptor,
385 ConversionPatternRewriter &rewriter) const {
386 MemRefType allocType = operation.getType();
387 if (!isAllocationSupported(operation, allocType))
388 return rewriter.notifyMatchFailure(operation, "unhandled allocation type");
389
390 // Get the SPIR-V type for the allocation.
391 Type spirvType = getTypeConverter()->convertType(allocType);
392 if (!spirvType)
393 return rewriter.notifyMatchFailure(operation, "type conversion failed");
394
395 // Insert spirv.GlobalVariable for this allocation.
396 Operation *parent =
397 SymbolTable::getNearestSymbolTable(operation->getParentOp());
398 if (!parent)
399 return failure();
400 Location loc = operation.getLoc();
401 spirv::GlobalVariableOp varOp;
402 {
403 OpBuilder::InsertionGuard guard(rewriter);
404 Block &entryBlock = *parent->getRegion(0).begin();
405 rewriter.setInsertionPointToStart(&entryBlock);
406 auto varOps = entryBlock.getOps<spirv::GlobalVariableOp>();
407 std::string varName =
408 std::string("__workgroup_mem__") +
409 std::to_string(std::distance(varOps.begin(), varOps.end()));
410 varOp = spirv::GlobalVariableOp::create(rewriter, loc, spirvType, varName,
411 /*initializer=*/nullptr);
412 }
413
414 // Get pointer to global variable at the current scope.
415 rewriter.replaceOpWithNewOp<spirv::AddressOfOp>(operation, varOp);
416 return success();
417}
418
419//===----------------------------------------------------------------------===//
420// AllocOp
421//===----------------------------------------------------------------------===//
422
423LogicalResult
424AtomicRMWOpPattern::matchAndRewrite(memref::AtomicRMWOp atomicOp,
425 OpAdaptor adaptor,
426 ConversionPatternRewriter &rewriter) const {
427 if (isa<FloatType>(atomicOp.getType()))
428 return rewriter.notifyMatchFailure(atomicOp,
429 "unimplemented floating-point case");
430
431 auto memrefType = cast<MemRefType>(atomicOp.getMemref().getType());
432 std::optional<spirv::Scope> scope = getAtomicOpScope(memrefType);
433 if (!scope)
434 return rewriter.notifyMatchFailure(atomicOp,
435 "unsupported memref memory space");
436
437 auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>();
438 Type resultType = typeConverter.convertType(atomicOp.getType());
439 if (!resultType)
440 return rewriter.notifyMatchFailure(atomicOp,
441 "failed to convert result type");
442
443 auto loc = atomicOp.getLoc();
444 Value ptr =
445 spirv::getElementPtr(typeConverter, memrefType, adaptor.getMemref(),
446 adaptor.getIndices(), loc, rewriter);
447
448 if (!ptr)
449 return failure();
450
451 // Determine the source and destination bitwidths. The source is the original
452 // memref element type and the destination is the SPIR-V storage type (e.g.,
453 // i32 for Vulkan).
454 int srcBits = memrefType.getElementType().getIntOrFloatBitWidth();
455 auto pointerType = typeConverter.convertType<spirv::PointerType>(memrefType);
456 if (!pointerType)
457 return rewriter.notifyMatchFailure(atomicOp,
458 "failed to convert memref type");
459
460 Type pointeeType = pointerType.getPointeeType();
461 auto dstType = dyn_cast<IntegerType>(
462 getElementTypeForStoragePointer(pointeeType, typeConverter));
463 if (!dstType)
464 return rewriter.notifyMatchFailure(
465 atomicOp, "failed to determine destination element type");
466
467 int dstBits = static_cast<int>(dstType.getWidth());
468 assert(dstBits % srcBits == 0);
469
470 // When the source and destination bitwidths match, emit the atomic operation
471 // directly.
472 if (srcBits == dstBits) {
473#define ATOMIC_CASE(kind, spirvOp) \
474 case arith::AtomicRMWKind::kind: \
475 rewriter.replaceOpWithNewOp<spirv::spirvOp>( \
476 atomicOp, resultType, ptr, *scope, \
477 spirv::MemorySemantics::AcquireRelease, adaptor.getValue()); \
478 break
479
480 switch (atomicOp.getKind()) {
481 ATOMIC_CASE(addi, AtomicIAddOp);
482 ATOMIC_CASE(maxs, AtomicSMaxOp);
483 ATOMIC_CASE(maxu, AtomicUMaxOp);
484 ATOMIC_CASE(mins, AtomicSMinOp);
485 ATOMIC_CASE(minu, AtomicUMinOp);
486 ATOMIC_CASE(ori, AtomicOrOp);
487 ATOMIC_CASE(andi, AtomicAndOp);
488 default:
489 return rewriter.notifyMatchFailure(atomicOp, "unimplemented atomic kind");
490 }
491
492#undef ATOMIC_CASE
493
494 return success();
495 }
496
497 // Sub-element-width atomic: the element type (e.g., i8) is narrower than the
498 // storage type (e.g., i32). We need to adjust the index and shift/mask the
499 // value to operate on the correct bits within the wider storage element.
500 //
501 // Only ori and andi can be emulated because they operate bitwise and don't
502 // carry across byte boundaries. Other kinds (addi, max, min) would require
503 // CAS loops.
504 if (atomicOp.getKind() != arith::AtomicRMWKind::ori &&
505 atomicOp.getKind() != arith::AtomicRMWKind::andi) {
506 return rewriter.notifyMatchFailure(
507 atomicOp,
508 "atomic op on sub-element-width types is only supported for ori/andi");
509 }
510
511 // Bitcasting is currently unsupported for Kernel capability /
512 // spirv.PtrAccessChain.
513 if (typeConverter.allows(spirv::Capability::Kernel))
514 return rewriter.notifyMatchFailure(
515 atomicOp,
516 "sub-element-width atomic ops unsupported with Kernel capability");
517
518 auto accessChainOp = ptr.getDefiningOp<spirv::AccessChainOp>();
519 if (!accessChainOp)
520 return failure();
521
522 // Compute the bit offset within the storage element and adjust the pointer
523 // to address the containing storage element.
524 assert(accessChainOp.getIndices().size() == 2);
525 Value lastDim = accessChainOp->getOperand(accessChainOp.getNumOperands() - 1);
526 Value offset = getOffsetForBitwidth(loc, lastDim, srcBits, dstBits, rewriter);
527 Value adjustedPtr = adjustAccessChainForBitwidth(typeConverter, accessChainOp,
528 srcBits, dstBits, rewriter);
529 Value result;
530 switch (atomicOp.getKind()) {
531 case arith::AtomicRMWKind::ori: {
532 // OR only sets bits, so shifting the value to the target position and
533 // ORing with zeros in other positions preserves the unaffected bits.
534 Value elemMask = rewriter.createOrFold<spirv::ConstantOp>(
535 loc, dstType, rewriter.getIntegerAttr(dstType, (1uLL << srcBits) - 1));
536 Value storeVal =
537 shiftValue(loc, adaptor.getValue(), offset, elemMask, rewriter);
538 result = spirv::AtomicOrOp::create(
539 rewriter, loc, dstType, adjustedPtr, *scope,
540 spirv::MemorySemantics::AcquireRelease, storeVal);
541 break;
542 }
543 case arith::AtomicRMWKind::andi: {
544 // Build a mask that preserves all bits outside the target element
545 // and applies the operand mask to the target element.
546 // mask = (operand << offset) | ~(elemMask << offset)
547 Value elemMask = rewriter.createOrFold<spirv::ConstantOp>(
548 loc, dstType, rewriter.getIntegerAttr(dstType, (1uLL << srcBits) - 1));
549 Value storeVal =
550 shiftValue(loc, adaptor.getValue(), offset, elemMask, rewriter);
551 Value shiftedElemMask = rewriter.createOrFold<spirv::ShiftLeftLogicalOp>(
552 loc, dstType, elemMask, offset);
553 Value invertedElemMask =
554 rewriter.createOrFold<spirv::NotOp>(loc, dstType, shiftedElemMask);
555 Value mask = rewriter.createOrFold<spirv::BitwiseOrOp>(loc, storeVal,
556 invertedElemMask);
557 result = spirv::AtomicAndOp::create(
558 rewriter, loc, dstType, adjustedPtr, *scope,
559 spirv::MemorySemantics::AcquireRelease, mask);
560 break;
561 }
562 default:
563 return rewriter.notifyMatchFailure(atomicOp, "unimplemented atomic kind");
564 }
565
566 // The atomic op returns the old value of the full storage element (e.g.,
567 // i32). Extract the original sub-element value from the correct position.
568 result = rewriter.createOrFold<spirv::ShiftRightLogicalOp>(loc, dstType,
569 result, offset);
570 Value mask = rewriter.createOrFold<spirv::ConstantOp>(
571 loc, dstType, rewriter.getIntegerAttr(dstType, (1uLL << srcBits) - 1));
572 result =
573 rewriter.createOrFold<spirv::BitwiseAndOp>(loc, dstType, result, mask);
574 rewriter.replaceOp(atomicOp, result);
575
576 return success();
577}
578
579//===----------------------------------------------------------------------===//
580// DeallocOp
581//===----------------------------------------------------------------------===//
582
583LogicalResult
584DeallocOpPattern::matchAndRewrite(memref::DeallocOp operation,
585 OpAdaptor adaptor,
586 ConversionPatternRewriter &rewriter) const {
587 MemRefType deallocType = cast<MemRefType>(operation.getMemref().getType());
588 if (!isAllocationSupported(operation, deallocType))
589 return rewriter.notifyMatchFailure(operation, "unhandled allocation type");
590 rewriter.eraseOp(operation);
591 return success();
592}
593
594//===----------------------------------------------------------------------===//
595// LoadOp
596//===----------------------------------------------------------------------===//
597
599 spirv::MemoryAccessAttr memoryAccess;
600 IntegerAttr alignment;
601};
602
603/// Given an accessed SPIR-V pointer, calculates its alignment requirements, if
604/// any.
605static FailureOr<MemoryRequirements>
606calculateMemoryRequirements(Value accessedPtr, bool isNontemporal,
607 uint64_t preferredAlignment) {
608 if (preferredAlignment >= std::numeric_limits<uint32_t>::max()) {
609 return failure();
610 }
611
612 MLIRContext *ctx = accessedPtr.getContext();
613
614 auto memoryAccess = spirv::MemoryAccess::None;
615 if (isNontemporal) {
616 memoryAccess = spirv::MemoryAccess::Nontemporal;
617 }
618
619 auto ptrType = cast<spirv::PointerType>(accessedPtr.getType());
620 bool mayOmitAlignment =
621 !preferredAlignment &&
622 ptrType.getStorageClass() != spirv::StorageClass::PhysicalStorageBuffer;
623 if (mayOmitAlignment) {
624 if (memoryAccess == spirv::MemoryAccess::None) {
625 return MemoryRequirements{spirv::MemoryAccessAttr{}, IntegerAttr{}};
626 }
627 return MemoryRequirements{spirv::MemoryAccessAttr::get(ctx, memoryAccess),
628 IntegerAttr{}};
629 }
630
631 // PhysicalStorageBuffers require the `Aligned` attribute.
632 // Other storage types may show an `Aligned` attribute.
633 auto pointeeType = dyn_cast<spirv::ScalarType>(ptrType.getPointeeType());
634 if (!pointeeType)
635 return failure();
636
637 // For scalar types, the alignment is determined by their size.
638 std::optional<int64_t> sizeInBytes = pointeeType.getSizeInBytes();
639 if (!sizeInBytes.has_value())
640 return failure();
641
642 memoryAccess |= spirv::MemoryAccess::Aligned;
643 auto memAccessAttr = spirv::MemoryAccessAttr::get(ctx, memoryAccess);
644 auto alignmentValue = preferredAlignment ? preferredAlignment : *sizeInBytes;
645 auto alignment = IntegerAttr::get(IntegerType::get(ctx, 32), alignmentValue);
646 return MemoryRequirements{memAccessAttr, alignment};
647}
648
649/// Given an accessed SPIR-V pointer and the original memref load/store
650/// `memAccess` op, calculates the alignment requirements, if any. Takes into
651/// account the alignment attributes applied to the load/store op.
652template <class LoadOrStoreOp>
653static FailureOr<MemoryRequirements>
654calculateMemoryRequirements(Value accessedPtr, LoadOrStoreOp loadOrStoreOp) {
655 static_assert(
656 llvm::is_one_of<LoadOrStoreOp, memref::LoadOp, memref::StoreOp>::value,
657 "Must be called on either memref::LoadOp or memref::StoreOp");
658
659 return calculateMemoryRequirements(accessedPtr,
660 loadOrStoreOp.getNontemporal(),
661 loadOrStoreOp.getAlignment().value_or(0));
662}
663
664LogicalResult
665IntLoadOpPattern::matchAndRewrite(memref::LoadOp loadOp, OpAdaptor adaptor,
666 ConversionPatternRewriter &rewriter) const {
667 auto loc = loadOp.getLoc();
668 auto memrefType = cast<MemRefType>(loadOp.getMemref().getType());
669 if (!memrefType.getElementType().isSignlessInteger())
670 return failure();
671
672 auto memorySpaceAttr =
673 dyn_cast_if_present<spirv::StorageClassAttr>(memrefType.getMemorySpace());
674 if (!memorySpaceAttr)
675 return rewriter.notifyMatchFailure(
676 loadOp, "missing memory space SPIR-V storage class attribute");
677
678 if (memorySpaceAttr.getValue() == spirv::StorageClass::Image)
679 return rewriter.notifyMatchFailure(
680 loadOp,
681 "failed to lower memref in image storage class to storage buffer");
682
683 const auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>();
684 Value accessChain =
685 spirv::getElementPtr(typeConverter, memrefType, adaptor.getMemref(),
686 adaptor.getIndices(), loc, rewriter);
687
688 if (!accessChain)
689 return failure();
690
691 int srcBits = memrefType.getElementType().getIntOrFloatBitWidth();
692 bool isBool = srcBits == 1;
693 if (isBool)
694 srcBits = typeConverter.getOptions().boolNumBits;
695
696 auto pointerType = typeConverter.convertType<spirv::PointerType>(memrefType);
697 if (!pointerType)
698 return rewriter.notifyMatchFailure(loadOp, "failed to convert memref type");
699
700 Type pointeeType = pointerType.getPointeeType();
701 Type dstType = getElementTypeForStoragePointer(pointeeType, typeConverter);
702 int dstBits = dstType.getIntOrFloatBitWidth();
703 assert(dstBits % srcBits == 0);
704
705 // If the rewritten load op has the same bit width, use the loading value
706 // directly.
707 if (srcBits == dstBits) {
708 auto memoryRequirements = calculateMemoryRequirements(accessChain, loadOp);
709 if (failed(memoryRequirements))
710 return rewriter.notifyMatchFailure(
711 loadOp, "failed to determine memory requirements");
712
713 auto [memoryAccess, alignment] = *memoryRequirements;
714 Value loadVal = spirv::LoadOp::create(rewriter, loc, accessChain,
715 memoryAccess, alignment);
716 if (isBool)
717 loadVal = castIntNToBool(loc, loadVal, rewriter);
718 rewriter.replaceOp(loadOp, loadVal);
719 return success();
720 }
721
722 // Bitcasting is currently unsupported for Kernel capability /
723 // spirv.PtrAccessChain.
724 if (typeConverter.allows(spirv::Capability::Kernel))
725 return failure();
726
727 auto accessChainOp = accessChain.getDefiningOp<spirv::AccessChainOp>();
728 if (!accessChainOp)
729 return failure();
730
731 // Assume that getElementPtr() works linearizely. If it's a scalar, the method
732 // still returns a linearized accessing. If the accessing is not linearized,
733 // there will be offset issues.
734 assert(accessChainOp.getIndices().size() == 2);
735 Value adjustedPtr = adjustAccessChainForBitwidth(typeConverter, accessChainOp,
736 srcBits, dstBits, rewriter);
737 auto memoryRequirements = calculateMemoryRequirements(adjustedPtr, loadOp);
738 if (failed(memoryRequirements))
739 return rewriter.notifyMatchFailure(
740 loadOp, "failed to determine memory requirements");
741
742 auto [memoryAccess, alignment] = *memoryRequirements;
743 Value spvLoadOp = spirv::LoadOp::create(rewriter, loc, dstType, adjustedPtr,
744 memoryAccess, alignment);
745
746 // Shift the bits to the rightmost.
747 // ____XXXX________ -> ____________XXXX
748 Value lastDim = accessChainOp->getOperand(accessChainOp.getNumOperands() - 1);
749 Value offset = getOffsetForBitwidth(loc, lastDim, srcBits, dstBits, rewriter);
750 Value result = rewriter.createOrFold<spirv::ShiftRightArithmeticOp>(
751 loc, spvLoadOp.getType(), spvLoadOp, offset);
752
753 // Apply the mask to extract corresponding bits.
754 Value mask = rewriter.createOrFold<spirv::ConstantOp>(
755 loc, dstType, rewriter.getIntegerAttr(dstType, (1 << srcBits) - 1));
756 result =
757 rewriter.createOrFold<spirv::BitwiseAndOp>(loc, dstType, result, mask);
758
759 // Apply sign extension on the loading value unconditionally. The signedness
760 // semantic is carried in the operator itself, we relies other pattern to
761 // handle the casting.
762 IntegerAttr shiftValueAttr =
763 rewriter.getIntegerAttr(dstType, dstBits - srcBits);
764 Value shiftValue =
765 rewriter.createOrFold<spirv::ConstantOp>(loc, dstType, shiftValueAttr);
766 result = rewriter.createOrFold<spirv::ShiftLeftLogicalOp>(loc, dstType,
768 result = rewriter.createOrFold<spirv::ShiftRightArithmeticOp>(
769 loc, dstType, result, shiftValue);
770
771 rewriter.replaceOp(loadOp, result);
772
773 assert(accessChainOp.use_empty());
774 rewriter.eraseOp(accessChainOp);
775
776 return success();
777}
778
779LogicalResult
780LoadOpPattern::matchAndRewrite(memref::LoadOp loadOp, OpAdaptor adaptor,
781 ConversionPatternRewriter &rewriter) const {
782 auto memrefType = cast<MemRefType>(loadOp.getMemref().getType());
783 if (memrefType.getElementType().isSignlessInteger())
784 return failure();
785
786 auto memorySpaceAttr =
787 dyn_cast_if_present<spirv::StorageClassAttr>(memrefType.getMemorySpace());
788 if (!memorySpaceAttr)
789 return rewriter.notifyMatchFailure(
790 loadOp, "missing memory space SPIR-V storage class attribute");
791
792 if (memorySpaceAttr.getValue() == spirv::StorageClass::Image)
793 return rewriter.notifyMatchFailure(
794 loadOp,
795 "failed to lower memref in image storage class to storage buffer");
796
797 Value loadPtr = spirv::getElementPtr(
798 *getTypeConverter<SPIRVTypeConverter>(), memrefType, adaptor.getMemref(),
799 adaptor.getIndices(), loadOp.getLoc(), rewriter);
800
801 if (!loadPtr)
802 return failure();
803
804 auto memoryRequirements = calculateMemoryRequirements(loadPtr, loadOp);
805 if (failed(memoryRequirements))
806 return rewriter.notifyMatchFailure(
807 loadOp, "failed to determine memory requirements");
808
809 auto [memoryAccess, alignment] = *memoryRequirements;
810 rewriter.replaceOpWithNewOp<spirv::LoadOp>(loadOp, loadPtr, memoryAccess,
811 alignment);
812 return success();
813}
814
815template <typename OpAdaptor>
816static FailureOr<SmallVector<Value>>
817extractLoadCoordsForComposite(memref::LoadOp loadOp, OpAdaptor adaptor,
818 ConversionPatternRewriter &rewriter) {
819 // At present we only support linear "tiling" as specified in Vulkan, this
820 // means that texels are assumed to be laid out in memory in a row-major
821 // order. This allows us to support any memref layout that is a permutation of
822 // the dimensions. Future work will pass an optional image layout to the
823 // rewrite pattern so that we can support optimized target specific tilings.
824 SmallVector<Value> indices = adaptor.getIndices();
825 AffineMap map = loadOp.getMemRefType().getLayout().getAffineMap();
826 if (!map.isPermutation())
827 return rewriter.notifyMatchFailure(
828 loadOp,
829 "Cannot lower memrefs with memory layout which is not a permutation");
830
831 // The memrefs layout determines the dimension ordering so we need to follow
832 // the map to get the ordering of the dimensions/indices.
833 const unsigned dimCount = map.getNumDims();
834 SmallVector<Value, 3> coords(dimCount);
835 for (unsigned dim = 0; dim < dimCount; ++dim)
836 coords[map.getDimPosition(dim)] = indices[dim];
837
838 // We need to reverse the coordinates because the memref layout is slowest to
839 // fastest moving and the vector coordinates for the image op is fastest to
840 // slowest moving.
841 return llvm::to_vector(llvm::reverse(coords));
842}
843
844LogicalResult
845ImageLoadOpPattern::matchAndRewrite(memref::LoadOp loadOp, OpAdaptor adaptor,
846 ConversionPatternRewriter &rewriter) const {
847 auto memrefType = cast<MemRefType>(loadOp.getMemref().getType());
848
849 auto memorySpaceAttr =
850 dyn_cast_if_present<spirv::StorageClassAttr>(memrefType.getMemorySpace());
851 if (!memorySpaceAttr)
852 return rewriter.notifyMatchFailure(
853 loadOp, "missing memory space SPIR-V storage class attribute");
854
855 if (memorySpaceAttr.getValue() != spirv::StorageClass::Image)
856 return rewriter.notifyMatchFailure(
857 loadOp, "failed to lower memref in non-image storage class to image");
858
859 Value loadPtr = adaptor.getMemref();
860 auto memoryRequirements = calculateMemoryRequirements(loadPtr, loadOp);
861 if (failed(memoryRequirements))
862 return rewriter.notifyMatchFailure(
863 loadOp, "failed to determine memory requirements");
864
865 const auto [memoryAccess, alignment] = *memoryRequirements;
866
867 if (!loadOp.getMemRefType().hasRank())
868 return rewriter.notifyMatchFailure(
869 loadOp, "cannot lower unranked memrefs to SPIR-V images");
870
871 // We currently only support lowering of scalar memref elements to texels in
872 // the R[16|32][f|i|ui] formats. Future work will enable lowering of vector
873 // elements to texels in richer formats.
874 if (!isa<spirv::ScalarType>(loadOp.getMemRefType().getElementType()))
875 return rewriter.notifyMatchFailure(
876 loadOp,
877 "cannot lower memrefs who's element type is not a SPIR-V scalar type"
878 "to SPIR-V images");
879
880 // We currently only support sampled images since OpImageFetch does not work
881 // for plain images and the OpImageRead instruction needs to be materialized
882 // instead or texels need to be accessed via atomics through a texel pointer.
883 // Future work will generalize support to plain images.
884 auto convertedPointeeType = cast<spirv::PointerType>(
885 getTypeConverter()->convertType(loadOp.getMemRefType()));
886 if (!isa<spirv::SampledImageType>(convertedPointeeType.getPointeeType()))
887 return rewriter.notifyMatchFailure(loadOp,
888 "cannot lower memrefs which do not "
889 "convert to SPIR-V sampled images");
890
891 // Materialize the lowering.
892 Location loc = loadOp->getLoc();
893 auto imageLoadOp =
894 spirv::LoadOp::create(rewriter, loc, loadPtr, memoryAccess, alignment);
895 // Extract the image from the sampled image.
896 auto imageOp = spirv::ImageOp::create(rewriter, loc, imageLoadOp);
897
898 // Build a vector of coordinates or just a scalar index if we have a 1D image.
899 Value coords;
900 if (memrefType.getRank() == 1) {
901 coords = adaptor.getIndices()[0];
902 } else {
903 FailureOr<SmallVector<Value>> maybeCoords =
904 extractLoadCoordsForComposite(loadOp, adaptor, rewriter);
905 if (failed(maybeCoords))
906 return failure();
907 auto coordVectorType = VectorType::get({loadOp.getMemRefType().getRank()},
908 adaptor.getIndices().getType()[0]);
909 coords = spirv::CompositeConstructOp::create(rewriter, loc, coordVectorType,
910 maybeCoords.value());
911 }
912
913 // Fetch the value out of the image.
914 auto resultVectorType = VectorType::get({4}, loadOp.getType());
915 auto fetchOp = spirv::ImageFetchOp::create(
916 rewriter, loc, resultVectorType, imageOp, coords,
917 mlir::spirv::ImageOperandsAttr{}, ValueRange{});
918
919 // Note that because OpImageFetch returns a rank 4 vector we need to extract
920 // the elements corresponding to the load which will since we only support the
921 // R[16|32][f|i|ui] formats will always be the R(red) 0th vector element.
922 auto compositeExtractOp =
923 spirv::CompositeExtractOp::create(rewriter, loc, fetchOp, 0);
924
925 rewriter.replaceOp(loadOp, compositeExtractOp);
926 return success();
927}
928
929LogicalResult
930IntStoreOpPattern::matchAndRewrite(memref::StoreOp storeOp, OpAdaptor adaptor,
931 ConversionPatternRewriter &rewriter) const {
932 auto memrefType = cast<MemRefType>(storeOp.getMemref().getType());
933 if (!memrefType.getElementType().isSignlessInteger())
934 return rewriter.notifyMatchFailure(storeOp,
935 "element type is not a signless int");
936
937 auto loc = storeOp.getLoc();
938 auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>();
939 Value accessChain =
940 spirv::getElementPtr(typeConverter, memrefType, adaptor.getMemref(),
941 adaptor.getIndices(), loc, rewriter);
942
943 if (!accessChain)
944 return rewriter.notifyMatchFailure(
945 storeOp, "failed to convert element pointer type");
946
947 int srcBits = memrefType.getElementType().getIntOrFloatBitWidth();
948
949 bool isBool = srcBits == 1;
950 if (isBool)
951 srcBits = typeConverter.getOptions().boolNumBits;
952
953 auto pointerType = typeConverter.convertType<spirv::PointerType>(memrefType);
954 if (!pointerType)
955 return rewriter.notifyMatchFailure(storeOp,
956 "failed to convert memref type");
957
958 Type pointeeType = pointerType.getPointeeType();
959 auto dstType = dyn_cast<IntegerType>(
960 getElementTypeForStoragePointer(pointeeType, typeConverter));
961 if (!dstType)
962 return rewriter.notifyMatchFailure(
963 storeOp, "failed to determine destination element type");
964
965 int dstBits = static_cast<int>(dstType.getWidth());
966 assert(dstBits % srcBits == 0);
967
968 if (srcBits == dstBits) {
969 auto memoryRequirements = calculateMemoryRequirements(accessChain, storeOp);
970 if (failed(memoryRequirements))
971 return rewriter.notifyMatchFailure(
972 storeOp, "failed to determine memory requirements");
973
974 auto [memoryAccess, alignment] = *memoryRequirements;
975 Value storeVal = adaptor.getValue();
976 if (isBool)
977 storeVal = castBoolToIntN(loc, storeVal, dstType, rewriter);
978 rewriter.replaceOpWithNewOp<spirv::StoreOp>(storeOp, accessChain, storeVal,
979 memoryAccess, alignment);
980 return success();
981 }
982
983 // Bitcasting is currently unsupported for Kernel capability /
984 // spirv.PtrAccessChain.
985 if (typeConverter.allows(spirv::Capability::Kernel))
986 return failure();
987
988 auto accessChainOp = accessChain.getDefiningOp<spirv::AccessChainOp>();
989 if (!accessChainOp)
990 return failure();
991
992 // Since there are multiple threads in the processing, the emulation will be
993 // done with atomic operations. E.g., if the stored value is i8, rewrite the
994 // StoreOp to:
995 // 1) load a 32-bit integer
996 // 2) clear 8 bits in the loaded value
997 // 3) set 8 bits in the loaded value
998 // 4) store 32-bit value back
999 //
1000 // Step 2 is done with AtomicAnd, and step 3 is done with AtomicOr (of the
1001 // loaded 32-bit value and the shifted 8-bit store value) as another atomic
1002 // step.
1003 assert(accessChainOp.getIndices().size() == 2);
1004 Value lastDim = accessChainOp->getOperand(accessChainOp.getNumOperands() - 1);
1005 Value offset = getOffsetForBitwidth(loc, lastDim, srcBits, dstBits, rewriter);
1006
1007 // Create a mask to clear the destination. E.g., if it is the second i8 in
1008 // i32, 0xFFFF00FF is created.
1009 Value mask = rewriter.createOrFold<spirv::ConstantOp>(
1010 loc, dstType, rewriter.getIntegerAttr(dstType, (1 << srcBits) - 1));
1011 Value clearBitsMask = rewriter.createOrFold<spirv::ShiftLeftLogicalOp>(
1012 loc, dstType, mask, offset);
1013 clearBitsMask =
1014 rewriter.createOrFold<spirv::NotOp>(loc, dstType, clearBitsMask);
1015
1016 Value storeVal = shiftValue(loc, adaptor.getValue(), offset, mask, rewriter);
1017 Value adjustedPtr = adjustAccessChainForBitwidth(typeConverter, accessChainOp,
1018 srcBits, dstBits, rewriter);
1019 std::optional<spirv::Scope> scope = getAtomicOpScope(memrefType);
1020 if (!scope)
1021 return rewriter.notifyMatchFailure(storeOp, "atomic scope not available");
1022
1023 Value result = spirv::AtomicAndOp::create(
1024 rewriter, loc, dstType, adjustedPtr, *scope,
1025 spirv::MemorySemantics::AcquireRelease, clearBitsMask);
1026 result = spirv::AtomicOrOp::create(
1027 rewriter, loc, dstType, adjustedPtr, *scope,
1028 spirv::MemorySemantics::AcquireRelease, storeVal);
1029
1030 // The AtomicOrOp has no side effect. Since it is already inserted, we can
1031 // just remove the original StoreOp. Note that rewriter.replaceOp()
1032 // doesn't work because it only accepts that the numbers of result are the
1033 // same.
1034 rewriter.eraseOp(storeOp);
1035
1036 assert(accessChainOp.use_empty());
1037 rewriter.eraseOp(accessChainOp);
1038
1039 return success();
1040}
1041
1042//===----------------------------------------------------------------------===//
1043// MemorySpaceCastOp
1044//===----------------------------------------------------------------------===//
1045
1046LogicalResult MemorySpaceCastOpPattern::matchAndRewrite(
1047 memref::MemorySpaceCastOp addrCastOp, OpAdaptor adaptor,
1048 ConversionPatternRewriter &rewriter) const {
1049 Location loc = addrCastOp.getLoc();
1050 auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>();
1051 if (!typeConverter.allows(spirv::Capability::Kernel))
1052 return rewriter.notifyMatchFailure(
1053 loc, "address space casts require kernel capability");
1054
1055 auto sourceType = dyn_cast<MemRefType>(addrCastOp.getSource().getType());
1056 if (!sourceType)
1057 return rewriter.notifyMatchFailure(
1058 loc, "SPIR-V lowering requires ranked memref types");
1059 auto resultType = cast<MemRefType>(addrCastOp.getResult().getType());
1060
1061 auto sourceStorageClassAttr =
1062 dyn_cast_or_null<spirv::StorageClassAttr>(sourceType.getMemorySpace());
1063 if (!sourceStorageClassAttr)
1064 return rewriter.notifyMatchFailure(loc, [sourceType](Diagnostic &diag) {
1065 diag << "source address space " << sourceType.getMemorySpace()
1066 << " must be a SPIR-V storage class";
1067 });
1068 auto resultStorageClassAttr =
1069 dyn_cast_or_null<spirv::StorageClassAttr>(resultType.getMemorySpace());
1070 if (!resultStorageClassAttr)
1071 return rewriter.notifyMatchFailure(loc, [resultType](Diagnostic &diag) {
1072 diag << "result address space " << resultType.getMemorySpace()
1073 << " must be a SPIR-V storage class";
1074 });
1075
1076 spirv::StorageClass sourceSc = sourceStorageClassAttr.getValue();
1077 spirv::StorageClass resultSc = resultStorageClassAttr.getValue();
1078
1079 Value result = adaptor.getSource();
1080 Type resultPtrType = typeConverter.convertType(resultType);
1081 if (!resultPtrType)
1082 return rewriter.notifyMatchFailure(addrCastOp,
1083 "failed to convert memref type");
1084
1085 Type genericPtrType = resultPtrType;
1086 // SPIR-V doesn't have a general address space cast operation. Instead, it has
1087 // conversions to and from generic pointers. To implement the general case,
1088 // we use specific-to-generic conversions when the source class is not
1089 // generic. Then when the result storage class is not generic, we convert the
1090 // generic pointer (either the input on ar intermediate result) to that
1091 // class. This also means that we'll need the intermediate generic pointer
1092 // type if neither the source or destination have it.
1093 if (sourceSc != spirv::StorageClass::Generic &&
1094 resultSc != spirv::StorageClass::Generic) {
1095 Type intermediateType =
1096 MemRefType::get(sourceType.getShape(), sourceType.getElementType(),
1097 sourceType.getLayout(),
1098 rewriter.getAttr<spirv::StorageClassAttr>(
1099 spirv::StorageClass::Generic));
1100 genericPtrType = typeConverter.convertType(intermediateType);
1101 }
1102 if (sourceSc != spirv::StorageClass::Generic) {
1103 result = spirv::PtrCastToGenericOp::create(rewriter, loc, genericPtrType,
1104 result);
1105 }
1106 if (resultSc != spirv::StorageClass::Generic) {
1107 result =
1108 spirv::GenericCastToPtrOp::create(rewriter, loc, resultPtrType, result);
1109 }
1110 rewriter.replaceOp(addrCastOp, result);
1111 return success();
1112}
1113
1114LogicalResult
1115StoreOpPattern::matchAndRewrite(memref::StoreOp storeOp, OpAdaptor adaptor,
1116 ConversionPatternRewriter &rewriter) const {
1117 auto memrefType = cast<MemRefType>(storeOp.getMemref().getType());
1118 if (memrefType.getElementType().isSignlessInteger())
1119 return rewriter.notifyMatchFailure(storeOp, "signless int");
1120 auto storePtr = spirv::getElementPtr(
1121 *getTypeConverter<SPIRVTypeConverter>(), memrefType, adaptor.getMemref(),
1122 adaptor.getIndices(), storeOp.getLoc(), rewriter);
1123
1124 if (!storePtr)
1125 return rewriter.notifyMatchFailure(storeOp, "type conversion failed");
1126
1127 auto memoryRequirements = calculateMemoryRequirements(storePtr, storeOp);
1128 if (failed(memoryRequirements))
1129 return rewriter.notifyMatchFailure(
1130 storeOp, "failed to determine memory requirements");
1131
1132 auto [memoryAccess, alignment] = *memoryRequirements;
1133 rewriter.replaceOpWithNewOp<spirv::StoreOp>(
1134 storeOp, storePtr, adaptor.getValue(), memoryAccess, alignment);
1135 return success();
1136}
1137
1138LogicalResult ReinterpretCastPattern::matchAndRewrite(
1139 memref::ReinterpretCastOp op, OpAdaptor adaptor,
1140 ConversionPatternRewriter &rewriter) const {
1141 Value src = adaptor.getSource();
1142 auto srcType = dyn_cast<spirv::PointerType>(src.getType());
1143
1144 if (!srcType)
1145 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
1146 diag << "invalid src type " << src.getType();
1147 });
1148
1149 const TypeConverter *converter = getTypeConverter();
1150
1151 auto dstType = converter->convertType<spirv::PointerType>(op.getType());
1152 if (dstType != srcType)
1153 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
1154 diag << "invalid dst type " << op.getType();
1155 });
1156
1157 OpFoldResult offset =
1158 getMixedValues(adaptor.getStaticOffsets(), adaptor.getOffsets(), rewriter)
1159 .front();
1160 if (isZeroInteger(offset)) {
1161 rewriter.replaceOp(op, src);
1162 return success();
1163 }
1164
1165 Type intType = converter->convertType(rewriter.getIndexType());
1166 if (!intType)
1167 return rewriter.notifyMatchFailure(op, "failed to convert index type");
1168
1169 Location loc = op.getLoc();
1170 auto offsetValue = [&]() -> Value {
1171 if (auto val = dyn_cast<Value>(offset))
1172 return val;
1173
1174 int64_t attrVal = cast<IntegerAttr>(cast<Attribute>(offset)).getInt();
1175 Attribute attr = rewriter.getIntegerAttr(intType, attrVal);
1176 return rewriter.createOrFold<spirv::ConstantOp>(loc, intType, attr);
1177 }();
1178
1179 rewriter.replaceOpWithNewOp<spirv::InBoundsPtrAccessChainOp>(
1180 op, src, offsetValue, ValueRange());
1181 return success();
1182}
1183
1184//===----------------------------------------------------------------------===//
1185// ExtractAlignedPointerAsIndexOp
1186//===----------------------------------------------------------------------===//
1187
1188LogicalResult ExtractAlignedPointerAsIndexOpPattern::matchAndRewrite(
1189 memref::ExtractAlignedPointerAsIndexOp extractOp, OpAdaptor adaptor,
1190 ConversionPatternRewriter &rewriter) const {
1191 auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>();
1192 Type indexType = typeConverter.getIndexType();
1193 rewriter.replaceOpWithNewOp<spirv::ConvertPtrToUOp>(extractOp, indexType,
1194 adaptor.getSource());
1195 return success();
1196}
1197
1198//===----------------------------------------------------------------------===//
1199// Pattern population
1200//===----------------------------------------------------------------------===//
1201
1202namespace mlir {
1204 RewritePatternSet &patterns) {
1205 patterns.add<AllocaOpPattern, AllocOpPattern, AtomicRMWOpPattern,
1206 DeallocOpPattern, IntLoadOpPattern, ImageLoadOpPattern,
1207 IntStoreOpPattern, LoadOpPattern, MemorySpaceCastOpPattern,
1208 StoreOpPattern, ReinterpretCastPattern, CastPattern,
1209 ExtractAlignedPointerAsIndexOpPattern>(typeConverter,
1210 patterns.getContext());
1211}
1212} // namespace mlir
return success()
static Value castIntNToBool(Location loc, Value srcInt, OpBuilder &builder)
Casts the given srcInt into a boolean value.
static Type getElementTypeForStoragePointer(Type pointeeType, const SPIRVTypeConverter &typeConverter)
Extracts the element type from a SPIR-V pointer type pointing to storage.
static std::optional< spirv::Scope > getAtomicOpScope(MemRefType type)
Returns the scope to use for atomic operations use for emulating store operations of unsupported inte...
static Value shiftValue(Location loc, Value value, Value offset, Value mask, OpBuilder &builder)
Returns the targetBits-bit value shifted by the given offset, and cast to the type destination type,...
static FailureOr< SmallVector< Value > > extractLoadCoordsForComposite(memref::LoadOp loadOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter)
static Value adjustAccessChainForBitwidth(const SPIRVTypeConverter &typeConverter, spirv::AccessChainOp op, int sourceBits, int targetBits, OpBuilder &builder)
Returns an adjusted spirv::AccessChainOp.
static bool isAllocationSupported(Operation *allocOp, MemRefType type)
Returns true if the allocations of memref type generated from allocOp can be lowered to SPIR-V.
static Value getOffsetForBitwidth(Location loc, Value srcIdx, int sourceBits, int targetBits, OpBuilder &builder)
Returns the offset of the value in targetBits representation.
#define ATOMIC_CASE(kind, spirvOp)
static FailureOr< MemoryRequirements > calculateMemoryRequirements(Value accessedPtr, bool isNontemporal, uint64_t preferredAlignment)
Given an accessed SPIR-V pointer, calculates its alignment requirements, if any.
static Value castBoolToIntN(Location loc, Value srcBool, Type dstType, OpBuilder &builder)
Casts the given srcBool into an integer of dstType.
static std::string diag(const llvm::Value &value)
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition AffineMap.h:46
unsigned getDimPosition(unsigned idx) const
Extracts the position of the dimensional expression at the given result, when the caller knows it is ...
unsigned getNumDims() const
bool isPermutation() const
Returns true if the AffineMap represents a symbol-less permutation map.
iterator_range< op_iterator< OpT > > getOps()
Return an iterator range over the operations within this block that are of 'OpT'.
Definition Block.h:203
IntegerAttr getIntegerAttr(Type type, int64_t value)
Definition Builders.cpp:232
IntegerType getIntegerType(unsigned width)
Definition Builders.cpp:71
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
This class helps build Operations.
Definition Builders.h:209
void createOrFold(SmallVectorImpl< Value > &results, Location location, Args &&...args)
Create an operation of specific op type at the current insertion point, and immediately try to fold i...
Definition Builders.h:528
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Definition Operation.h:715
iterator begin()
Definition Region.h:55
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.
Type conversion from builtin types to SPIR-V types for shader interface.
bool allows(spirv::Capability capability) const
Checks if the SPIR-V capability inquired is supported.
static Operation * getNearestSymbolTable(Operation *from)
Returns the nearest symbol table from a given operation from.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:58
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition Types.cpp:118
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:124
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
MLIRContext * getContext() const
Utility to get the associated MLIRContext that this value is defined in.
Definition Value.h:108
Type getType() const
Return the type of this value.
Definition Value.h:105
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:717
Value getElementPtr(const SPIRVTypeConverter &typeConverter, MemRefType baseType, Value basePtr, ValueRange indices, Location loc, OpBuilder &builder)
Performs the index computation to get to the element at indices of the memory pointed to by basePtr,...
Include the generated interface declarations.
SmallVector< OpFoldResult > getMixedValues(ArrayRef< int64_t > staticValues, ValueRange dynamicValues, MLIRContext *context)
Return a vector of OpFoldResults with the same size a staticValues, but all elements for which Shaped...
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:305
bool isZeroInteger(OpFoldResult v)
Return "true" if v is an integer value/attribute with constant value 0.
void populateMemRefToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating MemRef ops to SPIR-V ops.
spirv::MemoryAccessAttr memoryAccess