1edd9515bSthomasraoux //===- VectorToGPU.cpp - Convert vector to GPU dialect ----------*- C++ -*-===//
2edd9515bSthomasraoux //
3edd9515bSthomasraoux // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4edd9515bSthomasraoux // See https://llvm.org/LICENSE.txt for license information.
5edd9515bSthomasraoux // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6edd9515bSthomasraoux //
7edd9515bSthomasraoux //===----------------------------------------------------------------------===//
8edd9515bSthomasraoux //
9edd9515bSthomasraoux // This file implements lowering of vector operations to GPU dialect ops.
10edd9515bSthomasraoux //
11edd9515bSthomasraoux //===----------------------------------------------------------------------===//
12edd9515bSthomasraoux 
13edd9515bSthomasraoux #include <type_traits>
14edd9515bSthomasraoux 
15edd9515bSthomasraoux #include "mlir/Conversion/VectorToGPU/VectorToGPU.h"
16edd9515bSthomasraoux 
17edd9515bSthomasraoux #include "../PassDetail.h"
18edd9515bSthomasraoux #include "mlir/Analysis/SliceAnalysis.h"
19edd9515bSthomasraoux #include "mlir/Dialect/GPU/GPUDialect.h"
2066f878ceSMatthias Springer #include "mlir/Dialect/MemRef/IR/MemRef.h"
21edd9515bSthomasraoux #include "mlir/Dialect/Utils/StructuredOpsUtils.h"
22edd9515bSthomasraoux #include "mlir/Dialect/Vector/VectorOps.h"
23edd9515bSthomasraoux #include "mlir/Dialect/Vector/VectorUtils.h"
24edd9515bSthomasraoux #include "mlir/IR/Builders.h"
25edd9515bSthomasraoux #include "mlir/Pass/Pass.h"
26edd9515bSthomasraoux #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
27edd9515bSthomasraoux #include "mlir/Transforms/Passes.h"
28edd9515bSthomasraoux 
29edd9515bSthomasraoux using namespace mlir;
30edd9515bSthomasraoux 
31edd9515bSthomasraoux // Return true if the contract op can be convert to MMA matmul.
32edd9515bSthomasraoux static bool contractSupportsMMAMatrixType(vector::ContractionOp contract) {
33edd9515bSthomasraoux   if (llvm::size(contract.masks()) != 0)
34edd9515bSthomasraoux     return false;
35edd9515bSthomasraoux 
36edd9515bSthomasraoux   using MapList = ArrayRef<ArrayRef<AffineExpr>>;
37edd9515bSthomasraoux   auto infer = [](MapList m) { return AffineMap::inferFromExprList(m); };
38edd9515bSthomasraoux   AffineExpr m, n, k;
39edd9515bSthomasraoux   bindDims(contract.getContext(), m, n, k);
40edd9515bSthomasraoux   auto iteratorTypes = contract.iterator_types().getValue();
41edd9515bSthomasraoux   if (!(isParallelIterator(iteratorTypes[0]) &&
42edd9515bSthomasraoux         isParallelIterator(iteratorTypes[1]) &&
43edd9515bSthomasraoux         isReductionIterator(iteratorTypes[2])))
44edd9515bSthomasraoux     return false;
45edd9515bSthomasraoux 
46edd9515bSthomasraoux   // The contract needs to represent a matmul to be able to convert to
47edd9515bSthomasraoux   // MMAMatrix matmul.
48edd9515bSthomasraoux   if (contract.getIndexingMaps() != infer({{m, k}, {k, n}, {m, n}}))
49edd9515bSthomasraoux     return false;
50edd9515bSthomasraoux 
51edd9515bSthomasraoux   // Check that the size matches what is natively supported.
52edd9515bSthomasraoux   VectorType lhsType = contract.lhs().getType().cast<VectorType>();
53edd9515bSthomasraoux   VectorType rhsType = contract.rhs().getType().cast<VectorType>();
54edd9515bSthomasraoux   VectorType accType = contract.acc().getType().cast<VectorType>();
55edd9515bSthomasraoux 
56edd9515bSthomasraoux   std::tuple<int, int, int> dim(lhsType.getDimSize(0), rhsType.getDimSize(1),
57edd9515bSthomasraoux                                 lhsType.getDimSize(1));
58edd9515bSthomasraoux   if (lhsType.getElementType().isInteger(8) &&
59edd9515bSthomasraoux       rhsType.getElementType().isInteger(8) &&
60edd9515bSthomasraoux       accType.getElementType().isInteger(32) &&
61edd9515bSthomasraoux       (dim == std::make_tuple(8, 8, 32) || dim == std::make_tuple(16, 16, 32) ||
62edd9515bSthomasraoux        dim == std::make_tuple(16, 8, 32)))
63edd9515bSthomasraoux     return true;
64edd9515bSthomasraoux 
65edd9515bSthomasraoux   if (lhsType.getElementType().isF16() && rhsType.getElementType().isF16() &&
66edd9515bSthomasraoux       (accType.getElementType().isF16() || accType.getElementType().isF32()) &&
67edd9515bSthomasraoux       (dim == std::make_tuple(8, 8, 16) || dim == std::make_tuple(16, 16, 16) ||
68edd9515bSthomasraoux        dim == std::make_tuple(16, 8, 16)))
69edd9515bSthomasraoux     return true;
70edd9515bSthomasraoux   return false;
71edd9515bSthomasraoux }
72edd9515bSthomasraoux 
73edd9515bSthomasraoux // Return the stide for the dimension 0 of |type| if it is a memref and has a
74edd9515bSthomasraoux // constant stride.
75edd9515bSthomasraoux static llvm::Optional<int64_t>
76edd9515bSthomasraoux getMemrefConstantHorizontalStride(ShapedType type) {
77edd9515bSthomasraoux   auto memrefType = type.dyn_cast<MemRefType>();
78edd9515bSthomasraoux   if (!memrefType)
79edd9515bSthomasraoux     return false;
80edd9515bSthomasraoux   int64_t offset = 0;
81edd9515bSthomasraoux   SmallVector<int64_t, 2> strides;
82edd9515bSthomasraoux   if (failed(getStridesAndOffset(memrefType, strides, offset)))
83edd9515bSthomasraoux     return llvm::None;
84edd9515bSthomasraoux   if (strides[0] == ShapedType::kDynamicStrideOrOffset)
85edd9515bSthomasraoux     return llvm::None;
86edd9515bSthomasraoux   return strides[0];
87edd9515bSthomasraoux }
88edd9515bSthomasraoux 
89edd9515bSthomasraoux // Return true if the transfer op can be converted to a MMA matrix load.
90edd9515bSthomasraoux static bool transferReadSupportsMMAMatrixType(vector::TransferReadOp readOp) {
91edd9515bSthomasraoux   if (readOp.mask() || readOp.hasOutOfBoundsDim() ||
92edd9515bSthomasraoux       readOp.getVectorType().getRank() != 2)
93edd9515bSthomasraoux     return false;
94edd9515bSthomasraoux   if (!getMemrefConstantHorizontalStride(readOp.getShapedType()))
95edd9515bSthomasraoux     return false;
96edd9515bSthomasraoux   // TODO: Support transpose once it is added to GPU dialect ops.
97edd9515bSthomasraoux   if (!readOp.permutation_map().isMinorIdentity())
98edd9515bSthomasraoux     return false;
99edd9515bSthomasraoux   return true;
100edd9515bSthomasraoux }
101edd9515bSthomasraoux 
102edd9515bSthomasraoux // Return true if the transfer op can be converted to a MMA matrix store.
103edd9515bSthomasraoux static bool
104edd9515bSthomasraoux transferWriteSupportsMMAMatrixType(vector::TransferWriteOp writeOp) {
105edd9515bSthomasraoux   if (writeOp.mask() || writeOp.hasOutOfBoundsDim() ||
106edd9515bSthomasraoux       writeOp.getVectorType().getRank() != 2)
107edd9515bSthomasraoux     return false;
108edd9515bSthomasraoux   if (!getMemrefConstantHorizontalStride(writeOp.getShapedType()))
109edd9515bSthomasraoux     return false;
110edd9515bSthomasraoux   // TODO: Support transpose once it is added to GPU dialect ops.
111edd9515bSthomasraoux   if (!writeOp.permutation_map().isMinorIdentity())
112edd9515bSthomasraoux     return false;
113edd9515bSthomasraoux   return true;
114edd9515bSthomasraoux }
115edd9515bSthomasraoux 
116*6413226dSthomasraoux /// Return true if the constant is a splat to a 2D vector so that it can be
117*6413226dSthomasraoux /// converted to a MMA constant matrix op.
118*6413226dSthomasraoux static bool constantSupportsMMAMatrixType(ConstantOp constantOp) {
119*6413226dSthomasraoux   auto vecType = constantOp.getType().dyn_cast<VectorType>();
120*6413226dSthomasraoux   if (!vecType || vecType.getRank() != 2)
121*6413226dSthomasraoux     return false;
122*6413226dSthomasraoux   return constantOp.value().isa<SplatElementsAttr>();
123*6413226dSthomasraoux }
124*6413226dSthomasraoux 
125edd9515bSthomasraoux static bool supportsMMaMatrixType(Operation *op) {
126edd9515bSthomasraoux   if (auto transferRead = dyn_cast<vector::TransferReadOp>(op))
127edd9515bSthomasraoux     return transferReadSupportsMMAMatrixType(transferRead);
128edd9515bSthomasraoux   if (auto transferWrite = dyn_cast<vector::TransferWriteOp>(op))
129edd9515bSthomasraoux     return transferWriteSupportsMMAMatrixType(transferWrite);
130edd9515bSthomasraoux   if (auto contract = dyn_cast<vector::ContractionOp>(op))
131edd9515bSthomasraoux     return contractSupportsMMAMatrixType(contract);
132*6413226dSthomasraoux   if (auto constant = dyn_cast<ConstantOp>(op))
133*6413226dSthomasraoux     return constantSupportsMMAMatrixType(constant);
134edd9515bSthomasraoux   return false;
135edd9515bSthomasraoux }
136edd9515bSthomasraoux 
137edd9515bSthomasraoux // Analyze slice of operations based on convert op to figure out if the whole
138edd9515bSthomasraoux // slice can be converted to MMA operations.
139edd9515bSthomasraoux static SetVector<Operation *> getOpToConvert(mlir::Operation *op) {
140edd9515bSthomasraoux   auto hasVectorDest = [](Operation *op) {
141edd9515bSthomasraoux     return op->getNumResults() == 0 ||
142edd9515bSthomasraoux            llvm::any_of(op->getResultTypes(),
143edd9515bSthomasraoux                         [](Type t) { return t.isa<VectorType>(); });
144edd9515bSthomasraoux   };
145edd9515bSthomasraoux   SetVector<Operation *> opToConvert;
146edd9515bSthomasraoux   op->walk([&](vector::ContractionOp contract) {
147edd9515bSthomasraoux     if (opToConvert.contains(contract.getOperation()))
148edd9515bSthomasraoux       return;
149edd9515bSthomasraoux     SetVector<Operation *> dependentOps =
150edd9515bSthomasraoux         getSlice(contract, hasVectorDest, hasVectorDest);
151edd9515bSthomasraoux     // If any instruction cannot use MMA matrix type drop the whole
152edd9515bSthomasraoux     // chaine. MMA matrix are stored in an opaque type so they cannot be used
153edd9515bSthomasraoux     // by all operations.
154edd9515bSthomasraoux     if (llvm::any_of(dependentOps,
155edd9515bSthomasraoux                      [](Operation *op) { return !supportsMMaMatrixType(op); }))
156edd9515bSthomasraoux       return;
157edd9515bSthomasraoux     opToConvert.insert(dependentOps.begin(), dependentOps.end());
158edd9515bSthomasraoux   });
159edd9515bSthomasraoux   return opToConvert;
160edd9515bSthomasraoux }
161edd9515bSthomasraoux 
162edd9515bSthomasraoux namespace {
163edd9515bSthomasraoux // Transform contract into (m, k)x(k, n)x(m, n) form so that it can be converted
164edd9515bSthomasraoux // to MMA matmul.
165edd9515bSthomasraoux struct PrepareContractToGPUMMA
166edd9515bSthomasraoux     : public OpRewritePattern<vector::ContractionOp> {
167edd9515bSthomasraoux   using OpRewritePattern<vector::ContractionOp>::OpRewritePattern;
168edd9515bSthomasraoux 
169edd9515bSthomasraoux   LogicalResult matchAndRewrite(vector::ContractionOp op,
170edd9515bSthomasraoux                                 PatternRewriter &rewriter) const override {
171edd9515bSthomasraoux     Location loc = op.getLoc();
172edd9515bSthomasraoux     Value lhs = op.lhs(), rhs = op.rhs(), res = op.acc();
173edd9515bSthomasraoux 
174edd9515bSthomasraoux     // Set up the parallel/reduction structure in right form.
175edd9515bSthomasraoux     using MapList = ArrayRef<ArrayRef<AffineExpr>>;
176edd9515bSthomasraoux     auto infer = [](MapList m) { return AffineMap::inferFromExprList(m); };
177edd9515bSthomasraoux     AffineExpr m, n, k;
178edd9515bSthomasraoux     bindDims(rewriter.getContext(), m, n, k);
179edd9515bSthomasraoux     static constexpr std::array<int64_t, 2> perm = {1, 0};
180edd9515bSthomasraoux     auto iteratorTypes = op.iterator_types().getValue();
181edd9515bSthomasraoux     SmallVector<AffineMap, 4> maps = op.getIndexingMaps();
182edd9515bSthomasraoux     if (!(isParallelIterator(iteratorTypes[0]) &&
183edd9515bSthomasraoux           isParallelIterator(iteratorTypes[1]) &&
184edd9515bSthomasraoux           isReductionIterator(iteratorTypes[2])))
185edd9515bSthomasraoux       return failure();
186edd9515bSthomasraoux     //
187edd9515bSthomasraoux     // Two outer parallel, one inner reduction (matmat flavor).
188edd9515bSthomasraoux     //
189edd9515bSthomasraoux     if (maps == infer({{m, k}, {k, n}, {m, n}})) {
190edd9515bSthomasraoux       // This is the classical row-major matmul, nothing to do.
191edd9515bSthomasraoux       return failure();
192edd9515bSthomasraoux     }
193edd9515bSthomasraoux     if (maps == infer({{m, k}, {n, k}, {m, n}})) {
194edd9515bSthomasraoux       rhs = rewriter.create<vector::TransposeOp>(loc, rhs, perm);
195edd9515bSthomasraoux     } else if (maps == infer({{k, m}, {k, n}, {m, n}})) {
196edd9515bSthomasraoux       lhs = rewriter.create<vector::TransposeOp>(loc, lhs, perm);
197edd9515bSthomasraoux     } else if (maps == infer({{k, m}, {n, k}, {m, n}})) {
198edd9515bSthomasraoux       rhs = rewriter.create<vector::TransposeOp>(loc, rhs, perm);
199edd9515bSthomasraoux       lhs = rewriter.create<vector::TransposeOp>(loc, lhs, perm);
200edd9515bSthomasraoux     } else if (maps == infer({{m, k}, {k, n}, {n, m}})) {
201edd9515bSthomasraoux       std::swap(rhs, lhs);
202edd9515bSthomasraoux       rhs = rewriter.create<vector::TransposeOp>(loc, rhs, perm);
203edd9515bSthomasraoux       lhs = rewriter.create<vector::TransposeOp>(loc, lhs, perm);
204edd9515bSthomasraoux     } else if (maps == infer({{m, k}, {n, k}, {n, m}})) {
205edd9515bSthomasraoux       std::swap(rhs, lhs);
206edd9515bSthomasraoux       rhs = rewriter.create<vector::TransposeOp>(loc, rhs, perm);
207edd9515bSthomasraoux     } else if (maps == infer({{k, m}, {k, n}, {n, m}})) {
208edd9515bSthomasraoux       std::swap(lhs, rhs);
209edd9515bSthomasraoux       lhs = rewriter.create<vector::TransposeOp>(loc, lhs, perm);
210edd9515bSthomasraoux     } else if (maps == infer({{k, m}, {n, k}, {n, m}})) {
211edd9515bSthomasraoux       std::swap(lhs, rhs);
212edd9515bSthomasraoux     } else {
213edd9515bSthomasraoux       return failure();
214edd9515bSthomasraoux     }
215edd9515bSthomasraoux     rewriter.replaceOpWithNewOp<vector::ContractionOp>(
216edd9515bSthomasraoux         op, lhs, rhs, res,
217edd9515bSthomasraoux         rewriter.getAffineMapArrayAttr(infer({{m, k}, {k, n}, {m, n}})),
218edd9515bSthomasraoux         op.iterator_types());
219edd9515bSthomasraoux     return success();
220edd9515bSthomasraoux   }
221edd9515bSthomasraoux };
222edd9515bSthomasraoux 
223edd9515bSthomasraoux // Merge transpose op into the transfer read op. Transpose are not supported on
224edd9515bSthomasraoux // MMA types but MMA load can transpose the matrix when loading.
225edd9515bSthomasraoux struct CombineTransferReadOpTranspose final
226edd9515bSthomasraoux     : public OpRewritePattern<vector::TransposeOp> {
227edd9515bSthomasraoux   using OpRewritePattern<vector::TransposeOp>::OpRewritePattern;
228edd9515bSthomasraoux 
229edd9515bSthomasraoux   LogicalResult matchAndRewrite(vector::TransposeOp op,
230edd9515bSthomasraoux                                 PatternRewriter &rewriter) const override {
231edd9515bSthomasraoux     auto transferReadOp = op.vector().getDefiningOp<vector::TransferReadOp>();
232edd9515bSthomasraoux     if (!transferReadOp)
233edd9515bSthomasraoux       return failure();
234edd9515bSthomasraoux     if (transferReadOp.mask() || transferReadOp.hasOutOfBoundsDim())
235edd9515bSthomasraoux       return failure();
236edd9515bSthomasraoux     SmallVector<int64_t, 2> perm;
237edd9515bSthomasraoux     op.getTransp(perm);
238edd9515bSthomasraoux     SmallVector<unsigned, 2> permU;
239edd9515bSthomasraoux     for (int64_t o : perm)
240edd9515bSthomasraoux       permU.push_back(unsigned(o));
241edd9515bSthomasraoux     AffineMap permutationMap =
242edd9515bSthomasraoux         AffineMap::getPermutationMap(permU, op.getContext());
243edd9515bSthomasraoux     AffineMap newMap = permutationMap.compose(transferReadOp.permutation_map());
244edd9515bSthomasraoux     rewriter.replaceOpWithNewOp<vector::TransferReadOp>(
245edd9515bSthomasraoux         op, op.getType(), transferReadOp.source(), transferReadOp.indices(),
246edd9515bSthomasraoux         newMap, transferReadOp.padding(), transferReadOp.mask(),
247edd9515bSthomasraoux         transferReadOp.in_boundsAttr());
248edd9515bSthomasraoux     return success();
249edd9515bSthomasraoux   }
250edd9515bSthomasraoux };
251edd9515bSthomasraoux 
252edd9515bSthomasraoux } // namespace
253edd9515bSthomasraoux 
254edd9515bSthomasraoux // MMA types have different layout based on how they are used in matmul ops.
255*6413226dSthomasraoux // Figure the right layout to use by looking at op uses.
256edd9515bSthomasraoux // TODO: Change the GPU dialect to abstract the layout at the this level and
257edd9515bSthomasraoux // only care about it during lowering to NVVM.
258*6413226dSthomasraoux template <typename OpTy>
259*6413226dSthomasraoux static const char *inferFragType(OpTy op) {
260edd9515bSthomasraoux   for (Operation *users : op->getUsers()) {
261edd9515bSthomasraoux     auto contract = dyn_cast<vector::ContractionOp>(users);
262edd9515bSthomasraoux     if (!contract)
263edd9515bSthomasraoux       continue;
264edd9515bSthomasraoux     if (contract.lhs() == op.getResult())
265edd9515bSthomasraoux       return "AOp";
266edd9515bSthomasraoux     if (contract.rhs() == op.getResult())
267edd9515bSthomasraoux       return "BOp";
268edd9515bSthomasraoux   }
269edd9515bSthomasraoux   return "COp";
270edd9515bSthomasraoux }
271edd9515bSthomasraoux 
272edd9515bSthomasraoux static void convertTransferReadOp(vector::TransferReadOp op,
273edd9515bSthomasraoux                                   llvm::DenseMap<Value, Value> &valueMapping) {
274edd9515bSthomasraoux   assert(transferReadSupportsMMAMatrixType(op));
275edd9515bSthomasraoux   Optional<int64_t> stride =
276edd9515bSthomasraoux       getMemrefConstantHorizontalStride(op.getShapedType());
277edd9515bSthomasraoux   assert(stride);
278edd9515bSthomasraoux   const char *fragType = inferFragType(op);
279edd9515bSthomasraoux   gpu::MMAMatrixType type =
280edd9515bSthomasraoux       gpu::MMAMatrixType::get(op.getVectorType().getShape(),
281edd9515bSthomasraoux                               op.getVectorType().getElementType(), fragType);
282edd9515bSthomasraoux   OpBuilder b(op);
283edd9515bSthomasraoux   Value load = b.create<gpu::SubgroupMmaLoadMatrixOp>(
284edd9515bSthomasraoux       op.getLoc(), type, op.source(), op.indices(), b.getIndexAttr(*stride));
285edd9515bSthomasraoux   valueMapping[op.getResult()] = load;
286edd9515bSthomasraoux }
287edd9515bSthomasraoux 
288edd9515bSthomasraoux static void convertTransferWriteOp(vector::TransferWriteOp op,
289edd9515bSthomasraoux                                    llvm::DenseMap<Value, Value> &valueMapping) {
290edd9515bSthomasraoux   assert(transferWriteSupportsMMAMatrixType(op));
291edd9515bSthomasraoux   Optional<int64_t> stride =
292edd9515bSthomasraoux       getMemrefConstantHorizontalStride(op.getShapedType());
293edd9515bSthomasraoux   assert(stride);
294edd9515bSthomasraoux   OpBuilder b(op);
295edd9515bSthomasraoux   Value matrix = valueMapping.find(op.vector())->second;
296edd9515bSthomasraoux   b.create<gpu::SubgroupMmaStoreMatrixOp>(
297edd9515bSthomasraoux       op.getLoc(), matrix, op.source(), op.indices(), b.getIndexAttr(*stride));
298edd9515bSthomasraoux   op.erase();
299edd9515bSthomasraoux }
300edd9515bSthomasraoux 
301edd9515bSthomasraoux static void convertContractOp(vector::ContractionOp op,
302edd9515bSthomasraoux                               llvm::DenseMap<Value, Value> &valueMapping) {
303edd9515bSthomasraoux   OpBuilder b(op);
304edd9515bSthomasraoux   Value opA = valueMapping.find(op.lhs())->second;
305edd9515bSthomasraoux   Value opB = valueMapping.find(op.rhs())->second;
306edd9515bSthomasraoux   Value opC = valueMapping.find(op.acc())->second;
307edd9515bSthomasraoux   Value matmul = b.create<gpu::SubgroupMmaComputeOp>(op.getLoc(), opC.getType(),
308edd9515bSthomasraoux                                                      opA, opB, opC);
309edd9515bSthomasraoux   valueMapping[op.getResult()] = matmul;
310edd9515bSthomasraoux }
311edd9515bSthomasraoux 
312*6413226dSthomasraoux /// Convert a 2D splat ConstantOp to a SubgroupMmaConstantMatrix op.
313*6413226dSthomasraoux static void convertConstantOp(ConstantOp op,
314*6413226dSthomasraoux                               llvm::DenseMap<Value, Value> &valueMapping) {
315*6413226dSthomasraoux   assert(constantSupportsMMAMatrixType(op));
316*6413226dSthomasraoux   OpBuilder b(op);
317*6413226dSthomasraoux   Attribute splat = op.getValue().cast<SplatElementsAttr>().getSplatValue();
318*6413226dSthomasraoux   auto scalarConstant =
319*6413226dSthomasraoux       b.create<ConstantOp>(op.getLoc(), splat.getType(), splat);
320*6413226dSthomasraoux   const char *fragType = inferFragType(op);
321*6413226dSthomasraoux   auto vecType = op.getType().cast<VectorType>();
322*6413226dSthomasraoux   gpu::MMAMatrixType type = gpu::MMAMatrixType::get(
323*6413226dSthomasraoux       vecType.getShape(), vecType.getElementType(), llvm::StringRef(fragType));
324*6413226dSthomasraoux   auto matrix = b.create<gpu::SubgroupMmaConstantMatrixOp>(op.getLoc(), type,
325*6413226dSthomasraoux                                                            scalarConstant);
326*6413226dSthomasraoux   valueMapping[op.getResult()] = matrix;
327*6413226dSthomasraoux }
328*6413226dSthomasraoux 
329edd9515bSthomasraoux namespace mlir {
330edd9515bSthomasraoux 
331edd9515bSthomasraoux void populatePrepareVectorToMMAPatterns(RewritePatternSet &patterns) {
332edd9515bSthomasraoux   patterns.add<PrepareContractToGPUMMA, CombineTransferReadOpTranspose>(
333edd9515bSthomasraoux       patterns.getContext());
334edd9515bSthomasraoux }
335edd9515bSthomasraoux 
336edd9515bSthomasraoux void convertVectorToMMAOps(FuncOp funcOp) {
337edd9515bSthomasraoux   SetVector<Operation *> ops = getOpToConvert(funcOp);
338edd9515bSthomasraoux   llvm::DenseMap<Value, Value> valueMapping;
339edd9515bSthomasraoux   for (Operation *op : ops) {
340edd9515bSthomasraoux     if (auto transferRead = dyn_cast<vector::TransferReadOp>(op)) {
341edd9515bSthomasraoux       convertTransferReadOp(transferRead, valueMapping);
342edd9515bSthomasraoux     } else if (auto transferWrite = dyn_cast<vector::TransferWriteOp>(op)) {
343edd9515bSthomasraoux       convertTransferWriteOp(transferWrite, valueMapping);
344edd9515bSthomasraoux     } else if (auto contractOp = dyn_cast<vector::ContractionOp>(op)) {
345edd9515bSthomasraoux       convertContractOp(contractOp, valueMapping);
346*6413226dSthomasraoux     } else if (auto constantOp = dyn_cast<ConstantOp>(op)) {
347*6413226dSthomasraoux       convertConstantOp(constantOp, valueMapping);
348edd9515bSthomasraoux     }
349edd9515bSthomasraoux   }
350edd9515bSthomasraoux }
351edd9515bSthomasraoux 
352edd9515bSthomasraoux } // namespace mlir
353edd9515bSthomasraoux namespace {
354edd9515bSthomasraoux 
355edd9515bSthomasraoux struct ConvertVectorToGPUPass
356edd9515bSthomasraoux     : public ConvertVectorToGPUBase<ConvertVectorToGPUPass> {
357edd9515bSthomasraoux   void runOnFunction() override {
358edd9515bSthomasraoux     RewritePatternSet patterns(getFunction().getContext());
359edd9515bSthomasraoux     populatePrepareVectorToMMAPatterns(patterns);
360edd9515bSthomasraoux     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
361edd9515bSthomasraoux 
362edd9515bSthomasraoux     convertVectorToMMAOps(getFunction());
363edd9515bSthomasraoux   }
364edd9515bSthomasraoux };
365edd9515bSthomasraoux 
366edd9515bSthomasraoux } // namespace
367edd9515bSthomasraoux 
368edd9515bSthomasraoux std::unique_ptr<Pass> mlir::createConvertVectorToGPUPass() {
369edd9515bSthomasraoux   return std::make_unique<ConvertVectorToGPUPass>();
370edd9515bSthomasraoux }
371