blob: 75f62299e1b2b004a27adc8c51d3336461662fb0 [file] [log] [blame]
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -08001//===- VectorAnalysis.cpp - Analysis for Vectorization --------------------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17
18#include "mlir/Analysis/VectorAnalysis.h"
19#include "mlir/IR/BuiltinOps.h"
20#include "mlir/IR/Statements.h"
21#include "mlir/Support/Functional.h"
22#include "mlir/Support/STLExtras.h"
23
24///
25/// Implements Analysis functions specific to vectors which support
26/// the vectorization and vectorization materialization passes.
27///
28
29using namespace mlir;
30
31bool mlir::isaVectorTransferRead(const OperationStmt &stmt) {
32 return stmt.getName().getStringRef().str() == kVectorTransferReadOpName;
33}
34
35bool mlir::isaVectorTransferWrite(const OperationStmt &stmt) {
36 return stmt.getName().getStringRef().str() == kVectorTransferWriteOpName;
37}
38
39Optional<SmallVector<unsigned, 4>> mlir::shapeRatio(ArrayRef<int> superShape,
40 ArrayRef<int> subShape) {
41 if (superShape.size() < subShape.size()) {
42 return Optional<SmallVector<unsigned, 4>>();
43 }
44
45 // Starting from the end, compute the integer divisors.
46 // Set the boolean `divides` if integral division is not possible.
47 std::vector<unsigned> result;
48 result.reserve(superShape.size());
49 bool divides = true;
50 auto divide = [&divides, &result](int superSize, int subSize) {
51 assert(superSize > 0 && "superSize must be > 0");
52 assert(subSize > 0 && "subSize must be > 0");
53 divides &= (superSize % subSize == 0);
54 result.push_back(superSize / subSize);
55 };
Nicolas Vasilacheb8863072018-11-21 12:34:10 -080056 functional::zipApply(
57 divide, SmallVector<int, 8>{superShape.rbegin(), superShape.rend()},
58 SmallVector<int, 8>{subShape.rbegin(), subShape.rend()});
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -080059
60 // If integral division does not occur, return and let the caller decide.
61 if (!divides) {
Nicolas Vasilacheb8863072018-11-21 12:34:10 -080062 return None;
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -080063 }
64
Nicolas Vasilacheb8863072018-11-21 12:34:10 -080065 // At this point we computed the ratio (in reverse) for the common
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -080066 // size. Fill with the remaining entries from the super-vector shape (still in
67 // reverse).
68 int commonSize = subShape.size();
69 std::copy(superShape.rbegin() + commonSize, superShape.rend(),
70 std::back_inserter(result));
71
72 assert(result.size() == superShape.size() &&
Nicolas Vasilacheb8863072018-11-21 12:34:10 -080073 "super to sub shape ratio is not of the same size as the super rank");
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -080074
75 // Reverse again to get it back in the proper order and return.
76 return SmallVector<unsigned, 4>{result.rbegin(), result.rend()};
77}
78
79Optional<SmallVector<unsigned, 4>> mlir::shapeRatio(VectorType superVectorType,
80 VectorType subVectorType) {
81 assert(superVectorType.getElementType() == subVectorType.getElementType() &&
82 "NYI: vector types must be of the same elemental type");
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -080083 return shapeRatio(superVectorType.getShape(), subVectorType.getShape());
84}
85
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -080086bool mlir::matcher::operatesOnStrictSuperVectors(const OperationStmt &opStmt,
87 VectorType subVectorType) {
88 // First, extract the vector type and ditinguish between:
89 // a. ops that *must* lower a super-vector (i.e. vector_transfer_read,
90 // vector_transfer_write); and
91 // b. ops that *may* lower a super-vector (all other ops).
Nicolas Vasilacheb8863072018-11-21 12:34:10 -080092 // The ops that *may* lower a super-vector only do so if the super-vector to
93 // sub-vector ratio is striclty greater than 1. The ops that *must* lower a
94 // super-vector are explicitly checked for this property.
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -080095 /// TODO(ntv): there should be a single function for all ops to do this so we
96 /// do not have to special case. Maybe a trait, or just a method, unclear atm.
97 bool mustDivide = false;
98 VectorType superVectorType;
99 if (isaVectorTransferRead(opStmt)) {
100 superVectorType = opStmt.getResult(0)->getType().cast<VectorType>();
101 mustDivide = true;
102 } else if (isaVectorTransferWrite(opStmt)) {
103 // TODO(ntv): if vector_transfer_write had store-like semantics we could
104 // have written something similar to:
105 // auto store = storeOp->cast<StoreOp>();
106 // auto *value = store->getValueToStore();
107 superVectorType = opStmt.getOperand(0)->getType().cast<VectorType>();
108 mustDivide = true;
109 } else if (opStmt.getNumResults() == 0) {
Nicolas Vasilacheb8863072018-11-21 12:34:10 -0800110 assert(opStmt.isa<ReturnOp>() &&
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -0800111 "NYI: assuming only return statements can have 0 results at this "
112 "point");
113 return false;
114 } else if (opStmt.getNumResults() == 1) {
115 if (auto v = opStmt.getResult(0)->getType().dyn_cast<VectorType>()) {
116 superVectorType = v;
117 } else {
118 // Not a vector type.
119 return false;
120 }
121 } else {
122 // Not a vector_transfer and has more than 1 result, fail hard for now to
123 // wake us up when something changes.
124 assert(false && "NYI: statement has more than 1 result");
125 return false;
126 }
127
Nicolas Vasilacheb8863072018-11-21 12:34:10 -0800128 // Get the ratio.
129 auto ratio = shapeRatio(superVectorType, subVectorType);
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -0800130
131 // Sanity check.
Nicolas Vasilacheb8863072018-11-21 12:34:10 -0800132 assert((ratio.hasValue() || !mustDivide) &&
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -0800133 "NYI: vector_transfer instruction in which super-vector size is not an"
134 " integer multiple of sub-vector size");
135
136 // This catches cases that are not strictly necessary to have multiplicity but
137 // still aren't divisible by the sub-vector shape.
138 // This could be useful information if we wanted to reshape at the level of
139 // the vector type (but we would have to look at the compute and distinguish
140 // between parallel, reduction and possibly other cases.
Nicolas Vasilacheb8863072018-11-21 12:34:10 -0800141 if (!ratio.hasValue()) {
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -0800142 return false;
143 }
144
145 // A strict super-vector is at least 2 sub-vectors.
Nicolas Vasilacheb8863072018-11-21 12:34:10 -0800146 for (auto m : *ratio) {
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -0800147 if (m > 1) {
148 return true;
149 }
150 }
151
152 // Not a strict super-vector.
153 return false;
154}