← Back to Pull requests

[MLIR] ValueBoundsOpInterface: slice size from source size #221756

Merged
krsch-mobileye merged 2 files (100 lines) on Oct 04, 2026 23:19 UTC
View on GitHub
krsch-mobileye krsch-mobileye commented on Sep 07, 2026 15:09 UTC
Author

Improve ValueBoundsOpInterface for tensor.extract_slice by adding an upper bound of ceil((sourceSize - offset) / stride).

Diff: 100 lines (+100 / -0) across 2 files Reviewer: matthias-springer Review duration: 39369m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.32) × (1 − Review Confidence (0.86)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.32 · 100 lines diff across 2 files
0.32
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.87 · 5 substantive comment(s)
0.87
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
86%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.276
Real PR Conversation Timeline
12 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Sep 07, 2026 15:10 UTC
Bot

<!--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:

  • (modified) mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp (+15)

  • (modified) mlir/test/Dialect/Tensor/value-bounds-op-interface-impl.mlir (+79)
  • ``````

    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>

    matthias-springer matthias-springer commented on code on Sep 08, 2026 11:27 UTC
    Code Review
    mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp:1
    @@ -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;

    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.
    

    matthias-springer matthias-springer commented on code on Sep 08, 2026 11:27 UTC
    Code Review
    mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp:1
    @@ -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;

    Same here.

    krsch-mobileye krsch-mobileye commented on code on Sep 08, 2026 11:41 UTC
    Code Review
    mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp:1
    @@ -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;

    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.

    matthias-springer matthias-springer commented on code on Sep 08, 2026 11:48 UTC
    Code Review
    mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp:1
    @@ -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;

    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.

    krsch-mobileye krsch-mobileye commented on code on Sep 09, 2026 08:10 UTC
    Code Review
    mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp:1
    @@ -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;

    Done

    krsch-mobileye krsch-mobileye commented on Sep 10, 2026 14:38 UTC
    Author

    @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?

    github-actions[bot] github-actions[bot] commented on Sep 14, 2026 14:22 UTC
    Bot

    <!--LLVM CODE FORMAT COMMENT: {clang-format}-->

    :white_check_mark: With the latest revision this PR passed the C/C++ code formatter.

    matthias-springer matthias-springer commented on code on Sep 15, 2026 01:16 UTC
    Code Review
    mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp:1
    @@ -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) {
    +          if (auto sizeValue =

    Can you add a comment (maybe with the small IR snippet from the RFC)?

    matthias-springer matthias-springer approved these changes on Sep 15, 2026 01:19 UTC
    Approved Reviewer

    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.

    krsch-mobileye krsch-mobileye commented on code on Oct 04, 2026 10:55 UTC
    Code Review
    mlir/lib/Dialect/Tensor/IR/ValueBoundsOpInterfaceImpl.cpp:1
    @@ -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) {
    +          if (auto sizeValue =

    I've added the comment, but I don't have write access to merge the PR

    Assignees
    krsch-mobileye krsch-mobileye
    Labels
    risk: low confidence: 86.1% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 32.1% (LOW) · Higher = larger blast radius
    32.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 86.1% · Lower = rushed or shallow review
    86.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.5% · Safe to merge
    4.5%