Skip to content

[mlir][vector] Update the folder for vector.{insert|extract} #136579

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

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: 3 additions & 2 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2019,8 +2019,9 @@ static Value extractInsertFoldConstantOp(OpType op, AdaptorType adaptor,
Value position = dynamicPosition[index++];
if (auto attr = mlir::dyn_cast_if_present<IntegerAttr>(positionAttr)) {
int64_t value = attr.getInt();
// Do not fold if the value is out of bounds.
if (value >= 0 && value < vectorShape[i]) {
// Do not fold if the value is out of bounds (-1 signifies a poison
// value rather than OOB index).
if (value >= -1 && value < vectorShape[i]) {
staticPosition[i] = attr.getInt();
opChange = true;
continue;
Expand Down
93 changes: 55 additions & 38 deletions mlir/test/Dialect/Vector/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,33 @@ func.func @extract_scalar_poison_idx(%a: vector<4x5xf32>) -> f32 {
return %0 : f32
}

// -----

// Similar to the test above, but the index is not a static constant.

// CHECK-LABEL: @extract_scalar_poison_idx_non_cst
func.func @extract_scalar_poison_idx_non_cst(%a: vector<4x5xf32>) -> f32 {
// CHECK-NEXT: %[[UB:.*]] = ub.poison : f32
// CHECK-NOT: vector.extract
// CHECK-NEXT: return %[[UB]] : f32
%c_neg_1 = arith.constant -1 : index
%0 = vector.extract %a[%c_neg_1, 0] : f32 from vector<4x5xf32>
return %0 : f32
}

// -----

// Similar to test above, but now the index is out-of-bounds.

// CHECK-LABEL: @no_fold_extract_scalar_oob_idx
func.func @no_fold_extract_scalar_oob_idx(%a: vector<4x5xf32>) -> f32 {
// CHECK: vector.extract
%c_neg_2 = arith.constant -2 : index
%0 = vector.extract %a[%c_neg_2, 0] : f32 from vector<4x5xf32>
return %0 : f32
}


// -----

// CHECK-LABEL: @extract_vector_poison_idx
Expand Down Expand Up @@ -3062,6 +3089,34 @@ func.func @insert_vector_poison_idx(%a: vector<4x5xf32>, %b: vector<5xf32>)

// -----

// Similar to the test above, but the index is not a static constant.

// CHECK-LABEL: @insert_vector_poison_idx_non_cst
func.func @insert_vector_poison_idx_non_cst(%a: vector<4x5xf32>, %b: vector<5xf32>)
-> vector<4x5xf32> {
// CHECK-NEXT: %[[UB:.*]] = ub.poison : vector<4x5xf32>
// CHECK-NOT: vector.insert
// CHECK-NEXT: return %[[UB]] : vector<4x5xf32>
%c_neg_1 = arith.constant -1 : index
%0 = vector.insert %b, %a[%c_neg_1] : vector<5xf32> into vector<4x5xf32>
return %0 : vector<4x5xf32>
}

// -----

// Similar to test above, but now the index is out-of-bounds.

// CHECK-LABEL: @no_fold_insert_scalar_idx_oob
func.func @no_fold_insert_scalar_idx_oob(%a: vector<4x5xf32>, %b: vector<5xf32>)
-> vector<4x5xf32> {
// CHECK: vector.insert
%c_neg_2 = arith.constant -2 : index
%0 = vector.insert %b, %a[%c_neg_2] : vector<5xf32> into vector<4x5xf32>
return %0 : vector<4x5xf32>
}

// -----

// CHECK-LABEL: @insert_multiple_poison_idx
func.func @insert_multiple_poison_idx(%a: vector<4x5x8xf32>, %b: vector<8xf32>)
-> vector<4x5x8xf32> {
Expand Down Expand Up @@ -3311,41 +3366,3 @@ func.func @fold_insert_constant_indices(%arg : vector<4x1xi32>) -> vector<4x1xi3
%res = vector.insert %1, %arg[%0, %0] : i32 into vector<4x1xi32>
return %res : vector<4x1xi32>
}

// -----

// Check that out of bounds indices are not folded for vector.insert.

// CHECK-LABEL: @fold_insert_oob
// CHECK-SAME: %[[ARG:.*]]: vector<4x1x2xi32>) -> vector<4x1x2xi32> {
// CHECK: %[[OOB1:.*]] = arith.constant -2 : index
// CHECK: %[[OOB2:.*]] = arith.constant 2 : index
// CHECK: %[[VAL:.*]] = arith.constant 1 : i32
// CHECK: %[[RES:.*]] = vector.insert %[[VAL]], %[[ARG]] [0, %[[OOB1]], %[[OOB2]]] : i32 into vector<4x1x2xi32>
// CHECK: return %[[RES]] : vector<4x1x2xi32>
func.func @fold_insert_oob(%arg : vector<4x1x2xi32>) -> vector<4x1x2xi32> {
%c0 = arith.constant 0 : index
%c-2 = arith.constant -2 : index
%c2 = arith.constant 2 : index
%c1 = arith.constant 1 : i32
%res = vector.insert %c1, %arg[%c0, %c-2, %c2] : i32 into vector<4x1x2xi32>
return %res : vector<4x1x2xi32>
}

// -----

// Check that out of bounds indices are not folded for vector.extract.

// CHECK-LABEL: @fold_extract_oob
// CHECK-SAME: %[[ARG:.*]]: vector<4x1x2xi32>) -> i32 {
// CHECK: %[[OOB1:.*]] = arith.constant -2 : index
// CHECK: %[[OOB2:.*]] = arith.constant 2 : index
// CHECK: %[[RES:.*]] = vector.extract %[[ARG]][0, %[[OOB1]], %[[OOB2]]] : i32 from vector<4x1x2xi32>
// CHECK: return %[[RES]] : i32
func.func @fold_extract_oob(%arg : vector<4x1x2xi32>) -> i32 {
%c0 = arith.constant 0 : index
%c-2 = arith.constant -2 : index
%c2 = arith.constant 2 : index
%res = vector.extract %arg[%c0, %c-2, %c2] : i32 from vector<4x1x2xi32>
return %res : i32
}
Loading