Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2492,6 +2492,7 @@ def Vector_ConstantMaskOp :

let assemblyFormat = "$mask_dim_sizes attr-dict `:` type(results)";
let hasVerifier = 1;
let hasFolder = 1;
}

def Vector_CreateMaskOp :
Expand Down
20 changes: 20 additions & 0 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6594,6 +6594,26 @@ bool ConstantMaskOp::isAllOnesMask() {
return true;
}

static Attribute createBoolSplat(ShapedType ty, bool x) {
return SplatElementsAttr::get(ty, BoolAttr::get(ty.getContext(), x));
}

OpFoldResult ConstantMaskOp::fold(FoldAdaptor adaptor) {
ArrayRef<int64_t> bounds = getMaskDimSizes();
ArrayRef<int64_t> vectorSizes = getVectorType().getShape();
// Check the corner case of 0-D vectors first.
if (vectorSizes.size() == 0) {
assert(bounds.size() == 1 && "invalid sizes for zero rank mask");
return createBoolSplat(getVectorType(), bounds[0] == 1);
}
// Fold vector.constant_mask to splat if possible.
if (bounds == vectorSizes)
return createBoolSplat(getVectorType(), true);
if (llvm::all_of(bounds, [](int64_t x) { return x == 0; }))
return createBoolSplat(getVectorType(), false);
return {};
}

//===----------------------------------------------------------------------===//
// CreateMaskOp
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/SparseTensor/sparse_vector_peeled.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// CHECK-DAG: %[[c0:.*]] = arith.constant 0 : index
// CHECK-DAG: %[[c1:.*]] = arith.constant 1 : index
// CHECK-DAG: %[[c16:.*]] = arith.constant 16 : index
// CHECK-DAG: %[[mask:.*]] = arith.constant dense<true> : vector<16xi1>
// CHECK: %[[p:.*]] = memref.load %{{.*}}[%[[c0]]] : memref<?xi32>
// CHECK: %[[a:.*]] = arith.extui %[[p]] : i32 to i64
// CHECK: %[[q:.*]] = arith.index_cast %[[a]] : i64 to index
Expand All @@ -31,7 +32,6 @@
// CHECK: %[[s:.*]] = arith.index_cast %[[b]] : i64 to index
// CHECK: %[[boundary:.*]] = affine.apply #[[$map0]]()[%[[q]], %[[s]]]
// CHECK: scf.for %[[i:.*]] = %[[q]] to %[[boundary]] step %[[c16]] {
// CHECK: %[[mask:.*]] = vector.constant_mask [16] : vector<16xi1>
// CHECK: %[[li:.*]] = vector.load %{{.*}}[%[[i]]] : memref<?xi32>, vector<16xi32>
// CHECK: %[[zi:.*]] = arith.extui %[[li]] : vector<16xi32> to vector<16xi64>
// CHECK: %[[la:.*]] = vector.load %{{.*}}[%[[i]]] : memref<?xf32>, vector<16xf32>
Expand Down
40 changes: 32 additions & 8 deletions mlir/test/Dialect/Vector/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func.func @create_vector_mask_to_constant_mask() -> (vector<4x3xi1>) {
// CHECK-LABEL: create_scalable_vector_mask_to_constant_mask
func.func @create_scalable_vector_mask_to_constant_mask() -> (vector<[8]xi1>) {
%c-1 = arith.constant -1 : index
// CHECK: vector.constant_mask [0] : vector<[8]xi1>
// CHECK: arith.constant dense<false> : vector<[8]xi1>
%0 = vector.create_mask %c-1 : vector<[8]xi1>
return %0 : vector<[8]xi1>
}
Expand All @@ -36,7 +36,7 @@ func.func @create_vector_mask_to_constant_mask_truncation() -> (vector<4x3xi1>)
func.func @create_vector_mask_to_constant_mask_truncation_neg() -> (vector<4x3xi1>) {
%cneg2 = arith.constant -2 : index
%c5 = arith.constant 5 : index
// CHECK: vector.constant_mask [0, 0] : vector<4x3xi1>
// CHECK: arith.constant dense<false> : vector<4x3xi1>
%0 = vector.create_mask %c5, %cneg2 : vector<4x3xi1>
return %0 : vector<4x3xi1>
}
Expand All @@ -47,7 +47,7 @@ func.func @create_vector_mask_to_constant_mask_truncation_neg() -> (vector<4x3xi
func.func @create_vector_mask_to_constant_mask_truncation_zero() -> (vector<4x3xi1>) {
%c2 = arith.constant 2 : index
%c0 = arith.constant 0 : index
// CHECK: vector.constant_mask [0, 0] : vector<4x3xi1>
// CHECK: arith.constant dense<false> : vector<4x3xi1>
%0 = vector.create_mask %c0, %c2 : vector<4x3xi1>
return %0 : vector<4x3xi1>
}
Expand All @@ -60,7 +60,7 @@ func.func @create_vector_mask_to_constant_mask_scalable_all_true() -> (vector<8x
%c16 = arith.constant 16 : index
%0 = vector.vscale
%1 = arith.muli %0, %c16 : index
// CHECK: vector.constant_mask [8, 16] : vector<8x[16]xi1>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errr... this original output doesn't look correct to me... @banach-space? Do we have a bug somewhere else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sizes that correspond to scalable dimensions are implicitly multiplied by vscale, though currently only zero (none set) or the size of the dim/vscale (all set) are supported

Seems correct

// CHECK: arith.constant dense<true> : vector<8x[16]xi1>
%10 = vector.create_mask %c8, %1 : vector<8x[16]xi1>
return %10 : vector<8x[16]xi1>
}
Expand Down Expand Up @@ -272,6 +272,30 @@ func.func @extract_from_non_constant_create_mask(%dim0: index) -> vector<[2]xi1>

// -----

// CHECK-LABEL: constant_mask_to_true_splat
func.func @constant_mask_to_true_splat() -> vector<2x4xi1> {
// CHECK: arith.constant dense<true>
// CHECK-NOT: vector.constant_mask
%0 = vector.constant_mask [2, 4] : vector<2x4xi1>
return %0 : vector<2x4xi1>
}

// CHECK-LABEL: constant_mask_to_false_splat
func.func @constant_mask_to_false_splat() -> vector<2x4xi1> {
// CHECK: arith.constant dense<false>
// CHECK-NOT: vector.constant_mask
%0 = vector.constant_mask [0, 0] : vector<2x4xi1>
return %0 : vector<2x4xi1>
}

// CHECK-LABEL: constant_mask_to_true_splat_0d
func.func @constant_mask_to_true_splat_0d() -> vector<i1> {
// CHECK: arith.constant dense<true>
// CHECK-NOT: vector.constant_mask
%0 = vector.constant_mask [1] : vector<i1>
return %0 : vector<i1>
}

// CHECK-LABEL: constant_mask_transpose_to_transposed_constant_mask
func.func @constant_mask_transpose_to_transposed_constant_mask() -> (vector<2x3x4xi1>, vector<4x2x3xi1>) {
// CHECK: vector.constant_mask [1, 2, 3] : vector<2x3x4xi1>
Expand All @@ -289,7 +313,7 @@ func.func @extract_strided_slice_of_constant_mask() -> (vector<2x2xi1>) {
%1 = vector.extract_strided_slice %0
{offsets = [0, 0], sizes = [2, 2], strides = [1, 1]}
: vector<4x3xi1> to vector<2x2xi1>
// CHECK: vector.constant_mask [2, 2] : vector<2x2xi1>
// CHECK: arith.constant dense<true> : vector<2x2xi1>
return %1 : vector<2x2xi1>
}

Expand Down Expand Up @@ -322,7 +346,7 @@ func.func @extract_strided_slice_of_constant_mask() -> (vector<2x2xi1>) {
%1 = vector.extract_strided_slice %0
{offsets = [2, 0], sizes = [2, 2], strides = [1, 1]}
: vector<4x3xi1> to vector<2x2xi1>
// CHECK: vector.constant_mask [0, 0] : vector<2x2xi1>
// CHECK: arith.constant dense<false> : vector<2x2xi1>
return %1 : vector<2x2xi1>
}

Expand All @@ -333,7 +357,7 @@ func.func @extract_strided_slice_of_constant_mask() -> (vector<2x1xi1>) {
%1 = vector.extract_strided_slice %0
{offsets = [0, 2], sizes = [2, 1], strides = [1, 1]}
: vector<4x3xi1> to vector<2x1xi1>
// CHECK: vector.constant_mask [0, 0] : vector<2x1xi1>
// CHECK: arith.constant dense<false> : vector<2x1xi1>
return %1 : vector<2x1xi1>
}

Expand All @@ -344,7 +368,7 @@ func.func @extract_strided_slice_of_constant_mask() -> (vector<2x1xi1>) {
%1 = vector.extract_strided_slice %0
{offsets = [0, 1], sizes = [2, 1], strides = [1, 1]}
: vector<4x3xi1> to vector<2x1xi1>
// CHECK: vector.constant_mask [2, 1] : vector<2x1xi1>
// CHECK: arith.constant dense<true> : vector<2x1xi1>
return %1 : vector<2x1xi1>
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/test/Dialect/Vector/vector-mem-transforms.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func.func @maskedstore2(%base: memref<16xf32>, %value: vector<16xf32>) {
// CHECK-SAME: %[[A1:.*]]: vector<16xi32>,
// CHECK-SAME: %[[A2:.*]]: vector<16xf32>) -> vector<16xf32> {
// CHECK-NEXT: %[[C:.*]] = arith.constant 0 : index
// CHECK-NEXT: %[[M:.*]] = vector.constant_mask [16] : vector<16xi1>
// CHECK-NEXT: %[[M:.*]] = arith.constant dense<true> : vector<16xi1>
// CHECK-NEXT: %[[G:.*]] = vector.gather %[[A0]][%[[C]]] [%[[A1]]], %[[M]], %[[A2]] : memref<16xf32>, vector<16xi32>, vector<16xi1>, vector<16xf32> into vector<16xf32>
// CHECK-NEXT: return %[[G]] : vector<16xf32>
func.func @gather1(%base: memref<16xf32>, %indices: vector<16xi32>, %pass_thru: vector<16xf32>) -> vector<16xf32> {
Expand Down Expand Up @@ -112,7 +112,7 @@ func.func @gather2(%base: memref<16xf32>, %indices: vector<16xi32>, %pass_thru:
// CHECK-SAME: %[[A1:.*]]: vector<16xi32>,
// CHECK-SAME: %[[A2:.*]]: vector<16xf32>) {
// CHECK-NEXT: %[[C:.*]] = arith.constant 0 : index
// CHECK-NEXT: %[[M:.*]] = vector.constant_mask [16] : vector<16xi1>
// CHECK-NEXT: %[[M:.*]] = arith.constant dense<true> : vector<16xi1>
// CHECK-NEXT: vector.scatter %[[A0]][%[[C]]] [%[[A1]]], %[[M]], %[[A2]] : memref<16xf32>, vector<16xi32>, vector<16xi1>, vector<16xf32>
// CHECK-NEXT: return
func.func @scatter1(%base: memref<16xf32>, %indices: vector<16xi32>, %value: vector<16xf32>) {
Expand Down
Loading