Skip to content

Commit ce21721

Browse files
authored
[MLIR] Fix tblgen properties printing to filter them out of discardable attrs dict (#79243)
This is to fix the bug reported in https://discourse.llvm.org/t/whats-the-recommended-way-to-use-prop-dict/75921 When `prop-dict` is used in the assembly format, `attr-dict` should print discardable attributes only. Co-authored-by: Arthurq Qiu <[email protected]>
1 parent 36e4a7e commit ce21721

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

mlir/test/IR/properties.mlir

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@
22
// # RUN: mlir-opt %s -mlir-print-op-generic -split-input-file | mlir-opt -mlir-print-op-generic | FileCheck %s --check-prefix=GENERIC
33

44
// CHECK: test.with_properties
5-
// CHECK-SAME: <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}>
5+
// CHECK-SAME: <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}>{{$}}
66
// GENERIC: "test.with_properties"()
77
// GENERIC-SAME: <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}> : () -> ()
88
test.with_properties <{a = 32 : i64, array = array<i64: 1, 2, 3, 4>, b = "foo"}>
99

1010
// CHECK: test.with_nice_properties
11-
// CHECK-SAME: "foo bar" is -3
11+
// CHECK-SAME: "foo bar" is -3{{$}}
1212
// GENERIC: "test.with_nice_properties"()
1313
// GENERIC-SAME: <{prop = {label = "foo bar", value = -3 : i32}}> : () -> ()
1414
test.with_nice_properties "foo bar" is -3
1515

1616
// CHECK: test.with_wrapped_properties
17-
// CHECK-SAME: "content for properties"
17+
// CHECK-SAME: <{prop = "content for properties"}>{{$}}
1818
// GENERIC: "test.with_wrapped_properties"()
1919
// GENERIC-SAME: <{prop = "content for properties"}> : () -> ()
2020
test.with_wrapped_properties <{prop = "content for properties"}>
2121

2222
// CHECK: test.using_property_in_custom
23-
// CHECK-SAME: [1, 4, 20]
23+
// CHECK-SAME: [1, 4, 20]{{$}}
2424
// GENERIC: "test.using_property_in_custom"()
2525
// GENERIC-SAME: prop = array<i64: 1, 4, 20>
2626
test.using_property_in_custom [1, 4, 20]
2727

2828
// CHECK: test.using_property_ref_in_custom
29-
// CHECK-SAME: 1 + 4 = 5
29+
// CHECK-SAME: 1 + 4 = 5{{$}}
3030
// GENERIC: "test.using_property_ref_in_custom"()
3131
// GENERIC-SAME: <{
3232
// GENERIC-SAME: first = 1

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ struct OperationFormat {
366366
/// Indicate whether attribute are stored in properties.
367367
bool useProperties;
368368

369+
/// Indicate whether prop-dict is used in the format
370+
bool hasPropDict;
371+
369372
/// The Operation class name
370373
StringRef opCppClassName;
371374

@@ -1810,9 +1813,14 @@ static void genAttrDictPrinter(OperationFormat &fmt, Operator &op,
18101813
body << " }\n";
18111814
}
18121815
}
1813-
body << " _odsPrinter.printOptionalAttrDict"
1814-
<< (withKeyword ? "WithKeyword" : "")
1815-
<< "((*this)->getAttrs(), elidedAttrs);\n";
1816+
if (fmt.hasPropDict)
1817+
body << " _odsPrinter.printOptionalAttrDict"
1818+
<< (withKeyword ? "WithKeyword" : "")
1819+
<< "(llvm::to_vector((*this)->getDiscardableAttrs()), elidedAttrs);\n";
1820+
else
1821+
body << " _odsPrinter.printOptionalAttrDict"
1822+
<< (withKeyword ? "WithKeyword" : "")
1823+
<< "((*this)->getAttrs(), elidedAttrs);\n";
18161824
}
18171825

18181826
/// Generate the printer for a literal value. `shouldEmitSpace` is true if a
@@ -2560,6 +2568,9 @@ LogicalResult OpFormatParser::verify(SMLoc loc,
25602568

25612569
// Collect the set of used attributes in the format.
25622570
fmt.usedAttributes = seenAttrs.takeVector();
2571+
2572+
// Set whether prop-dict is used in the format
2573+
fmt.hasPropDict = hasPropDict;
25632574
return success();
25642575
}
25652576

0 commit comments

Comments
 (0)