|
| 1 | +//===- VectorToAMDGPU.cpp - Vector to AMDGPU dialect conversion ---------===// |
| 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 | +#include "mlir/Conversion/VectorToAMDGPU/VectorToAMDGPU.h" |
| 10 | + |
| 11 | +#include "mlir/Dialect/AMDGPU/IR/AMDGPUDialect.h" |
| 12 | +#include "mlir/Dialect/Vector/IR/VectorOps.h" |
| 13 | +#include "mlir/IR/BuiltinTypes.h" |
| 14 | +#include "mlir/IR/PatternMatch.h" |
| 15 | +#include "mlir/IR/TypeUtilities.h" |
| 16 | +#include "mlir/Pass/Pass.h" |
| 17 | +#include "mlir/Support/LogicalResult.h" |
| 18 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 19 | + |
| 20 | +namespace mlir { |
| 21 | +#define GEN_PASS_DEF_CONVERTVECTORTOAMDGPUPASS |
| 22 | +#include "mlir/Conversion/Passes.h.inc" |
| 23 | +} // namespace mlir |
| 24 | + |
| 25 | +using namespace mlir; |
| 26 | + |
| 27 | +/// This pattern supports lowering of: |
| 28 | +/// `vector.transfer_read` to a combination of `vector.load`, `arith.select` and |
| 29 | +/// `vector.broadcast` if all of the following hold: |
| 30 | +/// - The transfer op is masked. |
| 31 | +/// - The memref is in buffer address space. |
| 32 | +/// - Stride of most minor memref dimension must be 1. |
| 33 | +/// - Out-of-bounds masking is not required. |
| 34 | +/// - If the memref's element type is a vector type then it coincides with the |
| 35 | +/// result type. |
| 36 | +/// - The permutation map doesn't perform permutation (broadcasting is allowed). |
| 37 | +/// Note: those conditions mostly come from TransferReadToVectorLoadLowering |
| 38 | +/// pass. |
| 39 | +static LogicalResult |
| 40 | +transferPreconditions(PatternRewriter &rewriter, |
| 41 | + VectorTransferOpInterface xferOp, |
| 42 | + SmallVector<unsigned> &broadcastedDims, |
| 43 | + VectorType &unbroadcastedVectorType) { |
| 44 | + if (!xferOp.getMask()) |
| 45 | + return rewriter.notifyMatchFailure(xferOp, "Only support masked transfer"); |
| 46 | + |
| 47 | + // Permutations are handled by VectorToSCF or |
| 48 | + // populateVectorTransferPermutationMapLoweringPatterns. |
| 49 | + // We let the 0-d corner case pass-through as it is supported. |
| 50 | + if (!xferOp.getPermutationMap().isMinorIdentityWithBroadcasting( |
| 51 | + &broadcastedDims)) |
| 52 | + return rewriter.notifyMatchFailure(xferOp, "not minor identity + bcast"); |
| 53 | + |
| 54 | + auto memRefType = dyn_cast<MemRefType>(xferOp.getShapedType()); |
| 55 | + if (!memRefType) |
| 56 | + return rewriter.notifyMatchFailure(xferOp, "not a memref source"); |
| 57 | + |
| 58 | + Attribute addrSpace = memRefType.getMemorySpace(); |
| 59 | + if (!addrSpace || |
| 60 | + llvm::dyn_cast<amdgpu::AddressSpaceAttr>(addrSpace).getValue() != |
| 61 | + amdgpu::AddressSpace::FatRawBuffer) |
| 62 | + return rewriter.notifyMatchFailure(xferOp, "not in buffer address space"); |
| 63 | + |
| 64 | + // Non-unit strides are handled by VectorToSCF. |
| 65 | + if (!memRefType.isLastDimUnitStride()) |
| 66 | + return rewriter.notifyMatchFailure(xferOp, "!= 1 stride needs VectorToSCF"); |
| 67 | + |
| 68 | + // If there is broadcasting involved then we first load the unbroadcasted |
| 69 | + // vector, and then broadcast it with `vector.broadcast`. |
| 70 | + ArrayRef<int64_t> vectorShape = xferOp.getVectorType().getShape(); |
| 71 | + SmallVector<int64_t> unbroadcastedVectorShape(vectorShape); |
| 72 | + for (unsigned i : broadcastedDims) |
| 73 | + unbroadcastedVectorShape[i] = 1; |
| 74 | + unbroadcastedVectorType = xferOp.getVectorType().cloneWith( |
| 75 | + unbroadcastedVectorShape, xferOp.getVectorType().getElementType()); |
| 76 | + |
| 77 | + // `vector.load` supports vector types as memref's elements only when the |
| 78 | + // resulting vector type is the same as the element type. |
| 79 | + auto memrefElTy = memRefType.getElementType(); |
| 80 | + if (isa<VectorType>(memrefElTy) && memrefElTy != unbroadcastedVectorType) |
| 81 | + return rewriter.notifyMatchFailure(xferOp, "incompatible element type"); |
| 82 | + |
| 83 | + // Otherwise, element types of the memref and the vector must match. |
| 84 | + if (!isa<VectorType>(memrefElTy) && |
| 85 | + memrefElTy != xferOp.getVectorType().getElementType()) |
| 86 | + return rewriter.notifyMatchFailure(xferOp, "non-matching element type"); |
| 87 | + |
| 88 | + // Out-of-bounds dims are handled by MaterializeTransferMask. |
| 89 | + if (xferOp.hasOutOfBoundsDim()) |
| 90 | + return rewriter.notifyMatchFailure(xferOp, "out-of-bounds needs mask"); |
| 91 | + |
| 92 | + if (xferOp.getVectorType().getRank() != 1) |
| 93 | + // vector.maskedload operates on 1-D vectors. |
| 94 | + return rewriter.notifyMatchFailure( |
| 95 | + xferOp, "vector type is not rank 1, can't create masked load, needs " |
| 96 | + "VectorToSCF"); |
| 97 | + |
| 98 | + return success(); |
| 99 | +} |
| 100 | + |
| 101 | +struct TransferReadLowering : public OpRewritePattern<vector::TransferReadOp> { |
| 102 | + using OpRewritePattern<vector::TransferReadOp>::OpRewritePattern; |
| 103 | + |
| 104 | + LogicalResult matchAndRewrite(vector::TransferReadOp readOp, |
| 105 | + PatternRewriter &rewriter) const override { |
| 106 | + |
| 107 | + SmallVector<unsigned> broadcastedDims; |
| 108 | + VectorType unbroadcastedVectorType; |
| 109 | + if (failed(transferPreconditions(rewriter, readOp, broadcastedDims, |
| 110 | + unbroadcastedVectorType))) { |
| 111 | + return failure(); |
| 112 | + } |
| 113 | + |
| 114 | + Value fill = rewriter.create<vector::SplatOp>( |
| 115 | + readOp.getLoc(), unbroadcastedVectorType, readOp.getPadding()); |
| 116 | + Value load = rewriter.create<vector::LoadOp>( |
| 117 | + readOp.getLoc(), unbroadcastedVectorType, readOp.getSource(), |
| 118 | + readOp.getIndices()); |
| 119 | + Value res = rewriter.create<arith::SelectOp>( |
| 120 | + readOp.getLoc(), unbroadcastedVectorType, readOp.getMask(), load, fill); |
| 121 | + |
| 122 | + // Insert a broadcasting op if required. |
| 123 | + if (!broadcastedDims.empty()) { |
| 124 | + res = rewriter.create<vector::BroadcastOp>(readOp.getLoc(), |
| 125 | + readOp.getVectorType(), res); |
| 126 | + } |
| 127 | + |
| 128 | + rewriter.replaceOp(readOp, res); |
| 129 | + |
| 130 | + return success(); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +void mlir::populateVectorToAMDGPUConversionPatterns( |
| 135 | + RewritePatternSet &patterns) { |
| 136 | + patterns.add<TransferReadLowering>(patterns.getContext()); |
| 137 | +} |
| 138 | + |
| 139 | +struct ConvertVectorToAMDGPUPass |
| 140 | + : public impl::ConvertVectorToAMDGPUPassBase<ConvertVectorToAMDGPUPass> { |
| 141 | + void runOnOperation() override { |
| 142 | + RewritePatternSet patterns(&getContext()); |
| 143 | + populateVectorToAMDGPUConversionPatterns(patterns); |
| 144 | + if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) |
| 145 | + return signalPassFailure(); |
| 146 | + } |
| 147 | +}; |
0 commit comments