[Mlir-commits] [mlir] [mlir][vector] Fix out-of-bounds access (PR #126734)

Longsheng Mou llvmlistbot at llvm.org
Tue Feb 11 06:17:19 PST 2025


https://github.com/CoTinker created https://github.com/llvm/llvm-project/pull/126734

This PR fixes an out-of-bounds bug that occurs when there are no unit dimensions in the source of `vector.extract_strided_slice`, causing access to `sizes` to go out of bounds. Fixes #126196.

>From 7067ff225dc282b3192fd17d8d407ec9e3a096f3 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Tue, 11 Feb 2025 22:04:10 +0800
Subject: [PATCH] [mlir][vector] Fix out-of-bounds access

This PR fixes an out-of-bounds bug that occurs when there are no unit
dimensions in the source of `vector.extract_strided_slice`, causing
access to `sizes` to go out of bounds.
---
 mlir/lib/Dialect/Vector/IR/VectorOps.cpp   |  4 ++--
 mlir/test/Dialect/Vector/canonicalize.mlir | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
index 30ff2df7c38fc3..f3f20b46add783 100644
--- a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
@@ -3968,8 +3968,8 @@ class ContiguousExtractStridedSliceToExtract final
     // Avoid generating slices that have leading unit dimensions. The shape_cast
     // op that we create below would take bad generic fallback patterns
     // (ShapeCastOpRewritePattern).
-    while (sizes[numOffsets] == 1 &&
-           numOffsets < static_cast<int>(sizes.size()) - 1) {
+    while (numOffsets < static_cast<int>(sizes.size()) - 1 &&
+           sizes[numOffsets] == 1) {
       ++numOffsets;
     }
 
diff --git a/mlir/test/Dialect/Vector/canonicalize.mlir b/mlir/test/Dialect/Vector/canonicalize.mlir
index 61e858f5f226a1..0fc0140666696f 100644
--- a/mlir/test/Dialect/Vector/canonicalize.mlir
+++ b/mlir/test/Dialect/Vector/canonicalize.mlir
@@ -2932,6 +2932,17 @@ func.func @contiguous_extract_strided_slices_to_extract(%arg0 : vector<8x1x2x1x1
 
 // -----
 
+// CHECK-LABEL: @contiguous_extract_strided_slices_to_extract_no_unit_dims
+// CHECK:        %[[EXTRACT:.+]] = vector.extract {{.*}}[0, 0] : vector<4xi32> from vector<8x2x4xi32>
+// CHECK-NEXT:   return %[[EXTRACT]] :  vector<4xi32>
+func.func @contiguous_extract_strided_slices_to_extract_no_unit_dims(%arg0 : vector<8x2x4xi32>) -> vector<4xi32> {
+  %1 = vector.extract_strided_slice %arg0 {offsets = [0, 0], sizes = [1, 1], strides = [1, 1]} : vector<8x2x4xi32> to vector<1x1x4xi32>
+  %2 = vector.shape_cast %1 : vector<1x1x4xi32> to vector<4xi32>
+  return %2 : vector<4xi32>
+}
+
+// -----
+
 // CHECK-LABEL: @contiguous_extract_strided_slices_to_extract_shorter_size_list
 // CHECK:        %[[EXTRACT:.+]] = vector.extract {{.*}}[0, 0, 0, 0] : vector<1x4xi32> from vector<8x1x2x1x1x4xi32>
 // CHECK-NEXT:   return %[[EXTRACT]] :  vector<1x4xi32>



More information about the Mlir-commits mailing list