Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 1 | //===- LoopAnalysis.cpp - Misc loop analysis routines //-------------------===// |
| 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 | // This file implements miscellaneous loop analysis routines. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "mlir/Analysis/LoopAnalysis.h" |
| 23 | |
| 24 | #include "mlir/Analysis/AffineAnalysis.h" |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 25 | #include "mlir/Analysis/AffineStructures.h" |
| 26 | #include "mlir/Analysis/MLFunctionMatcher.h" |
Nicolas Vasilache | 13b3bce | 2018-11-20 08:36:07 -0800 | [diff] [blame] | 27 | #include "mlir/Analysis/VectorAnalysis.h" |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 28 | #include "mlir/IR/Builders.h" |
| 29 | #include "mlir/IR/BuiltinOps.h" |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 30 | #include "mlir/IR/Statements.h" |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 31 | #include "mlir/StandardOps/StandardOps.h" |
Alex Zinenko | f9e30b9 | 2018-12-14 09:31:17 -0800 | [diff] [blame^] | 32 | #include "mlir/SuperVectorOps/SuperVectorOps.h" |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 33 | #include "mlir/Support/Functional.h" |
Uday Bondhugula | 48e4c4b | 2018-10-03 10:07:54 -0700 | [diff] [blame] | 34 | #include "mlir/Support/MathExtras.h" |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 35 | |
| 36 | #include "llvm/ADT/DenseSet.h" |
Nicolas Vasilache | 6b19746 | 2018-11-14 04:04:10 -0800 | [diff] [blame] | 37 | #include "llvm/ADT/SmallString.h" |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 38 | #include <type_traits> |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 39 | |
Nicolas Vasilache | 5373b09 | 2018-10-03 15:39:12 -0700 | [diff] [blame] | 40 | using namespace mlir; |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 41 | |
| 42 | /// Returns the trip count of the loop as an affine expression if the latter is |
| 43 | /// expressible as an affine expression, and nullptr otherwise. The trip count |
| 44 | /// expression is simplified before returning. |
Nicolas Vasilache | fb11e0e | 2018-10-08 13:47:18 -0700 | [diff] [blame] | 45 | AffineExpr mlir::getTripCountExpr(const ForStmt &forStmt) { |
Nicolas Vasilache | ff30328 | 2018-11-07 05:44:50 -0800 | [diff] [blame] | 46 | // upper_bound - lower_bound |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 47 | int64_t loopSpan; |
| 48 | |
| 49 | int64_t step = forStmt.getStep(); |
| 50 | auto *context = forStmt.getContext(); |
| 51 | |
| 52 | if (forStmt.hasConstantBounds()) { |
| 53 | int64_t lb = forStmt.getConstantLowerBound(); |
| 54 | int64_t ub = forStmt.getConstantUpperBound(); |
Nicolas Vasilache | ff30328 | 2018-11-07 05:44:50 -0800 | [diff] [blame] | 55 | loopSpan = ub - lb; |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 56 | } else { |
Nicolas Vasilache | 75ed337 | 2018-10-09 16:39:24 -0700 | [diff] [blame] | 57 | auto lbMap = forStmt.getLowerBoundMap(); |
| 58 | auto ubMap = forStmt.getUpperBoundMap(); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 59 | // TODO(bondhugula): handle max/min of multiple expressions. |
Nicolas Vasilache | 75ed337 | 2018-10-09 16:39:24 -0700 | [diff] [blame] | 60 | if (lbMap.getNumResults() != 1 || ubMap.getNumResults() != 1) |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 61 | return nullptr; |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 62 | |
| 63 | // TODO(bondhugula): handle bounds with different operands. |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 64 | // Bounds have different operands, unhandled for now. |
Uday Bondhugula | 5912e87 | 2018-09-18 10:22:03 -0700 | [diff] [blame] | 65 | if (!forStmt.matchingBoundOperandList()) |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 66 | return nullptr; |
| 67 | |
Nicolas Vasilache | ff30328 | 2018-11-07 05:44:50 -0800 | [diff] [blame] | 68 | // ub_expr - lb_expr |
Nicolas Vasilache | 75ed337 | 2018-10-09 16:39:24 -0700 | [diff] [blame] | 69 | AffineExpr lbExpr(lbMap.getResult(0)); |
| 70 | AffineExpr ubExpr(ubMap.getResult(0)); |
Nicolas Vasilache | 5373b09 | 2018-10-03 15:39:12 -0700 | [diff] [blame] | 71 | auto loopSpanExpr = simplifyAffineExpr( |
Nicolas Vasilache | ff30328 | 2018-11-07 05:44:50 -0800 | [diff] [blame] | 72 | ubExpr - lbExpr, std::max(lbMap.getNumDims(), ubMap.getNumDims()), |
Nicolas Vasilache | 75ed337 | 2018-10-09 16:39:24 -0700 | [diff] [blame] | 73 | std::max(lbMap.getNumSymbols(), ubMap.getNumSymbols())); |
Nicolas Vasilache | fb11e0e | 2018-10-08 13:47:18 -0700 | [diff] [blame] | 74 | auto cExpr = loopSpanExpr.dyn_cast<AffineConstantExpr>(); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 75 | if (!cExpr) |
Nicolas Vasilache | bc74609 | 2018-10-08 10:20:25 -0700 | [diff] [blame] | 76 | return loopSpanExpr.ceilDiv(step); |
Nicolas Vasilache | b771709 | 2018-10-09 10:59:27 -0700 | [diff] [blame] | 77 | loopSpan = cExpr.getValue(); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // 0 iteration loops. |
Uday Bondhugula | ff5d6bd | 2018-09-27 18:03:27 -0700 | [diff] [blame] | 81 | if (loopSpan < 0) |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 82 | return 0; |
| 83 | |
Nicolas Vasilache | bc74609 | 2018-10-08 10:20:25 -0700 | [diff] [blame] | 84 | return getAffineConstantExpr(static_cast<uint64_t>(ceilDiv(loopSpan, step)), |
| 85 | context); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /// Returns the trip count of the loop if it's a constant, None otherwise. This |
| 89 | /// method uses affine expression analysis (in turn using getTripCount) and is |
| 90 | /// able to determine constant trip count in non-trivial cases. |
| 91 | llvm::Optional<uint64_t> mlir::getConstantTripCount(const ForStmt &forStmt) { |
Nicolas Vasilache | 5373b09 | 2018-10-03 15:39:12 -0700 | [diff] [blame] | 92 | auto tripCountExpr = getTripCountExpr(forStmt); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 93 | |
Nicolas Vasilache | 32402e5 | 2018-10-08 08:09:50 -0700 | [diff] [blame] | 94 | if (!tripCountExpr) |
| 95 | return None; |
| 96 | |
Nicolas Vasilache | fb11e0e | 2018-10-08 13:47:18 -0700 | [diff] [blame] | 97 | if (auto constExpr = tripCountExpr.dyn_cast<AffineConstantExpr>()) |
Nicolas Vasilache | b771709 | 2018-10-09 10:59:27 -0700 | [diff] [blame] | 98 | return constExpr.getValue(); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 99 | |
| 100 | return None; |
| 101 | } |
| 102 | |
| 103 | /// Returns the greatest known integral divisor of the trip count. Affine |
| 104 | /// expression analysis is used (indirectly through getTripCount), and |
| 105 | /// this method is thus able to determine non-trivial divisors. |
| 106 | uint64_t mlir::getLargestDivisorOfTripCount(const ForStmt &forStmt) { |
Nicolas Vasilache | 5373b09 | 2018-10-03 15:39:12 -0700 | [diff] [blame] | 107 | auto tripCountExpr = getTripCountExpr(forStmt); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 108 | |
| 109 | if (!tripCountExpr) |
| 110 | return 1; |
| 111 | |
Nicolas Vasilache | fb11e0e | 2018-10-08 13:47:18 -0700 | [diff] [blame] | 112 | if (auto constExpr = tripCountExpr.dyn_cast<AffineConstantExpr>()) { |
Nicolas Vasilache | b771709 | 2018-10-09 10:59:27 -0700 | [diff] [blame] | 113 | uint64_t tripCount = constExpr.getValue(); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 114 | |
| 115 | // 0 iteration loops (greatest divisor is 2^64 - 1). |
| 116 | if (tripCount == 0) |
| 117 | return ULONG_MAX; |
| 118 | |
| 119 | // The greatest divisor is the trip count. |
| 120 | return tripCount; |
| 121 | } |
| 122 | |
| 123 | // Trip count is not a known constant; return its largest known divisor. |
Nicolas Vasilache | b771709 | 2018-10-09 10:59:27 -0700 | [diff] [blame] | 124 | return tripCountExpr.getLargestKnownDivisor(); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 125 | } |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 126 | |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 127 | bool mlir::isAccessInvariant(const MLValue &iv, const MLValue &index) { |
| 128 | assert(isa<ForStmt>(iv) && "iv must be a ForStmt"); |
| 129 | assert(index.getType().isa<IndexType>() && "index must be of IndexType"); |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 130 | SmallVector<OperationStmt *, 4> affineApplyOps; |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 131 | getReachableAffineApplyOps({const_cast<MLValue *>(&index)}, affineApplyOps); |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 132 | |
| 133 | if (affineApplyOps.empty()) { |
| 134 | // Pointer equality test because of MLValue pointer semantics. |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 135 | return &index != &iv; |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Nicolas Vasilache | c040bd5 | 2018-12-06 11:38:09 -0800 | [diff] [blame] | 138 | if (affineApplyOps.size() > 1) { |
| 139 | affineApplyOps[0]->emitError( |
| 140 | "CompositionAffineMapsPass must have been run: there should be at most " |
| 141 | "one AffineApplyOp"); |
| 142 | return false; |
| 143 | } |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 144 | |
Feng Liu | ec065d7 | 2018-10-19 09:07:58 -0700 | [diff] [blame] | 145 | auto composeOp = affineApplyOps[0]->cast<AffineApplyOp>(); |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 146 | // We need yet another level of indirection because the `dim` index of the |
| 147 | // access may not correspond to the `dim` index of composeOp. |
| 148 | unsigned idx = std::numeric_limits<unsigned>::max(); |
| 149 | unsigned numResults = composeOp->getNumResults(); |
| 150 | for (unsigned i = 0; i < numResults; ++i) { |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 151 | if (&index == composeOp->getResult(i)) { |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 152 | idx = i; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | assert(idx < std::numeric_limits<unsigned>::max()); |
| 157 | return !AffineValueMap(*composeOp) |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 158 | .isFunctionOf(idx, &const_cast<MLValue &>(iv)); |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 161 | llvm::DenseSet<const MLValue *> |
| 162 | mlir::getInvariantAccesses(const MLValue &iv, |
| 163 | llvm::ArrayRef<const MLValue *> indices) { |
| 164 | llvm::DenseSet<const MLValue *> res; |
| 165 | for (unsigned idx = 0, n = indices.size(); idx < n; ++idx) { |
| 166 | auto *val = indices[idx]; |
| 167 | if (isAccessInvariant(iv, *val)) { |
| 168 | res.insert(val); |
| 169 | } |
| 170 | } |
| 171 | return res; |
| 172 | } |
| 173 | |
| 174 | /// Given: |
| 175 | /// 1. an induction variable `iv` of type ForStmt; |
| 176 | /// 2. a `memoryOp` of type const LoadOp& or const StoreOp&; |
| 177 | /// 3. the index of the `fastestVaryingDim` along which to check; |
| 178 | /// determines whether `memoryOp`[`fastestVaryingDim`] is a contiguous access |
| 179 | /// along `iv`. |
| 180 | /// Contiguous is defined as either invariant or varying only along |
| 181 | /// `fastestVaryingDim`. |
| 182 | /// |
| 183 | /// Prerequisites: |
| 184 | /// 1. `iv` of the proper type; |
| 185 | /// 2. the MemRef accessed by `memoryOp` has no layout map or at most an |
| 186 | /// identity layout map. |
| 187 | /// |
Nicolas Vasilache | c040bd5 | 2018-12-06 11:38:09 -0800 | [diff] [blame] | 188 | /// Currently only supports no layoutMap or identity layoutMap in the MemRef. |
| 189 | /// Returns false if the MemRef has a non-identity layoutMap or more than |
| 190 | /// 1 layoutMap. This is conservative. |
| 191 | /// |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 192 | // TODO(ntv): check strides. |
| 193 | template <typename LoadOrStoreOp> |
| 194 | static bool isContiguousAccess(const MLValue &iv, const LoadOrStoreOp &memoryOp, |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 195 | unsigned fastestVaryingDim) { |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 196 | static_assert(std::is_same<LoadOrStoreOp, LoadOp>::value || |
| 197 | std::is_same<LoadOrStoreOp, StoreOp>::value, |
| 198 | "Must be called on either const LoadOp & or const StoreOp &"); |
| 199 | auto memRefType = memoryOp.getMemRefType(); |
| 200 | auto layoutMap = memRefType.getAffineMaps(); |
Nicolas Vasilache | c040bd5 | 2018-12-06 11:38:09 -0800 | [diff] [blame] | 201 | // TODO(ntv): remove dependence on Builder once we support non-identity |
| 202 | // layout map. |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 203 | Builder b(memoryOp.getOperation()->getContext()); |
Nicolas Vasilache | c040bd5 | 2018-12-06 11:38:09 -0800 | [diff] [blame] | 204 | if (layoutMap.size() >= 2 || |
| 205 | (layoutMap.size() == 1 && |
| 206 | !(layoutMap[0] == |
| 207 | b.getMultiDimIdentityMap(layoutMap[0].getNumDims())))) { |
| 208 | return memoryOp.emitError("NYI: non-trivial layoutMap"), false; |
| 209 | } |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 210 | assert(fastestVaryingDim < memRefType.getRank()); |
| 211 | |
| 212 | auto indices = memoryOp.getIndices(); |
| 213 | // TODO(clattner): should iterator_range have a size method? |
| 214 | auto numIndices = indices.end() - indices.begin(); |
| 215 | unsigned d = 0; |
| 216 | for (auto index : indices) { |
| 217 | if (fastestVaryingDim == (numIndices - 1) - d++) { |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 218 | continue; |
| 219 | } |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 220 | if (!isAccessInvariant(iv, cast<MLValue>(*index))) { |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 227 | template <typename LoadOrStoreOpPointer> |
| 228 | static bool isVectorElement(LoadOrStoreOpPointer memoryOp) { |
River Riddle | 666dfbe | 2018-10-30 14:59:22 -0700 | [diff] [blame] | 229 | auto memRefType = memoryOp->getMemRefType(); |
| 230 | return memRefType.getElementType().template isa<VectorType>(); |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Nicolas Vasilache | 6b19746 | 2018-11-14 04:04:10 -0800 | [diff] [blame] | 233 | static bool isVectorTransferReadOrWrite(const Statement &stmt) { |
| 234 | const auto *opStmt = cast<OperationStmt>(&stmt); |
Nicolas Vasilache | 9a19ada | 2018-12-03 15:21:27 -0800 | [diff] [blame] | 235 | return opStmt->isa<VectorTransferReadOp>() || |
| 236 | opStmt->isa<VectorTransferWriteOp>(); |
Nicolas Vasilache | 6b19746 | 2018-11-14 04:04:10 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Nicolas Vasilache | d64816a | 2018-11-01 07:14:14 -0700 | [diff] [blame] | 239 | using VectorizableStmtFun = |
| 240 | std::function<bool(const ForStmt &, const OperationStmt &)>; |
| 241 | |
| 242 | static bool isVectorizableLoopWithCond(const ForStmt &loop, |
| 243 | VectorizableStmtFun isVectorizableStmt) { |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 244 | if (!matcher::isParallelLoop(loop) && !matcher::isReductionLoop(loop)) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | // No vectorization across conditionals for now. |
| 249 | auto conditionals = matcher::If(); |
| 250 | auto *forStmt = const_cast<ForStmt *>(&loop); |
| 251 | auto conditionalsMatched = conditionals.match(forStmt); |
| 252 | if (!conditionalsMatched.empty()) { |
| 253 | return false; |
| 254 | } |
| 255 | |
Nicolas Vasilache | 6b19746 | 2018-11-14 04:04:10 -0800 | [diff] [blame] | 256 | auto vectorTransfers = matcher::Op(isVectorTransferReadOrWrite); |
| 257 | auto vectorTransfersMatched = vectorTransfers.match(forStmt); |
| 258 | if (!vectorTransfersMatched.empty()) { |
| 259 | return false; |
| 260 | } |
| 261 | |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 262 | auto loadAndStores = matcher::Op(matcher::isLoadOrStore); |
| 263 | auto loadAndStoresMatched = loadAndStores.match(forStmt); |
| 264 | for (auto ls : loadAndStoresMatched) { |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 265 | auto *op = cast<OperationStmt>(ls.first); |
Feng Liu | ec065d7 | 2018-10-19 09:07:58 -0700 | [diff] [blame] | 266 | auto load = op->dyn_cast<LoadOp>(); |
| 267 | auto store = op->dyn_cast<StoreOp>(); |
Nicolas Vasilache | 078d9b9 | 2018-10-30 07:54:23 -0700 | [diff] [blame] | 268 | // Only scalar types are considered vectorizable, all load/store must be |
| 269 | // vectorizable for a loop to qualify as vectorizable. |
| 270 | // TODO(ntv): ponder whether we want to be more general here. |
| 271 | bool vector = load ? isVectorElement(load) : isVectorElement(store); |
| 272 | if (vector) { |
| 273 | return false; |
| 274 | } |
Nicolas Vasilache | d64816a | 2018-11-01 07:14:14 -0700 | [diff] [blame] | 275 | if (!isVectorizableStmt(loop, *op)) { |
Nicolas Vasilache | fd8d256 | 2018-10-17 18:01:44 -0700 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | return true; |
| 280 | } |
Uday Bondhugula | 861fe64 | 2018-10-18 11:14:26 -0700 | [diff] [blame] | 281 | |
Nicolas Vasilache | d64816a | 2018-11-01 07:14:14 -0700 | [diff] [blame] | 282 | bool mlir::isVectorizableLoopAlongFastestVaryingMemRefDim( |
| 283 | const ForStmt &loop, unsigned fastestVaryingDim) { |
| 284 | VectorizableStmtFun fun( |
| 285 | [fastestVaryingDim](const ForStmt &loop, const OperationStmt &op) { |
| 286 | auto load = op.dyn_cast<LoadOp>(); |
| 287 | auto store = op.dyn_cast<StoreOp>(); |
Nicolas Vasilache | 787a93c | 2018-12-06 11:37:25 -0800 | [diff] [blame] | 288 | return load ? isContiguousAccess(loop, *load, fastestVaryingDim) |
| 289 | : isContiguousAccess(loop, *store, fastestVaryingDim); |
Nicolas Vasilache | d64816a | 2018-11-01 07:14:14 -0700 | [diff] [blame] | 290 | }); |
| 291 | return isVectorizableLoopWithCond(loop, fun); |
| 292 | } |
| 293 | |
| 294 | bool mlir::isVectorizableLoop(const ForStmt &loop) { |
| 295 | VectorizableStmtFun fun( |
| 296 | // TODO: implement me |
| 297 | [](const ForStmt &loop, const OperationStmt &op) { return true; }); |
| 298 | return isVectorizableLoopWithCond(loop, fun); |
| 299 | } |
| 300 | |
Uday Bondhugula | 861fe64 | 2018-10-18 11:14:26 -0700 | [diff] [blame] | 301 | /// Checks whether SSA dominance would be violated if a for stmt's body |
| 302 | /// statements are shifted by the specified shifts. This method checks if a |
| 303 | /// 'def' and all its uses have the same shift factor. |
| 304 | // TODO(mlir-team): extend this to check for memory-based dependence |
| 305 | // violation when we have the support. |
| 306 | bool mlir::isStmtwiseShiftValid(const ForStmt &forStmt, |
| 307 | ArrayRef<uint64_t> shifts) { |
| 308 | assert(shifts.size() == forStmt.getStatements().size()); |
| 309 | unsigned s = 0; |
| 310 | for (const auto &stmt : forStmt) { |
| 311 | // A for or if stmt does not produce any def/results (that are used |
| 312 | // outside). |
| 313 | if (const auto *opStmt = dyn_cast<OperationStmt>(&stmt)) { |
| 314 | for (unsigned i = 0, e = opStmt->getNumResults(); i < e; ++i) { |
| 315 | const MLValue *result = opStmt->getResult(i); |
| 316 | for (const StmtOperand &use : result->getUses()) { |
| 317 | // If an ancestor statement doesn't lie in the block of forStmt, there |
| 318 | // is no shift to check. |
| 319 | // This is a naive way. If performance becomes an issue, a map can |
| 320 | // be used to store 'shifts' - to look up the shift for a statement in |
| 321 | // constant time. |
| 322 | if (auto *ancStmt = forStmt.findAncestorStmtInBlock(*use.getOwner())) |
| 323 | if (shifts[s] != shifts[forStmt.findStmtPosInBlock(*ancStmt)]) |
| 324 | return false; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | s++; |
| 329 | } |
| 330 | return true; |
| 331 | } |