Skip to content

Commit 3e223e3

Browse files
authored
[mlir][vector] Fix out-of-bounds access (llvm#126734)
This PR fixes an out-of-bounds bug that occurs when there are no overlap dimensions between the `sizes` and source of `vector.extract_strided_slice`, causing access to `sizes` to go out of bounds. Fixes llvm#126196.
1 parent 8421ad7 commit 3e223e3

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

mlir/lib/Dialect/Vector/IR/VectorOps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4023,8 +4023,8 @@ class ContiguousExtractStridedSliceToExtract final
40234023
// Avoid generating slices that have leading unit dimensions. The shape_cast
40244024
// op that we create below would take bad generic fallback patterns
40254025
// (ShapeCastOpRewritePattern).
4026-
while (sizes[numOffsets] == 1 &&
4027-
numOffsets < static_cast<int>(sizes.size()) - 1) {
4026+
while (numOffsets < static_cast<int>(sizes.size()) - 1 &&
4027+
sizes[numOffsets] == 1) {
40284028
++numOffsets;
40294029
}
40304030

mlir/test/Dialect/Vector/canonicalize.mlir

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,17 +2984,28 @@ func.func @insert_multiple_poison_idx(%a: vector<4x5x8xf32>, %b: vector<8xf32>)
29842984

29852985
// -----
29862986

2987-
// CHECK-LABEL: @contiguous_extract_strided_slices_to_extract
2987+
// CHECK-LABEL: @contiguous_extract_strided_slices_to_extract_sizes_and_outer_source_dims_overlap
29882988
// CHECK: %[[EXTRACT:.+]] = vector.extract {{.*}}[0, 0, 0, 0, 0] : vector<4xi32> from vector<8x1x2x1x1x4xi32>
29892989
// CHECK-NEXT: return %[[EXTRACT]] : vector<4xi32>
2990-
func.func @contiguous_extract_strided_slices_to_extract(%arg0 : vector<8x1x2x1x1x4xi32>) -> vector<4xi32> {
2990+
func.func @contiguous_extract_strided_slices_to_extract_sizes_and_outer_source_dims_overlap(%arg0 : vector<8x1x2x1x1x4xi32>) -> vector<4xi32> {
29912991
%1 = vector.extract_strided_slice %arg0 {offsets = [0, 0, 0, 0, 0, 0], sizes = [1, 1, 1, 1, 1, 4], strides = [1, 1, 1, 1, 1, 1]} : vector<8x1x2x1x1x4xi32> to vector<1x1x1x1x1x4xi32>
29922992
%2 = vector.shape_cast %1 : vector<1x1x1x1x1x4xi32> to vector<4xi32>
29932993
return %2 : vector<4xi32>
29942994
}
29952995

29962996
// -----
29972997

2998+
// CHECK-LABEL: @contiguous_extract_strided_slices_to_extract_sizes_and_outer_source_dims_no_overlap
2999+
// CHECK: %[[EXTRACT:.+]] = vector.extract {{.*}}[0, 0] : vector<4xi32> from vector<8x2x4xi32>
3000+
// CHECK-NEXT: return %[[EXTRACT]] : vector<4xi32>
3001+
func.func @contiguous_extract_strided_slices_to_extract_sizes_and_outer_source_dims_no_overlap(%arg0 : vector<8x2x4xi32>) -> vector<4xi32> {
3002+
%1 = vector.extract_strided_slice %arg0 {offsets = [0, 0], sizes = [1, 1], strides = [1, 1]} : vector<8x2x4xi32> to vector<1x1x4xi32>
3003+
%2 = vector.shape_cast %1 : vector<1x1x4xi32> to vector<4xi32>
3004+
return %2 : vector<4xi32>
3005+
}
3006+
3007+
// -----
3008+
29983009
// CHECK-LABEL: @contiguous_extract_strided_slices_to_extract_shorter_size_list
29993010
// CHECK: %[[EXTRACT:.+]] = vector.extract {{.*}}[0, 0, 0, 0] : vector<1x4xi32> from vector<8x1x2x1x1x4xi32>
30003011
// CHECK-NEXT: return %[[EXTRACT]] : vector<1x4xi32>

0 commit comments

Comments
 (0)