| 1 | //===- VectorToGPUUtils.h - Shared vector-to-MMA helpers -------*- C++ -*-===// |
| 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 | #ifndef MLIR_CONVERSION_VECTORTOGPUCOMMON_VECTORTOGPUUTILS_H |
| 10 | #define MLIR_CONVERSION_VECTORTOGPUCOMMON_VECTORTOGPUUTILS_H |
| 11 | |
| 12 | #include "mlir/IR/Value.h" |
| 13 | #include "mlir/Support/LogicalResult.h" |
| 14 | #include "llvm/ADT/DenseMap.h" |
| 15 | #include <cstdint> |
| 16 | #include <optional> |
| 17 | |
| 18 | namespace mlir { |
| 19 | class Operation; |
| 20 | class RewriterBase; |
| 21 | class RewritePatternSet; |
| 22 | |
| 23 | namespace arith { |
| 24 | class ConstantOp; |
| 25 | class ExtSIOp; |
| 26 | class ExtUIOp; |
| 27 | } // namespace arith |
| 28 | |
| 29 | namespace gpu { |
| 30 | enum class MMAElementwiseOp : uint32_t; |
| 31 | } // namespace gpu |
| 32 | |
| 33 | namespace scf { |
| 34 | class ForOp; |
| 35 | class YieldOp; |
| 36 | } // namespace scf |
| 37 | |
| 38 | namespace vector { |
| 39 | class BroadcastOp; |
| 40 | } // namespace vector |
| 41 | |
| 42 | /// Return true if the constant is a splat to a 2D vector so that it can be |
| 43 | /// converted to an MMA constant matrix op. |
| 44 | bool constantSupportsMMAMatrixType(arith::ConstantOp constantOp); |
| 45 | |
| 46 | /// Return true if this is a broadcast from scalar to a 2D vector. |
| 47 | bool broadcastSupportsMMAMatrixType(vector::BroadcastOp broadcastOp); |
| 48 | |
| 49 | /// Return true if this integer extend op can be folded into a contract op. |
| 50 | /// The operation must be arith.extsi or arith.extui. |
| 51 | template <typename ExtOpTy> |
| 52 | bool integerExtendSupportsMMAMatrixType(ExtOpTy extOp); |
| 53 | |
| 54 | extern template bool |
| 55 | integerExtendSupportsMMAMatrixType<arith::ExtSIOp>(arith::ExtSIOp); |
| 56 | |
| 57 | extern template bool |
| 58 | integerExtendSupportsMMAMatrixType<arith::ExtUIOp>(arith::ExtUIOp); |
| 59 | |
| 60 | /// Return the MMA elementwise enum associated with the operation if supported. |
| 61 | /// Return std::nullopt otherwise. |
| 62 | std::optional<gpu::MMAElementwiseOp> convertElementwiseOpToMMA(Operation *op); |
| 63 | |
| 64 | /// Return true if the operation is supported as elementwise on an MMA matrix. |
| 65 | bool elementwiseSupportsMMAMatrixType(Operation *op); |
| 66 | |
| 67 | /// Extend loop-carried values with their converted counterparts. |
| 68 | LogicalResult convertForOpForMMA(RewriterBase &rewriter, scf::ForOp op, |
| 69 | llvm::DenseMap<Value, Value> &valueMapping); |
| 70 | |
| 71 | /// Update a yield operation to yield the converted values. |
| 72 | LogicalResult convertYieldOpForMMA(RewriterBase &rewriter, scf::YieldOp op, |
| 73 | llvm::DenseMap<Value, Value> &valueMapping); |
| 74 | |
| 75 | /// Populate patterns that fold vector.transpose into vector.transfer_read, |
| 76 | /// including through arithmetic extension operations. |
| 77 | void populateVectorTransferReadTransposePatterns(RewritePatternSet &patterns); |
| 78 | } // namespace mlir |
| 79 | |
| 80 | #endif // MLIR_CONVERSION_VECTORTOGPUCOMMON_VECTORTOGPUUTILS_H |
| 81 | |