Skip to content

[mlir][linalg] Support ParamType in vector_sizes option of VectorizeOp transform #87557

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
merged 7 commits into from
Apr 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2138,25 +2138,16 @@ def VectorizeOp : Op<Transform_Dialect, "structured.vectorize",
}];

let arguments = (ins TransformHandleTypeInterface:$target,
Variadic<TransformHandleTypeInterface>:$vector_sizes,
Variadic<TransformAnyParamTypeOrAnyHandle>:$vector_sizes,
DefaultValuedOptionalAttr<DenseI64ArrayAttr, "{}">:
$static_vector_sizes,
OptionalAttr<UnitAttr>:$vectorize_nd_extract,
DefaultValuedOptionalAttr<DenseBoolArrayAttr, "{}">:
$scalable_sizes,
DefaultValuedOptionalAttr<DenseI64ArrayAttr, "{}">:
$static_vector_sizes);
$scalable_sizes);

let results = (outs);
let assemblyFormat = [{
$target oilist(
`vector_sizes` custom<DynamicIndexList>($vector_sizes,
$static_vector_sizes,
type($vector_sizes),
$scalable_sizes) |
`vectorize_nd_extract` $vectorize_nd_extract
)
attr-dict
`:` type($target)
}];

let hasCustomAssemblyFormat = 1;
let hasVerifier = 1;

let extraClassDeclaration = [{
Expand Down
82 changes: 82 additions & 0 deletions mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3122,6 +3122,81 @@ transform::VectorizeChildrenAndApplyPatternsOp::applyToOne(
//===----------------------------------------------------------------------===//
// VectorizeOp
//===----------------------------------------------------------------------===//

static const StringLiteral kVectorSizesKeyword = "vector_sizes";

ParseResult transform::VectorizeOp::parse(OpAsmParser &parser,
OperationState &result) {
OpAsmParser::UnresolvedOperand target;
SmallVector<OpAsmParser::UnresolvedOperand> dynamicSizes;
DenseI64ArrayAttr staticSizes;
SmallVector<Type> operandTypes;
llvm::SMLoc operandLoc;
DenseBoolArrayAttr scalableVals;

if (parser.parseOperand(target) || parser.getCurrentLocation(&operandLoc))
return ParseResult::failure();

if (succeeded(parser.parseOptionalKeyword(kVectorSizesKeyword))) {
if (failed(parseDynamicIndexList(parser, dynamicSizes, staticSizes,
scalableVals)))
return ParseResult::failure();
}

if (succeeded(parser.parseOptionalKeyword(
getVectorizeNdExtractAttrName(result.name))))
result.addAttribute(getVectorizeNdExtractAttrName(result.name),
parser.getBuilder().getUnitAttr());

if (parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonTypeList(operandTypes))
return ParseResult::failure();

if (operandTypes.size() != dynamicSizes.size() + 1) {
return parser.emitError(operandLoc)
<< "expected " << dynamicSizes.size() + 1 << " operand type(s)";
}
if (parser.resolveOperand(target, operandTypes.front(), result.operands) ||
parser.resolveOperands(dynamicSizes, ArrayRef(operandTypes).drop_front(),
operandLoc, result.operands)) {
return failure();
}

if (scalableVals)
result.addAttribute(getScalableSizesAttrName(result.name), scalableVals);
if (staticSizes)
result.addAttribute(getStaticVectorSizesAttrName(result.name), staticSizes);

return success();
}

void transform::VectorizeOp::print(OpAsmPrinter &p) {
p << ' ' << getTarget() << ' ';
if (!getMixedVectorSizes().empty()) {
p << kVectorSizesKeyword << ' ';
printDynamicIndexList(p, getOperation(), getVectorSizes(),
getStaticVectorSizesAttr(),
/*valueTypes=*/{}, getScalableSizesAttr(),
OpAsmParser::Delimiter::Square);
}

if (getVectorizeNdExtract())
p << getVectorizeNdExtractAttrName() << ' ';

p.printOptionalAttrDict(
(*this)->getAttrs(),
/*elidedAttrs=*/{
getScalableSizesAttrName(getOperation()->getName()),
getStaticVectorSizesAttrName(getOperation()->getName())});
p << " : ";
p << getTarget().getType();
if (!getVectorSizes().empty()) {
p << ", ";
llvm::interleaveComma(getVectorSizes(), p,
[&](Value operand) { p << operand.getType(); });
}
}

DiagnosedSilenceableFailure transform::VectorizeOp::apply(
transform::TransformRewriter &rewriter,
mlir::transform::TransformResults &transformResults,
Expand All @@ -3136,6 +3211,13 @@ DiagnosedSilenceableFailure transform::VectorizeOp::apply(
auto attr = sz.get<Attribute>();
vectorSizes.push_back(cast<IntegerAttr>(attr).getInt());
continue;
} else if (sz.is<Value>() && isa<ParamType>(sz.get<Value>().getType())) {
ArrayRef<Attribute> params = state.getParams(sz.get<Value>());
if (params.size() != 1)
return emitSilenceableFailure(getLoc()) << "expected a single param";
vectorSizes.push_back(
cast<IntegerAttr>(params.front()).getValue().getSExtValue());
continue;
}

auto szPayloads = state.getPayloadOps(sz.get<Value>());
Expand Down
21 changes: 21 additions & 0 deletions mlir/test/Dialect/Linalg/transform-ops-invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,24 @@ transform.sequence failures(propagate) {
: (!transform.any_op) -> !transform.op<"linalg.generic">

}

// -----

transform.sequence failures(propagate) {
^bb0(%arg0: !transform.any_op):
%0 = transform.param.constant 2 : i64 -> !transform.param<i64>
// expected-error@below {{custom op 'transform.structured.vectorize' expected 2 operand type(s)}}
transform.structured.vectorize %arg0 vector_sizes [%0, 2] : !transform.any_op, !transform.param<i64>, !transform.param<i64>

}

// -----

transform.sequence failures(propagate) {
^bb0(%arg0: !transform.any_op):
%0 = transform.param.constant 2 : i64 -> !transform.param<i64>
// expected-error@below {{expected ']' in dynamic index list}}
// expected-error@below {{custom op 'transform.structured.vectorize' expected SSA value or integer}}
transform.structured.vectorize %arg0 vector_sizes [%0 : !transform.param<i64>, 2] : !transform.any_op, !transform.param<i64>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ftynse does this suffice for dynamic list parsing test?

Copy link
Member

Choose a reason for hiding this comment

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

Yep. It's strange that we get two diagnostics for the same error, but it's not in your code. You are welcome to investigate that in a separate patch if you want.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks! ya i may look into that in my downtime


}
11 changes: 10 additions & 1 deletion mlir/test/Dialect/Linalg/transform-ops.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: mlir-opt %s | mlir-opt | FileCheck %s
// RUN: mlir-opt %s --split-input-file | mlir-opt | FileCheck %s

transform.sequence failures(propagate) {
^bb1(%arg0: !transform.any_op):
Expand Down Expand Up @@ -57,3 +57,12 @@ transform.sequence failures(propagate) {
%1:2 = transform.structured.fuse_into_containing_op %arg2 into %loop
: (!transform.any_op, !transform.any_op) -> (!transform.any_op, !transform.any_op)
}

// -----

transform.sequence failures(propagate) {
^bb0(%arg0: !transform.any_op):
// CHECK: transform.structured.vectorize %arg0 : !transform.any_op
transform.structured.vectorize %arg0 vector_sizes [] : !transform.any_op

}
118 changes: 118 additions & 0 deletions mlir/test/Dialect/Linalg/vectorization.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,81 @@ module attributes {transform.with_named_sequence} {

// -----

func.func @vectorize_dynamic_identity_with_constant(%arg0: tensor<?xf32>,
%arg1: tensor<?xf32>,
%arg2: tensor<?xf32>) -> tensor<?xf32> {
%c4 = arith.constant 4 : index
%0 = linalg.generic { indexing_maps = [affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"] }
ins(%arg0, %arg1 : tensor<?xf32>, tensor<?xf32>)
outs(%arg2 : tensor<?xf32>) {
^bb(%in0: f32, %in1: f32, %out: f32) :
%0 = arith.addf %in0, %in1 : f32
linalg.yield %0 : f32
} -> tensor<?xf32>
return %0 : tensor<?xf32>
}

// CHECK-LABEL: @vectorize_dynamic_identity_with_constant
// CHECK: %[[VAL_3:.*]] = arith.constant 0 : index
// CHECK: %[[VAL_4:.*]] = tensor.dim %{{.*}}, %[[VAL_3]] : tensor<?xf32>
// CHECK: %[[VAL_7:.*]] = vector.create_mask %[[VAL_4]] : vector<4xi1>
// CHECK: %[[VAL_8:.*]] = vector.mask %[[VAL_7]] { vector.transfer_read %{{.*}} {in_bounds = [true]} : tensor<?xf32>, vector<4xf32> } : vector<4xi1> -> vector<4xf32>
// CHECK: %[[VAL_10:.*]] = vector.mask %[[VAL_7]] { vector.transfer_read %{{.*}} {in_bounds = [true]} : tensor<?xf32>, vector<4xf32> } : vector<4xi1> -> vector<4xf32>
// CHECK: %[[VAL_12:.*]] = vector.mask %[[VAL_7]] { vector.transfer_read %{{.*}} {in_bounds = [true]} : tensor<?xf32>, vector<4xf32> } : vector<4xi1> -> vector<4xf32>
// CHECK: %[[VAL_13:.*]] = arith.addf %[[VAL_8]], %[[VAL_10]] : vector<4xf32>
// CHECK: %[[VAL_14:.*]] = vector.mask %[[VAL_7]] { vector.transfer_write %{{.*}} {in_bounds = [true]} : vector<4xf32>, tensor<?xf32> } : vector<4xi1> -> tensor<?xf32>

module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
%0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op
%size = transform.structured.match ops{["arith.constant"]} in %arg1 : (!transform.any_op) -> !transform.any_op
transform.structured.vectorize %0 vector_sizes [%size] : !transform.any_op, !transform.any_op
transform.yield
}
}

// -----

func.func @vectorize_dynamic_identity_with_param(%arg0: tensor<?xf32>,
%arg1: tensor<?xf32>,
%arg2: tensor<?xf32>) -> tensor<?xf32> {
%0 = linalg.generic { indexing_maps = [affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"] }
ins(%arg0, %arg1 : tensor<?xf32>, tensor<?xf32>)
outs(%arg2 : tensor<?xf32>) {
^bb(%in0: f32, %in1: f32, %out: f32) :
%0 = arith.addf %in0, %in1 : f32
linalg.yield %0 : f32
} -> tensor<?xf32>
return %0 : tensor<?xf32>
}

// CHECK-LABEL: @vectorize_dynamic_identity_with_param
// CHECK: %[[VAL_3:.*]] = arith.constant 0 : index
// CHECK: %[[VAL_4:.*]] = tensor.dim %{{.*}}, %[[VAL_3]] : tensor<?xf32>
// CHECK: %[[VAL_7:.*]] = vector.create_mask %[[VAL_4]] : vector<4xi1>
// CHECK: %[[VAL_8:.*]] = vector.mask %[[VAL_7]] { vector.transfer_read %{{.*}} {in_bounds = [true]} : tensor<?xf32>, vector<4xf32> } : vector<4xi1> -> vector<4xf32>
// CHECK: %[[VAL_10:.*]] = vector.mask %[[VAL_7]] { vector.transfer_read %{{.*}} {in_bounds = [true]} : tensor<?xf32>, vector<4xf32> } : vector<4xi1> -> vector<4xf32>
// CHECK: %[[VAL_12:.*]] = vector.mask %[[VAL_7]] { vector.transfer_read %{{.*}} {in_bounds = [true]} : tensor<?xf32>, vector<4xf32> } : vector<4xi1> -> vector<4xf32>
// CHECK: %[[VAL_13:.*]] = arith.addf %[[VAL_8]], %[[VAL_10]] : vector<4xf32>
// CHECK: %[[VAL_14:.*]] = vector.mask %[[VAL_7]] { vector.transfer_write %{{.*}} {in_bounds = [true]} : vector<4xf32>, tensor<?xf32> } : vector<4xi1> -> tensor<?xf32>

module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
%0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op
%vector_size = transform.param.constant 4 : i64 -> !transform.param<i64>
transform.structured.vectorize %0 vector_sizes [%vector_size] : !transform.any_op, !transform.param<i64>
transform.yield
}
}

// -----

func.func @vectorize_dynamic_1d_broadcast(%arg0: tensor<?xf32>,
%arg1: tensor<?xf32>,
%arg2: tensor<?xf32>) -> tensor<?xf32> {
Expand Down Expand Up @@ -231,6 +306,49 @@ module attributes {transform.with_named_sequence} {

// -----

func.func @vectorize_dynamic_transpose_reduction_with_params(%arg0: tensor<?x?x?xf32>,
%arg1: tensor<?x?xf32>) -> tensor<?x?xf32> {
%0 = linalg.generic { indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>,
affine_map<(d0, d1, d2) -> (d2, d1)>],
iterator_types = ["reduction", "parallel", "parallel"] }
ins(%arg0 : tensor<?x?x?xf32>)
outs(%arg1 : tensor<?x?xf32>) {
^bb(%in: f32, %out: f32) :
%0 = arith.addf %in, %out : f32
linalg.yield %0 : f32
} -> tensor<?x?xf32>
return %0 : tensor<?x?xf32>
}

module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
%0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op
%vector_size_0 = transform.param.constant 4 : i64 -> !transform.param<i64>
%vector_size_2 = transform.param.constant 16 : i64 -> !transform.param<i64>
transform.structured.vectorize %0 vector_sizes
[%vector_size_0, 8, %vector_size_2] : !transform.any_op, !transform.param<i64>, !transform.param<i64>
transform.yield
}
}

// CHECK-LABEL: @vectorize_dynamic_transpose_reduction_with_params(
// CHECK-SAME: %[[VAL_0:.*]]: tensor<?x?x?xf32>,
// CHECK-SAME: %[[VAL_1:.*]]: tensor<?x?xf32>) -> tensor<?x?xf32> {
// CHECK: %[[VAL_2:.*]] = arith.constant 0 : index
// CHECK: %[[VAL_3:.*]] = tensor.dim %[[VAL_0]], %[[VAL_2]] : tensor<?x?x?xf32>
// CHECK: %[[VAL_4:.*]] = arith.constant 1 : index
// CHECK: %[[VAL_5:.*]] = tensor.dim %[[VAL_0]], %[[VAL_4]] : tensor<?x?x?xf32>
// CHECK: %[[VAL_6:.*]] = arith.constant 2 : index
// CHECK: %[[VAL_7:.*]] = tensor.dim %[[VAL_0]], %[[VAL_6]] : tensor<?x?x?xf32>
// CHECK: %[[VAL_10:.*]] = vector.create_mask %[[VAL_3]], %[[VAL_5]], %[[VAL_7]] : vector<4x8x16xi1>
// CHECK: %[[VAL_11:.*]] = vector.mask %[[VAL_10]] { vector.transfer_read %[[VAL_0]]{{.*}} {in_bounds = [true, true, true]} : tensor<?x?x?xf32>, vector<4x8x16xf32> } : vector<4x8x16xi1> -> vector<4x8x16xf32>
// CHECK: %[[VAL_13:.*]] = vector.create_mask %[[VAL_7]], %[[VAL_5]] : vector<16x8xi1>
// CHECK: %[[VAL_14:.*]] = vector.mask %[[VAL_13]] { vector.transfer_read %[[VAL_1]]{{.*}} {in_bounds = [true, true], permutation_map = #{{.*}}} : tensor<?x?xf32>, vector<8x16xf32> } : vector<16x8xi1> -> vector<8x16xf32>
// CHECK: %[[VAL_15:.*]] = vector.mask %[[VAL_10]] { vector.multi_reduction <add>, %[[VAL_11]], %[[VAL_14]] [0] : vector<4x8x16xf32> to vector<8x16xf32> } : vector<4x8x16xi1> -> vector<8x16xf32>
// CHECK: %[[VAL_17:.*]] = vector.mask %[[VAL_13]] { vector.transfer_write %[[VAL_15]], %{{.*}} {in_bounds = [true, true], permutation_map = #{{.*}}} : vector<8x16xf32>, tensor<?x?xf32> } : vector<16x8xi1> -> tensor<?x?xf32>

// -----

func.func @vectorize_partial_dynamic_identity(%arg0: tensor<8x?xf32>,
%arg1: tensor<8x?xf32>,
%arg2: tensor<8x?xf32>) -> tensor<8x?xf32> {
Expand Down
14 changes: 12 additions & 2 deletions mlir/test/python/dialects/transform_structured_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,17 @@ def testVectorizeMixed(target):
# CHECK: transform.sequence
# CHECK: %[[V0:.*]] = transform.structured.match
# CHECK: transform.structured.vectorize
# CHECK-SAME: vector_sizes [%[[V0]] : !transform.any_op, 4]
# CHECK-SAME: vector_sizes [%[[V0]], 4]


@run
@create_sequence
def testVectorizeEmpty(target):
structured.VectorizeOp(target, [])
# CHECK-LABEL: TEST: testVectorizeEmpty
# CHECK: transform.sequence
# CHECK: transform.structured.vectorize
# CHECK-NOT: vector_sizes


@run
Expand All @@ -223,7 +233,7 @@ def testVectorizeScalable(target):
# CHECK: transform.sequence
# CHECK-DAG: %[[V0:.*]] = transform.structured.match
# CHECK-DAG: transform.structured.vectorize
# CHECK-SAME: vector_sizes [16, [%[[V0]] : !transform.any_op], [4], [8]]
# CHECK-SAME: vector_sizes [16, [%[[V0]]], [4], [8]]


@run
Expand Down