1*db011775SGeoffrey Martin-Noble //===- StructuredOpsUtils.cpp - Utilities used by structured ops ----------===//
2*db011775SGeoffrey Martin-Noble //
3*db011775SGeoffrey Martin-Noble // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*db011775SGeoffrey Martin-Noble // See https://llvm.org/LICENSE.txt for license information.
5*db011775SGeoffrey Martin-Noble // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*db011775SGeoffrey Martin-Noble //
7*db011775SGeoffrey Martin-Noble //===----------------------------------------------------------------------===//
8*db011775SGeoffrey Martin-Noble 
9*db011775SGeoffrey Martin-Noble #include "mlir/Dialect/Utils/StructuredOpsUtils.h"
10*db011775SGeoffrey Martin-Noble #include "mlir/IR/AffineMap.h"
11*db011775SGeoffrey Martin-Noble #include "mlir/IR/BuiltinAttributes.h"
12*db011775SGeoffrey Martin-Noble 
13*db011775SGeoffrey Martin-Noble using namespace mlir;
14*db011775SGeoffrey Martin-Noble 
isRowMajorMatmul(ArrayAttr indexingMaps)15*db011775SGeoffrey Martin-Noble bool mlir::isRowMajorMatmul(ArrayAttr indexingMaps) {
16*db011775SGeoffrey Martin-Noble   if (indexingMaps.size() != 3)
17*db011775SGeoffrey Martin-Noble     return false;
18*db011775SGeoffrey Martin-Noble 
19*db011775SGeoffrey Martin-Noble   auto map0 = indexingMaps[0].cast<AffineMapAttr>().getValue();
20*db011775SGeoffrey Martin-Noble   auto map1 = indexingMaps[1].cast<AffineMapAttr>().getValue();
21*db011775SGeoffrey Martin-Noble   auto map2 = indexingMaps[2].cast<AffineMapAttr>().getValue();
22*db011775SGeoffrey Martin-Noble 
23*db011775SGeoffrey Martin-Noble   if (map0.getNumResults() != 2 || map1.getNumResults() != 2 ||
24*db011775SGeoffrey Martin-Noble       map2.getNumResults() != 2 || map0.getNumInputs() != 3 ||
25*db011775SGeoffrey Martin-Noble       map1.getNumInputs() != 3 || map2.getNumInputs() != 3) {
26*db011775SGeoffrey Martin-Noble     return false;
27*db011775SGeoffrey Martin-Noble   }
28*db011775SGeoffrey Martin-Noble 
29*db011775SGeoffrey Martin-Noble   // Extract dimensions for MxK * KxN -> MxN
30*db011775SGeoffrey Martin-Noble   AffineExpr m = map2.getResult(0);
31*db011775SGeoffrey Martin-Noble   AffineExpr n = map2.getResult(1);
32*db011775SGeoffrey Martin-Noble   AffineExpr k = map0.getResult(1);
33*db011775SGeoffrey Martin-Noble   auto *context = indexingMaps.getContext();
34*db011775SGeoffrey Martin-Noble   auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {m, k}, context));
35*db011775SGeoffrey Martin-Noble   auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {k, n}, context));
36*db011775SGeoffrey Martin-Noble   auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {m, n}, context));
37*db011775SGeoffrey Martin-Noble   auto maps = ArrayAttr::get(context, {mapA, mapB, mapC});
38*db011775SGeoffrey Martin-Noble   return indexingMaps == maps;
39*db011775SGeoffrey Martin-Noble }
40*db011775SGeoffrey Martin-Noble 
isColumnMajorMatmul(ArrayAttr indexingMaps)41*db011775SGeoffrey Martin-Noble bool mlir::isColumnMajorMatmul(ArrayAttr indexingMaps) {
42*db011775SGeoffrey Martin-Noble   if (indexingMaps.size() != 3)
43*db011775SGeoffrey Martin-Noble     return false;
44*db011775SGeoffrey Martin-Noble 
45*db011775SGeoffrey Martin-Noble   auto map0 = indexingMaps[0].cast<AffineMapAttr>().getValue();
46*db011775SGeoffrey Martin-Noble   auto map1 = indexingMaps[1].cast<AffineMapAttr>().getValue();
47*db011775SGeoffrey Martin-Noble   auto map2 = indexingMaps[2].cast<AffineMapAttr>().getValue();
48*db011775SGeoffrey Martin-Noble 
49*db011775SGeoffrey Martin-Noble   if (map0.getNumResults() != 2 || map1.getNumResults() != 2 ||
50*db011775SGeoffrey Martin-Noble       map2.getNumResults() != 2 || map0.getNumInputs() != 3 ||
51*db011775SGeoffrey Martin-Noble       map1.getNumInputs() != 3 || map2.getNumInputs() != 3) {
52*db011775SGeoffrey Martin-Noble     return false;
53*db011775SGeoffrey Martin-Noble   }
54*db011775SGeoffrey Martin-Noble 
55*db011775SGeoffrey Martin-Noble   // Extract dimensions for KxM * NxK -> NxM
56*db011775SGeoffrey Martin-Noble   AffineExpr n = map2.getResult(0);
57*db011775SGeoffrey Martin-Noble   AffineExpr m = map2.getResult(1);
58*db011775SGeoffrey Martin-Noble   AffineExpr k = map0.getResult(0);
59*db011775SGeoffrey Martin-Noble   auto *context = indexingMaps.getContext();
60*db011775SGeoffrey Martin-Noble   auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {k, m}, context));
61*db011775SGeoffrey Martin-Noble   auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {n, k}, context));
62*db011775SGeoffrey Martin-Noble   auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {n, m}, context));
63*db011775SGeoffrey Martin-Noble   auto maps = ArrayAttr::get(context, {mapA, mapB, mapC});
64*db011775SGeoffrey Martin-Noble   return indexingMaps == maps;
65*db011775SGeoffrey Martin-Noble }
66*db011775SGeoffrey Martin-Noble 
isRowMajorBatchMatmul(ArrayAttr indexingMaps)67*db011775SGeoffrey Martin-Noble bool mlir::isRowMajorBatchMatmul(ArrayAttr indexingMaps) {
68*db011775SGeoffrey Martin-Noble   if (indexingMaps.size() != 3)
69*db011775SGeoffrey Martin-Noble     return false;
70*db011775SGeoffrey Martin-Noble 
71*db011775SGeoffrey Martin-Noble   auto map0 = indexingMaps[0].cast<AffineMapAttr>().getValue();
72*db011775SGeoffrey Martin-Noble   auto map1 = indexingMaps[1].cast<AffineMapAttr>().getValue();
73*db011775SGeoffrey Martin-Noble   auto map2 = indexingMaps[2].cast<AffineMapAttr>().getValue();
74*db011775SGeoffrey Martin-Noble 
75*db011775SGeoffrey Martin-Noble   if (map0.getNumResults() != 3 || map1.getNumResults() != 3 ||
76*db011775SGeoffrey Martin-Noble       map2.getNumResults() != 3 || map0.getNumInputs() != 4 ||
77*db011775SGeoffrey Martin-Noble       map1.getNumInputs() != 4 || map2.getNumInputs() != 4) {
78*db011775SGeoffrey Martin-Noble     return false;
79*db011775SGeoffrey Martin-Noble   }
80*db011775SGeoffrey Martin-Noble 
81*db011775SGeoffrey Martin-Noble   // Extract dimensions for BxMxK * BxKxN -> BxMxN
82*db011775SGeoffrey Martin-Noble   AffineExpr b = map2.getResult(0);
83*db011775SGeoffrey Martin-Noble   AffineExpr m = map2.getResult(1);
84*db011775SGeoffrey Martin-Noble   AffineExpr n = map2.getResult(2);
85*db011775SGeoffrey Martin-Noble   AffineExpr k = map0.getResult(2);
86*db011775SGeoffrey Martin-Noble   auto *context = indexingMaps.getContext();
87*db011775SGeoffrey Martin-Noble   auto mapA = AffineMapAttr::get(AffineMap::get(4, 0, {b, m, k}, context));
88*db011775SGeoffrey Martin-Noble   auto mapB = AffineMapAttr::get(AffineMap::get(4, 0, {b, k, n}, context));
89*db011775SGeoffrey Martin-Noble   auto mapC = AffineMapAttr::get(AffineMap::get(4, 0, {b, m, n}, context));
90*db011775SGeoffrey Martin-Noble   auto maps = ArrayAttr::get(context, {mapA, mapB, mapC});
91*db011775SGeoffrey Martin-Noble   return indexingMaps == maps;
92*db011775SGeoffrey Martin-Noble }
93