Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir][Vector] Add support for 0-d shapes in extract-shape_cast folder #116650

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1756,11 +1756,6 @@ static Value foldExtractFromShapeCast(ExtractOp extractOp) {
if (!shapeCastOp)
return Value();

// 0-D vectors not supported.
assert(!hasZeroDimVectors(extractOp) && "0-D vectors not supported");
if (hasZeroDimVectors(shapeCastOp))
return Value();

// Get the nth dimension size starting from lowest dimension.
auto getDimReverse = [](VectorType type, int64_t n) {
return type.getShape().take_back(n + 1).front();
Expand Down
36 changes: 24 additions & 12 deletions mlir/test/Dialect/Vector/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -782,30 +782,42 @@ func.func @fold_extract_shapecast(%arg0 : vector<5x1x3x2xf32>,

// -----

// CHECK-LABEL: fold_extract_shapecast_negative
// CHECK: %[[V:.*]] = vector.shape_cast %{{.*}} : vector<16xf32> to vector<2x4x2xf32>
// CHECK: %[[R:.*]] = vector.extract %[[V]][1] : vector<4x2xf32> from vector<2x4x2xf32>
// CHECK: return %[[R]] : vector<4x2xf32>
func.func @fold_extract_shapecast_negative(%arg0 : vector<16xf32>) -> vector<4x2xf32> {
%0 = vector.shape_cast %arg0 : vector<16xf32> to vector<2x4x2xf32>
%r = vector.extract %0[1] : vector<4x2xf32> from vector<2x4x2xf32>
return %r : vector<4x2xf32>
// CHECK-LABEL: fold_extract_shapecast_0d_result
// CHECK-SAME: %[[IN:.*]]: vector<1x1x1xf32>
// CHECK: %[[R:.*]] = vector.extract %[[IN]][0, 0, 0] : f32 from vector<1x1x1xf32>
// CHECK: return %[[R]] : f32
func.func @fold_extract_shapecast_0d_result(%arg0 : vector<1x1x1xf32>) -> f32 {
%0 = vector.shape_cast %arg0 : vector<1x1x1xf32> to vector<f32>
%r = vector.extract %0[] : f32 from vector<f32>
return %r : f32
}

// -----

// CHECK-LABEL: dont_fold_0d_extract_shapecast
// CHECK: %[[V:.*]] = vector.shape_cast %{{.*}} : vector<f32> to vector<1xf32>
// CHECK: %[[R:.*]] = vector.extract %[[V]][0] : f32 from vector<1xf32>
// CHECK-LABEL: fold_extract_shapecast_0d_source
// CHECK-SAME: %[[IN:.*]]: vector<f32>
// CHECK: %[[R:.*]] = vector.extract %[[IN]][] : f32 from vector<f32>
// CHECK: return %[[R]] : f32
func.func @dont_fold_0d_extract_shapecast(%arg0 : vector<f32>) -> f32 {
func.func @fold_extract_shapecast_0d_source(%arg0 : vector<f32>) -> f32 {
%0 = vector.shape_cast %arg0 : vector<f32> to vector<1xf32>
%r = vector.extract %0[0] : f32 from vector<1xf32>
return %r : f32
}

// -----

// CHECK-LABEL: fold_extract_shapecast_negative
// CHECK: %[[V:.*]] = vector.shape_cast %{{.*}} : vector<16xf32> to vector<2x4x2xf32>
// CHECK: %[[R:.*]] = vector.extract %[[V]][1] : vector<4x2xf32> from vector<2x4x2xf32>
// CHECK: return %[[R]] : vector<4x2xf32>
func.func @fold_extract_shapecast_negative(%arg0 : vector<16xf32>) -> vector<4x2xf32> {
%0 = vector.shape_cast %arg0 : vector<16xf32> to vector<2x4x2xf32>
%r = vector.extract %0[1] : vector<4x2xf32> from vector<2x4x2xf32>
return %r : vector<4x2xf32>
}

// -----

// CHECK-LABEL: fold_extract_shapecast_to_shapecast
// CHECK-SAME: (%[[ARG:.+]]: vector<3x4xf32>)
// CHECK: %[[R:.+]] = vector.shape_cast %[[ARG]] : vector<3x4xf32> to vector<12xf32>
Expand Down
Loading