|
| 1 | +//===- ConstraintsSet.cpp - Extensions for FlatAffineConstraints ----------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// Linalg-specific constraints set extensions. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "mlir/Dialect/Linalg/Analysis/ConstraintsSet.h" |
| 14 | +#include "mlir/Dialect/Affine/IR/AffineValueMap.h" |
| 15 | +#include "mlir/IR/AffineMap.h" |
| 16 | + |
| 17 | +using namespace mlir; |
| 18 | + |
| 19 | +unsigned ConstraintsSet::lookupPos(Value id) const { |
| 20 | + unsigned pos; |
| 21 | + if (!findId(id, &pos)) { |
| 22 | + llvm::errs() << "Lookup failed: " << id << "\n"; |
| 23 | + llvm_unreachable("Lookup failed"); |
| 24 | + } |
| 25 | + return pos; |
| 26 | +} |
| 27 | + |
| 28 | +LogicalResult ConstraintsSet::ensureIdOfType(Value v, bool asDim) { |
| 29 | + if (!containsId(v)) { |
| 30 | + if (asDim) |
| 31 | + addDimId(getNumDimIds(), v); |
| 32 | + else |
| 33 | + addSymbolId(getNumSymbolIds(), v); |
| 34 | + return success(); |
| 35 | + } |
| 36 | + unsigned pos = lookupPos(v); |
| 37 | + return success((asDim && pos < getNumDimIds()) || |
| 38 | + (!asDim && getNumDimIds() <= pos && |
| 39 | + pos < getNumDimIds() + getNumSymbolIds())); |
| 40 | +} |
| 41 | + |
| 42 | +LogicalResult ConstraintsSet::composeAffineApply(Value val, AffineMap map, |
| 43 | + ValueRange operands) { |
| 44 | + AffineValueMap avm(map, operands, val); |
| 45 | + return composeMap(&avm); |
| 46 | +} |
| 47 | + |
| 48 | +LogicalResult ConstraintsSet::composeMinOrMaxMapAndOperands(Value val, |
| 49 | + AffineMap map, |
| 50 | + ValueRange operands, |
| 51 | + bool min) { |
| 52 | + ConstraintsSet localCst; |
| 53 | + std::vector<SmallVector<int64_t, 8>> flatExprs; |
| 54 | + if (failed(getFlattenedAffineExprs(map, &flatExprs, &localCst))) |
| 55 | + return failure(); |
| 56 | + assert(flatExprs.size() == map.getNumResults() && |
| 57 | + "incorrect number of flattened expressiosn"); |
| 58 | + |
| 59 | + // Local vars on a per-need basis. |
| 60 | + if (localCst.getNumLocalIds() != 0) |
| 61 | + return failure(); |
| 62 | + |
| 63 | + // Add one inequality for each result connecting `val` to the other ids in |
| 64 | + // `operands`. For instance, uf the expression is: |
| 65 | + // `16 * i0 + i1` and |
| 66 | + // `min` is true |
| 67 | + // add: |
| 68 | + // -d_val + 16 * i0 + i1 >= 0. |
| 69 | + for (const auto &flatExpr : flatExprs) { |
| 70 | + assert(flatExpr.size() >= operands.size() + 1); |
| 71 | + SmallVector<int64_t, 8> ineq(getNumCols(), 0); |
| 72 | + for (unsigned i = 0, e = operands.size(); i < e; i++) |
| 73 | + ineq[lookupPos(operands[i])] = min ? flatExpr[i] : -flatExpr[i]; |
| 74 | + |
| 75 | + // Set the coefficient for `d_val`. |
| 76 | + ineq[lookupPos(val)] = min ? -1 : 1; |
| 77 | + |
| 78 | + // Set the constant term (upper bound in flatExpr is exclusive). |
| 79 | + ineq[getNumCols() - 1] = min ? flatExpr[flatExpr.size() - 1] - 1 |
| 80 | + : -flatExpr[flatExpr.size() - 1]; |
| 81 | + |
| 82 | + // Add the inequality connecting the result of the map to the rest. |
| 83 | + addInequality(ineq); |
| 84 | + } |
| 85 | + |
| 86 | + return success(); |
| 87 | +} |
0 commit comments