Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 1 | //===- 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 | |
| 29 | using namespace mlir; |
| 30 | |
| 31 | bool mlir::isaVectorTransferRead(const OperationStmt &stmt) { |
| 32 | return stmt.getName().getStringRef().str() == kVectorTransferReadOpName; |
| 33 | } |
| 34 | |
| 35 | bool mlir::isaVectorTransferWrite(const OperationStmt &stmt) { |
| 36 | return stmt.getName().getStringRef().str() == kVectorTransferWriteOpName; |
| 37 | } |
| 38 | |
| 39 | Optional<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 = [÷s, &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 Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 56 | functional::zipApply( |
| 57 | divide, SmallVector<int, 8>{superShape.rbegin(), superShape.rend()}, |
| 58 | SmallVector<int, 8>{subShape.rbegin(), subShape.rend()}); |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 59 | |
| 60 | // If integral division does not occur, return and let the caller decide. |
| 61 | if (!divides) { |
Nicolas Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 62 | return None; |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Nicolas Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 65 | // At this point we computed the ratio (in reverse) for the common |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 66 | // 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 Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 73 | "super to sub shape ratio is not of the same size as the super rank"); |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 74 | |
| 75 | // Reverse again to get it back in the proper order and return. |
| 76 | return SmallVector<unsigned, 4>{result.rbegin(), result.rend()}; |
| 77 | } |
| 78 | |
| 79 | Optional<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 Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 83 | return shapeRatio(superVectorType.getShape(), subVectorType.getShape()); |
| 84 | } |
| 85 | |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 86 | bool 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 Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 92 | // 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 Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 95 | /// 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 Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 110 | assert(opStmt.isa<ReturnOp>() && |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 111 | "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 Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 128 | // Get the ratio. |
| 129 | auto ratio = shapeRatio(superVectorType, subVectorType); |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 130 | |
| 131 | // Sanity check. |
Nicolas Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 132 | assert((ratio.hasValue() || !mustDivide) && |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 133 | "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 Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 141 | if (!ratio.hasValue()) { |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | |
| 145 | // A strict super-vector is at least 2 sub-vectors. |
Nicolas Vasilache | b886307 | 2018-11-21 12:34:10 -0800 | [diff] [blame^] | 146 | for (auto m : *ratio) { |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 147 | if (m > 1) { |
| 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Not a strict super-vector. |
| 153 | return false; |
| 154 | } |