| 1 | //===- VectorToLLVM.cpp - Conversion from Vector to the LLVM dialect ------===// |
| 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/VectorToLLVM/ConvertVectorToLLVM.h" |
| 10 | |
| 11 | #include "mlir/Conversion/ArithAttrToLLVM/AttrToLLVMConverter.h" |
| 12 | #include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h" |
| 13 | #include "mlir/Conversion/LLVMCommon/PrintCallHelper.h" |
| 14 | #include "mlir/Conversion/LLVMCommon/TypeConverter.h" |
| 15 | #include "mlir/Conversion/LLVMCommon/VectorPattern.h" |
| 16 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 17 | #include "mlir/Dialect/Arith/Utils/Utils.h" |
| 18 | #include "mlir/Dialect/LLVMIR/FunctionCallUtils.h" |
| 19 | #include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 20 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 21 | #include "mlir/Dialect/MemRef/Utils/MemRefUtils.h" |
| 22 | #include "mlir/Dialect/Vector/IR/VectorOps.h" |
| 23 | #include "mlir/Dialect/Vector/Interfaces/MaskableOpInterface.h" |
| 24 | #include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h" |
| 25 | #include "mlir/Dialect/Vector/Transforms/VectorTransforms.h" |
| 26 | #include "mlir/IR/BuiltinAttributes.h" |
| 27 | #include "mlir/IR/BuiltinTypeInterfaces.h" |
| 28 | #include "mlir/IR/BuiltinTypes.h" |
| 29 | #include "mlir/IR/TypeUtilities.h" |
| 30 | #include "mlir/Target/LLVMIR/TypeToLLVM.h" |
| 31 | #include "mlir/Transforms/DialectConversion.h" |
| 32 | #include "llvm/ADT/APFloat.h" |
| 33 | #include "llvm/IR/LLVMContext.h" |
| 34 | #include "llvm/Support/Casting.h" |
| 35 | |
| 36 | #include <optional> |
| 37 | |
| 38 | using namespace mlir; |
| 39 | using namespace mlir::vector; |
| 40 | |
| 41 | // Helper that picks the proper sequence for inserting. |
| 42 | static Value insertOne(ConversionPatternRewriter &rewriter, |
| 43 | const LLVMTypeConverter &typeConverter, Location loc, |
| 44 | Value val1, Value val2, Type llvmType, int64_t rank, |
| 45 | int64_t pos) { |
| 46 | assert(rank > 0 && "0-D vector corner case should have been handled already" ); |
| 47 | if (rank == 1) { |
| 48 | Type idxType = typeConverter.convertType(t: rewriter.getIndexType()); |
| 49 | auto constant = LLVM::ConstantOp::create( |
| 50 | builder&: rewriter, location: loc, res: idxType, value: rewriter.getIntegerAttr(type: idxType, value: pos)); |
| 51 | return LLVM::InsertElementOp::create(builder&: rewriter, location: loc, res: llvmType, vector: val1, value: val2, |
| 52 | position: constant); |
| 53 | } |
| 54 | return LLVM::InsertValueOp::create(builder&: rewriter, location: loc, container: val1, value: val2, position: pos); |
| 55 | } |
| 56 | |
| 57 | // Helper that picks the proper sequence for extracting. |
| 58 | static Value (ConversionPatternRewriter &rewriter, |
| 59 | const LLVMTypeConverter &typeConverter, Location loc, |
| 60 | Value val, Type llvmType, int64_t rank, int64_t pos) { |
| 61 | if (rank <= 1) { |
| 62 | Type idxType = typeConverter.convertType(t: rewriter.getIndexType()); |
| 63 | auto constant = LLVM::ConstantOp::create( |
| 64 | builder&: rewriter, location: loc, res: idxType, value: rewriter.getIntegerAttr(type: idxType, value: pos)); |
| 65 | return LLVM::ExtractElementOp::create(builder&: rewriter, location: loc, res: llvmType, vector: val, |
| 66 | position: constant); |
| 67 | } |
| 68 | return LLVM::ExtractValueOp::create(builder&: rewriter, location: loc, container: val, position: pos); |
| 69 | } |
| 70 | |
| 71 | // Helper that returns data layout alignment of a vector. |
| 72 | LogicalResult getVectorAlignment(const LLVMTypeConverter &typeConverter, |
| 73 | VectorType vectorType, unsigned &align) { |
| 74 | Type convertedVectorTy = typeConverter.convertType(t: vectorType); |
| 75 | if (!convertedVectorTy) |
| 76 | return failure(); |
| 77 | |
| 78 | llvm::LLVMContext llvmContext; |
| 79 | align = LLVM::TypeToLLVMIRTranslator(llvmContext) |
| 80 | .getPreferredAlignment(type: convertedVectorTy, |
| 81 | layout: typeConverter.getDataLayout()); |
| 82 | |
| 83 | return success(); |
| 84 | } |
| 85 | |
| 86 | // Helper that returns data layout alignment of a memref. |
| 87 | LogicalResult getMemRefAlignment(const LLVMTypeConverter &typeConverter, |
| 88 | MemRefType memrefType, unsigned &align) { |
| 89 | Type elementTy = typeConverter.convertType(t: memrefType.getElementType()); |
| 90 | if (!elementTy) |
| 91 | return failure(); |
| 92 | |
| 93 | // TODO: this should use the MLIR data layout when it becomes available and |
| 94 | // stop depending on translation. |
| 95 | llvm::LLVMContext llvmContext; |
| 96 | align = LLVM::TypeToLLVMIRTranslator(llvmContext) |
| 97 | .getPreferredAlignment(type: elementTy, layout: typeConverter.getDataLayout()); |
| 98 | return success(); |
| 99 | } |
| 100 | |
| 101 | // Helper to resolve the alignment for vector load/store, gather and scatter |
| 102 | // ops. If useVectorAlignment is true, get the preferred alignment for the |
| 103 | // vector type in the operation. This option is used for hardware backends with |
| 104 | // vectorization. Otherwise, use the preferred alignment of the element type of |
| 105 | // the memref. Note that if you choose to use vector alignment, the shape of the |
| 106 | // vector type must be resolved before the ConvertVectorToLLVM pass is run. |
| 107 | LogicalResult getVectorToLLVMAlignment(const LLVMTypeConverter &typeConverter, |
| 108 | VectorType vectorType, |
| 109 | MemRefType memrefType, unsigned &align, |
| 110 | bool useVectorAlignment) { |
| 111 | if (useVectorAlignment) { |
| 112 | if (failed(Result: getVectorAlignment(typeConverter, vectorType, align))) { |
| 113 | return failure(); |
| 114 | } |
| 115 | } else { |
| 116 | if (failed(Result: getMemRefAlignment(typeConverter, memrefType, align))) { |
| 117 | return failure(); |
| 118 | } |
| 119 | } |
| 120 | return success(); |
| 121 | } |
| 122 | |
| 123 | // Check if the last stride is non-unit and has a valid memory space. |
| 124 | static LogicalResult isMemRefTypeSupported(MemRefType memRefType, |
| 125 | const LLVMTypeConverter &converter) { |
| 126 | if (!memRefType.isLastDimUnitStride()) |
| 127 | return failure(); |
| 128 | if (failed(Result: converter.getMemRefAddressSpace(type: memRefType))) |
| 129 | return failure(); |
| 130 | return success(); |
| 131 | } |
| 132 | |
| 133 | // Add an index vector component to a base pointer. |
| 134 | static Value getIndexedPtrs(ConversionPatternRewriter &rewriter, Location loc, |
| 135 | const LLVMTypeConverter &typeConverter, |
| 136 | MemRefType memRefType, Value llvmMemref, Value base, |
| 137 | Value index, VectorType vectorType) { |
| 138 | assert(succeeded(isMemRefTypeSupported(memRefType, typeConverter)) && |
| 139 | "unsupported memref type" ); |
| 140 | assert(vectorType.getRank() == 1 && "expected a 1-d vector type" ); |
| 141 | auto pType = MemRefDescriptor(llvmMemref).getElementPtrType(); |
| 142 | auto ptrsType = |
| 143 | LLVM::getVectorType(elementType: pType, numElements: vectorType.getDimSize(idx: 0), |
| 144 | /*isScalable=*/vectorType.getScalableDims()[0]); |
| 145 | return LLVM::GEPOp::create( |
| 146 | builder&: rewriter, location: loc, resultType: ptrsType, |
| 147 | elementType: typeConverter.convertType(t: memRefType.getElementType()), basePtr: base, indices: index); |
| 148 | } |
| 149 | |
| 150 | /// Convert `foldResult` into a Value. Integer attribute is converted to |
| 151 | /// an LLVM constant op. |
| 152 | static Value getAsLLVMValue(OpBuilder &builder, Location loc, |
| 153 | OpFoldResult foldResult) { |
| 154 | if (auto attr = dyn_cast<Attribute>(Val&: foldResult)) { |
| 155 | auto intAttr = cast<IntegerAttr>(Val&: attr); |
| 156 | return LLVM::ConstantOp::create(builder, location: loc, value: intAttr).getResult(); |
| 157 | } |
| 158 | |
| 159 | return cast<Value>(Val&: foldResult); |
| 160 | } |
| 161 | |
| 162 | namespace { |
| 163 | |
| 164 | /// Trivial Vector to LLVM conversions |
| 165 | using VectorScaleOpConversion = |
| 166 | OneToOneConvertToLLVMPattern<vector::VectorScaleOp, LLVM::vscale>; |
| 167 | |
| 168 | /// Conversion pattern for a vector.bitcast. |
| 169 | class VectorBitCastOpConversion |
| 170 | : public ConvertOpToLLVMPattern<vector::BitCastOp> { |
| 171 | public: |
| 172 | using ConvertOpToLLVMPattern<vector::BitCastOp>::ConvertOpToLLVMPattern; |
| 173 | |
| 174 | LogicalResult |
| 175 | matchAndRewrite(vector::BitCastOp bitCastOp, OpAdaptor adaptor, |
| 176 | ConversionPatternRewriter &rewriter) const override { |
| 177 | // Only 0-D and 1-D vectors can be lowered to LLVM. |
| 178 | VectorType resultTy = bitCastOp.getResultVectorType(); |
| 179 | if (resultTy.getRank() > 1) |
| 180 | return failure(); |
| 181 | Type newResultTy = typeConverter->convertType(t: resultTy); |
| 182 | rewriter.replaceOpWithNewOp<LLVM::BitcastOp>(op: bitCastOp, args&: newResultTy, |
| 183 | args: adaptor.getOperands()[0]); |
| 184 | return success(); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | /// Overloaded utility that replaces a vector.load, vector.store, |
| 189 | /// vector.maskedload and vector.maskedstore with their respective LLVM |
| 190 | /// couterparts. |
| 191 | static void replaceLoadOrStoreOp(vector::LoadOp loadOp, |
| 192 | vector::LoadOpAdaptor adaptor, |
| 193 | VectorType vectorTy, Value ptr, unsigned align, |
| 194 | ConversionPatternRewriter &rewriter) { |
| 195 | rewriter.replaceOpWithNewOp<LLVM::LoadOp>(op: loadOp, args&: vectorTy, args&: ptr, args&: align, |
| 196 | /*volatile_=*/args: false, |
| 197 | args: loadOp.getNontemporal()); |
| 198 | } |
| 199 | |
| 200 | static void replaceLoadOrStoreOp(vector::MaskedLoadOp loadOp, |
| 201 | vector::MaskedLoadOpAdaptor adaptor, |
| 202 | VectorType vectorTy, Value ptr, unsigned align, |
| 203 | ConversionPatternRewriter &rewriter) { |
| 204 | rewriter.replaceOpWithNewOp<LLVM::MaskedLoadOp>( |
| 205 | op: loadOp, args&: vectorTy, args&: ptr, args: adaptor.getMask(), args: adaptor.getPassThru(), args&: align); |
| 206 | } |
| 207 | |
| 208 | static void replaceLoadOrStoreOp(vector::StoreOp storeOp, |
| 209 | vector::StoreOpAdaptor adaptor, |
| 210 | VectorType vectorTy, Value ptr, unsigned align, |
| 211 | ConversionPatternRewriter &rewriter) { |
| 212 | rewriter.replaceOpWithNewOp<LLVM::StoreOp>(op: storeOp, args: adaptor.getValueToStore(), |
| 213 | args&: ptr, args&: align, /*volatile_=*/args: false, |
| 214 | args: storeOp.getNontemporal()); |
| 215 | } |
| 216 | |
| 217 | static void replaceLoadOrStoreOp(vector::MaskedStoreOp storeOp, |
| 218 | vector::MaskedStoreOpAdaptor adaptor, |
| 219 | VectorType vectorTy, Value ptr, unsigned align, |
| 220 | ConversionPatternRewriter &rewriter) { |
| 221 | rewriter.replaceOpWithNewOp<LLVM::MaskedStoreOp>( |
| 222 | op: storeOp, args: adaptor.getValueToStore(), args&: ptr, args: adaptor.getMask(), args&: align); |
| 223 | } |
| 224 | |
| 225 | /// Conversion pattern for a vector.load, vector.store, vector.maskedload, and |
| 226 | /// vector.maskedstore. |
| 227 | template <class LoadOrStoreOp> |
| 228 | class VectorLoadStoreConversion : public ConvertOpToLLVMPattern<LoadOrStoreOp> { |
| 229 | public: |
| 230 | explicit VectorLoadStoreConversion(const LLVMTypeConverter &typeConv, |
| 231 | bool useVectorAlign, |
| 232 | bool enableGEPInboundsNuw) |
| 233 | : ConvertOpToLLVMPattern<LoadOrStoreOp>(typeConv), |
| 234 | useVectorAlignment(useVectorAlign), |
| 235 | enableGEPInboundsNuw(enableGEPInboundsNuw) {} |
| 236 | |
| 237 | LogicalResult |
| 238 | matchAndRewrite(LoadOrStoreOp loadOrStoreOp, |
| 239 | typename LoadOrStoreOp::Adaptor adaptor, |
| 240 | ConversionPatternRewriter &rewriter) const override { |
| 241 | // Only 1-D vectors can be lowered to LLVM. |
| 242 | VectorType vectorTy = loadOrStoreOp.getVectorType(); |
| 243 | if (vectorTy.getRank() > 1) |
| 244 | return failure(); |
| 245 | |
| 246 | auto loc = loadOrStoreOp->getLoc(); |
| 247 | MemRefType memRefTy = loadOrStoreOp.getMemRefType(); |
| 248 | |
| 249 | // Resolve alignment. |
| 250 | // Explicit alignment takes priority over use-vector-alignment. |
| 251 | unsigned align = loadOrStoreOp.getAlignment().value_or(0); |
| 252 | if (!align && |
| 253 | failed(getVectorToLLVMAlignment(*this->getTypeConverter(), vectorTy, |
| 254 | memRefTy, align, useVectorAlignment))) |
| 255 | return rewriter.notifyMatchFailure(loadOrStoreOp, |
| 256 | "could not resolve alignment" ); |
| 257 | |
| 258 | // Resolve address. |
| 259 | // When --enable-gep-inbounds-nuw is set, emit inbounds|nuw on the GEP so |
| 260 | // LLVM can apply no-wrap optimizations on the index arithmetic. This |
| 261 | // assumes 0 <= idx < dim_size and non-negative strides; the caller is |
| 262 | // responsible for ensuring those conditions hold. Masked variants are |
| 263 | // designed for near-boundary access and never receive these flags. |
| 264 | LLVM::GEPNoWrapFlags noWrapFlags = LLVM::GEPNoWrapFlags::none; |
| 265 | if constexpr (std::is_same_v<LoadOrStoreOp, vector::LoadOp> || |
| 266 | std::is_same_v<LoadOrStoreOp, vector::StoreOp>) { |
| 267 | // The verifier (verifyLoadStoreMemRefLayout) guarantees that the |
| 268 | // trailing (most minor) stride of the memref is 1. Assert to make |
| 269 | // the invariant explicit in the lowering code. |
| 270 | auto [strides, offset] = memRefTy.getStridesAndOffset(); |
| 271 | assert((strides.empty() || strides.back() == 1) && |
| 272 | "vector.load/store requires unit trailing memref stride" ); |
| 273 | if (enableGEPInboundsNuw) { |
| 274 | noWrapFlags = noWrapFlags | LLVM::GEPNoWrapFlags::inbounds; |
| 275 | |
| 276 | // `nuw` additionally requires non-negative strides. |
| 277 | assert( |
| 278 | !(memref::hasNegativeStaticStride(memRefTy)) && |
| 279 | "Invalid MemRef type - should have been rejected by Op verifier." ); |
| 280 | noWrapFlags = noWrapFlags | LLVM::GEPNoWrapFlags::nuw; |
| 281 | } |
| 282 | } |
| 283 | auto vtype = cast<VectorType>( |
| 284 | this->typeConverter->convertType(loadOrStoreOp.getVectorType())); |
| 285 | Value dataPtr = |
| 286 | this->getStridedElementPtr(rewriter, loc, memRefTy, adaptor.getBase(), |
| 287 | adaptor.getIndices(), noWrapFlags); |
| 288 | replaceLoadOrStoreOp(loadOrStoreOp, adaptor, vtype, dataPtr, align, |
| 289 | rewriter); |
| 290 | return success(); |
| 291 | } |
| 292 | |
| 293 | private: |
| 294 | // If true, use the preferred alignment of the vector type. |
| 295 | // If false, use the preferred alignment of the element type |
| 296 | // of the memref. This flag is intended for use with hardware |
| 297 | // backends that require alignment of vector operations. |
| 298 | const bool useVectorAlignment; |
| 299 | const bool enableGEPInboundsNuw; |
| 300 | }; |
| 301 | |
| 302 | /// Conversion pattern for a vector.gather. |
| 303 | class VectorGatherOpConversion |
| 304 | : public ConvertOpToLLVMPattern<vector::GatherOp> { |
| 305 | public: |
| 306 | explicit VectorGatherOpConversion(const LLVMTypeConverter &typeConv, |
| 307 | bool useVectorAlign) |
| 308 | : ConvertOpToLLVMPattern<vector::GatherOp>(typeConv), |
| 309 | useVectorAlignment(useVectorAlign) {} |
| 310 | using ConvertOpToLLVMPattern<vector::GatherOp>::ConvertOpToLLVMPattern; |
| 311 | |
| 312 | LogicalResult |
| 313 | matchAndRewrite(vector::GatherOp gather, OpAdaptor adaptor, |
| 314 | ConversionPatternRewriter &rewriter) const override { |
| 315 | Location loc = gather->getLoc(); |
| 316 | MemRefType memRefType = dyn_cast<MemRefType>(Val: gather.getBaseType()); |
| 317 | assert(memRefType && "The base should be bufferized" ); |
| 318 | |
| 319 | // TODO: Add support for strided MemRef. |
| 320 | if (failed(Result: isMemRefTypeSupported(memRefType, converter: *this->getTypeConverter()))) |
| 321 | return rewriter.notifyMatchFailure(arg&: gather, msg: "memref type not supported" ); |
| 322 | |
| 323 | VectorType vType = gather.getVectorType(); |
| 324 | if (vType.getRank() > 1) { |
| 325 | return rewriter.notifyMatchFailure( |
| 326 | arg&: gather, msg: "only 1-D vectors can be lowered to LLVM" ); |
| 327 | } |
| 328 | |
| 329 | // Resolve alignment. |
| 330 | // Explicit alignment takes priority over use-vector-alignment. |
| 331 | unsigned align = gather.getAlignment().value_or(u: 0); |
| 332 | if (!align && |
| 333 | failed(Result: getVectorToLLVMAlignment(typeConverter: *this->getTypeConverter(), vectorType: vType, |
| 334 | memrefType: memRefType, align, useVectorAlignment))) |
| 335 | return rewriter.notifyMatchFailure(arg&: gather, msg: "could not resolve alignment" ); |
| 336 | |
| 337 | // Resolve address. |
| 338 | Value ptr = getStridedElementPtr(rewriter, loc, type: memRefType, |
| 339 | memRefDesc: adaptor.getBase(), indices: adaptor.getOffsets()); |
| 340 | Value base = adaptor.getBase(); |
| 341 | Value ptrs = |
| 342 | getIndexedPtrs(rewriter, loc, typeConverter: *this->getTypeConverter(), memRefType, |
| 343 | llvmMemref: base, base: ptr, index: adaptor.getIndices(), vectorType: vType); |
| 344 | |
| 345 | // Replace with the gather intrinsic. |
| 346 | rewriter.replaceOpWithNewOp<LLVM::masked_gather>( |
| 347 | op: gather, args: typeConverter->convertType(t: vType), args&: ptrs, args: adaptor.getMask(), |
| 348 | args: adaptor.getPassThru(), args&: align); |
| 349 | return success(); |
| 350 | } |
| 351 | |
| 352 | private: |
| 353 | // If true, use the preferred alignment of the vector type. |
| 354 | // If false, use the preferred alignment of the element type |
| 355 | // of the memref. This flag is intended for use with hardware |
| 356 | // backends that require alignment of vector operations. |
| 357 | const bool useVectorAlignment; |
| 358 | }; |
| 359 | |
| 360 | /// Conversion pattern for a vector.scatter. |
| 361 | class VectorScatterOpConversion |
| 362 | : public ConvertOpToLLVMPattern<vector::ScatterOp> { |
| 363 | public: |
| 364 | explicit VectorScatterOpConversion(const LLVMTypeConverter &typeConv, |
| 365 | bool useVectorAlign) |
| 366 | : ConvertOpToLLVMPattern<vector::ScatterOp>(typeConv), |
| 367 | useVectorAlignment(useVectorAlign) {} |
| 368 | |
| 369 | using ConvertOpToLLVMPattern<vector::ScatterOp>::ConvertOpToLLVMPattern; |
| 370 | |
| 371 | LogicalResult |
| 372 | matchAndRewrite(vector::ScatterOp scatter, OpAdaptor adaptor, |
| 373 | ConversionPatternRewriter &rewriter) const override { |
| 374 | auto loc = scatter->getLoc(); |
| 375 | auto memRefType = dyn_cast<MemRefType>(Val: scatter.getBaseType()); |
| 376 | assert(memRefType && "The base should be bufferized" ); |
| 377 | |
| 378 | // TODO: Add support for strided MemRef. |
| 379 | if (failed(Result: isMemRefTypeSupported(memRefType, converter: *this->getTypeConverter()))) |
| 380 | return rewriter.notifyMatchFailure(arg&: scatter, msg: "memref type not supported" ); |
| 381 | |
| 382 | VectorType vType = scatter.getVectorType(); |
| 383 | if (vType.getRank() > 1) { |
| 384 | return rewriter.notifyMatchFailure( |
| 385 | arg&: scatter, msg: "only 1-D vectors can be lowered to LLVM" ); |
| 386 | } |
| 387 | |
| 388 | // Resolve alignment. |
| 389 | // Explicit alignment takes priority over use-vector-alignment. |
| 390 | unsigned align = scatter.getAlignment().value_or(u: 0); |
| 391 | if (!align && |
| 392 | failed(Result: getVectorToLLVMAlignment(typeConverter: *this->getTypeConverter(), vectorType: vType, |
| 393 | memrefType: memRefType, align, useVectorAlignment))) |
| 394 | return rewriter.notifyMatchFailure(arg&: scatter, |
| 395 | msg: "could not resolve alignment" ); |
| 396 | |
| 397 | // Resolve address. |
| 398 | Value ptr = getStridedElementPtr(rewriter, loc, type: memRefType, |
| 399 | memRefDesc: adaptor.getBase(), indices: adaptor.getOffsets()); |
| 400 | Value ptrs = |
| 401 | getIndexedPtrs(rewriter, loc, typeConverter: *this->getTypeConverter(), memRefType, |
| 402 | llvmMemref: adaptor.getBase(), base: ptr, index: adaptor.getIndices(), vectorType: vType); |
| 403 | |
| 404 | // Replace with the scatter intrinsic. |
| 405 | rewriter.replaceOpWithNewOp<LLVM::masked_scatter>( |
| 406 | op: scatter, args: adaptor.getValueToStore(), args&: ptrs, args: adaptor.getMask(), args&: align); |
| 407 | return success(); |
| 408 | } |
| 409 | |
| 410 | private: |
| 411 | // If true, use the preferred alignment of the vector type. |
| 412 | // If false, use the preferred alignment of the element type |
| 413 | // of the memref. This flag is intended for use with hardware |
| 414 | // backends that require alignment of vector operations. |
| 415 | const bool useVectorAlignment; |
| 416 | }; |
| 417 | |
| 418 | /// Conversion pattern for a vector.expandload. |
| 419 | class VectorExpandLoadOpConversion |
| 420 | : public ConvertOpToLLVMPattern<vector::ExpandLoadOp> { |
| 421 | public: |
| 422 | using ConvertOpToLLVMPattern<vector::ExpandLoadOp>::ConvertOpToLLVMPattern; |
| 423 | |
| 424 | LogicalResult |
| 425 | matchAndRewrite(vector::ExpandLoadOp expand, OpAdaptor adaptor, |
| 426 | ConversionPatternRewriter &rewriter) const override { |
| 427 | auto loc = expand->getLoc(); |
| 428 | MemRefType memRefType = expand.getMemRefType(); |
| 429 | |
| 430 | // Resolve address. |
| 431 | auto vtype = typeConverter->convertType(t: expand.getVectorType()); |
| 432 | Value ptr = getStridedElementPtr(rewriter, loc, type: memRefType, |
| 433 | memRefDesc: adaptor.getBase(), indices: adaptor.getIndices()); |
| 434 | |
| 435 | // From: |
| 436 | // https://llvm.org/docs/LangRef.html#llvm-masked-expandload-intrinsics |
| 437 | // The pointer alignment defaults to 1. |
| 438 | uint64_t alignment = expand.getAlignment().value_or(u: 1); |
| 439 | |
| 440 | rewriter.replaceOpWithNewOp<LLVM::masked_expandload>( |
| 441 | op: expand, args&: vtype, args&: ptr, args: adaptor.getMask(), args: adaptor.getPassThru(), |
| 442 | args&: alignment); |
| 443 | return success(); |
| 444 | } |
| 445 | }; |
| 446 | |
| 447 | /// Conversion pattern for a vector.compressstore. |
| 448 | class VectorCompressStoreOpConversion |
| 449 | : public ConvertOpToLLVMPattern<vector::CompressStoreOp> { |
| 450 | public: |
| 451 | using ConvertOpToLLVMPattern<vector::CompressStoreOp>::ConvertOpToLLVMPattern; |
| 452 | |
| 453 | LogicalResult |
| 454 | matchAndRewrite(vector::CompressStoreOp compress, OpAdaptor adaptor, |
| 455 | ConversionPatternRewriter &rewriter) const override { |
| 456 | auto loc = compress->getLoc(); |
| 457 | MemRefType memRefType = compress.getMemRefType(); |
| 458 | |
| 459 | // Resolve address. |
| 460 | Value ptr = getStridedElementPtr(rewriter, loc, type: memRefType, |
| 461 | memRefDesc: adaptor.getBase(), indices: adaptor.getIndices()); |
| 462 | |
| 463 | // From: |
| 464 | // https://llvm.org/docs/LangRef.html#llvm-masked-compressstore-intrinsics |
| 465 | // The pointer alignment defaults to 1. |
| 466 | uint64_t alignment = compress.getAlignment().value_or(u: 1); |
| 467 | |
| 468 | rewriter.replaceOpWithNewOp<LLVM::masked_compressstore>( |
| 469 | op: compress, args: adaptor.getValueToStore(), args&: ptr, args: adaptor.getMask(), args&: alignment); |
| 470 | return success(); |
| 471 | } |
| 472 | }; |
| 473 | |
| 474 | /// Reduction neutral classes for overloading. |
| 475 | class ReductionNeutralZero {}; |
| 476 | class ReductionNeutralIntOne {}; |
| 477 | class ReductionNeutralFPOne {}; |
| 478 | class ReductionNeutralAllOnes {}; |
| 479 | class ReductionNeutralSIntMin {}; |
| 480 | class ReductionNeutralUIntMin {}; |
| 481 | class ReductionNeutralSIntMax {}; |
| 482 | class ReductionNeutralUIntMax {}; |
| 483 | class ReductionNeutralFPQNaN {}; |
| 484 | class ReductionNeutralFPNegQNaN {}; |
| 485 | class ReductionNeutralFPNegInf {}; |
| 486 | class ReductionNeutralFPPosInf {}; |
| 487 | class ReductionNeutralFPLowestFinite {}; |
| 488 | class ReductionNeutralFPLargestFinite {}; |
| 489 | |
| 490 | /// Create the reduction neutral zero value. |
| 491 | static Value createReductionNeutralValue(ReductionNeutralZero neutral, |
| 492 | ConversionPatternRewriter &rewriter, |
| 493 | Location loc, Type llvmType) { |
| 494 | return LLVM::ConstantOp::create(builder&: rewriter, location: loc, res: llvmType, |
| 495 | value: rewriter.getZeroAttr(type: llvmType)); |
| 496 | } |
| 497 | |
| 498 | /// Create the reduction neutral integer one value. |
| 499 | static Value createReductionNeutralValue(ReductionNeutralIntOne neutral, |
| 500 | ConversionPatternRewriter &rewriter, |
| 501 | Location loc, Type llvmType) { |
| 502 | return LLVM::ConstantOp::create(builder&: rewriter, location: loc, res: llvmType, |
| 503 | value: rewriter.getIntegerAttr(type: llvmType, value: 1)); |
| 504 | } |
| 505 | |
| 506 | /// Create the reduction neutral fp one value. |
| 507 | static Value createReductionNeutralValue(ReductionNeutralFPOne neutral, |
| 508 | ConversionPatternRewriter &rewriter, |
| 509 | Location loc, Type llvmType) { |
| 510 | return LLVM::ConstantOp::create(builder&: rewriter, location: loc, res: llvmType, |
| 511 | value: rewriter.getFloatAttr(type: llvmType, value: 1.0)); |
| 512 | } |
| 513 | |
| 514 | /// Create the reduction neutral all-ones value. |
| 515 | static Value createReductionNeutralValue(ReductionNeutralAllOnes neutral, |
| 516 | ConversionPatternRewriter &rewriter, |
| 517 | Location loc, Type llvmType) { |
| 518 | return LLVM::ConstantOp::create( |
| 519 | builder&: rewriter, location: loc, res: llvmType, |
| 520 | value: rewriter.getIntegerAttr( |
| 521 | type: llvmType, value: llvm::APInt::getAllOnes(numBits: llvmType.getIntOrFloatBitWidth()))); |
| 522 | } |
| 523 | |
| 524 | /// Create the reduction neutral signed int minimum value. |
| 525 | static Value createReductionNeutralValue(ReductionNeutralSIntMin neutral, |
| 526 | ConversionPatternRewriter &rewriter, |
| 527 | Location loc, Type llvmType) { |
| 528 | return LLVM::ConstantOp::create( |
| 529 | builder&: rewriter, location: loc, res: llvmType, |
| 530 | value: rewriter.getIntegerAttr(type: llvmType, value: llvm::APInt::getSignedMinValue( |
| 531 | numBits: llvmType.getIntOrFloatBitWidth()))); |
| 532 | } |
| 533 | |
| 534 | /// Create the reduction neutral unsigned int minimum value. |
| 535 | static Value createReductionNeutralValue(ReductionNeutralUIntMin neutral, |
| 536 | ConversionPatternRewriter &rewriter, |
| 537 | Location loc, Type llvmType) { |
| 538 | return LLVM::ConstantOp::create( |
| 539 | builder&: rewriter, location: loc, res: llvmType, |
| 540 | value: rewriter.getIntegerAttr(type: llvmType, value: llvm::APInt::getMinValue( |
| 541 | numBits: llvmType.getIntOrFloatBitWidth()))); |
| 542 | } |
| 543 | |
| 544 | /// Create the reduction neutral signed int maximum value. |
| 545 | static Value createReductionNeutralValue(ReductionNeutralSIntMax neutral, |
| 546 | ConversionPatternRewriter &rewriter, |
| 547 | Location loc, Type llvmType) { |
| 548 | return LLVM::ConstantOp::create( |
| 549 | builder&: rewriter, location: loc, res: llvmType, |
| 550 | value: rewriter.getIntegerAttr(type: llvmType, value: llvm::APInt::getSignedMaxValue( |
| 551 | numBits: llvmType.getIntOrFloatBitWidth()))); |
| 552 | } |
| 553 | |
| 554 | /// Create the reduction neutral unsigned int maximum value. |
| 555 | static Value createReductionNeutralValue(ReductionNeutralUIntMax neutral, |
| 556 | ConversionPatternRewriter &rewriter, |
| 557 | Location loc, Type llvmType) { |
| 558 | return LLVM::ConstantOp::create( |
| 559 | builder&: rewriter, location: loc, res: llvmType, |
| 560 | value: rewriter.getIntegerAttr(type: llvmType, value: llvm::APInt::getMaxValue( |
| 561 | numBits: llvmType.getIntOrFloatBitWidth()))); |
| 562 | } |
| 563 | |
| 564 | /// Create the reduction neutral quiet NaN value. |
| 565 | static Value createReductionNeutralValue(ReductionNeutralFPQNaN neutral, |
| 566 | ConversionPatternRewriter &rewriter, |
| 567 | Location loc, Type llvmType) { |
| 568 | auto floatType = cast<FloatType>(Val&: llvmType); |
| 569 | return LLVM::ConstantOp::create( |
| 570 | builder&: rewriter, location: loc, res: llvmType, |
| 571 | value: rewriter.getFloatAttr( |
| 572 | type: llvmType, value: llvm::APFloat::getQNaN(Sem: floatType.getFloatSemantics(), |
| 573 | /*Negative=*/false))); |
| 574 | } |
| 575 | |
| 576 | /// Create the reduction neutral negative quiet NaN value. |
| 577 | static Value createReductionNeutralValue(ReductionNeutralFPNegQNaN neutral, |
| 578 | ConversionPatternRewriter &rewriter, |
| 579 | Location loc, Type llvmType) { |
| 580 | auto floatType = cast<FloatType>(Val&: llvmType); |
| 581 | return LLVM::ConstantOp::create( |
| 582 | builder&: rewriter, location: loc, res: llvmType, |
| 583 | value: rewriter.getFloatAttr( |
| 584 | type: llvmType, value: llvm::APFloat::getQNaN(Sem: floatType.getFloatSemantics(), |
| 585 | /*Negative=*/true))); |
| 586 | } |
| 587 | |
| 588 | /// Create the reduction neutral negative infinity value. |
| 589 | static Value createReductionNeutralValue(ReductionNeutralFPNegInf neutral, |
| 590 | ConversionPatternRewriter &rewriter, |
| 591 | Location loc, Type llvmType) { |
| 592 | auto floatType = cast<FloatType>(Val&: llvmType); |
| 593 | return LLVM::ConstantOp::create( |
| 594 | builder&: rewriter, location: loc, res: llvmType, |
| 595 | value: rewriter.getFloatAttr(type: llvmType, |
| 596 | value: llvm::APFloat::getInf(Sem: floatType.getFloatSemantics(), |
| 597 | /*Negative=*/true))); |
| 598 | } |
| 599 | |
| 600 | /// Create the reduction neutral positive infinity value. |
| 601 | static Value createReductionNeutralValue(ReductionNeutralFPPosInf neutral, |
| 602 | ConversionPatternRewriter &rewriter, |
| 603 | Location loc, Type llvmType) { |
| 604 | auto floatType = cast<FloatType>(Val&: llvmType); |
| 605 | return LLVM::ConstantOp::create( |
| 606 | builder&: rewriter, location: loc, res: llvmType, |
| 607 | value: rewriter.getFloatAttr(type: llvmType, |
| 608 | value: llvm::APFloat::getInf(Sem: floatType.getFloatSemantics(), |
| 609 | /*Negative=*/false))); |
| 610 | } |
| 611 | |
| 612 | /// Create the reduction neutral lowest finite value. |
| 613 | static Value createReductionNeutralValue(ReductionNeutralFPLowestFinite neutral, |
| 614 | ConversionPatternRewriter &rewriter, |
| 615 | Location loc, Type llvmType) { |
| 616 | auto floatType = cast<FloatType>(Val&: llvmType); |
| 617 | return LLVM::ConstantOp::create( |
| 618 | builder&: rewriter, location: loc, res: llvmType, |
| 619 | value: rewriter.getFloatAttr( |
| 620 | type: llvmType, value: llvm::APFloat::getLargest(Sem: floatType.getFloatSemantics(), |
| 621 | /*Negative=*/true))); |
| 622 | } |
| 623 | |
| 624 | /// Create the reduction neutral largest finite value. |
| 625 | static Value |
| 626 | createReductionNeutralValue(ReductionNeutralFPLargestFinite neutral, |
| 627 | ConversionPatternRewriter &rewriter, Location loc, |
| 628 | Type llvmType) { |
| 629 | auto floatType = cast<FloatType>(Val&: llvmType); |
| 630 | return LLVM::ConstantOp::create( |
| 631 | builder&: rewriter, location: loc, res: llvmType, |
| 632 | value: rewriter.getFloatAttr( |
| 633 | type: llvmType, value: llvm::APFloat::getLargest(Sem: floatType.getFloatSemantics(), |
| 634 | /*Negative=*/false))); |
| 635 | } |
| 636 | |
| 637 | /// Returns `accumulator` if it has a valid value. Otherwise, creates and |
| 638 | /// returns a new accumulator value using `ReductionNeutral`. |
| 639 | template <class ReductionNeutral> |
| 640 | static Value getOrCreateAccumulator(ConversionPatternRewriter &rewriter, |
| 641 | Location loc, Type llvmType, |
| 642 | Value accumulator) { |
| 643 | if (accumulator) |
| 644 | return accumulator; |
| 645 | |
| 646 | return createReductionNeutralValue(ReductionNeutral(), rewriter, loc, |
| 647 | llvmType); |
| 648 | } |
| 649 | |
| 650 | /// Creates a value with the 1-D vector shape provided in `llvmType`. |
| 651 | /// This is used as effective vector length by some intrinsics supporting |
| 652 | /// dynamic vector lengths at runtime. |
| 653 | static Value createVectorLengthValue(ConversionPatternRewriter &rewriter, |
| 654 | Location loc, Type llvmType) { |
| 655 | VectorType vType = cast<VectorType>(Val&: llvmType); |
| 656 | auto vShape = vType.getShape(); |
| 657 | assert(vShape.size() == 1 && "Unexpected multi-dim vector type" ); |
| 658 | |
| 659 | Value baseVecLength = LLVM::ConstantOp::create( |
| 660 | builder&: rewriter, location: loc, res: rewriter.getI32Type(), |
| 661 | value: rewriter.getIntegerAttr(type: rewriter.getI32Type(), value: vShape[0])); |
| 662 | |
| 663 | if (!vType.getScalableDims()[0]) |
| 664 | return baseVecLength; |
| 665 | |
| 666 | // For a scalable vector type, create and return `vScale * baseVecLength`. |
| 667 | Value vScale = vector::VectorScaleOp::create(builder&: rewriter, location: loc); |
| 668 | vScale = |
| 669 | arith::IndexCastOp::create(builder&: rewriter, location: loc, out: rewriter.getI32Type(), in: vScale); |
| 670 | Value scalableVecLength = |
| 671 | arith::MulIOp::create(builder&: rewriter, location: loc, lhs: baseVecLength, rhs: vScale); |
| 672 | return scalableVecLength; |
| 673 | } |
| 674 | |
| 675 | /// Helper method to lower a `vector.reduction` op that performs an arithmetic |
| 676 | /// operation like add,mul, etc.. `VectorOp` is the LLVM vector intrinsic to use |
| 677 | /// and `ScalarOp` is the scalar operation used to add the accumulation value if |
| 678 | /// non-null. |
| 679 | template <class LLVMRedIntrinOp, class ScalarOp> |
| 680 | static Value createIntegerReductionArithmeticOpLowering( |
| 681 | ConversionPatternRewriter &rewriter, Location loc, Type llvmType, |
| 682 | Value vectorOperand, Value accumulator) { |
| 683 | |
| 684 | Value result = |
| 685 | LLVMRedIntrinOp::create(rewriter, loc, llvmType, vectorOperand); |
| 686 | |
| 687 | if (accumulator) |
| 688 | result = ScalarOp::create(rewriter, loc, accumulator, result); |
| 689 | return result; |
| 690 | } |
| 691 | |
| 692 | /// Helper method to lower a `vector.reduction` operation that performs |
| 693 | /// a comparison operation like `min`/`max`. `VectorOp` is the LLVM vector |
| 694 | /// intrinsic to use and `predicate` is the predicate to use to compare+combine |
| 695 | /// the accumulator value if non-null. |
| 696 | template <class LLVMRedIntrinOp> |
| 697 | static Value createIntegerReductionComparisonOpLowering( |
| 698 | ConversionPatternRewriter &rewriter, Location loc, Type llvmType, |
| 699 | Value vectorOperand, Value accumulator, LLVM::ICmpPredicate predicate) { |
| 700 | Value result = |
| 701 | LLVMRedIntrinOp::create(rewriter, loc, llvmType, vectorOperand); |
| 702 | if (accumulator) { |
| 703 | Value cmp = |
| 704 | LLVM::ICmpOp::create(builder&: rewriter, location: loc, predicate, lhs: accumulator, rhs: result); |
| 705 | result = LLVM::SelectOp::create(builder&: rewriter, location: loc, condition: cmp, trueValue: accumulator, falseValue: result); |
| 706 | } |
| 707 | return result; |
| 708 | } |
| 709 | |
| 710 | namespace { |
| 711 | template <typename Source> |
| 712 | struct VectorToScalarMapper; |
| 713 | template <> |
| 714 | struct VectorToScalarMapper<LLVM::vector_reduce_fmaximum> { |
| 715 | using Type = LLVM::MaximumOp; |
| 716 | }; |
| 717 | template <> |
| 718 | struct VectorToScalarMapper<LLVM::vector_reduce_fminimum> { |
| 719 | using Type = LLVM::MinimumOp; |
| 720 | }; |
| 721 | template <> |
| 722 | struct VectorToScalarMapper<LLVM::vector_reduce_fmax> { |
| 723 | using Type = LLVM::MaxNumOp; |
| 724 | }; |
| 725 | template <> |
| 726 | struct VectorToScalarMapper<LLVM::vector_reduce_fmin> { |
| 727 | using Type = LLVM::MinNumOp; |
| 728 | }; |
| 729 | template <> |
| 730 | struct VectorToScalarMapper<LLVM::vector_reduce_fmaximumnum> { |
| 731 | using Type = LLVM::MaximumNumOp; |
| 732 | }; |
| 733 | template <> |
| 734 | struct VectorToScalarMapper<LLVM::vector_reduce_fminimumnum> { |
| 735 | using Type = LLVM::MinimumNumOp; |
| 736 | }; |
| 737 | } // namespace |
| 738 | |
| 739 | template <class LLVMRedIntrinOp> |
| 740 | static Value createFPReductionComparisonOpLowering( |
| 741 | ConversionPatternRewriter &rewriter, Location loc, Type llvmType, |
| 742 | Value vectorOperand, Value accumulator, LLVM::FastmathFlagsAttr fmf) { |
| 743 | Value result = |
| 744 | LLVMRedIntrinOp::create(rewriter, loc, llvmType, vectorOperand, fmf); |
| 745 | |
| 746 | if (accumulator) { |
| 747 | result = VectorToScalarMapper<LLVMRedIntrinOp>::Type::create( |
| 748 | rewriter, loc, result, accumulator); |
| 749 | } |
| 750 | |
| 751 | return result; |
| 752 | } |
| 753 | |
| 754 | /// Mask neutral classes for overloading. |
| 755 | class MaskNeutralFMaximumNum {}; |
| 756 | class MaskNeutralFMinimumNum {}; |
| 757 | |
| 758 | /// Get the mask neutral value for a `fmaximumnum` reduction. `maximumnum` |
| 759 | /// ignores NaN operands, making a quiet NaN the identity element. When `nnan` |
| 760 | /// promises no NaN reaches the reduction, fall back to the smallest |
| 761 | /// representable value, which `ninf` further constrains to be finite. |
| 762 | static llvm::APFloat getMaskNeutralValue(MaskNeutralFMaximumNum, |
| 763 | const llvm::fltSemantics &semantics, |
| 764 | bool noNaNs, bool noInfs) { |
| 765 | if (!noNaNs) |
| 766 | return llvm::APFloat::getQNaN(Sem: semantics); |
| 767 | if (noInfs) |
| 768 | return llvm::APFloat::getLargest(Sem: semantics, /*Negative=*/true); |
| 769 | return llvm::APFloat::getInf(Sem: semantics, /*Negative=*/true); |
| 770 | } |
| 771 | |
| 772 | /// Get the mask neutral value for a `fminimumnum` reduction. See the |
| 773 | /// `fmaximumnum` overload above for the rationale. |
| 774 | static llvm::APFloat getMaskNeutralValue(MaskNeutralFMinimumNum, |
| 775 | const llvm::fltSemantics &semantics, |
| 776 | bool noNaNs, bool noInfs) { |
| 777 | if (!noNaNs) |
| 778 | return llvm::APFloat::getQNaN(Sem: semantics); |
| 779 | if (noInfs) |
| 780 | return llvm::APFloat::getLargest(Sem: semantics); |
| 781 | return llvm::APFloat::getInf(Sem: semantics); |
| 782 | } |
| 783 | |
| 784 | /// Lowers masked `fmaximumnum` and `fminimumnum` reductions using the |
| 785 | /// non-masked intrinsics, since LLVM has no predicated counterpart for them. |
| 786 | /// Inactive lanes are replaced by a mask neutral value before reducing. |
| 787 | /// TODO: Switch to `lowerPredicatedReductionWithStartValue` once |
| 788 | /// `llvm.vp.reduce.fmaximumnum`/`fminimumnum` are added to LLVM IR. |
| 789 | template <class LLVMRedIntrinOp, class MaskNeutral> |
| 790 | static Value |
| 791 | lowerMaskedReductionWithRegular(ConversionPatternRewriter &rewriter, |
| 792 | Location loc, Type llvmType, |
| 793 | Value vectorOperand, Value accumulator, |
| 794 | Value mask, LLVM::FastmathFlagsAttr fmf) { |
| 795 | const auto &floatSemantics = cast<FloatType>(Val&: llvmType).getFloatSemantics(); |
| 796 | auto value = getMaskNeutralValue( |
| 797 | MaskNeutral{}, floatSemantics, |
| 798 | LLVM::bitEnumContainsAny(bits: fmf.getValue(), bit: LLVM::FastmathFlags::nnan), |
| 799 | LLVM::bitEnumContainsAny(bits: fmf.getValue(), bit: LLVM::FastmathFlags::ninf)); |
| 800 | Type vectorType = vectorOperand.getType(); |
| 801 | auto denseValue = DenseElementsAttr::get(cast<ShapedType>(Val&: vectorType), value); |
| 802 | const Value vectorMaskNeutral = |
| 803 | LLVM::ConstantOp::create(rewriter, loc, vectorType, denseValue); |
| 804 | const Value selectedVectorByMask = LLVM::SelectOp::create( |
| 805 | builder&: rewriter, location: loc, condition: mask, trueValue: vectorOperand, falseValue: vectorMaskNeutral); |
| 806 | return createFPReductionComparisonOpLowering<LLVMRedIntrinOp>( |
| 807 | rewriter, loc, llvmType, selectedVectorByMask, accumulator, fmf); |
| 808 | } |
| 809 | |
| 810 | template <class LLVMRedIntrinOp, class ReductionNeutral> |
| 811 | static Value |
| 812 | lowerReductionWithStartValue(ConversionPatternRewriter &rewriter, Location loc, |
| 813 | Type llvmType, Value vectorOperand, |
| 814 | Value accumulator, LLVM::FastmathFlagsAttr fmf) { |
| 815 | accumulator = getOrCreateAccumulator<ReductionNeutral>(rewriter, loc, |
| 816 | llvmType, accumulator); |
| 817 | return LLVMRedIntrinOp::create(rewriter, loc, llvmType, |
| 818 | /*start_value=*/accumulator, vectorOperand, |
| 819 | fmf); |
| 820 | } |
| 821 | |
| 822 | template <class LLVMVPRedIntrinOp, class ReductionNeutral> |
| 823 | static Value lowerPredicatedReductionWithStartValue( |
| 824 | ConversionPatternRewriter &rewriter, Location loc, Type llvmType, |
| 825 | Value vectorOperand, Value accumulator, Value mask) { |
| 826 | accumulator = getOrCreateAccumulator<ReductionNeutral>(rewriter, loc, |
| 827 | llvmType, accumulator); |
| 828 | Value vectorLength = |
| 829 | createVectorLengthValue(rewriter, loc, llvmType: vectorOperand.getType()); |
| 830 | return LLVMVPRedIntrinOp::create(rewriter, loc, llvmType, |
| 831 | /*satrt_value=*/accumulator, vectorOperand, |
| 832 | mask, vectorLength); |
| 833 | } |
| 834 | |
| 835 | template <class LLVMIntVPRedIntrinOp, class IntReductionNeutral, |
| 836 | class LLVMFPVPRedIntrinOp, class FPReductionNeutral> |
| 837 | static Value lowerPredicatedReductionWithStartValue( |
| 838 | ConversionPatternRewriter &rewriter, Location loc, Type llvmType, |
| 839 | Value vectorOperand, Value accumulator, Value mask) { |
| 840 | if (llvmType.isIntOrIndex()) |
| 841 | return lowerPredicatedReductionWithStartValue<LLVMIntVPRedIntrinOp, |
| 842 | IntReductionNeutral>( |
| 843 | rewriter, loc, llvmType, vectorOperand, accumulator, mask); |
| 844 | |
| 845 | // FP dispatch. |
| 846 | return lowerPredicatedReductionWithStartValue<LLVMFPVPRedIntrinOp, |
| 847 | FPReductionNeutral>( |
| 848 | rewriter, loc, llvmType, vectorOperand, accumulator, mask); |
| 849 | } |
| 850 | |
| 851 | /// Conversion pattern for all vector reductions. |
| 852 | class VectorReductionOpConversion |
| 853 | : public ConvertOpToLLVMPattern<vector::ReductionOp> { |
| 854 | public: |
| 855 | explicit VectorReductionOpConversion(const LLVMTypeConverter &typeConv, |
| 856 | bool reassociateFPRed) |
| 857 | : ConvertOpToLLVMPattern<vector::ReductionOp>(typeConv), |
| 858 | reassociateFPReductions(reassociateFPRed) {} |
| 859 | |
| 860 | LogicalResult |
| 861 | matchAndRewrite(vector::ReductionOp reductionOp, OpAdaptor adaptor, |
| 862 | ConversionPatternRewriter &rewriter) const override { |
| 863 | auto kind = reductionOp.getKind(); |
| 864 | Type eltType = reductionOp.getResult().getType(); |
| 865 | Type llvmType = typeConverter->convertType(t: eltType); |
| 866 | Value operand = adaptor.getVector(); |
| 867 | Value acc = adaptor.getAcc(); |
| 868 | Location loc = reductionOp.getLoc(); |
| 869 | |
| 870 | if (eltType.isIntOrIndex()) { |
| 871 | // Integer reductions: add/mul/min/max/and/or/xor. |
| 872 | Value result; |
| 873 | switch (kind) { |
| 874 | case vector::CombiningKind::ADD: |
| 875 | result = |
| 876 | createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_add, |
| 877 | LLVM::AddOp>( |
| 878 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc); |
| 879 | break; |
| 880 | case vector::CombiningKind::MUL: |
| 881 | result = |
| 882 | createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_mul, |
| 883 | LLVM::MulOp>( |
| 884 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc); |
| 885 | break; |
| 886 | case vector::CombiningKind::MINUI: |
| 887 | result = createIntegerReductionComparisonOpLowering< |
| 888 | LLVM::vector_reduce_umin>(rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, |
| 889 | predicate: LLVM::ICmpPredicate::ule); |
| 890 | break; |
| 891 | case vector::CombiningKind::MINSI: |
| 892 | result = createIntegerReductionComparisonOpLowering< |
| 893 | LLVM::vector_reduce_smin>(rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, |
| 894 | predicate: LLVM::ICmpPredicate::sle); |
| 895 | break; |
| 896 | case vector::CombiningKind::MAXUI: |
| 897 | result = createIntegerReductionComparisonOpLowering< |
| 898 | LLVM::vector_reduce_umax>(rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, |
| 899 | predicate: LLVM::ICmpPredicate::uge); |
| 900 | break; |
| 901 | case vector::CombiningKind::MAXSI: |
| 902 | result = createIntegerReductionComparisonOpLowering< |
| 903 | LLVM::vector_reduce_smax>(rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, |
| 904 | predicate: LLVM::ICmpPredicate::sge); |
| 905 | break; |
| 906 | case vector::CombiningKind::AND: |
| 907 | result = |
| 908 | createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_and, |
| 909 | LLVM::AndOp>( |
| 910 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc); |
| 911 | break; |
| 912 | case vector::CombiningKind::OR: |
| 913 | result = |
| 914 | createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_or, |
| 915 | LLVM::OrOp>( |
| 916 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc); |
| 917 | break; |
| 918 | case vector::CombiningKind::XOR: |
| 919 | result = |
| 920 | createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_xor, |
| 921 | LLVM::XOrOp>( |
| 922 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc); |
| 923 | break; |
| 924 | default: |
| 925 | return failure(); |
| 926 | } |
| 927 | rewriter.replaceOp(op: reductionOp, newValues: result); |
| 928 | |
| 929 | return success(); |
| 930 | } |
| 931 | |
| 932 | if (!isa<FloatType>(Val: eltType)) |
| 933 | return failure(); |
| 934 | |
| 935 | arith::FastMathFlagsAttr fMFAttr = reductionOp.getFastMathFlagsAttr(); |
| 936 | LLVM::FastmathFlagsAttr fmf = LLVM::FastmathFlagsAttr::get( |
| 937 | context: reductionOp.getContext(), |
| 938 | value: convertArithFastMathFlagsToLLVM(arithFMF: fMFAttr.getValue())); |
| 939 | fmf = LLVM::FastmathFlagsAttr::get( |
| 940 | context: reductionOp.getContext(), |
| 941 | value: fmf.getValue() | (reassociateFPReductions ? LLVM::FastmathFlags::reassoc |
| 942 | : LLVM::FastmathFlags::none)); |
| 943 | |
| 944 | // Floating-point reductions: add/mul/min/max |
| 945 | Value result; |
| 946 | switch (kind) { |
| 947 | case vector::CombiningKind::ADD: |
| 948 | result = lowerReductionWithStartValue<LLVM::vector_reduce_fadd, |
| 949 | ReductionNeutralZero>( |
| 950 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, fmf); |
| 951 | break; |
| 952 | case vector::CombiningKind::MUL: |
| 953 | result = lowerReductionWithStartValue<LLVM::vector_reduce_fmul, |
| 954 | ReductionNeutralFPOne>( |
| 955 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, fmf); |
| 956 | break; |
| 957 | case vector::CombiningKind::MINIMUMF: |
| 958 | result = |
| 959 | createFPReductionComparisonOpLowering<LLVM::vector_reduce_fminimum>( |
| 960 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, fmf); |
| 961 | break; |
| 962 | case vector::CombiningKind::MAXIMUMF: |
| 963 | result = |
| 964 | createFPReductionComparisonOpLowering<LLVM::vector_reduce_fmaximum>( |
| 965 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, fmf); |
| 966 | break; |
| 967 | case vector::CombiningKind::MINNUMF: |
| 968 | result = createFPReductionComparisonOpLowering<LLVM::vector_reduce_fmin>( |
| 969 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, fmf); |
| 970 | break; |
| 971 | case vector::CombiningKind::MAXNUMF: |
| 972 | result = createFPReductionComparisonOpLowering<LLVM::vector_reduce_fmax>( |
| 973 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, fmf); |
| 974 | break; |
| 975 | case vector::CombiningKind::MAXIMUMNUMF: |
| 976 | result = createFPReductionComparisonOpLowering< |
| 977 | LLVM::vector_reduce_fmaximumnum>(rewriter, loc, llvmType, vectorOperand: operand, |
| 978 | accumulator: acc, fmf); |
| 979 | break; |
| 980 | case vector::CombiningKind::MINIMUMNUMF: |
| 981 | result = createFPReductionComparisonOpLowering< |
| 982 | LLVM::vector_reduce_fminimumnum>(rewriter, loc, llvmType, vectorOperand: operand, |
| 983 | accumulator: acc, fmf); |
| 984 | break; |
| 985 | default: |
| 986 | return failure(); |
| 987 | } |
| 988 | |
| 989 | rewriter.replaceOp(op: reductionOp, newValues: result); |
| 990 | return success(); |
| 991 | } |
| 992 | |
| 993 | private: |
| 994 | const bool reassociateFPReductions; |
| 995 | }; |
| 996 | |
| 997 | /// Base class to convert a `vector.mask` operation while matching traits |
| 998 | /// of the maskable operation nested inside. A `VectorMaskOpConversionBase` |
| 999 | /// instance matches against a `vector.mask` operation. The `matchAndRewrite` |
| 1000 | /// method performs a second match against the maskable operation `MaskedOp`. |
| 1001 | /// Finally, it invokes the virtual method `matchAndRewriteMaskableOp` to be |
| 1002 | /// implemented by the concrete conversion classes. This method can match |
| 1003 | /// against specific traits of the `vector.mask` and the maskable operation. It |
| 1004 | /// must replace the `vector.mask` operation. |
| 1005 | template <class MaskedOp> |
| 1006 | class VectorMaskOpConversionBase |
| 1007 | : public ConvertOpToLLVMPattern<vector::MaskOp> { |
| 1008 | public: |
| 1009 | using ConvertOpToLLVMPattern<vector::MaskOp>::ConvertOpToLLVMPattern; |
| 1010 | |
| 1011 | LogicalResult |
| 1012 | matchAndRewrite(vector::MaskOp maskOp, OpAdaptor adaptor, |
| 1013 | ConversionPatternRewriter &rewriter) const final { |
| 1014 | // Match against the maskable operation kind. |
| 1015 | auto maskedOp = llvm::dyn_cast_or_null<MaskedOp>(maskOp.getMaskableOp()); |
| 1016 | if (!maskedOp) |
| 1017 | return failure(); |
| 1018 | return matchAndRewriteMaskableOp(maskOp, maskableOp: maskedOp, rewriter); |
| 1019 | } |
| 1020 | |
| 1021 | protected: |
| 1022 | virtual LogicalResult |
| 1023 | matchAndRewriteMaskableOp(vector::MaskOp maskOp, |
| 1024 | vector::MaskableOpInterface maskableOp, |
| 1025 | ConversionPatternRewriter &rewriter) const = 0; |
| 1026 | }; |
| 1027 | |
| 1028 | class MaskedReductionOpConversion |
| 1029 | : public VectorMaskOpConversionBase<vector::ReductionOp> { |
| 1030 | |
| 1031 | public: |
| 1032 | using VectorMaskOpConversionBase< |
| 1033 | vector::ReductionOp>::VectorMaskOpConversionBase; |
| 1034 | |
| 1035 | LogicalResult matchAndRewriteMaskableOp( |
| 1036 | vector::MaskOp maskOp, MaskableOpInterface maskableOp, |
| 1037 | ConversionPatternRewriter &rewriter) const override { |
| 1038 | auto reductionOp = cast<ReductionOp>(Val: maskableOp.getOperation()); |
| 1039 | auto kind = reductionOp.getKind(); |
| 1040 | Type eltType = reductionOp.getResult().getType(); |
| 1041 | Type llvmType = typeConverter->convertType(t: eltType); |
| 1042 | Value operand = reductionOp.getVector(); |
| 1043 | Value acc = reductionOp.getAcc(); |
| 1044 | Location loc = reductionOp.getLoc(); |
| 1045 | |
| 1046 | arith::FastMathFlagsAttr fMFAttr = reductionOp.getFastMathFlagsAttr(); |
| 1047 | LLVM::FastmathFlagsAttr fmf = LLVM::FastmathFlagsAttr::get( |
| 1048 | context: reductionOp.getContext(), |
| 1049 | value: convertArithFastMathFlagsToLLVM(arithFMF: fMFAttr.getValue())); |
| 1050 | const bool noInfs = |
| 1051 | LLVM::bitEnumContainsAny(bits: fmf.getValue(), bit: LLVM::FastmathFlags::ninf); |
| 1052 | |
| 1053 | Value result; |
| 1054 | switch (kind) { |
| 1055 | case vector::CombiningKind::ADD: |
| 1056 | result = lowerPredicatedReductionWithStartValue< |
| 1057 | LLVM::VPReduceAddOp, ReductionNeutralZero, LLVM::VPReduceFAddOp, |
| 1058 | ReductionNeutralZero>(rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, |
| 1059 | mask: maskOp.getMask()); |
| 1060 | break; |
| 1061 | case vector::CombiningKind::MUL: |
| 1062 | result = lowerPredicatedReductionWithStartValue< |
| 1063 | LLVM::VPReduceMulOp, ReductionNeutralIntOne, LLVM::VPReduceFMulOp, |
| 1064 | ReductionNeutralFPOne>(rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, |
| 1065 | mask: maskOp.getMask()); |
| 1066 | break; |
| 1067 | case vector::CombiningKind::MINUI: |
| 1068 | result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceUMinOp, |
| 1069 | ReductionNeutralUIntMax>( |
| 1070 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1071 | break; |
| 1072 | case vector::CombiningKind::MINSI: |
| 1073 | result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceSMinOp, |
| 1074 | ReductionNeutralSIntMax>( |
| 1075 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1076 | break; |
| 1077 | case vector::CombiningKind::MAXUI: |
| 1078 | result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceUMaxOp, |
| 1079 | ReductionNeutralUIntMin>( |
| 1080 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1081 | break; |
| 1082 | case vector::CombiningKind::MAXSI: |
| 1083 | result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceSMaxOp, |
| 1084 | ReductionNeutralSIntMin>( |
| 1085 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1086 | break; |
| 1087 | case vector::CombiningKind::AND: |
| 1088 | result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceAndOp, |
| 1089 | ReductionNeutralAllOnes>( |
| 1090 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1091 | break; |
| 1092 | case vector::CombiningKind::OR: |
| 1093 | result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceOrOp, |
| 1094 | ReductionNeutralZero>( |
| 1095 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1096 | break; |
| 1097 | case vector::CombiningKind::XOR: |
| 1098 | result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceXorOp, |
| 1099 | ReductionNeutralZero>( |
| 1100 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1101 | break; |
| 1102 | case vector::CombiningKind::MINNUMF: |
| 1103 | result = |
| 1104 | lowerPredicatedReductionWithStartValue<LLVM::VPReduceFMinOp, |
| 1105 | ReductionNeutralFPNegQNaN>( |
| 1106 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1107 | break; |
| 1108 | case vector::CombiningKind::MAXNUMF: |
| 1109 | result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceFMaxOp, |
| 1110 | ReductionNeutralFPQNaN>( |
| 1111 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1112 | break; |
| 1113 | case CombiningKind::MAXIMUMF: |
| 1114 | // `ninf` promises no infinity reaches the reduction, so the neutral start |
| 1115 | // value must stay finite. |
| 1116 | result = |
| 1117 | noInfs |
| 1118 | ? lowerPredicatedReductionWithStartValue< |
| 1119 | LLVM::VPReduceFMaximumOp, ReductionNeutralFPLowestFinite>( |
| 1120 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()) |
| 1121 | : lowerPredicatedReductionWithStartValue< |
| 1122 | LLVM::VPReduceFMaximumOp, ReductionNeutralFPNegInf>( |
| 1123 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1124 | break; |
| 1125 | case CombiningKind::MINIMUMF: |
| 1126 | result = |
| 1127 | noInfs |
| 1128 | ? lowerPredicatedReductionWithStartValue< |
| 1129 | LLVM::VPReduceFMinimumOp, ReductionNeutralFPLargestFinite>( |
| 1130 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()) |
| 1131 | : lowerPredicatedReductionWithStartValue< |
| 1132 | LLVM::VPReduceFMinimumOp, ReductionNeutralFPPosInf>( |
| 1133 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask()); |
| 1134 | break; |
| 1135 | case CombiningKind::MAXIMUMNUMF: |
| 1136 | result = lowerMaskedReductionWithRegular<LLVM::vector_reduce_fmaximumnum, |
| 1137 | MaskNeutralFMaximumNum>( |
| 1138 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask(), fmf); |
| 1139 | break; |
| 1140 | case CombiningKind::MINIMUMNUMF: |
| 1141 | result = lowerMaskedReductionWithRegular<LLVM::vector_reduce_fminimumnum, |
| 1142 | MaskNeutralFMinimumNum>( |
| 1143 | rewriter, loc, llvmType, vectorOperand: operand, accumulator: acc, mask: maskOp.getMask(), fmf); |
| 1144 | break; |
| 1145 | } |
| 1146 | |
| 1147 | // Replace `vector.mask` operation altogether. |
| 1148 | rewriter.replaceOp(op: maskOp, newValues: result); |
| 1149 | return success(); |
| 1150 | } |
| 1151 | }; |
| 1152 | |
| 1153 | class VectorShuffleOpConversion |
| 1154 | : public ConvertOpToLLVMPattern<vector::ShuffleOp> { |
| 1155 | public: |
| 1156 | using ConvertOpToLLVMPattern<vector::ShuffleOp>::ConvertOpToLLVMPattern; |
| 1157 | |
| 1158 | LogicalResult |
| 1159 | matchAndRewrite(vector::ShuffleOp shuffleOp, OpAdaptor adaptor, |
| 1160 | ConversionPatternRewriter &rewriter) const override { |
| 1161 | auto loc = shuffleOp->getLoc(); |
| 1162 | auto v1Type = shuffleOp.getV1VectorType(); |
| 1163 | auto v2Type = shuffleOp.getV2VectorType(); |
| 1164 | auto vectorType = shuffleOp.getResultVectorType(); |
| 1165 | Type llvmType = typeConverter->convertType(t: vectorType); |
| 1166 | ArrayRef<int64_t> mask = shuffleOp.getMask(); |
| 1167 | |
| 1168 | // Bail if result type cannot be lowered. |
| 1169 | if (!llvmType) |
| 1170 | return failure(); |
| 1171 | |
| 1172 | // Get rank and dimension sizes. |
| 1173 | int64_t rank = vectorType.getRank(); |
| 1174 | #ifndef NDEBUG |
| 1175 | bool wellFormed0DCase = |
| 1176 | v1Type.getRank() == 0 && v2Type.getRank() == 0 && rank == 1; |
| 1177 | bool wellFormedNDCase = |
| 1178 | v1Type.getRank() == rank && v2Type.getRank() == rank; |
| 1179 | assert((wellFormed0DCase || wellFormedNDCase) && "op is not well-formed" ); |
| 1180 | #endif |
| 1181 | |
| 1182 | // For rank 0 and 1, where both operands have *exactly* the same vector |
| 1183 | // type, there is direct shuffle support in LLVM. Use it! |
| 1184 | if (rank <= 1 && v1Type == v2Type) { |
| 1185 | Value llvmShuffleOp = LLVM::ShuffleVectorOp::create( |
| 1186 | builder&: rewriter, location: loc, v1: adaptor.getV1(), v2: adaptor.getV2(), |
| 1187 | mask: llvm::to_vector_of<int32_t>(Range&: mask)); |
| 1188 | rewriter.replaceOp(op: shuffleOp, newValues: llvmShuffleOp); |
| 1189 | return success(); |
| 1190 | } |
| 1191 | |
| 1192 | // For all other cases, insert the individual values individually. |
| 1193 | int64_t v1Dim = v1Type.getDimSize(idx: 0); |
| 1194 | Type eltType; |
| 1195 | if (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(Val&: llvmType)) |
| 1196 | eltType = arrayType.getElementType(); |
| 1197 | else |
| 1198 | eltType = cast<VectorType>(Val&: llvmType).getElementType(); |
| 1199 | Value insert = LLVM::PoisonOp::create(builder&: rewriter, location: loc, res: llvmType); |
| 1200 | int64_t insPos = 0; |
| 1201 | for (int64_t extPos : mask) { |
| 1202 | // The result starts as poison, so a poison index needs no insertion. |
| 1203 | if (extPos == vector::ShuffleOp::kPoisonIndex) { |
| 1204 | ++insPos; |
| 1205 | continue; |
| 1206 | } |
| 1207 | Value value = adaptor.getV1(); |
| 1208 | if (extPos >= v1Dim) { |
| 1209 | extPos -= v1Dim; |
| 1210 | value = adaptor.getV2(); |
| 1211 | } |
| 1212 | Value = extractOne(rewriter, typeConverter: *getTypeConverter(), loc, val: value, |
| 1213 | llvmType: eltType, rank, pos: extPos); |
| 1214 | insert = insertOne(rewriter, typeConverter: *getTypeConverter(), loc, val1: insert, val2: extract, |
| 1215 | llvmType, rank, pos: insPos++); |
| 1216 | } |
| 1217 | rewriter.replaceOp(op: shuffleOp, newValues: insert); |
| 1218 | return success(); |
| 1219 | } |
| 1220 | }; |
| 1221 | |
| 1222 | class |
| 1223 | : public ConvertOpToLLVMPattern<vector::ExtractOp> { |
| 1224 | public: |
| 1225 | using ConvertOpToLLVMPattern<vector::ExtractOp>::ConvertOpToLLVMPattern; |
| 1226 | |
| 1227 | LogicalResult |
| 1228 | matchAndRewrite(vector::ExtractOp , OpAdaptor adaptor, |
| 1229 | ConversionPatternRewriter &rewriter) const override { |
| 1230 | auto loc = extractOp->getLoc(); |
| 1231 | auto resultType = extractOp.getResult().getType(); |
| 1232 | auto llvmResultType = typeConverter->convertType(t: resultType); |
| 1233 | // Bail if result type cannot be lowered. |
| 1234 | if (!llvmResultType) |
| 1235 | return failure(); |
| 1236 | |
| 1237 | SmallVector<OpFoldResult> positionVec = getMixedValues( |
| 1238 | staticValues: adaptor.getStaticPosition(), dynamicValues: adaptor.getDynamicPosition(), b&: rewriter); |
| 1239 | |
| 1240 | // The Vector -> LLVM lowering models N-D vectors as nested aggregates of |
| 1241 | // 1-d vectors. This nesting is modeled using arrays. We do this conversion |
| 1242 | // from a N-d vector extract to a nested aggregate vector extract in two |
| 1243 | // steps: |
| 1244 | // - Extract a member from the nested aggregate. The result can be |
| 1245 | // a lower rank nested aggregate or a vector (1-D). This is done using |
| 1246 | // `llvm.extractvalue`. |
| 1247 | // - Extract a scalar out of the vector if needed. This is done using |
| 1248 | // `llvm.extractelement`. |
| 1249 | |
| 1250 | // Determine if we need to extract a member out of the aggregate. We |
| 1251 | // always need to extract a member if the input rank >= 2. |
| 1252 | bool = extractOp.getSourceVectorType().getRank() >= 2; |
| 1253 | // Determine if we need to extract a scalar as the result. We extract |
| 1254 | // a scalar if the extract is full rank, i.e., the number of indices is |
| 1255 | // equal to source vector rank. |
| 1256 | bool = static_cast<int64_t>(positionVec.size()) == |
| 1257 | extractOp.getSourceVectorType().getRank(); |
| 1258 | |
| 1259 | // Since the LLVM type converter converts 0-d vectors to 1-d vectors, we |
| 1260 | // need to add a position for this change. |
| 1261 | if (extractOp.getSourceVectorType().getRank() == 0) { |
| 1262 | Type idxType = typeConverter->convertType(t: rewriter.getIndexType()); |
| 1263 | positionVec.push_back(Elt: rewriter.getZeroAttr(type: idxType)); |
| 1264 | } |
| 1265 | |
| 1266 | Value = adaptor.getSource(); |
| 1267 | if (extractsAggregate) { |
| 1268 | ArrayRef<OpFoldResult> position(positionVec); |
| 1269 | if (extractsScalar) { |
| 1270 | // If we are extracting a scalar from the extracted member, we drop |
| 1271 | // the last index, which will be used to extract the scalar out of the |
| 1272 | // vector. |
| 1273 | position = position.drop_back(); |
| 1274 | } |
| 1275 | // llvm.extractvalue does not support dynamic dimensions. |
| 1276 | if (!llvm::all_of(Range&: position, P: llvm::IsaPred<Attribute>)) { |
| 1277 | return failure(); |
| 1278 | } |
| 1279 | extracted = LLVM::ExtractValueOp::create(builder&: rewriter, location: loc, container: extracted, |
| 1280 | position: getAsIntegers(foldResults: position)); |
| 1281 | } |
| 1282 | |
| 1283 | if (extractsScalar) { |
| 1284 | extracted = LLVM::ExtractElementOp::create( |
| 1285 | builder&: rewriter, location: loc, vector: extracted, |
| 1286 | position: getAsLLVMValue(builder&: rewriter, loc, foldResult: positionVec.back())); |
| 1287 | } |
| 1288 | |
| 1289 | rewriter.replaceOp(op: extractOp, newValues: extracted); |
| 1290 | return success(); |
| 1291 | } |
| 1292 | }; |
| 1293 | |
| 1294 | /// Conversion pattern that turns a vector.fma on a 1-D vector |
| 1295 | /// into an llvm.intr.fmuladd. This is a trivial 1-1 conversion. |
| 1296 | /// This does not match vectors of n >= 2 rank. |
| 1297 | /// |
| 1298 | /// Example: |
| 1299 | /// ``` |
| 1300 | /// vector.fma %a, %a, %a : vector<8xf32> |
| 1301 | /// ``` |
| 1302 | /// is converted to: |
| 1303 | /// ``` |
| 1304 | /// llvm.intr.fmuladd %va, %va, %va: |
| 1305 | /// (!llvm."<8 x f32>">, !llvm<"<8 x f32>">, !llvm<"<8 x f32>">) |
| 1306 | /// -> !llvm."<8 x f32>"> |
| 1307 | /// ``` |
| 1308 | class VectorFMAOp1DConversion : public ConvertOpToLLVMPattern<vector::FMAOp> { |
| 1309 | public: |
| 1310 | using ConvertOpToLLVMPattern<vector::FMAOp>::ConvertOpToLLVMPattern; |
| 1311 | |
| 1312 | LogicalResult |
| 1313 | matchAndRewrite(vector::FMAOp fmaOp, OpAdaptor adaptor, |
| 1314 | ConversionPatternRewriter &rewriter) const override { |
| 1315 | VectorType vType = fmaOp.getVectorType(); |
| 1316 | if (vType.getRank() > 1) |
| 1317 | return failure(); |
| 1318 | |
| 1319 | rewriter.replaceOpWithNewOp<LLVM::FMulAddOp>( |
| 1320 | op: fmaOp, args: adaptor.getLhs(), args: adaptor.getRhs(), args: adaptor.getAcc()); |
| 1321 | return success(); |
| 1322 | } |
| 1323 | }; |
| 1324 | |
| 1325 | class VectorInsertOpConversion |
| 1326 | : public ConvertOpToLLVMPattern<vector::InsertOp> { |
| 1327 | public: |
| 1328 | using ConvertOpToLLVMPattern<vector::InsertOp>::ConvertOpToLLVMPattern; |
| 1329 | |
| 1330 | LogicalResult |
| 1331 | matchAndRewrite(vector::InsertOp insertOp, OpAdaptor adaptor, |
| 1332 | ConversionPatternRewriter &rewriter) const override { |
| 1333 | auto loc = insertOp->getLoc(); |
| 1334 | auto destVectorType = insertOp.getDestVectorType(); |
| 1335 | auto llvmResultType = typeConverter->convertType(t: destVectorType); |
| 1336 | // Bail if result type cannot be lowered. |
| 1337 | if (!llvmResultType) |
| 1338 | return failure(); |
| 1339 | |
| 1340 | SmallVector<OpFoldResult> positionVec = getMixedValues( |
| 1341 | staticValues: adaptor.getStaticPosition(), dynamicValues: adaptor.getDynamicPosition(), b&: rewriter); |
| 1342 | |
| 1343 | // The logic in this pattern mirrors VectorExtractOpConversion. Refer to |
| 1344 | // its explanatory comment about how N-D vectors are converted as nested |
| 1345 | // aggregates (llvm.array's) of 1D vectors. |
| 1346 | // |
| 1347 | // The innermost dimension of the destination vector, when converted to a |
| 1348 | // nested aggregate form, will always be a 1D vector. |
| 1349 | // |
| 1350 | // * If the insertion is happening into the innermost dimension of the |
| 1351 | // destination vector: |
| 1352 | // - If the destination is a nested aggregate, extract a 1D vector out of |
| 1353 | // the aggregate. This can be done using llvm.extractvalue. The |
| 1354 | // destination is now guaranteed to be a 1D vector, to which we are |
| 1355 | // inserting. |
| 1356 | // - Do the insertion into the 1D destination vector, and make the result |
| 1357 | // the new source nested aggregate. This can be done using |
| 1358 | // llvm.insertelement. |
| 1359 | // * Insert the source nested aggregate into the destination nested |
| 1360 | // aggregate. |
| 1361 | |
| 1362 | // Determine if we need to extract/insert a 1D vector out of the aggregate. |
| 1363 | bool isNestedAggregate = isa<LLVM::LLVMArrayType>(Val: llvmResultType); |
| 1364 | // Determine if we need to insert a scalar into the 1D vector. |
| 1365 | bool insertIntoInnermostDim = |
| 1366 | static_cast<int64_t>(positionVec.size()) == destVectorType.getRank(); |
| 1367 | |
| 1368 | ArrayRef<OpFoldResult> positionOf1DVectorWithinAggregate( |
| 1369 | positionVec.begin(), |
| 1370 | insertIntoInnermostDim ? positionVec.size() - 1 : positionVec.size()); |
| 1371 | OpFoldResult positionOfScalarWithin1DVector; |
| 1372 | if (destVectorType.getRank() == 0) { |
| 1373 | // Since the LLVM type converter converts 0D vectors to 1D vectors, we |
| 1374 | // need to create a 0 here as the position into the 1D vector. |
| 1375 | Type idxType = typeConverter->convertType(t: rewriter.getIndexType()); |
| 1376 | positionOfScalarWithin1DVector = rewriter.getZeroAttr(type: idxType); |
| 1377 | } else if (insertIntoInnermostDim) { |
| 1378 | positionOfScalarWithin1DVector = positionVec.back(); |
| 1379 | } |
| 1380 | |
| 1381 | // We are going to mutate this 1D vector until it is either the final |
| 1382 | // result (in the non-aggregate case) or the value that needs to be |
| 1383 | // inserted into the aggregate result. |
| 1384 | Value sourceAggregate = adaptor.getValueToStore(); |
| 1385 | if (insertIntoInnermostDim) { |
| 1386 | // Scalar-into-1D-vector case, so we know we will have to create a |
| 1387 | // InsertElementOp. The question is into what destination. |
| 1388 | if (isNestedAggregate) { |
| 1389 | // Aggregate case: the destination for the InsertElementOp needs to be |
| 1390 | // extracted from the aggregate. |
| 1391 | if (!llvm::all_of(Range&: positionOf1DVectorWithinAggregate, |
| 1392 | P: llvm::IsaPred<Attribute>)) { |
| 1393 | // llvm.extractvalue does not support dynamic dimensions. |
| 1394 | return failure(); |
| 1395 | } |
| 1396 | sourceAggregate = LLVM::ExtractValueOp::create( |
| 1397 | builder&: rewriter, location: loc, container: adaptor.getDest(), |
| 1398 | position: getAsIntegers(foldResults: positionOf1DVectorWithinAggregate)); |
| 1399 | } else { |
| 1400 | // No-aggregate case. The destination for the InsertElementOp is just |
| 1401 | // the insertOp's destination. |
| 1402 | sourceAggregate = adaptor.getDest(); |
| 1403 | } |
| 1404 | // Insert the scalar into the 1D vector. |
| 1405 | sourceAggregate = LLVM::InsertElementOp::create( |
| 1406 | builder&: rewriter, location: loc, res: sourceAggregate.getType(), vector: sourceAggregate, |
| 1407 | value: adaptor.getValueToStore(), |
| 1408 | position: getAsLLVMValue(builder&: rewriter, loc, foldResult: positionOfScalarWithin1DVector)); |
| 1409 | } |
| 1410 | |
| 1411 | Value result = sourceAggregate; |
| 1412 | if (isNestedAggregate) { |
| 1413 | if (!llvm::all_of(Range&: positionOf1DVectorWithinAggregate, |
| 1414 | P: llvm::IsaPred<Attribute>)) { |
| 1415 | // llvm.insertvalue does not support dynamic dimensions. |
| 1416 | return failure(); |
| 1417 | } |
| 1418 | result = LLVM::InsertValueOp::create( |
| 1419 | builder&: rewriter, location: loc, container: adaptor.getDest(), value: sourceAggregate, |
| 1420 | position: getAsIntegers(foldResults: positionOf1DVectorWithinAggregate)); |
| 1421 | } |
| 1422 | |
| 1423 | rewriter.replaceOp(op: insertOp, newValues: result); |
| 1424 | return success(); |
| 1425 | } |
| 1426 | }; |
| 1427 | |
| 1428 | /// Lower vector.scalable.insert ops to LLVM vector.insert |
| 1429 | struct VectorScalableInsertOpLowering |
| 1430 | : public ConvertOpToLLVMPattern<vector::ScalableInsertOp> { |
| 1431 | using ConvertOpToLLVMPattern< |
| 1432 | vector::ScalableInsertOp>::ConvertOpToLLVMPattern; |
| 1433 | |
| 1434 | LogicalResult |
| 1435 | matchAndRewrite(vector::ScalableInsertOp insOp, OpAdaptor adaptor, |
| 1436 | ConversionPatternRewriter &rewriter) const override { |
| 1437 | rewriter.replaceOpWithNewOp<LLVM::vector_insert>( |
| 1438 | op: insOp, args: adaptor.getDest(), args: adaptor.getValueToStore(), args: adaptor.getPos()); |
| 1439 | return success(); |
| 1440 | } |
| 1441 | }; |
| 1442 | |
| 1443 | /// Lower vector.scalable.extract ops to LLVM vector.extract |
| 1444 | struct |
| 1445 | : public ConvertOpToLLVMPattern<vector::ScalableExtractOp> { |
| 1446 | using ConvertOpToLLVMPattern< |
| 1447 | vector::ScalableExtractOp>::ConvertOpToLLVMPattern; |
| 1448 | |
| 1449 | LogicalResult |
| 1450 | matchAndRewrite(vector::ScalableExtractOp extOp, OpAdaptor adaptor, |
| 1451 | ConversionPatternRewriter &rewriter) const override { |
| 1452 | rewriter.replaceOpWithNewOp<LLVM::vector_extract>( |
| 1453 | op: extOp, args: typeConverter->convertType(t: extOp.getResultVectorType()), |
| 1454 | args: adaptor.getSource(), args: adaptor.getPos()); |
| 1455 | return success(); |
| 1456 | } |
| 1457 | }; |
| 1458 | |
| 1459 | /// Rank reducing rewrite for n-D FMA into (n-1)-D FMA where n > 1. |
| 1460 | /// |
| 1461 | /// Example: |
| 1462 | /// ``` |
| 1463 | /// %d = vector.fma %a, %b, %c : vector<2x4xf32> |
| 1464 | /// ``` |
| 1465 | /// is rewritten into: |
| 1466 | /// ``` |
| 1467 | /// %r = vector.broadcast %f0 : f32 to vector<2x4xf32> |
| 1468 | /// %va = vector.extractvalue %a[0] : vector<2x4xf32> |
| 1469 | /// %vb = vector.extractvalue %b[0] : vector<2x4xf32> |
| 1470 | /// %vc = vector.extractvalue %c[0] : vector<2x4xf32> |
| 1471 | /// %vd = vector.fma %va, %vb, %vc : vector<4xf32> |
| 1472 | /// %r2 = vector.insertvalue %vd, %r[0] : vector<4xf32> into vector<2x4xf32> |
| 1473 | /// %va2 = vector.extractvalue %a2[1] : vector<2x4xf32> |
| 1474 | /// %vb2 = vector.extractvalue %b2[1] : vector<2x4xf32> |
| 1475 | /// %vc2 = vector.extractvalue %c2[1] : vector<2x4xf32> |
| 1476 | /// %vd2 = vector.fma %va2, %vb2, %vc2 : vector<4xf32> |
| 1477 | /// %r3 = vector.insertvalue %vd2, %r2[1] : vector<4xf32> into vector<2x4xf32> |
| 1478 | /// // %r3 holds the final value. |
| 1479 | /// ``` |
| 1480 | class VectorFMAOpNDRewritePattern : public OpRewritePattern<FMAOp> { |
| 1481 | public: |
| 1482 | using Base::Base; |
| 1483 | |
| 1484 | void initialize() { |
| 1485 | // This pattern recursively unpacks one dimension at a time. The recursion |
| 1486 | // bounded as the rank is strictly decreasing. |
| 1487 | setHasBoundedRewriteRecursion(); |
| 1488 | } |
| 1489 | |
| 1490 | LogicalResult matchAndRewrite(FMAOp op, |
| 1491 | PatternRewriter &rewriter) const override { |
| 1492 | auto vType = op.getVectorType(); |
| 1493 | if (vType.getRank() < 2) |
| 1494 | return failure(); |
| 1495 | |
| 1496 | auto loc = op.getLoc(); |
| 1497 | auto elemType = vType.getElementType(); |
| 1498 | Value zero = arith::ConstantOp::create(builder&: rewriter, location: loc, result: elemType, |
| 1499 | value: rewriter.getZeroAttr(type: elemType)); |
| 1500 | Value desc = vector::BroadcastOp::create(builder&: rewriter, location: loc, result: vType, source: zero); |
| 1501 | for (int64_t i = 0, e = vType.getShape().front(); i != e; ++i) { |
| 1502 | Value extrLHS = ExtractOp::create(builder&: rewriter, location: loc, source: op.getLhs(), position: i); |
| 1503 | Value extrRHS = ExtractOp::create(builder&: rewriter, location: loc, source: op.getRhs(), position: i); |
| 1504 | Value = ExtractOp::create(builder&: rewriter, location: loc, source: op.getAcc(), position: i); |
| 1505 | Value fma = FMAOp::create(builder&: rewriter, location: loc, lhs: extrLHS, rhs: extrRHS, acc: extrACC); |
| 1506 | desc = InsertOp::create(builder&: rewriter, location: loc, valueToStore: fma, dest: desc, position: i); |
| 1507 | } |
| 1508 | rewriter.replaceOp(op, newValues: desc); |
| 1509 | return success(); |
| 1510 | } |
| 1511 | }; |
| 1512 | |
| 1513 | /// Returns the strides if the memory underlying `memRefType` has a contiguous |
| 1514 | /// static layout. |
| 1515 | static std::optional<SmallVector<int64_t, 4>> |
| 1516 | computeContiguousStrides(MemRefType memRefType) { |
| 1517 | int64_t offset; |
| 1518 | SmallVector<int64_t, 4> strides; |
| 1519 | if (failed(Result: memRefType.getStridesAndOffset(strides, offset))) |
| 1520 | return std::nullopt; |
| 1521 | if (!strides.empty() && strides.back() != 1) |
| 1522 | return std::nullopt; |
| 1523 | // If no layout or identity layout, this is contiguous by definition. |
| 1524 | if (memRefType.getLayout().isIdentity()) |
| 1525 | return strides; |
| 1526 | |
| 1527 | // Otherwise, we must determine contiguity form shapes. This can only ever |
| 1528 | // work in static cases because MemRefType is underspecified to represent |
| 1529 | // contiguous dynamic shapes in other ways than with just empty/identity |
| 1530 | // layout. |
| 1531 | auto sizes = memRefType.getShape(); |
| 1532 | for (int index = 0, e = strides.size() - 1; index < e; ++index) { |
| 1533 | if (ShapedType::isDynamic(dValue: sizes[index + 1]) || |
| 1534 | ShapedType::isDynamic(dValue: strides[index]) || |
| 1535 | ShapedType::isDynamic(dValue: strides[index + 1])) |
| 1536 | return std::nullopt; |
| 1537 | if (strides[index] != strides[index + 1] * sizes[index + 1]) |
| 1538 | return std::nullopt; |
| 1539 | } |
| 1540 | return strides; |
| 1541 | } |
| 1542 | |
| 1543 | class VectorTypeCastOpConversion |
| 1544 | : public ConvertOpToLLVMPattern<vector::TypeCastOp> { |
| 1545 | public: |
| 1546 | using ConvertOpToLLVMPattern<vector::TypeCastOp>::ConvertOpToLLVMPattern; |
| 1547 | |
| 1548 | LogicalResult |
| 1549 | matchAndRewrite(vector::TypeCastOp castOp, OpAdaptor adaptor, |
| 1550 | ConversionPatternRewriter &rewriter) const override { |
| 1551 | auto loc = castOp->getLoc(); |
| 1552 | MemRefType sourceMemRefType = |
| 1553 | cast<MemRefType>(Val: castOp.getOperand().getType()); |
| 1554 | MemRefType targetMemRefType = castOp.getType(); |
| 1555 | |
| 1556 | // Only static shape casts supported atm. |
| 1557 | if (!sourceMemRefType.hasStaticShape() || |
| 1558 | !targetMemRefType.hasStaticShape()) |
| 1559 | return failure(); |
| 1560 | |
| 1561 | auto llvmSourceDescriptorTy = |
| 1562 | dyn_cast<LLVM::LLVMStructType>(Val: adaptor.getOperands()[0].getType()); |
| 1563 | if (!llvmSourceDescriptorTy) |
| 1564 | return failure(); |
| 1565 | MemRefDescriptor sourceMemRef(adaptor.getOperands()[0]); |
| 1566 | |
| 1567 | auto llvmTargetDescriptorTy = dyn_cast_or_null<LLVM::LLVMStructType>( |
| 1568 | Val: typeConverter->convertType(t: targetMemRefType)); |
| 1569 | if (!llvmTargetDescriptorTy) |
| 1570 | return failure(); |
| 1571 | |
| 1572 | // Only contiguous source buffers supported atm. |
| 1573 | auto sourceStrides = computeContiguousStrides(memRefType: sourceMemRefType); |
| 1574 | if (!sourceStrides) |
| 1575 | return failure(); |
| 1576 | auto targetStrides = computeContiguousStrides(memRefType: targetMemRefType); |
| 1577 | if (!targetStrides) |
| 1578 | return failure(); |
| 1579 | // Only support static strides for now, regardless of contiguity. |
| 1580 | if (llvm::any_of(Range&: *targetStrides, P: ShapedType::isDynamic)) |
| 1581 | return failure(); |
| 1582 | |
| 1583 | // The offset, size and stride fields of a memref descriptor use the |
| 1584 | // converted index type. |
| 1585 | Type indexTy = getTypeConverter()->getIndexType(); |
| 1586 | |
| 1587 | // Create descriptor. |
| 1588 | auto desc = MemRefDescriptor::poison(builder&: rewriter, loc, descriptorType: llvmTargetDescriptorTy); |
| 1589 | // Set allocated ptr. |
| 1590 | Value allocated = sourceMemRef.allocatedPtr(builder&: rewriter, loc); |
| 1591 | desc.setAllocatedPtr(builder&: rewriter, loc, ptr: allocated); |
| 1592 | |
| 1593 | // Set aligned ptr. |
| 1594 | Value ptr = sourceMemRef.alignedPtr(builder&: rewriter, loc); |
| 1595 | desc.setAlignedPtr(builder&: rewriter, loc, ptr); |
| 1596 | // Fill offset 0. |
| 1597 | desc.setOffset(builder&: rewriter, loc, |
| 1598 | offset: LLVM::createIndexAttrConstant(builder&: rewriter, loc, resultType: indexTy, value: 0)); |
| 1599 | |
| 1600 | // Fill size and stride descriptors in memref. |
| 1601 | for (const auto &indexedSize : |
| 1602 | llvm::enumerate(First: targetMemRefType.getShape())) { |
| 1603 | int64_t index = indexedSize.index(); |
| 1604 | desc.setSize(builder&: rewriter, loc, pos: index, |
| 1605 | size: LLVM::createIndexAttrConstant(builder&: rewriter, loc, resultType: indexTy, |
| 1606 | value: indexedSize.value())); |
| 1607 | desc.setStride(builder&: rewriter, loc, pos: index, |
| 1608 | stride: LLVM::createIndexAttrConstant(builder&: rewriter, loc, resultType: indexTy, |
| 1609 | value: (*targetStrides)[index])); |
| 1610 | } |
| 1611 | |
| 1612 | rewriter.replaceOp(op: castOp, newValues: {desc}); |
| 1613 | return success(); |
| 1614 | } |
| 1615 | }; |
| 1616 | |
| 1617 | /// Conversion pattern for a `vector.create_mask` (1-D scalable vectors only). |
| 1618 | /// Non-scalable versions of this operation are handled in Vector Transforms. |
| 1619 | class VectorCreateMaskOpConversion |
| 1620 | : public OpConversionPattern<vector::CreateMaskOp> { |
| 1621 | public: |
| 1622 | explicit VectorCreateMaskOpConversion(MLIRContext *context, |
| 1623 | bool enableIndexOpt) |
| 1624 | : OpConversionPattern<vector::CreateMaskOp>(context), |
| 1625 | force32BitVectorIndices(enableIndexOpt) {} |
| 1626 | |
| 1627 | LogicalResult |
| 1628 | matchAndRewrite(vector::CreateMaskOp op, OpAdaptor adaptor, |
| 1629 | ConversionPatternRewriter &rewriter) const override { |
| 1630 | auto dstType = op.getType(); |
| 1631 | if (dstType.getRank() != 1 || !cast<VectorType>(Val&: dstType).isScalable()) |
| 1632 | return failure(); |
| 1633 | IntegerType idxType = |
| 1634 | force32BitVectorIndices ? rewriter.getI32Type() : rewriter.getI64Type(); |
| 1635 | auto loc = op->getLoc(); |
| 1636 | Value indices = LLVM::StepVectorOp::create( |
| 1637 | builder&: rewriter, location: loc, |
| 1638 | res: LLVM::getVectorType(elementType: idxType, numElements: dstType.getShape()[0], |
| 1639 | /*isScalable=*/true)); |
| 1640 | Value maskBound = adaptor.getOperands()[0]; |
| 1641 | // When using 32-bit indices, cap the bound at INT32_MAX in index type |
| 1642 | // before casting. For scalable vectors the runtime size (vscale * dim) is |
| 1643 | // unknown at compile time, so we can't clamp to `dim` as in the fixed-size |
| 1644 | // path. Clamping to INT32_MAX is safe because any realistic scalable vector |
| 1645 | // size fits well below this limit, so a bound >= vscale*dim still produces |
| 1646 | // an all-true mask after the comparison. |
| 1647 | if (force32BitVectorIndices) { |
| 1648 | Value maxBound = |
| 1649 | arith::ConstantIndexOp::create(builder&: rewriter, location: loc, value: (1LL << 31) - 1); |
| 1650 | maskBound = arith::MinSIOp::create(builder&: rewriter, location: loc, lhs: maskBound, rhs: maxBound); |
| 1651 | } |
| 1652 | auto bound = |
| 1653 | getValueOrCreateCastToIndexLike(b&: rewriter, loc, targetType: idxType, value: maskBound); |
| 1654 | Value bounds = BroadcastOp::create(builder&: rewriter, location: loc, result: indices.getType(), source: bound); |
| 1655 | Value comp = arith::CmpIOp::create(builder&: rewriter, location: loc, predicate: arith::CmpIPredicate::slt, |
| 1656 | lhs: indices, rhs: bounds); |
| 1657 | rewriter.replaceOp(op, newValues: comp); |
| 1658 | return success(); |
| 1659 | } |
| 1660 | |
| 1661 | private: |
| 1662 | const bool force32BitVectorIndices; |
| 1663 | }; |
| 1664 | |
| 1665 | class VectorPrintOpConversion : public ConvertOpToLLVMPattern<vector::PrintOp> { |
| 1666 | SymbolTableCollection *symbolTables = nullptr; |
| 1667 | |
| 1668 | public: |
| 1669 | explicit VectorPrintOpConversion( |
| 1670 | const LLVMTypeConverter &typeConverter, |
| 1671 | SymbolTableCollection *symbolTables = nullptr) |
| 1672 | : ConvertOpToLLVMPattern<vector::PrintOp>(typeConverter), |
| 1673 | symbolTables(symbolTables) {} |
| 1674 | |
| 1675 | // Lowering implementation that relies on a small runtime support library, |
| 1676 | // which only needs to provide a few printing methods (single value for all |
| 1677 | // data types, opening/closing bracket, comma, newline). The lowering splits |
| 1678 | // the vector into elementary printing operations. The advantage of this |
| 1679 | // approach is that the library can remain unaware of all low-level |
| 1680 | // implementation details of vectors while still supporting output of any |
| 1681 | // shaped and dimensioned vector. |
| 1682 | // |
| 1683 | // Note: This lowering only handles scalars, n-D vectors are broken into |
| 1684 | // printing scalars in loops in VectorToSCF. |
| 1685 | // |
| 1686 | // TODO: rely solely on libc in future? something else? |
| 1687 | // |
| 1688 | LogicalResult |
| 1689 | matchAndRewrite(vector::PrintOp printOp, OpAdaptor adaptor, |
| 1690 | ConversionPatternRewriter &rewriter) const override { |
| 1691 | auto parent = printOp->getParentOfType<ModuleOp>(); |
| 1692 | if (!parent) |
| 1693 | return failure(); |
| 1694 | |
| 1695 | auto loc = printOp->getLoc(); |
| 1696 | |
| 1697 | if (auto value = adaptor.getSource()) { |
| 1698 | Type printType = printOp.getPrintType(); |
| 1699 | if (isa<VectorType>(Val: printType)) { |
| 1700 | // Vectors should be broken into elementary print ops in VectorToSCF. |
| 1701 | return failure(); |
| 1702 | } |
| 1703 | if (failed(Result: emitScalarPrint(rewriter, parent, loc, printType, value))) |
| 1704 | return failure(); |
| 1705 | } |
| 1706 | |
| 1707 | auto punct = printOp.getPunctuation(); |
| 1708 | if (auto stringLiteral = printOp.getStringLiteral()) { |
| 1709 | auto createResult = |
| 1710 | LLVM::createPrintStrCall(builder&: rewriter, loc, moduleOp: parent, symbolName: "vector_print_str" , |
| 1711 | string: *stringLiteral, typeConverter: *getTypeConverter(), |
| 1712 | /*addNewline=*/false); |
| 1713 | if (createResult.failed()) |
| 1714 | return failure(); |
| 1715 | |
| 1716 | } else if (punct != PrintPunctuation::NoPunctuation) { |
| 1717 | FailureOr<LLVM::LLVMFuncOp> op = [&]() { |
| 1718 | switch (punct) { |
| 1719 | case PrintPunctuation::Close: |
| 1720 | return LLVM::lookupOrCreatePrintCloseFn(b&: rewriter, moduleOp: parent, |
| 1721 | symbolTables); |
| 1722 | case PrintPunctuation::Open: |
| 1723 | return LLVM::lookupOrCreatePrintOpenFn(b&: rewriter, moduleOp: parent, |
| 1724 | symbolTables); |
| 1725 | case PrintPunctuation::Comma: |
| 1726 | return LLVM::lookupOrCreatePrintCommaFn(b&: rewriter, moduleOp: parent, |
| 1727 | symbolTables); |
| 1728 | case PrintPunctuation::NewLine: |
| 1729 | return LLVM::lookupOrCreatePrintNewlineFn(b&: rewriter, moduleOp: parent, |
| 1730 | symbolTables); |
| 1731 | default: |
| 1732 | llvm_unreachable("unexpected punctuation" ); |
| 1733 | } |
| 1734 | }(); |
| 1735 | if (failed(Result: op)) |
| 1736 | return failure(); |
| 1737 | emitCall(rewriter, loc: printOp->getLoc(), ref: op.value()); |
| 1738 | } |
| 1739 | |
| 1740 | rewriter.eraseOp(op: printOp); |
| 1741 | return success(); |
| 1742 | } |
| 1743 | |
| 1744 | private: |
| 1745 | enum class PrintConversion { |
| 1746 | // clang-format off |
| 1747 | None, |
| 1748 | ZeroExt64, |
| 1749 | SignExt64, |
| 1750 | Bitcast16 |
| 1751 | // clang-format on |
| 1752 | }; |
| 1753 | |
| 1754 | LogicalResult emitScalarPrint(ConversionPatternRewriter &rewriter, |
| 1755 | ModuleOp parent, Location loc, Type printType, |
| 1756 | Value value) const { |
| 1757 | if (typeConverter->convertType(t: printType) == nullptr) |
| 1758 | return failure(); |
| 1759 | |
| 1760 | // Make sure element type has runtime support. |
| 1761 | PrintConversion conversion = PrintConversion::None; |
| 1762 | FailureOr<Operation *> printer; |
| 1763 | if (printType.isF32()) { |
| 1764 | printer = LLVM::lookupOrCreatePrintF32Fn(b&: rewriter, moduleOp: parent, symbolTables); |
| 1765 | } else if (printType.isF64()) { |
| 1766 | printer = LLVM::lookupOrCreatePrintF64Fn(b&: rewriter, moduleOp: parent, symbolTables); |
| 1767 | } else if (printType.isF16()) { |
| 1768 | conversion = PrintConversion::Bitcast16; // bits! |
| 1769 | printer = LLVM::lookupOrCreatePrintF16Fn(b&: rewriter, moduleOp: parent, symbolTables); |
| 1770 | } else if (printType.isBF16()) { |
| 1771 | conversion = PrintConversion::Bitcast16; // bits! |
| 1772 | printer = LLVM::lookupOrCreatePrintBF16Fn(b&: rewriter, moduleOp: parent, symbolTables); |
| 1773 | } else if (printType.isIndex()) { |
| 1774 | printer = LLVM::lookupOrCreatePrintU64Fn(b&: rewriter, moduleOp: parent, symbolTables); |
| 1775 | } else if (auto intTy = dyn_cast<IntegerType>(Val&: printType)) { |
| 1776 | // Integers need a zero or sign extension on the operand |
| 1777 | // (depending on the source type) as well as a signed or |
| 1778 | // unsigned print method. Up to 64-bit is supported. |
| 1779 | unsigned width = intTy.getWidth(); |
| 1780 | if (intTy.isUnsigned()) { |
| 1781 | if (width <= 64) { |
| 1782 | if (width < 64) |
| 1783 | conversion = PrintConversion::ZeroExt64; |
| 1784 | printer = |
| 1785 | LLVM::lookupOrCreatePrintU64Fn(b&: rewriter, moduleOp: parent, symbolTables); |
| 1786 | } else { |
| 1787 | return failure(); |
| 1788 | } |
| 1789 | } else { |
| 1790 | assert(intTy.isSignless() || intTy.isSigned()); |
| 1791 | if (width <= 64) { |
| 1792 | // Note that we *always* zero extend booleans (1-bit integers), |
| 1793 | // so that true/false is printed as 1/0 rather than -1/0. |
| 1794 | if (width == 1) |
| 1795 | conversion = PrintConversion::ZeroExt64; |
| 1796 | else if (width < 64) |
| 1797 | conversion = PrintConversion::SignExt64; |
| 1798 | printer = |
| 1799 | LLVM::lookupOrCreatePrintI64Fn(b&: rewriter, moduleOp: parent, symbolTables); |
| 1800 | } else { |
| 1801 | return failure(); |
| 1802 | } |
| 1803 | } |
| 1804 | } else if (auto floatTy = dyn_cast<FloatType>(Val&: printType)) { |
| 1805 | // Print other floating-point types using the APFloat runtime library. |
| 1806 | int32_t sem = |
| 1807 | llvm::APFloatBase::SemanticsToEnum(Sem: floatTy.getFloatSemantics()); |
| 1808 | Value semValue = LLVM::ConstantOp::create( |
| 1809 | builder&: rewriter, location: loc, res: rewriter.getI32Type(), |
| 1810 | value: rewriter.getIntegerAttr(type: rewriter.getI32Type(), value: sem)); |
| 1811 | Value floatBits = |
| 1812 | LLVM::ZExtOp::create(builder&: rewriter, location: loc, res: rewriter.getI64Type(), arg: value); |
| 1813 | printer = |
| 1814 | LLVM::lookupOrCreateApFloatPrintFn(b&: rewriter, moduleOp: parent, symbolTables); |
| 1815 | emitCall(rewriter, loc, ref: printer.value(), |
| 1816 | params: ValueRange({semValue, floatBits})); |
| 1817 | return success(); |
| 1818 | } else { |
| 1819 | return failure(); |
| 1820 | } |
| 1821 | if (failed(Result: printer)) |
| 1822 | return failure(); |
| 1823 | |
| 1824 | switch (conversion) { |
| 1825 | case PrintConversion::ZeroExt64: |
| 1826 | value = arith::ExtUIOp::create( |
| 1827 | builder&: rewriter, location: loc, out: IntegerType::get(context: rewriter.getContext(), width: 64), in: value); |
| 1828 | break; |
| 1829 | case PrintConversion::SignExt64: |
| 1830 | value = arith::ExtSIOp::create( |
| 1831 | builder&: rewriter, location: loc, out: IntegerType::get(context: rewriter.getContext(), width: 64), in: value); |
| 1832 | break; |
| 1833 | case PrintConversion::Bitcast16: |
| 1834 | value = LLVM::BitcastOp::create( |
| 1835 | builder&: rewriter, location: loc, res: IntegerType::get(context: rewriter.getContext(), width: 16), arg: value); |
| 1836 | break; |
| 1837 | case PrintConversion::None: |
| 1838 | break; |
| 1839 | } |
| 1840 | emitCall(rewriter, loc, ref: printer.value(), params: value); |
| 1841 | return success(); |
| 1842 | } |
| 1843 | |
| 1844 | // Helper to emit a call. |
| 1845 | static void emitCall(ConversionPatternRewriter &rewriter, Location loc, |
| 1846 | Operation *ref, ValueRange params = ValueRange()) { |
| 1847 | LLVM::CallOp::create(builder&: rewriter, location: loc, results: TypeRange(), callee: SymbolRefAttr::get(symbol: ref), |
| 1848 | args: params); |
| 1849 | } |
| 1850 | }; |
| 1851 | |
| 1852 | /// A broadcast of a scalar is lowered to an insertelement + a shufflevector |
| 1853 | /// operation. Only broadcasts to 0-d and 1-d vectors are lowered by this |
| 1854 | /// pattern, the higher rank cases are handled by another pattern. |
| 1855 | struct VectorBroadcastScalarToLowRankLowering |
| 1856 | : public ConvertOpToLLVMPattern<vector::BroadcastOp> { |
| 1857 | using ConvertOpToLLVMPattern<vector::BroadcastOp>::ConvertOpToLLVMPattern; |
| 1858 | |
| 1859 | LogicalResult |
| 1860 | matchAndRewrite(vector::BroadcastOp broadcast, OpAdaptor adaptor, |
| 1861 | ConversionPatternRewriter &rewriter) const override { |
| 1862 | if (isa<VectorType>(Val: broadcast.getSourceType())) |
| 1863 | return rewriter.notifyMatchFailure( |
| 1864 | arg&: broadcast, msg: "broadcast from vector type not handled" ); |
| 1865 | |
| 1866 | VectorType resultType = broadcast.getType(); |
| 1867 | if (resultType.getRank() > 1) |
| 1868 | return rewriter.notifyMatchFailure(arg&: broadcast, |
| 1869 | msg: "broadcast to 2+-d handled elsewhere" ); |
| 1870 | |
| 1871 | // First insert it into a poison vector so we can shuffle it. |
| 1872 | auto vectorType = typeConverter->convertType(t: broadcast.getType()); |
| 1873 | Value poison = |
| 1874 | LLVM::PoisonOp::create(builder&: rewriter, location: broadcast.getLoc(), res: vectorType); |
| 1875 | auto zero = LLVM::ConstantOp::create( |
| 1876 | builder&: rewriter, location: broadcast.getLoc(), |
| 1877 | res: typeConverter->convertType(t: rewriter.getIntegerType(width: 32)), |
| 1878 | value: rewriter.getZeroAttr(type: rewriter.getIntegerType(width: 32))); |
| 1879 | |
| 1880 | // For 0-d vector, we simply do `insertelement`. |
| 1881 | if (resultType.getRank() == 0) { |
| 1882 | rewriter.replaceOpWithNewOp<LLVM::InsertElementOp>( |
| 1883 | op: broadcast, args&: vectorType, args&: poison, args: adaptor.getSource(), args&: zero); |
| 1884 | return success(); |
| 1885 | } |
| 1886 | |
| 1887 | auto v = |
| 1888 | LLVM::InsertElementOp::create(builder&: rewriter, location: broadcast.getLoc(), res: vectorType, |
| 1889 | vector: poison, value: adaptor.getSource(), position: zero); |
| 1890 | |
| 1891 | // For 1-d vector, we additionally do a `shufflevector`. |
| 1892 | int64_t width = cast<VectorType>(Val: broadcast.getType()).getDimSize(idx: 0); |
| 1893 | SmallVector<int32_t> zeroValues(width, 0); |
| 1894 | |
| 1895 | // Shuffle the value across the desired number of elements. |
| 1896 | auto shuffle = rewriter.createOrFold<LLVM::ShuffleVectorOp>( |
| 1897 | location: broadcast.getLoc(), args&: v, args&: poison, args&: zeroValues); |
| 1898 | rewriter.replaceOp(op: broadcast, newValues: shuffle); |
| 1899 | return success(); |
| 1900 | } |
| 1901 | }; |
| 1902 | |
| 1903 | /// The broadcast of a scalar is lowered to an insertelement + a shufflevector |
| 1904 | /// operation. Only broadcasts to 2+-d vector result types are lowered by this |
| 1905 | /// pattern, the 1-d case is handled by another pattern. Broadcasts from vectors |
| 1906 | /// are not converted to LLVM, only broadcasts from scalars are. |
| 1907 | struct VectorBroadcastScalarToNdLowering |
| 1908 | : public ConvertOpToLLVMPattern<BroadcastOp> { |
| 1909 | using ConvertOpToLLVMPattern<BroadcastOp>::ConvertOpToLLVMPattern; |
| 1910 | |
| 1911 | LogicalResult |
| 1912 | matchAndRewrite(BroadcastOp broadcast, OpAdaptor adaptor, |
| 1913 | ConversionPatternRewriter &rewriter) const override { |
| 1914 | if (isa<VectorType>(Val: broadcast.getSourceType())) |
| 1915 | return rewriter.notifyMatchFailure( |
| 1916 | arg&: broadcast, msg: "broadcast from vector type not handled" ); |
| 1917 | |
| 1918 | VectorType resultType = broadcast.getType(); |
| 1919 | if (resultType.getRank() <= 1) |
| 1920 | return rewriter.notifyMatchFailure( |
| 1921 | arg&: broadcast, msg: "broadcast to 1-d or 0-d handled elsewhere" ); |
| 1922 | |
| 1923 | // First insert it into a poison vector so we can shuffle it. |
| 1924 | auto loc = broadcast.getLoc(); |
| 1925 | auto vectorTypeInfo = |
| 1926 | LLVM::detail::extractNDVectorTypeInfo(vectorType: resultType, converter: *getTypeConverter()); |
| 1927 | auto llvmNDVectorTy = vectorTypeInfo.llvmNDVectorTy; |
| 1928 | auto llvm1DVectorTy = vectorTypeInfo.llvm1DVectorTy; |
| 1929 | if (!llvmNDVectorTy || !llvm1DVectorTy) |
| 1930 | return failure(); |
| 1931 | |
| 1932 | // Construct returned value. |
| 1933 | Value desc = LLVM::PoisonOp::create(builder&: rewriter, location: loc, res: llvmNDVectorTy); |
| 1934 | |
| 1935 | // Construct a 1-D vector with the broadcasted value that we insert in all |
| 1936 | // the places within the returned descriptor. |
| 1937 | Value vdesc = LLVM::PoisonOp::create(builder&: rewriter, location: loc, res: llvm1DVectorTy); |
| 1938 | auto zero = LLVM::ConstantOp::create( |
| 1939 | builder&: rewriter, location: loc, res: typeConverter->convertType(t: rewriter.getIntegerType(width: 32)), |
| 1940 | value: rewriter.getZeroAttr(type: rewriter.getIntegerType(width: 32))); |
| 1941 | Value v = LLVM::InsertElementOp::create(builder&: rewriter, location: loc, res: llvm1DVectorTy, |
| 1942 | vector: vdesc, value: adaptor.getSource(), position: zero); |
| 1943 | |
| 1944 | // Shuffle the value across the desired number of elements. |
| 1945 | int64_t width = resultType.getDimSize(idx: resultType.getRank() - 1); |
| 1946 | SmallVector<int32_t> zeroValues(width, 0); |
| 1947 | v = LLVM::ShuffleVectorOp::create(builder&: rewriter, location: loc, v1: v, v2: v, mask: zeroValues); |
| 1948 | |
| 1949 | // Iterate of linear index, convert to coords space and insert broadcasted |
| 1950 | // 1-D vector in each position. |
| 1951 | nDVectorIterate(info: vectorTypeInfo, builder&: rewriter, fun: [&](ArrayRef<int64_t> position) { |
| 1952 | desc = LLVM::InsertValueOp::create(builder&: rewriter, location: loc, container: desc, value: v, position); |
| 1953 | }); |
| 1954 | rewriter.replaceOp(op: broadcast, newValues: desc); |
| 1955 | return success(); |
| 1956 | } |
| 1957 | }; |
| 1958 | |
| 1959 | /// Conversion pattern for a `vector.interleave`. |
| 1960 | /// This supports fixed-sized vectors and scalable vectors. |
| 1961 | struct VectorInterleaveOpLowering |
| 1962 | : public ConvertOpToLLVMPattern<vector::InterleaveOp> { |
| 1963 | using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; |
| 1964 | |
| 1965 | LogicalResult |
| 1966 | matchAndRewrite(vector::InterleaveOp interleaveOp, OpAdaptor adaptor, |
| 1967 | ConversionPatternRewriter &rewriter) const override { |
| 1968 | VectorType resultType = interleaveOp.getResultVectorType(); |
| 1969 | // n-D interleaves should have been lowered already. |
| 1970 | if (resultType.getRank() != 1) |
| 1971 | return rewriter.notifyMatchFailure(arg&: interleaveOp, |
| 1972 | msg: "InterleaveOp not rank 1" ); |
| 1973 | // If the result is rank 1, then this directly maps to LLVM. |
| 1974 | if (resultType.isScalable()) { |
| 1975 | rewriter.replaceOpWithNewOp<LLVM::vector_interleave2>( |
| 1976 | op: interleaveOp, args: typeConverter->convertType(t: resultType), |
| 1977 | args: adaptor.getLhs(), args: adaptor.getRhs()); |
| 1978 | return success(); |
| 1979 | } |
| 1980 | // Lower fixed-size interleaves to a shufflevector. While the |
| 1981 | // vector.interleave2 intrinsic supports fixed and scalable vectors, the |
| 1982 | // langref still recommends fixed-vectors use shufflevector, see: |
| 1983 | // https://llvm.org/docs/LangRef.html#id876. |
| 1984 | int64_t resultVectorSize = resultType.getNumElements(); |
| 1985 | SmallVector<int32_t> interleaveShuffleMask; |
| 1986 | interleaveShuffleMask.reserve(N: resultVectorSize); |
| 1987 | for (int i = 0, end = resultVectorSize / 2; i < end; ++i) { |
| 1988 | interleaveShuffleMask.push_back(Elt: i); |
| 1989 | interleaveShuffleMask.push_back(Elt: (resultVectorSize / 2) + i); |
| 1990 | } |
| 1991 | rewriter.replaceOpWithNewOp<LLVM::ShuffleVectorOp>( |
| 1992 | op: interleaveOp, args: adaptor.getLhs(), args: adaptor.getRhs(), |
| 1993 | args&: interleaveShuffleMask); |
| 1994 | return success(); |
| 1995 | } |
| 1996 | }; |
| 1997 | |
| 1998 | /// Conversion pattern for a `vector.deinterleave`. |
| 1999 | /// This supports fixed-sized vectors and scalable vectors. |
| 2000 | struct VectorDeinterleaveOpLowering |
| 2001 | : public ConvertOpToLLVMPattern<vector::DeinterleaveOp> { |
| 2002 | using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; |
| 2003 | |
| 2004 | LogicalResult |
| 2005 | matchAndRewrite(vector::DeinterleaveOp deinterleaveOp, OpAdaptor adaptor, |
| 2006 | ConversionPatternRewriter &rewriter) const override { |
| 2007 | VectorType resultType = deinterleaveOp.getResultVectorType(); |
| 2008 | VectorType sourceType = deinterleaveOp.getSourceVectorType(); |
| 2009 | auto loc = deinterleaveOp.getLoc(); |
| 2010 | |
| 2011 | // Note: n-D deinterleave operations should be lowered to the 1-D before |
| 2012 | // converting to LLVM. |
| 2013 | if (resultType.getRank() != 1) |
| 2014 | return rewriter.notifyMatchFailure(arg&: deinterleaveOp, |
| 2015 | msg: "DeinterleaveOp not rank 1" ); |
| 2016 | |
| 2017 | if (resultType.isScalable()) { |
| 2018 | const auto *llvmTypeConverter = this->getTypeConverter(); |
| 2019 | auto deinterleaveResults = deinterleaveOp.getResultTypes(); |
| 2020 | auto packedOpResults = |
| 2021 | llvmTypeConverter->packOperationResults(types: deinterleaveResults); |
| 2022 | auto intrinsic = LLVM::vector_deinterleave2::create( |
| 2023 | builder&: rewriter, location: loc, res: packedOpResults, vec: adaptor.getSource()); |
| 2024 | |
| 2025 | auto evenResult = LLVM::ExtractValueOp::create( |
| 2026 | builder&: rewriter, location: loc, container: intrinsic->getResult(idx: 0), position: 0); |
| 2027 | auto oddResult = LLVM::ExtractValueOp::create(builder&: rewriter, location: loc, |
| 2028 | container: intrinsic->getResult(idx: 0), position: 1); |
| 2029 | |
| 2030 | rewriter.replaceOp(op: deinterleaveOp, newValues: ValueRange{evenResult, oddResult}); |
| 2031 | return success(); |
| 2032 | } |
| 2033 | // Lower fixed-size deinterleave to two shufflevectors. While the |
| 2034 | // vector.deinterleave2 intrinsic supports fixed and scalable vectors, the |
| 2035 | // langref still recommends fixed-vectors use shufflevector, see: |
| 2036 | // https://llvm.org/docs/LangRef.html#id889. |
| 2037 | int64_t resultVectorSize = resultType.getNumElements(); |
| 2038 | SmallVector<int32_t> evenShuffleMask; |
| 2039 | SmallVector<int32_t> oddShuffleMask; |
| 2040 | |
| 2041 | evenShuffleMask.reserve(N: resultVectorSize); |
| 2042 | oddShuffleMask.reserve(N: resultVectorSize); |
| 2043 | |
| 2044 | for (int i = 0; i < sourceType.getNumElements(); ++i) { |
| 2045 | if (i % 2 == 0) |
| 2046 | evenShuffleMask.push_back(Elt: i); |
| 2047 | else |
| 2048 | oddShuffleMask.push_back(Elt: i); |
| 2049 | } |
| 2050 | |
| 2051 | auto poison = LLVM::PoisonOp::create(builder&: rewriter, location: loc, res: sourceType); |
| 2052 | auto evenShuffle = LLVM::ShuffleVectorOp::create( |
| 2053 | builder&: rewriter, location: loc, v1: adaptor.getSource(), v2: poison, mask: evenShuffleMask); |
| 2054 | auto oddShuffle = LLVM::ShuffleVectorOp::create( |
| 2055 | builder&: rewriter, location: loc, v1: adaptor.getSource(), v2: poison, mask: oddShuffleMask); |
| 2056 | |
| 2057 | rewriter.replaceOp(op: deinterleaveOp, newValues: ValueRange{evenShuffle, oddShuffle}); |
| 2058 | return success(); |
| 2059 | } |
| 2060 | }; |
| 2061 | |
| 2062 | /// Conversion pattern for a `vector.from_elements`. |
| 2063 | struct VectorFromElementsLowering |
| 2064 | : public ConvertOpToLLVMPattern<vector::FromElementsOp> { |
| 2065 | using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; |
| 2066 | |
| 2067 | LogicalResult |
| 2068 | matchAndRewrite(vector::FromElementsOp fromElementsOp, OpAdaptor adaptor, |
| 2069 | ConversionPatternRewriter &rewriter) const override { |
| 2070 | Location loc = fromElementsOp.getLoc(); |
| 2071 | VectorType vectorType = fromElementsOp.getType(); |
| 2072 | // Only support 1-D vectors. Multi-dimensional vectors should have been |
| 2073 | // transformed to 1-D vectors by the vector-to-vector transformations before |
| 2074 | // this. |
| 2075 | if (vectorType.getRank() > 1) |
| 2076 | return rewriter.notifyMatchFailure(arg&: fromElementsOp, |
| 2077 | msg: "rank > 1 vectors are not supported" ); |
| 2078 | Type llvmType = typeConverter->convertType(t: vectorType); |
| 2079 | Type llvmIndexType = typeConverter->convertType(t: rewriter.getIndexType()); |
| 2080 | Value result = LLVM::PoisonOp::create(builder&: rewriter, location: loc, res: llvmType); |
| 2081 | for (auto [idx, val] : llvm::enumerate(First: adaptor.getElements())) { |
| 2082 | auto constIdx = |
| 2083 | LLVM::ConstantOp::create(builder&: rewriter, location: loc, type: llvmIndexType, value: idx); |
| 2084 | result = LLVM::InsertElementOp::create(builder&: rewriter, location: loc, res: llvmType, vector: result, |
| 2085 | value: val, position: constIdx); |
| 2086 | } |
| 2087 | rewriter.replaceOp(op: fromElementsOp, newValues: result); |
| 2088 | return success(); |
| 2089 | } |
| 2090 | }; |
| 2091 | |
| 2092 | /// Conversion pattern for a `vector.to_elements`. |
| 2093 | struct VectorToElementsLowering |
| 2094 | : public ConvertOpToLLVMPattern<vector::ToElementsOp> { |
| 2095 | using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; |
| 2096 | |
| 2097 | LogicalResult |
| 2098 | matchAndRewrite(vector::ToElementsOp toElementsOp, OpAdaptor adaptor, |
| 2099 | ConversionPatternRewriter &rewriter) const override { |
| 2100 | Location loc = toElementsOp.getLoc(); |
| 2101 | auto idxType = typeConverter->convertType(t: rewriter.getIndexType()); |
| 2102 | Value source = adaptor.getSource(); |
| 2103 | |
| 2104 | SmallVector<Value> results(toElementsOp->getNumResults()); |
| 2105 | for (auto [idx, element] : llvm::enumerate(First: toElementsOp.getElements())) { |
| 2106 | // Create an extractelement operation only for results that are not dead. |
| 2107 | if (element.use_empty()) |
| 2108 | continue; |
| 2109 | |
| 2110 | auto constIdx = LLVM::ConstantOp::create( |
| 2111 | builder&: rewriter, location: loc, res: idxType, value: rewriter.getIntegerAttr(type: idxType, value: idx)); |
| 2112 | auto llvmType = typeConverter->convertType(t: element.getType()); |
| 2113 | |
| 2114 | Value result = LLVM::ExtractElementOp::create(builder&: rewriter, location: loc, res: llvmType, |
| 2115 | vector: source, position: constIdx); |
| 2116 | results[idx] = result; |
| 2117 | } |
| 2118 | |
| 2119 | rewriter.replaceOp(op: toElementsOp, newValues: results); |
| 2120 | return success(); |
| 2121 | } |
| 2122 | }; |
| 2123 | |
| 2124 | /// Conversion pattern for vector.step. |
| 2125 | struct VectorStepOpLowering : public ConvertOpToLLVMPattern<vector::StepOp> { |
| 2126 | using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; |
| 2127 | |
| 2128 | LogicalResult |
| 2129 | matchAndRewrite(vector::StepOp stepOp, OpAdaptor adaptor, |
| 2130 | ConversionPatternRewriter &rewriter) const override { |
| 2131 | Type llvmType = typeConverter->convertType(t: stepOp.getType()); |
| 2132 | rewriter.replaceOpWithNewOp<LLVM::StepVectorOp>(op: stepOp, args&: llvmType); |
| 2133 | return success(); |
| 2134 | } |
| 2135 | }; |
| 2136 | |
| 2137 | /// Progressive lowering of a `vector.contract %a, %b, %c` with row-major matmul |
| 2138 | /// semantics to: |
| 2139 | /// ``` |
| 2140 | /// %flattened_a = vector.shape_cast %a |
| 2141 | /// %flattened_b = vector.shape_cast %b |
| 2142 | /// %flattened_d = vector.matrix_multiply %flattened_a, %flattened_b |
| 2143 | /// %d = vector.shape_cast %%flattened_d |
| 2144 | /// %e = add %c, %d |
| 2145 | /// ``` |
| 2146 | /// `vector.matrix_multiply` later lowers to `llvm.matrix.multiply`. |
| 2147 | class ContractionOpToMatmulOpLowering |
| 2148 | : public vector::MaskableOpRewritePattern<vector::ContractionOp> { |
| 2149 | public: |
| 2150 | using MaskableOpRewritePattern::MaskableOpRewritePattern; |
| 2151 | |
| 2152 | ContractionOpToMatmulOpLowering(MLIRContext *context, |
| 2153 | PatternBenefit benefit = 100) |
| 2154 | : MaskableOpRewritePattern<vector::ContractionOp>(context, benefit) {} |
| 2155 | |
| 2156 | FailureOr<Value> |
| 2157 | matchAndRewriteMaskableOp(vector::ContractionOp op, MaskingOpInterface maskOp, |
| 2158 | PatternRewriter &rewriter) const override; |
| 2159 | }; |
| 2160 | |
| 2161 | /// Lower a qualifying `vector.contract %a, %b, %c` (with row-major matmul |
| 2162 | /// semantics directly into `llvm.intr.matrix.multiply`: |
| 2163 | /// BEFORE: |
| 2164 | /// ```mlir |
| 2165 | /// %res = vector.contract #matmat_trait %lhs, %rhs, %acc |
| 2166 | /// : vector<2x4xf32>, vector<4x3xf32> into vector<2x3xf32> |
| 2167 | /// ``` |
| 2168 | /// |
| 2169 | /// AFTER: |
| 2170 | /// ```mlir |
| 2171 | /// %lhs = vector.shape_cast %arg0 : vector<2x4xf32> to vector<8xf32> |
| 2172 | /// %rhs = vector.shape_cast %arg1 : vector<4x3xf32> to vector<12xf32> |
| 2173 | /// %matmul = llvm.intr.matrix.multiply %lhs, %rhs |
| 2174 | /// %res = arith.addf %acc, %matmul : vector<2x3xf32> |
| 2175 | /// ``` |
| 2176 | // |
| 2177 | /// Scalable vectors are not supported. |
| 2178 | FailureOr<Value> ContractionOpToMatmulOpLowering::matchAndRewriteMaskableOp( |
| 2179 | vector::ContractionOp op, MaskingOpInterface maskOp, |
| 2180 | PatternRewriter &rew) const { |
| 2181 | // TODO: Support vector.mask. |
| 2182 | if (maskOp) |
| 2183 | return failure(); |
| 2184 | |
| 2185 | auto iteratorTypes = op.getIteratorTypes().getValue(); |
| 2186 | if (!isParallelIterator(attr: iteratorTypes[0]) || |
| 2187 | !isParallelIterator(attr: iteratorTypes[1]) || |
| 2188 | !isReductionIterator(attr: iteratorTypes[2])) |
| 2189 | return failure(); |
| 2190 | |
| 2191 | Type opResType = op.getType(); |
| 2192 | VectorType vecType = dyn_cast<VectorType>(Val&: opResType); |
| 2193 | if (vecType && vecType.isScalable()) |
| 2194 | return failure(); |
| 2195 | // The result type only reflects the M and N dims, so a scalable K |
| 2196 | // (reduction) dim would slip through the check above. |
| 2197 | if (op.getLhsType().isScalable() || op.getRhsType().isScalable()) |
| 2198 | return failure(); |
| 2199 | |
| 2200 | Type elementType = op.getLhsType().getElementType(); |
| 2201 | if (!elementType.isIntOrFloat()) |
| 2202 | return failure(); |
| 2203 | |
| 2204 | Type dstElementType = vecType ? vecType.getElementType() : opResType; |
| 2205 | if (elementType != dstElementType) |
| 2206 | return failure(); |
| 2207 | |
| 2208 | // Perform lhs + rhs transpositions to conform to matmul row-major semantics. |
| 2209 | // Bail out if the contraction cannot be put in this form. |
| 2210 | MLIRContext *ctx = op.getContext(); |
| 2211 | Location loc = op.getLoc(); |
| 2212 | AffineExpr m, n, k; |
| 2213 | bindDims(ctx: rew.getContext(), exprs&: m, exprs&: n, exprs&: k); |
| 2214 | // LHS must be A(m, k) or A(k, m). |
| 2215 | Value lhs = op.getLhs(); |
| 2216 | auto lhsMap = op.getIndexingMapsArray()[0]; |
| 2217 | if (lhsMap == AffineMap::get(dimCount: 3, symbolCount: 0, results: {k, m}, context: ctx)) |
| 2218 | lhs = vector::TransposeOp::create(builder&: rew, location: loc, vector: lhs, permutation: ArrayRef<int64_t>{1, 0}); |
| 2219 | else if (lhsMap != AffineMap::get(dimCount: 3, symbolCount: 0, results: {m, k}, context: ctx)) |
| 2220 | return failure(); |
| 2221 | |
| 2222 | // RHS must be B(k, n) or B(n, k). |
| 2223 | Value rhs = op.getRhs(); |
| 2224 | auto rhsMap = op.getIndexingMapsArray()[1]; |
| 2225 | if (rhsMap == AffineMap::get(dimCount: 3, symbolCount: 0, results: {n, k}, context: ctx)) |
| 2226 | rhs = vector::TransposeOp::create(builder&: rew, location: loc, vector: rhs, permutation: ArrayRef<int64_t>{1, 0}); |
| 2227 | else if (rhsMap != AffineMap::get(dimCount: 3, symbolCount: 0, results: {k, n}, context: ctx)) |
| 2228 | return failure(); |
| 2229 | |
| 2230 | // At this point lhs and rhs are in row-major. |
| 2231 | VectorType lhsType = cast<VectorType>(Val: lhs.getType()); |
| 2232 | VectorType rhsType = cast<VectorType>(Val: rhs.getType()); |
| 2233 | int64_t lhsRows = lhsType.getDimSize(idx: 0); |
| 2234 | int64_t lhsColumns = lhsType.getDimSize(idx: 1); |
| 2235 | int64_t rhsColumns = rhsType.getDimSize(idx: 1); |
| 2236 | |
| 2237 | Type flattenedLHSType = |
| 2238 | VectorType::get(shape: lhsType.getNumElements(), elementType: lhsType.getElementType()); |
| 2239 | lhs = vector::ShapeCastOp::create(builder&: rew, location: loc, result: flattenedLHSType, source: lhs); |
| 2240 | |
| 2241 | Type flattenedRHSType = |
| 2242 | VectorType::get(shape: rhsType.getNumElements(), elementType: rhsType.getElementType()); |
| 2243 | rhs = vector::ShapeCastOp::create(builder&: rew, location: loc, result: flattenedRHSType, source: rhs); |
| 2244 | |
| 2245 | Value mul = LLVM::MatrixMultiplyOp::create( |
| 2246 | builder&: rew, location: loc, |
| 2247 | res: VectorType::get(shape: lhsRows * rhsColumns, |
| 2248 | elementType: cast<VectorType>(Val: lhs.getType()).getElementType()), |
| 2249 | lhs, rhs, lhs_rows: lhsRows, lhs_columns: lhsColumns, rhs_columns: rhsColumns); |
| 2250 | |
| 2251 | mul = vector::ShapeCastOp::create( |
| 2252 | builder&: rew, location: loc, |
| 2253 | result: VectorType::get(shape: {lhsRows, rhsColumns}, |
| 2254 | elementType: getElementTypeOrSelf(type: op.getAcc().getType())), |
| 2255 | source: mul); |
| 2256 | |
| 2257 | // ACC must be C(m, n) or C(n, m). |
| 2258 | auto accMap = op.getIndexingMapsArray()[2]; |
| 2259 | if (accMap == AffineMap::get(dimCount: 3, symbolCount: 0, results: {n, m}, context: ctx)) |
| 2260 | mul = vector::TransposeOp::create(builder&: rew, location: loc, vector: mul, permutation: ArrayRef<int64_t>{1, 0}); |
| 2261 | else if (accMap != AffineMap::get(dimCount: 3, symbolCount: 0, results: {m, n}, context: ctx)) |
| 2262 | llvm_unreachable("invalid contraction semantics" ); |
| 2263 | |
| 2264 | Value res = isa<IntegerType>(Val: elementType) |
| 2265 | ? static_cast<Value>( |
| 2266 | arith::AddIOp::create(builder&: rew, location: loc, lhs: op.getAcc(), rhs: mul)) |
| 2267 | : static_cast<Value>( |
| 2268 | arith::AddFOp::create(builder&: rew, location: loc, lhs: op.getAcc(), rhs: mul)); |
| 2269 | |
| 2270 | return res; |
| 2271 | } |
| 2272 | |
| 2273 | /// Lowers vector.transpose directly to llvm.intr.matrix.transpose |
| 2274 | /// |
| 2275 | /// BEFORE: |
| 2276 | /// ```mlir |
| 2277 | /// %tr = vector.transpose %vec, [1, 0] : vector<2x4xf32> to vector<4x2xf32> |
| 2278 | /// ``` |
| 2279 | /// AFTER: |
| 2280 | /// ```mlir |
| 2281 | /// %vec_cs = vector.shape_cast %vec : vector<2x4xf32> to vector<8xf32> |
| 2282 | /// %tr = llvm.intr.matrix.transpose %vec_sc |
| 2283 | /// {columns = 2 : i32, rows = 4 : i32} : vector<8xf32> into vector<8xf32> |
| 2284 | /// %res = vector.shape_cast %tr : vector<8xf32> to vector<4x2xf32> |
| 2285 | /// ``` |
| 2286 | class TransposeOpToMatrixTransposeOpLowering |
| 2287 | : public OpRewritePattern<vector::TransposeOp> { |
| 2288 | public: |
| 2289 | using Base::Base; |
| 2290 | |
| 2291 | LogicalResult matchAndRewrite(vector::TransposeOp op, |
| 2292 | PatternRewriter &rewriter) const override { |
| 2293 | auto loc = op.getLoc(); |
| 2294 | |
| 2295 | Value input = op.getVector(); |
| 2296 | VectorType inputType = op.getSourceVectorType(); |
| 2297 | VectorType resType = op.getResultVectorType(); |
| 2298 | |
| 2299 | if (inputType.isScalable()) |
| 2300 | return rewriter.notifyMatchFailure( |
| 2301 | arg&: op, msg: "This lowering does not support scalable vectors" ); |
| 2302 | |
| 2303 | // Set up convenience transposition table. |
| 2304 | ArrayRef<int64_t> transp = op.getPermutation(); |
| 2305 | |
| 2306 | if (resType.getRank() != 2 || transp[0] != 1 || transp[1] != 0) { |
| 2307 | return failure(); |
| 2308 | } |
| 2309 | |
| 2310 | Type flattenedType = |
| 2311 | VectorType::get(shape: resType.getNumElements(), elementType: resType.getElementType()); |
| 2312 | auto matrix = |
| 2313 | vector::ShapeCastOp::create(builder&: rewriter, location: loc, result: flattenedType, source: input); |
| 2314 | auto rows = rewriter.getI32IntegerAttr(value: resType.getShape()[0]); |
| 2315 | auto columns = rewriter.getI32IntegerAttr(value: resType.getShape()[1]); |
| 2316 | Value trans = LLVM::MatrixTransposeOp::create(builder&: rewriter, location: loc, res: flattenedType, |
| 2317 | matrix, rows, columns); |
| 2318 | rewriter.replaceOpWithNewOp<vector::ShapeCastOp>(op, args&: resType, args&: trans); |
| 2319 | return success(); |
| 2320 | } |
| 2321 | }; |
| 2322 | |
| 2323 | } // namespace |
| 2324 | |
| 2325 | void mlir::vector::populateVectorRankReducingFMAPattern( |
| 2326 | RewritePatternSet &patterns) { |
| 2327 | patterns.add<VectorFMAOpNDRewritePattern>(arg: patterns.getContext()); |
| 2328 | } |
| 2329 | |
| 2330 | void mlir::vector::populateVectorContractToMatrixMultiply( |
| 2331 | RewritePatternSet &patterns, PatternBenefit benefit) { |
| 2332 | patterns.add<ContractionOpToMatmulOpLowering>(arg: patterns.getContext(), args&: benefit); |
| 2333 | } |
| 2334 | |
| 2335 | void mlir::vector::populateVectorTransposeToFlatTranspose( |
| 2336 | RewritePatternSet &patterns, PatternBenefit benefit) { |
| 2337 | patterns.add<TransposeOpToMatrixTransposeOpLowering>(arg: patterns.getContext(), |
| 2338 | args&: benefit); |
| 2339 | } |
| 2340 | |
| 2341 | /// Populate the given list with patterns that convert from Vector to LLVM. |
| 2342 | void mlir::populateVectorToLLVMConversionPatterns( |
| 2343 | const LLVMTypeConverter &converter, RewritePatternSet &patterns, |
| 2344 | bool reassociateFPReductions, bool force32BitVectorIndices, |
| 2345 | bool useVectorAlignment, bool enableGEPInboundsNuw) { |
| 2346 | // This function populates only ConversionPatterns, not RewritePatterns. |
| 2347 | MLIRContext *ctx = converter.getDialect()->getContext(); |
| 2348 | patterns.add<VectorReductionOpConversion>(arg: converter, args&: reassociateFPReductions); |
| 2349 | patterns.add<VectorCreateMaskOpConversion>(arg&: ctx, args&: force32BitVectorIndices); |
| 2350 | patterns.add<VectorLoadStoreConversion<vector::LoadOp>, |
| 2351 | VectorLoadStoreConversion<vector::MaskedLoadOp>, |
| 2352 | VectorLoadStoreConversion<vector::StoreOp>, |
| 2353 | VectorLoadStoreConversion<vector::MaskedStoreOp>>( |
| 2354 | arg: converter, args&: useVectorAlignment, args&: enableGEPInboundsNuw); |
| 2355 | patterns.add<VectorGatherOpConversion, VectorScatterOpConversion>( |
| 2356 | arg: converter, args&: useVectorAlignment); |
| 2357 | patterns.add<VectorBitCastOpConversion, VectorShuffleOpConversion, |
| 2358 | VectorExtractOpConversion, VectorFMAOp1DConversion, |
| 2359 | VectorInsertOpConversion, VectorPrintOpConversion, |
| 2360 | VectorTypeCastOpConversion, VectorScaleOpConversion, |
| 2361 | VectorExpandLoadOpConversion, VectorCompressStoreOpConversion, |
| 2362 | VectorBroadcastScalarToLowRankLowering, |
| 2363 | VectorBroadcastScalarToNdLowering, |
| 2364 | VectorScalableInsertOpLowering, VectorScalableExtractOpLowering, |
| 2365 | MaskedReductionOpConversion, VectorInterleaveOpLowering, |
| 2366 | VectorDeinterleaveOpLowering, VectorFromElementsLowering, |
| 2367 | VectorToElementsLowering, VectorStepOpLowering>(arg: converter); |
| 2368 | } |
| 2369 | |
| 2370 | namespace { |
| 2371 | struct VectorToLLVMDialectInterface : public ConvertToLLVMPatternInterface { |
| 2372 | VectorToLLVMDialectInterface(Dialect *dialect) |
| 2373 | : ConvertToLLVMPatternInterface(dialect) {} |
| 2374 | |
| 2375 | using ConvertToLLVMPatternInterface::ConvertToLLVMPatternInterface; |
| 2376 | void loadDependentDialects(MLIRContext *context) const final { |
| 2377 | context->loadDialect<LLVM::LLVMDialect>(); |
| 2378 | } |
| 2379 | |
| 2380 | /// Hook for derived dialect interface to provide conversion patterns |
| 2381 | /// and mark dialect legal for the conversion target. |
| 2382 | void populateConvertToLLVMConversionPatterns( |
| 2383 | ConversionTarget &target, LLVMTypeConverter &typeConverter, |
| 2384 | RewritePatternSet &patterns) const final { |
| 2385 | populateVectorToLLVMConversionPatterns(converter: typeConverter, patterns); |
| 2386 | } |
| 2387 | }; |
| 2388 | } // namespace |
| 2389 | |
| 2390 | void mlir::vector::registerConvertVectorToLLVMInterface( |
| 2391 | DialectRegistry ®istry) { |
| 2392 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, vector::VectorDialect *dialect) { |
| 2393 | dialect->addInterfaces<VectorToLLVMDialectInterface>(); |
| 2394 | }); |
| 2395 | } |
| 2396 | |