| 1 | //===- ValueBoundsOpInterfaceImpl.cpp - Impl. of ValueBoundsOpInterface ---===// |
| 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/Tensor/IR/ValueBoundsOpInterfaceImpl.h" |
| 10 | |
| 11 | #include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 12 | #include "mlir/Dialect/Utils/StaticValueUtils.h" |
| 13 | #include "mlir/Interfaces/ValueBoundsOpInterface.h" |
| 14 | |
| 15 | using namespace mlir; |
| 16 | |
| 17 | namespace mlir { |
| 18 | namespace tensor { |
| 19 | namespace { |
| 20 | |
| 21 | struct CastOpInterface |
| 22 | : public ValueBoundsOpInterface::ExternalModel<CastOpInterface, CastOp> { |
| 23 | void populateBoundsForShapedValueDim(Operation *op, Value value, int64_t dim, |
| 24 | ValueBoundsConstraintSet &cstr) const { |
| 25 | auto castOp = cast<CastOp>(Val: op); |
| 26 | assert(value == castOp.getResult() && "invalid value" ); |
| 27 | |
| 28 | if (llvm::isa<RankedTensorType>(Val: castOp.getResult().getType()) && |
| 29 | llvm::isa<RankedTensorType>(Val: castOp.getSource().getType())) { |
| 30 | cstr.bound(value)[dim] == cstr.getExpr(value: castOp.getSource(), dim); |
| 31 | } |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | struct CollapseShapeOpInterface |
| 36 | : public ValueBoundsOpInterface::ExternalModel<CollapseShapeOpInterface, |
| 37 | CollapseShapeOp> { |
| 38 | void populateBoundsForShapedValueDim(Operation *op, Value value, int64_t dim, |
| 39 | ValueBoundsConstraintSet &cstr) const { |
| 40 | auto collapseOp = cast<CollapseShapeOp>(Val: op); |
| 41 | assert(value == collapseOp.getResult() && "invalid value" ); |
| 42 | |
| 43 | // Multiply the expressions for the dimensions in the reassociation group. |
| 44 | const ReassociationIndices reassocIndices = |
| 45 | collapseOp.getReassociationIndices()[dim]; |
| 46 | AffineExpr productExpr = |
| 47 | cstr.getExpr(value: collapseOp.getSrc(), dim: reassocIndices[0]); |
| 48 | for (size_t i = 1; i < reassocIndices.size(); ++i) { |
| 49 | productExpr = |
| 50 | productExpr * cstr.getExpr(value: collapseOp.getSrc(), dim: reassocIndices[i]); |
| 51 | } |
| 52 | cstr.bound(value)[dim] == productExpr; |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | struct ConcatOpInterface |
| 57 | : public ValueBoundsOpInterface::ExternalModel<ConcatOpInterface, |
| 58 | ConcatOp> { |
| 59 | void populateBoundsForShapedValueDim(Operation *op, Value value, int64_t dim, |
| 60 | ValueBoundsConstraintSet &cstr) const { |
| 61 | auto concatOp = cast<ConcatOp>(Val: op); |
| 62 | assert(value == concatOp.getResult() && "invalid value" ); |
| 63 | |
| 64 | ValueRange inputs = concatOp.getInputs(); |
| 65 | if (dim != static_cast<int64_t>(concatOp.getDim())) { |
| 66 | // All inputs have the same size as the result in a dimension that is not |
| 67 | // concatenated. Relate the result to every input: relating it to a single |
| 68 | // input loses the bound when that input is the unbounded one. |
| 69 | for (Value input : inputs) |
| 70 | cstr.bound(value)[dim] == cstr.getExpr(value: input, dim); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // The concatenated dimension is the sum of the input sizes. |
| 75 | AffineExpr sum = cstr.getExpr(value: inputs.front(), dim); |
| 76 | for (Value input : inputs.drop_front()) |
| 77 | sum = sum + cstr.getExpr(value: input, dim); |
| 78 | cstr.bound(value)[dim] == sum; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | struct DimOpInterface |
| 83 | : public ValueBoundsOpInterface::ExternalModel<DimOpInterface, DimOp> { |
| 84 | void populateBoundsForIndexValue(Operation *op, Value value, |
| 85 | ValueBoundsConstraintSet &cstr) const { |
| 86 | auto dimOp = cast<DimOp>(Val: op); |
| 87 | assert(value == dimOp.getResult() && "invalid value" ); |
| 88 | |
| 89 | cstr.bound(value) >= 0; |
| 90 | auto constIndex = dimOp.getConstantIndex(); |
| 91 | if (!constIndex.has_value()) |
| 92 | return; |
| 93 | cstr.bound(value) == cstr.getExpr(value: dimOp.getSource(), dim: *constIndex); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | struct EmptyOpInterface |
| 98 | : public ValueBoundsOpInterface::ExternalModel<EmptyOpInterface, EmptyOp> { |
| 99 | void populateBoundsForShapedValueDim(Operation *op, Value value, int64_t dim, |
| 100 | ValueBoundsConstraintSet &cstr) const { |
| 101 | auto emptyOp = cast<EmptyOp>(Val: op); |
| 102 | assert(value == emptyOp.getResult() && "invalid value" ); |
| 103 | |
| 104 | cstr.bound(value)[dim] == emptyOp.getMixedSizes()[dim]; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | struct ExpandShapeOpInterface |
| 109 | : public ValueBoundsOpInterface::ExternalModel<ExpandShapeOpInterface, |
| 110 | ExpandShapeOp> { |
| 111 | void populateBoundsForShapedValueDim(Operation *op, Value value, int64_t dim, |
| 112 | ValueBoundsConstraintSet &cstr) const { |
| 113 | auto expandOp = cast<ExpandShapeOp>(Val: op); |
| 114 | assert(value == expandOp.getResult() && "invalid value" ); |
| 115 | cstr.bound(value)[dim] == expandOp.getMixedOutputShape()[dim]; |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | struct |
| 120 | : public ValueBoundsOpInterface::ExternalModel<ExtractSliceOpInterface, |
| 121 | ExtractSliceOp> { |
| 122 | void (Operation *op, Value value, int64_t dim, |
| 123 | ValueBoundsConstraintSet &cstr) const { |
| 124 | auto = cast<ExtractSliceOp>(Val: op); |
| 125 | assert(value == extractSliceOp.getResult() && "invalid value" ); |
| 126 | |
| 127 | llvm::SmallBitVector dropped = extractSliceOp.getDroppedDims(); |
| 128 | int64_t ctr = -1; |
| 129 | for (int64_t i = 0, e = extractSliceOp.getMixedSizes().size(); i < e; ++i) { |
| 130 | // Skip over rank-reduced dimensions. |
| 131 | if (!dropped.test(Idx: i)) |
| 132 | ++ctr; |
| 133 | if (ctr == dim) { |
| 134 | cstr.bound(value)[dim] == extractSliceOp.getMixedSizes()[i]; |
| 135 | int64_t staticStride = extractSliceOp.getStaticStrides()[i]; |
| 136 | if (staticStride > 0) { |
| 137 | // Force analysis of the size value to get a better bound. Example: |
| 138 | // %size = affine.min affine_map<(d0) -> (5, -d0 + 24)>(%iv) |
| 139 | // %slice = tensor.extract_slice %source[%iv][%size][1] |
| 140 | // : tensor<24xf32> to tensor<?xf32> |
| 141 | // Here we get a bound of 24 immediately, so %size is skipped under |
| 142 | // the default stop condition unless we force its analysis. |
| 143 | if (auto sizeValue = |
| 144 | llvm::dyn_cast<Value>(Val&: extractSliceOp.getMixedSizes()[i])) |
| 145 | cstr.populateConstraints(value: sizeValue, dim: std::nullopt); |
| 146 | // Encode in-bounds slice constraint: |
| 147 | OpFoldResult offset = extractSliceOp.getMixedOffsets()[i]; |
| 148 | AffineExpr offsetExpr = cstr.getExpr(ofr: offset); |
| 149 | AffineExpr sourceDimExpr = |
| 150 | cstr.getExpr(value: extractSliceOp.getSource(), dim: i); |
| 151 | cstr.bound(value)[dim] <= |
| 152 | (sourceDimExpr - offsetExpr).ceilDiv(v: staticStride); |
| 153 | cstr.bound(value)[dim] <= sourceDimExpr - offsetExpr; |
| 154 | cstr.bound(value)[dim] <= sourceDimExpr; |
| 155 | } |
| 156 | return; |
| 157 | } |
| 158 | } |
| 159 | llvm_unreachable("could not find non-rank-reduced dim" ); |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | struct PadOpInterface |
| 164 | : public ValueBoundsOpInterface::ExternalModel<PadOpInterface, PadOp> { |
| 165 | void populateBoundsForShapedValueDim(Operation *op, Value value, int64_t dim, |
| 166 | ValueBoundsConstraintSet &cstr) const { |
| 167 | auto padOp = cast<PadOp>(Val: op); |
| 168 | assert(value == padOp.getResult() && "invalid value" ); |
| 169 | |
| 170 | AffineExpr srcSize = cstr.getExpr(value: padOp.getSource(), dim); |
| 171 | AffineExpr lowPad = cstr.getExpr(ofr: padOp.getMixedLowPad()[dim]); |
| 172 | AffineExpr highPad = cstr.getExpr(ofr: padOp.getMixedHighPad()[dim]); |
| 173 | cstr.bound(value)[dim] == srcSize + lowPad + highPad; |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | struct RankOpInterface |
| 178 | : public ValueBoundsOpInterface::ExternalModel<RankOpInterface, RankOp> { |
| 179 | void populateBoundsForIndexValue(Operation *op, Value value, |
| 180 | ValueBoundsConstraintSet &cstr) const { |
| 181 | auto rankOp = cast<RankOp>(Val: op); |
| 182 | assert(value == rankOp.getResult() && "invalid value" ); |
| 183 | |
| 184 | auto tensorType = |
| 185 | llvm::dyn_cast<RankedTensorType>(Val: rankOp.getTensor().getType()); |
| 186 | if (!tensorType) |
| 187 | return; |
| 188 | cstr.bound(value) == tensorType.getRank(); |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | struct SplatOpInterface |
| 193 | : public ValueBoundsOpInterface::ExternalModel<SplatOpInterface, SplatOp> { |
| 194 | void populateBoundsForShapedValueDim(Operation *op, Value value, int64_t dim, |
| 195 | ValueBoundsConstraintSet &cstr) const { |
| 196 | auto splatOp = cast<SplatOp>(Val: op); |
| 197 | assert(value == splatOp.getAggregate() && "invalid value" ); |
| 198 | |
| 199 | RankedTensorType type = splatOp.getType(); |
| 200 | SmallVector<OpFoldResult> sizes = getMixedValues( |
| 201 | staticValues: type.getShape(), dynamicValues: splatOp.getDynamicSizes(), context: type.getContext()); |
| 202 | cstr.bound(value)[dim] == sizes[dim]; |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | } // namespace |
| 207 | } // namespace tensor |
| 208 | } // namespace mlir |
| 209 | |
| 210 | void mlir::tensor::registerValueBoundsOpInterfaceExternalModels( |
| 211 | DialectRegistry ®istry) { |
| 212 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, tensor::TensorDialect *dialect) { |
| 213 | tensor::CastOp::attachInterface<tensor::CastOpInterface>(context&: *ctx); |
| 214 | tensor::CollapseShapeOp::attachInterface<tensor::CollapseShapeOpInterface>( |
| 215 | context&: *ctx); |
| 216 | tensor::ConcatOp::attachInterface<tensor::ConcatOpInterface>(context&: *ctx); |
| 217 | tensor::DimOp::attachInterface<tensor::DimOpInterface>(context&: *ctx); |
| 218 | tensor::EmptyOp::attachInterface<tensor::EmptyOpInterface>(context&: *ctx); |
| 219 | tensor::ExpandShapeOp::attachInterface<tensor::ExpandShapeOpInterface>( |
| 220 | context&: *ctx); |
| 221 | tensor::ExtractSliceOp::attachInterface<tensor::ExtractSliceOpInterface>( |
| 222 | context&: *ctx); |
| 223 | tensor::PadOp::attachInterface<tensor::PadOpInterface>(context&: *ctx); |
| 224 | tensor::RankOp::attachInterface<tensor::RankOpInterface>(context&: *ctx); |
| 225 | tensor::SplatOp::attachInterface<tensor::SplatOpInterface>(context&: *ctx); |
| 226 | // Note: ValueBoundsOpInterface implementation is not required for ops that |
| 227 | // implement `DestinationStyleOpInterface` (for querying shaped OpResults). |
| 228 | }); |
| 229 | } |
| 230 | |