Skip to content

Commit e07c92a

Browse files
authored
[mlir] Fix TileUsingForOp attr-dict printing/parsing (#73261)
`TileUsingForOp` has an optional Attribute `interchange` which was given in curly braces like this: `{interchange = [...]}`. The way this was parsed meant that no `attr-dict` could be attached to the Op. This patch adds printing / parsing of an `attr-dict` to the Op and prints/parses the `interchange` Attribute separate from the discardable Attributes.
1 parent 3ab41f9 commit e07c92a

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,26 +2658,23 @@ SmallVector<OpFoldResult> transform::TileUsingForOp::getMixedSizes() {
26582658
// `array` prefix to be consistent in the IR with `parseDynamicIndexList`.
26592659
ParseResult parseOptionalInterchange(OpAsmParser &parser,
26602660
OperationState &result) {
2661-
if (succeeded(parser.parseOptionalLBrace())) {
2662-
if (failed(parser.parseKeyword("interchange")))
2663-
return parser.emitError(parser.getNameLoc()) << "expect `interchange`";
2664-
if (failed(parser.parseEqual()))
2665-
return parser.emitError(parser.getNameLoc()) << "expect `=`";
2666-
result.addAttribute("interchange",
2667-
DenseI64ArrayAttr::parse(parser, Type{}));
2668-
if (failed(parser.parseRBrace()))
2669-
return parser.emitError(parser.getNameLoc()) << "expect `}`";
2670-
}
2661+
if (failed(parser.parseOptionalKeyword("interchange")))
2662+
return success();
2663+
if (failed(parser.parseEqual()))
2664+
return failure();
2665+
result.addAttribute(
2666+
transform::TileUsingForOp::getInterchangeAttrName(result.name),
2667+
DenseI64ArrayAttr::parse(parser, Type{}));
26712668
return success();
26722669
}
26732670

26742671
void printOptionalInterchange(OpAsmPrinter &p,
26752672
ArrayRef<int64_t> interchangeVals) {
26762673
if (!interchangeVals.empty()) {
2677-
p << " {interchange = [";
2674+
p << " interchange = [";
26782675
llvm::interleaveComma(interchangeVals, p,
26792676
[&](int64_t integer) { p << integer; });
2680-
p << "]}";
2677+
p << "]";
26812678
}
26822679
}
26832680

@@ -2693,6 +2690,7 @@ ParseResult transform::TileUsingForOp::parse(OpAsmParser &parser,
26932690
if (parser.parseOperand(target) || parser.getCurrentLocation(&operandLoc) ||
26942691
parseDynamicIndexList(parser, dynamicSizes, staticSizes, scalableVals) ||
26952692
parseOptionalInterchange(parser, result) ||
2693+
parser.parseOptionalAttrDict(result.attributes) ||
26962694
parser.parseColonType(functionalType))
26972695
return ParseResult::failure();
26982696

@@ -2727,6 +2725,11 @@ void TileUsingForOp::print(OpAsmPrinter &p) {
27272725
/*valueTypes=*/{}, getScalableSizesAttr(),
27282726
OpAsmParser::Delimiter::Square);
27292727
printOptionalInterchange(p, getInterchange());
2728+
p.printOptionalAttrDict(
2729+
(*this)->getAttrs(),
2730+
/*elidedAttrs=*/{getInterchangeAttrName(getOperation()->getName()),
2731+
getScalableSizesAttrName(getOperation()->getName()),
2732+
getStaticSizesAttrName(getOperation()->getName())});
27302733
p << " : ";
27312734
p.printFunctionalType(getOperands().getTypes(), getResults().getTypes());
27322735
}

mlir/test/Dialect/Linalg/transform-ops.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ transform.sequence failures(propagate) {
66
%0, %1:2 = transform.structured.tile_using_for %arg0 [2, 0, 3] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op)
77
}
88

9+
// check that the Attributes of `tile_using_for` are preserved through printing
10+
// and parsing with and without use of the optional `interchange` Attribute.
11+
transform.sequence failures(propagate) {
12+
^bb1(%arg0: !transform.any_op):
13+
// CHECK %{{.*}}, %{{.*}}:2 = transform.structured.tile %arg0 [2, 0, 3] interchange = [2, 1] {test_attr1 = 1 : i64, test_attr2}
14+
%0, %1:2 = transform.structured.tile_using_for %arg0 [2, 0, 3] interchange = [2, 1] {test_attr1 = 1 : i64, test_attr2}: (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op)
15+
// CHECK %{{.*}}, %{{.*}}:2 = transform.structured.tile %arg0 [4, 5, 3] {test_attr3 = 1 : i64, test_attr4}
16+
%2, %3:2 = transform.structured.tile_using_for %0 [0, 5, 3] {test_attr3 = 1 : i64, test_attr4}: (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op)
17+
}
18+
919
transform.sequence failures(propagate) {
1020
^bb1(%arg0: !transform.any_op):
1121
%0:2 = transform.structured.split %arg0 after 42 { dimension = 0 } : !transform.any_op

mlir/test/Dialect/Linalg/transform-patterns.mlir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func.func @matvec_perm(%A: memref<?x?xf32, strided<[?, 1], offset: ?>>,
170170
module attributes {transform.with_named_sequence} {
171171
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
172172
%0 = transform.structured.match ops{["linalg.matvec"]} in %arg1 : (!transform.any_op) -> !transform.any_op
173-
%1, %loops:2 = transform.structured.tile_using_for %0 [5, 6] {interchange = [1, 0]} : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op)
173+
%1, %loops:2 = transform.structured.tile_using_for %0 [5, 6] interchange = [1, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op)
174174
transform.yield
175175
}
176176
}
@@ -199,8 +199,8 @@ func.func @matmul_perm(%A: memref<?x?xf32, strided<[?, 1], offset: ?>>,
199199
module attributes {transform.with_named_sequence} {
200200
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
201201
%0 = transform.structured.match ops{["linalg.matmul"]} in %arg1 : (!transform.any_op) -> !transform.any_op
202-
%1, %loops:3 = transform.structured.tile_using_for %0 [2000, 3000, 4000] {interchange = [1, 2, 0]} : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)
203-
%2, %loops_2:3 = transform.structured.tile_using_for %1 [200, 300, 400] {interchange = [1, 0, 2]} : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)
202+
%1, %loops:3 = transform.structured.tile_using_for %0 [2000, 3000, 4000] interchange = [1, 2, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)
203+
%2, %loops_2:3 = transform.structured.tile_using_for %1 [200, 300, 400] interchange = [1, 0, 2] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)
204204
%3, %loops_3:3 = transform.structured.tile_using_for %2 [20, 30, 40] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)
205205
transform.yield
206206
}

0 commit comments

Comments
 (0)