[Mlir-commits] [mlir] [mlir][vector] Fix crash in `vector.extract` folder (PR #95912)

Matthias Springer llvmlistbot at llvm.org
Tue Jun 18 05:24:00 PDT 2024


https://github.com/matthias-springer created https://github.com/llvm/llvm-project/pull/95912

Fix a bug in the `vector.extract` folder when the vector type is 0-d.

>From eab9043d0321d7d46b6cb1284f392ce43ae94a7e Mon Sep 17 00:00:00 2001
From: Matthias Springer <mspringer at nvidia.com>
Date: Tue, 18 Jun 2024 14:22:19 +0200
Subject: [PATCH] [mlir][vector] Fix crash in `vector.extract` folder

Fix a bug in the `vector.extract` folder when the vector type is 0-d.
---
 mlir/lib/Dialect/Vector/IR/VectorOps.cpp   |  5 ++++-
 mlir/test/Dialect/Vector/canonicalize.mlir | 11 +++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
index 58951641d33ce..5e5d3e002086a 100644
--- a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
@@ -1883,7 +1883,10 @@ static Value foldExtractStridedOpFromInsertChain(ExtractOp extractOp) {
 }
 
 OpFoldResult ExtractOp::fold(FoldAdaptor) {
-  if (getNumIndices() == 0)
+  // Fold "vector.extract %v[] : vector<2x2xf32> from vector<2x2xf32>" to %v.
+  // Note: Do not fold "vector.extract %v[] : f32 from vector<f32>" (type
+  // mismatch).
+  if (getNumIndices() == 0 && getVector().getType() == getResult().getType())
     return getVector();
   if (succeeded(foldExtractOpFromExtractChain(*this)))
     return getResult();
diff --git a/mlir/test/Dialect/Vector/canonicalize.mlir b/mlir/test/Dialect/Vector/canonicalize.mlir
index 22af91e0eb327..61269e3687ab3 100644
--- a/mlir/test/Dialect/Vector/canonicalize.mlir
+++ b/mlir/test/Dialect/Vector/canonicalize.mlir
@@ -2593,3 +2593,14 @@ func.func @rank_1_shuffle_to_interleave(%arg0: vector<6xi32>, %arg1: vector<6xi3
   %0 = vector.shuffle %arg0, %arg1 [0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11] : vector<6xi32>, vector<6xi32>
   return %0 : vector<12xi32>
 }
+
+// -----
+
+// CHECK-LABEL: func @extract_from_0d_regression(
+//  CHECK-SAME:     %[[v:.*]]: vector<f32>)
+//       CHECK:   %[[extract:.*]] = vector.extract %[[v]][] : f32 from vector<f32>
+//       CHECK:   return %[[extract]]
+func.func @extract_from_0d_regression(%v: vector<f32>) -> f32 {
+  %0 = vector.extract %v[] : f32 from vector<f32>
+  return %0 : f32
+}



More information about the Mlir-commits mailing list