| 1 | //===- ReifyValueBounds.cpp --- Reify value bounds with affine ops ------*-===// |
| 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 | #include "mlir/Dialect/Affine/Transforms/Transforms.h" |
| 10 | |
| 11 | #include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 12 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 13 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 14 | #include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 15 | #include "mlir/Interfaces/ValueBoundsOpInterface.h" |
| 16 | |
| 17 | using namespace mlir; |
| 18 | using namespace mlir::affine; |
| 19 | |
| 20 | FailureOr<OpFoldResult> mlir::affine::reifyValueBound( |
| 21 | OpBuilder &b, Location loc, presburger::BoundType type, |
| 22 | const ValueBoundsConstraintSet::Variable &var, |
| 23 | ValueBoundsConstraintSet::StopConditionFn stopCondition, |
| 24 | ValueBoundsOptions options) { |
| 25 | // Compute bound. |
| 26 | AffineMap boundMap; |
| 27 | ValueDimList mapOperands; |
| 28 | if (failed(Result: ValueBoundsConstraintSet::computeBound( |
| 29 | resultMap&: boundMap, mapOperands, type, var, stopCondition: std::move(stopCondition), options))) |
| 30 | return failure(); |
| 31 | |
| 32 | // Reify bound. |
| 33 | return affine::materializeComputedBound(b, loc, boundMap, mapOperands); |
| 34 | } |
| 35 | |
| 36 | OpFoldResult affine::materializeComputedBound( |
| 37 | OpBuilder &b, Location loc, AffineMap boundMap, |
| 38 | ArrayRef<std::pair<Value, std::optional<int64_t>>> mapOperands) { |
| 39 | // Materialize tensor.dim/memref.dim ops. |
| 40 | SmallVector<Value> operands; |
| 41 | for (auto valueDim : mapOperands) { |
| 42 | Value value = valueDim.first; |
| 43 | std::optional<int64_t> dim = valueDim.second; |
| 44 | |
| 45 | if (!dim.has_value()) { |
| 46 | // This is an index-typed/integer-typed value. |
| 47 | assert( |
| 48 | (value.getType().isIndex() || value.getType().isSignlessInteger()) && |
| 49 | "expected index or signless integer type" ); |
| 50 | if (value.getType().isSignlessInteger()) |
| 51 | value = arith::IndexCastOp::create(builder&: b, location: loc, out: b.getIndexType(), in: value); |
| 52 | operands.push_back(Elt: value); |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | assert(cast<ShapedType>(value.getType()).isDynamicDim(*dim) && |
| 57 | "expected dynamic dim" ); |
| 58 | if (isa<RankedTensorType>(Val: value.getType())) { |
| 59 | // A tensor dimension is used: generate a tensor.dim. |
| 60 | operands.push_back(Elt: tensor::DimOp::create(builder&: b, location: loc, source: value, index: *dim)); |
| 61 | } else if (isa<MemRefType>(Val: value.getType())) { |
| 62 | // A memref dimension is used: generate a memref.dim. |
| 63 | operands.push_back(Elt: memref::DimOp::create(builder&: b, location: loc, source: value, index: *dim)); |
| 64 | } else { |
| 65 | llvm_unreachable("cannot generate DimOp for unsupported shaped type" ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Simplify and return bound. |
| 70 | affine::canonicalizeMapAndOperands(map: &boundMap, operands: &operands); |
| 71 | // Check for special cases where no affine.apply op is needed. |
| 72 | if (boundMap.isSingleConstant()) { |
| 73 | // Bound is a constant: return an IntegerAttr. |
| 74 | return static_cast<OpFoldResult>( |
| 75 | b.getIndexAttr(value: boundMap.getSingleConstantResult())); |
| 76 | } |
| 77 | // No affine.apply op is needed if the bound is a single SSA value. |
| 78 | if (auto expr = dyn_cast<AffineDimExpr>(Val: boundMap.getResult(idx: 0))) |
| 79 | return static_cast<OpFoldResult>(operands[expr.getPosition()]); |
| 80 | if (auto expr = dyn_cast<AffineSymbolExpr>(Val: boundMap.getResult(idx: 0))) |
| 81 | return static_cast<OpFoldResult>( |
| 82 | operands[expr.getPosition() + boundMap.getNumDims()]); |
| 83 | // General case: build affine.apply op. |
| 84 | return static_cast<OpFoldResult>( |
| 85 | affine::AffineApplyOp::create(builder&: b, location: loc, map: boundMap, mapOperands: operands).getResult()); |
| 86 | } |
| 87 | |
| 88 | FailureOr<OpFoldResult> mlir::affine::reifyShapedValueDimBound( |
| 89 | OpBuilder &b, Location loc, presburger::BoundType type, Value value, |
| 90 | int64_t dim, ValueBoundsConstraintSet::StopConditionFn stopCondition, |
| 91 | ValueBoundsOptions options) { |
| 92 | auto reifyToOperands = [&](Value v, std::optional<int64_t> d, |
| 93 | ValueBoundsConstraintSet &cstr) { |
| 94 | // We are trying to reify a bound for `value` in terms of the owning op's |
| 95 | // operands. Construct a stop condition that evaluates to "true" for any SSA |
| 96 | // value except for `value`. I.e., the bound will be computed in terms of |
| 97 | // any SSA values except for `value`. The first such values are operands of |
| 98 | // the owner of `value`. |
| 99 | return v != value; |
| 100 | }; |
| 101 | return reifyValueBound( |
| 102 | b, loc, type, var: {value, dim}, |
| 103 | stopCondition: stopCondition ? std::move(stopCondition) : reifyToOperands, options); |
| 104 | } |
| 105 | |
| 106 | FailureOr<OpFoldResult> mlir::affine::reifyIndexValueBound( |
| 107 | OpBuilder &b, Location loc, presburger::BoundType type, Value value, |
| 108 | ValueBoundsConstraintSet::StopConditionFn stopCondition, |
| 109 | ValueBoundsOptions options) { |
| 110 | auto reifyToOperands = [&](Value v, std::optional<int64_t> d, |
| 111 | ValueBoundsConstraintSet &cstr) { |
| 112 | return v != value; |
| 113 | }; |
| 114 | return reifyValueBound( |
| 115 | b, loc, type, var: value, |
| 116 | stopCondition: stopCondition ? std::move(stopCondition) : reifyToOperands, options); |
| 117 | } |
| 118 | |