Skip to content

Commit f8fe920

Browse files
committed
Revert "[mlir][Tensor] Check for out-of-bounds slice in insert/extract_slice verifier (#130487)"
This reverts commit 418e07b.
1 parent add3564 commit f8fe920

File tree

5 files changed

+19
-100
lines changed

5 files changed

+19
-100
lines changed

mlir/lib/Dialect/Tensor/IR/TensorOps.cpp

+4-57
Original file line numberDiff line numberDiff line change
@@ -2352,52 +2352,13 @@ static LogicalResult produceSliceErrorMsg(SliceVerificationResult result,
23522352
}
23532353
}
23542354

2355-
/// Verify that the offsets/sizes/strides-style access into the given tensor
2356-
/// is in-bounds. Only static information is verified.
2357-
static LogicalResult verifyInBoundsSlice(Operation *op,
2358-
RankedTensorType tensorType,
2359-
ArrayRef<int64_t> staticOffsets,
2360-
ArrayRef<int64_t> staticSizes,
2361-
ArrayRef<int64_t> staticStrides) {
2362-
for (int64_t i = 0, e = tensorType.getRank(); i < e; ++i) {
2363-
// Nothing to verify for dynamic source dims.
2364-
if (tensorType.isDynamicDim(i))
2365-
continue;
2366-
// Nothing to verify if the offset is dynamic.
2367-
if (ShapedType::isDynamic(staticOffsets[i]))
2368-
continue;
2369-
if (staticOffsets[i] >= tensorType.getDimSize(i))
2370-
return op->emitOpError("offset ")
2371-
<< i << " is out-of-bounds: " << staticOffsets[i]
2372-
<< " >= " << tensorType.getDimSize(i);
2373-
if (ShapedType::isDynamic(staticSizes[i]) ||
2374-
ShapedType::isDynamic(staticStrides[i]))
2375-
continue;
2376-
int64_t lastPos =
2377-
staticOffsets[i] + (staticSizes[i] - 1) * staticStrides[i];
2378-
if (lastPos >= tensorType.getDimSize(i))
2379-
return op->emitOpError("slice along dimension ")
2380-
<< i << " runs out-of-bounds: " << lastPos
2381-
<< " >= " << tensorType.getDimSize(i);
2382-
}
2383-
return success();
2384-
}
2385-
23862355
/// Verifier for ExtractSliceOp.
23872356
LogicalResult ExtractSliceOp::verify() {
2388-
RankedTensorType sourceType = getSourceType();
2389-
23902357
// Verify result type against inferred type.
23912358
RankedTensorType expectedType = ExtractSliceOp::inferResultType(
2392-
sourceType, getMixedOffsets(), getMixedSizes(), getMixedStrides());
2359+
getSourceType(), getMixedOffsets(), getMixedSizes(), getMixedStrides());
23932360
SliceVerificationResult result = isRankReducedType(expectedType, getType());
2394-
if (result != SliceVerificationResult::Success)
2395-
return produceSliceErrorMsg(result, *this, expectedType);
2396-
2397-
// Verify that offsets, sizes, strides do not run out-of-bounds with respect
2398-
// to the source tensor.
2399-
return verifyInBoundsSlice(getOperation(), sourceType, getStaticOffsets(),
2400-
getStaticSizes(), getStaticStrides());
2361+
return produceSliceErrorMsg(result, *this, expectedType);
24012362
}
24022363

24032364
llvm::SmallBitVector ExtractSliceOp::getDroppedDims() {
@@ -2768,18 +2729,11 @@ static SliceVerificationResult verifyInsertSliceOp(
27682729

27692730
/// Verifier for InsertSliceOp.
27702731
LogicalResult InsertSliceOp::verify() {
2771-
// Verify result type against inferred type.
27722732
RankedTensorType expectedType;
27732733
SliceVerificationResult result =
27742734
verifyInsertSliceOp(getSourceType(), getType(), getStaticOffsets(),
27752735
getStaticSizes(), getStaticStrides(), &expectedType);
2776-
if (result != SliceVerificationResult::Success)
2777-
return produceSliceErrorMsg(result, *this, expectedType);
2778-
2779-
// Verify that offsets, sizes, strides do not run out-of-bounds with respect
2780-
// to the source tensor.
2781-
return verifyInBoundsSlice(getOperation(), getDestType(), getStaticOffsets(),
2782-
getStaticSizes(), getStaticStrides());
2736+
return produceSliceErrorMsg(result, *this, expectedType);
27832737
}
27842738

27852739
/// If we have two consecutive InsertSliceOp writing to the same slice, we
@@ -3793,18 +3747,11 @@ LogicalResult ParallelInsertSliceOp::verify() {
37933747
return this->emitError("expected ParallelCombiningOpInterface parent, got:")
37943748
<< *(getOperation()->getParentOp());
37953749

3796-
// Verify result type against inferred type.
37973750
RankedTensorType expectedType;
37983751
SliceVerificationResult result =
37993752
verifyInsertSliceOp(getSourceType(), getDestType(), getStaticOffsets(),
38003753
getStaticSizes(), getStaticStrides(), &expectedType);
3801-
if (result != SliceVerificationResult::Success)
3802-
return produceSliceErrorMsg(result, *this, expectedType);
3803-
3804-
// Verify that offsets, sizes, strides do not run out-of-bounds with respect
3805-
// to the source tensor.
3806-
return verifyInBoundsSlice(getOperation(), getDestType(), getStaticOffsets(),
3807-
getStaticSizes(), getStaticStrides());
3754+
return produceSliceErrorMsg(result, *this, expectedType);
38083755
}
38093756

38103757
void ParallelInsertSliceOp::getCanonicalizationPatterns(

mlir/test/Dialect/Tensor/bubble-up-extract-slice-op.mlir

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func.func @no_bubble_up_extract_slice_on_non_contiguous(%src: tensor<60xf32>) ->
3131

3232
func.func @no_bubble_up_extract_slice_on_stride(%src: tensor<60xf32>) -> tensor<1x1x5xf32> {
3333
%expand = tensor.expand_shape %src [[0, 1, 2]] output_shape [2, 3, 10] : tensor<60xf32> into tensor<2x3x10xf32>
34-
%extract = tensor.extract_slice %expand[0, 0, 0][1, 1, 5][1, 1, 2] : tensor<2x3x10xf32> to tensor<1x1x5xf32>
34+
%extract = tensor.extract_slice %expand[0, 0, 5][1, 1, 5][1, 1, 2] : tensor<2x3x10xf32> to tensor<1x1x5xf32>
3535
return %extract : tensor<1x1x5xf32>
3636
}
3737

mlir/test/Dialect/Tensor/drop-redundant-insert-slice-rank-expansion.mlir

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ func.func @no_fold_more_unit_dims_insert_slice_of_extract_slice(%in : tensor<?x8
5151

5252
// -----
5353

54-
func.func @no_fold_strided_insert_slice_of_extract_slice(%in : tensor<?x8x2x8xf32>, %dest : tensor<1x15x15xf32>) -> tensor<1x15x15xf32> {
54+
func.func @no_fold_strided_insert_slice_of_extract_slice(%in : tensor<?x8x2x8xf32>, %dest : tensor<1x4x4xf32>) -> tensor<1x4x4xf32> {
5555
%extracted_slice = tensor.extract_slice %in[0, 0, 0, 0] [1, 8, 1, 8] [1, 1, 1, 1] : tensor<?x8x2x8xf32> to tensor<8x8xf32>
56-
%inserted_slice = tensor.insert_slice %extracted_slice into %dest[0, 0, 0] [1, 8, 8] [1, 2, 2] : tensor<8x8xf32> into tensor<1x15x15xf32>
57-
return %inserted_slice : tensor<1x15x15xf32>
56+
%inserted_slice = tensor.insert_slice %extracted_slice into %dest[0, 0, 0] [1, 8, 8] [1, 2, 2] : tensor<8x8xf32> into tensor<1x4x4xf32>
57+
return %inserted_slice : tensor<1x4x4xf32>
5858
}
5959
// CHECK-LABEL: func.func @no_fold_strided_insert_slice_of_extract_slice(
6060
// CHECK-SAME: %[[ARG0:.*]]: tensor<?x8x2x8xf32>
6161
// CHECK: %[[EXTRACTED_SLICE:.*]] = tensor.extract_slice %[[ARG0]]
6262
// CHECK: %[[INSERTED_SLICE:.*]] = tensor.insert_slice %[[EXTRACTED_SLICE]]
63-
// CHECK: return %[[INSERTED_SLICE]] : tensor<1x15x15xf32>
63+
// CHECK: return %[[INSERTED_SLICE]] : tensor<1x4x4xf32>
6464

6565
// -----
6666

mlir/test/Dialect/Tensor/fold-tensor-subset-ops.mlir

+10-6
Original file line numberDiff line numberDiff line change
@@ -271,34 +271,38 @@ func.func @insert_slice_of_transfer_write_rank_extending(%t1 : tensor<?x?x12xf32
271271

272272
// -----
273273

274+
// CHECK: #[[$map:.*]] = affine_map<()[s0] -> (s0 + 2)>
274275
// CHECK-LABEL: func @insert_slice_of_insert_slice(
275276
// CHECK-SAME: %[[t:[0-9a-z]*]]: tensor<f32>
276277
// CHECK-SAME: %[[r1:[0-9a-z]*]]: tensor<1x14xf32>
277278
// CHECK-SAME: %[[pos:[0-9a-z]*]]: index
278-
// CHECK: tensor.insert_slice %[[t]] into %[[r1]][0, %[[pos]]] [1, 1] [1, 1] : tensor<f32> into tensor<1x14xf32>
279+
// CHECK: %[[add:.*]] = affine.apply #[[$map]]()[%[[pos]]]
280+
// CHECK: tensor.insert_slice %[[t]] into %[[r1]][4, %[[add]]] [1, 1] [1, 1] : tensor<f32> into tensor<1x14xf32>
279281
func.func @insert_slice_of_insert_slice(%t: tensor<f32>, %r0: tensor<1x1xf32>, %r1: tensor<1x14xf32>, %pos: index)
280282
-> tensor<1x14xf32>
281283
{
282-
%0 = tensor.insert_slice %t into %r0[0, 0] [1, 1] [1, 1]
284+
%0 = tensor.insert_slice %t into %r0[1, 2] [1, 1] [1, 1]
283285
: tensor<f32> into tensor<1x1xf32>
284-
%1 = tensor.insert_slice %0 into %r1[0, %pos] [1, 1] [1, 1]
286+
%1 = tensor.insert_slice %0 into %r1[3, %pos] [1, 1] [1, 1]
285287
: tensor<1x1xf32> into tensor<1x14xf32>
286288
return %1 : tensor<1x14xf32>
287289
}
288290

289291
// -----
290292

293+
// CHECK-DAG: #[[$map:.*]] = affine_map<()[s0] -> (s0 + 2)>
291294
// CHECK-LABEL: func @insert_slice_of_insert_slice(
292295
// CHECK-SAME: %[[t:[0-9a-z]*]]: tensor<f32>
293296
// CHECK-SAME: %[[r1:[0-9a-z]*]]: tensor<1x14xf32>
294297
// CHECK-SAME: %[[pos:[0-9a-z]*]]: index
295-
// CHECK: tensor.insert_slice %[[t]] into %[[r1]][0, %[[pos]]] [1, 1] [1, 1] : tensor<f32> into tensor<1x14xf32>
298+
// CHECK: %[[composed_pos:.+]] = affine.apply #[[$map]]()[%[[pos]]]
299+
// CHECK: tensor.insert_slice %[[t]] into %[[r1]][3, %[[composed_pos]]] [1, 1] [1, 1] : tensor<f32> into tensor<1x14xf32>
296300
func.func @insert_slice_of_insert_slice(%t: tensor<f32>, %r0: tensor<1xf32>, %r1: tensor<1x14xf32>, %pos: index)
297301
-> tensor<1x14xf32>
298302
{
299-
%0 = tensor.insert_slice %t into %r0[0] [1] [1]
303+
%0 = tensor.insert_slice %t into %r0[2] [1] [1]
300304
: tensor<f32> into tensor<1xf32>
301-
%1 = tensor.insert_slice %0 into %r1[0, %pos] [1, 1] [1, 1]
305+
%1 = tensor.insert_slice %0 into %r1[3, %pos] [1, 1] [1, 1]
302306
: tensor<1xf32> into tensor<1x14xf32>
303307
return %1 : tensor<1x14xf32>
304308
}

mlir/test/Dialect/Tensor/invalid.mlir

-32
Original file line numberDiff line numberDiff line change
@@ -258,22 +258,6 @@ func.func @illegal_num_offsets(%arg0 : tensor<?x?x?xf32>, %arg1 : index, %arg2 :
258258

259259
// -----
260260

261-
func.func @extract_slice_offset_out_of_bounds(%arg0: tensor<10xf32>) {
262-
// expected-error@+1 {{offset 0 is out-of-bounds: 10 >= 10}}
263-
%0 = tensor.extract_slice %arg0 [10][1][1] : tensor<10xf32> to tensor<1xf32>
264-
return
265-
}
266-
267-
// -----
268-
269-
func.func @extract_slice_runs_out_of_bounds(%arg0: tensor<9xf32>) {
270-
// expected-error@+1 {{slice along dimension 0 runs out-of-bounds: 9 >= 9}}
271-
%0 = tensor.extract_slice %arg0 [3][4][2] : tensor<9xf32> to tensor<4xf32>
272-
return
273-
}
274-
275-
// -----
276-
277261
func.func @insert_slice_wrong_result_rank(%t1: tensor<?xf32>, %t2: tensor<?x?xf32>, %idx : index) {
278262
// expected-error @+1 {{expected rank to be smaller or equal to the other rank.}}
279263
%0 = tensor.insert_slice %t2 into %t1[0][4][1] : tensor<?x?xf32> into tensor<?xf32>
@@ -312,22 +296,6 @@ func.func @insert_slice_wrong_dynamic_type(%t1: tensor<?x4x4xf32>, %t2: tensor<8
312296

313297
// -----
314298

315-
func.func @insert_slice_offset_out_of_bounds(%arg0: tensor<1xf32>, %arg1: tensor<10xf32>) {
316-
// expected-error@+1 {{offset 0 is out-of-bounds: 10 >= 10}}
317-
%0 = tensor.insert_slice %arg0 into %arg1 [10][1][1] : tensor<1xf32> into tensor<10xf32>
318-
return
319-
}
320-
321-
// -----
322-
323-
func.func @insert_slice_runs_out_of_bounds(%arg0: tensor<4xf32>, %arg1: tensor<9xf32>) {
324-
// expected-error@+1 {{slice along dimension 0 runs out-of-bounds: 9 >= 9}}
325-
%0 = tensor.insert_slice %arg0 into %arg1 [3][4][2] : tensor<4xf32> into tensor<9xf32>
326-
return
327-
}
328-
329-
// -----
330-
331299
func.func @illegal_expanding_reshape_static_tensor
332300
(%arg0: tensor<2x3x20xf32>) -> tensor<2x3x2x4x5xf32> {
333301
// expected-error @+1 {{expected dimension 2 of collapsed type to be static value of 40}}

0 commit comments

Comments
 (0)