[MLIR] ValueBoundsOpInterface: slice size from source size #221756
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-tensor
@llvm/pr-subscribers-mlir
Author: Alexey Kreshchuk (krsch-mobileye)
<details>
<summary>Changes</summary>
Improve ValueBoundsOpInterface for tensor.extract_slice by adding an upper bound of ceil((sourceSize - offset) / stride).
---
Full diff: https://github.com/llvm/llvm-project/pull/221756.diff
2 Files Affected:
````````````diff --git a/mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp b/mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp
index 684cec2d6748a..3296b1bc16018 100644
--- a/mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp
+++ b/mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp
@@ -132,6 +132,21 @@ struct ExtractSliceOpInterface
++ctr;
if (ctr == dim) {
cstr.bound(value)[dim] == extractSliceOp.getMixedSizes()[i];
+ int64_t staticStride = extractSliceOp.getStaticStrides()[i];
+ if (staticStride > 0) {
+ // Encode in-bounds slice constraint:
+ OpFoldResult offset = extractSliceOp.getMixedOffsets()[i];
+ AffineExpr offsetExpr = cstr.getExpr(offset);
+ AffineExpr sourceDimExpr =
+ cstr.getExpr(extractSliceOp.getSource(), i);
+ cstr.bound(value)[dim] <=
+ (sourceDimExpr - offsetExpr).ceilDiv(staticStride);
+ // To derive value[dim] <= source[i] - offset:
+ cstr.bound(extractSliceOp.getSource())[i] >= offsetExpr;
+ // To derive value[dim] <= source[i]:
+ if (auto offsetValue = llvm::dyn_cast_if_present<Value>(offset))
+ cstr.bound(offsetValue) >= 0;
+ }
return;
}
}
diff --git a/mlir/test/Dialect/Tensor/value-bounds-op-interface-impl.mlir b/mlir/test/Dialect/Tensor/value-bounds-op-interface-impl.mlir
index e957cc39831a7..2af2d917ca341 100644
--- a/mlir/test/Dialect/Tensor/value-bounds-op-interface-impl.mlir
+++ b/mlir/test/Dialect/Tensor/value-bounds-op-interface-impl.mlir
@@ -176,6 +176,85 @@ func.func @extract_slice_rank_reduce(%t: tensor<?x?xf32>, %sz: index) -> index {
// -----
+// An in-bounds extract_slice cannot exceed ceil((src - offset) / stride).
+// Default constant upper bounds are open, so size <= 8 reifies as 9.
+
+// CHECK-LABEL: func @extract_slice_in_bounds_constant_ub(
+// CHECK: %[[c9:.*]] = arith.constant 9 : index
+// CHECK: return %[[c9]]
+func.func @extract_slice_in_bounds_constant_ub(%t: tensor<10xf32>, %sz: index) -> index {
+ %0 = tensor.extract_slice %t[2][%sz][1] : tensor<10xf32> to tensor<?xf32>
+ %1 = "test.reify_bound"(%0) {dim = 0, type = "UB", constant} : (tensor<?xf32>) -> (index)
+ return %1 : index
+}
+
+// -----
+
+func.func @extract_slice_in_bounds_compare(%t: tensor<10xf32>, %sz: index) {
+ %c0 = arith.constant 0 : index
+ %c8 = arith.constant 8 : index
+ %0 = tensor.extract_slice %t[2][%sz][1] : tensor<10xf32> to tensor<?xf32>
+ %d = tensor.dim %0, %c0 : tensor<?xf32>
+ // expected-remark @below{{true}}
+ "test.compare"(%d, %c8) {cmp = "LE"} : (index, index) -> ()
+ return
+}
+
+// -----
+
+// src=10, stride=3 allows indices {0,3,6,9}, so size <= 4 = ceil(10/3).
+// floor(10/3) = 3 would be too tight.
+
+func.func @extract_slice_strided_in_bounds(%t: tensor<10xf32>, %sz: index) {
+ %c0 = arith.constant 0 : index
+ %c3 = arith.constant 3 : index
+ %c4 = arith.constant 4 : index
+ %0 = tensor.extract_slice %t[0][%sz][3] : tensor<10xf32> to tensor<?xf32>
+ %d = tensor.dim %0, %c0 : tensor<?xf32>
+ // expected-remark @below{{true}}
+ "test.compare"(%d, %c4) {cmp = "LE"} : (index, index) -> ()
+ // expected-error @below{{unknown}}
+ "test.compare"(%d, %c3) {cmp = "LE"} : (index, index) -> ()
+ return
+}
+
+// -----
+
+// A dynamic offset is non-negative, so size <= src - 0.
+
+func.func @extract_slice_dynamic_offset_in_bounds(%t: tensor<10xf32>, %off: index, %sz: index) {
+ %c0 = arith.constant 0 : index
+ %c10 = arith.constant 10 : index
+ %0 = tensor.extract_slice %t[%off][%sz][1] : tensor<10xf32> to tensor<?xf32>
+ %d = tensor.dim %0, %c0 : tensor<?xf32>
+ // expected-remark @below{{true}}
+ "test.compare"(%d, %c10) {cmp = "LE"} : (index, index) -> ()
+ return
+}
+
+// -----
+
+func.func @extract_slice_rank_reduce_in_bounds(%t: tensor<8x16xf32>, %sz: index) {
+ %c0 = arith.constant 0 : index
+ %c14 = arith.constant 14 : index
+ %0 = tensor.extract_slice %t[0, 2][1, %sz][1, 1] : tensor<8x16xf32> to tensor<?xf32>
+ %d = tensor.dim %0, %c0 : tensor<?xf32>
+ // expected-remark @below{{true}}
+ "test.compare"(%d, %c14) {cmp = "LE"} : (index, index) -> ()
+ return
+}
+
+// -----
+
+func.func @extract_slice_dynamic_stride_no_constant_ub(%t: tensor<10xf32>, %sz: index, %st: index) -> index {
+ %0 = tensor.extract_slice %t[0][%sz][%st] : tensor<10xf32> to tensor<?xf32>
+ // expected-error @below{{could not reify bound}}
+ %1 = "test.reify_bound"(%0) {dim = 0, type = "UB", constant} : (tensor<?xf32>) -> (index)
+ return %1 : index
+}
+
+// -----
+
// CHECK-LABEL: func @insert(
// CHECK-SAME: %[[t:.*]]: tensor<?xf32>
// CHECK: %[[c0:.*]] = arith.constant 0 : index
</details>
This looks odd. You typically put bounds on value, but not other SSA values.
Populate the constraint set with bounds for the size of the specified
dimension of the given shaped value.
Same here.
I just thought this bound makes a bit more sense than adding
1. cstr.bound(value)[dim] <= (sourceDimExpr - offsetExpr).ceilDiv(staticStride)
2. cstr.bound(value)[dim] <= sourceDimExpr - offsetExpr
3. cstr.bound(value)[dim] <= sourceDimExpr
one after the other. They are independent without bounds on offset.
If you think it looks nicer with three bounds on value[dim], I'll use them.
Let's do that. To be in line with the interface method description. If we find the need for adding other bounds, we can think about generalizing the contract/documentation of the interface method.
Done
@matthias-springer, I see a few ways to proceed:
1. Add cstr.populateConstraints(sizeValue, std::nullopt) before adding new bounds, so the old bound is always added.
2. Check cstr.populateAndCompare(sizeValue, LE, sourceDimExpr) so the new bound is only added if the old one doesn't work. It might be helpful for non-constant bounds.
3. Update the lit tests, accepting the regression in padding transformation.
4. Wait for some design decision in https://discourse.llvm.org/t/regressions-from-adding-weaker-bounds-to-valueboundsopinterface/91756
5. Close the PR
Did I miss any options? Which one would you prefer?
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
Can you add a comment (maybe with the small IR snippet from the RFC)?
Add cstr.populateConstraints(sizeValue, std::nullopt) before adding new bounds, so the old bound is always added.
This sounds reasonable to me. And compared to before, we don't analyze any extra IR. So compile time should not worsen due to this.
From an API point of view, changing the test cases would also be correct, but there could be regressions with downstream users, and ideally we would avoid that.
I've added the comment, but I don't have write access to merge the PR
matthias-springer
krsch-mobileye
Improve ValueBoundsOpInterface for tensor.extract_slice by adding an upper bound of
ceil((sourceSize - offset) / stride).