Skip to content

Commit d488b22

Browse files
bealwangBiao Wang
andauthored
[mlir][ods] Do not print default-valued properties when the value is equal to the default (#87970)
This diff causes the `tblgen`-erated printProperties() function to skip printing a `DefaultValuedAttr` property when the value is equal to the default. Co-authored-by: Biao Wang <[email protected]>
1 parent ad4e1ab commit d488b22

File tree

6 files changed

+85
-24
lines changed

6 files changed

+85
-24
lines changed

mlir/include/mlir/Dialect/XeGPU/IR/XeGPUOps.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class XeGPU_Op<string mnemonic, list<Trait> traits = []>:
2727

2828
code extraBaseClassDeclaration = [{
2929
void printProperties(::mlir::MLIRContext *ctx,
30-
::mlir::OpAsmPrinter &p, const Properties &prop) {
30+
::mlir::OpAsmPrinter &p, const Properties &prop,
31+
::mlir::ArrayRef<::llvm::StringRef> elidedProps) {
3132
Attribute propAttr = getPropertiesAsAttr(ctx, prop);
3233
if (propAttr)
3334
p << "<" << propAttr << ">";

mlir/include/mlir/IR/OpDefinition.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,10 @@ class OpState {
226226
static ParseResult genericParseProperties(OpAsmParser &parser,
227227
Attribute &result);
228228

229-
/// Print the properties as a Attribute.
230-
static void genericPrintProperties(OpAsmPrinter &p, Attribute properties);
229+
/// Print the properties as a Attribute with names not included within
230+
/// 'elidedProps'
231+
static void genericPrintProperties(OpAsmPrinter &p, Attribute properties,
232+
ArrayRef<StringRef> elidedProps = {});
231233

232234
/// Print an operation name, eliding the dialect prefix if necessary.
233235
static void printOpName(Operation *op, OpAsmPrinter &p,
@@ -1805,10 +1807,13 @@ class Op : public OpState, public Traits<ConcreteType>... {
18051807
template <typename T>
18061808
using detect_has_print = llvm::is_detected<has_print, T>;
18071809

1808-
/// Trait to check if printProperties(OpAsmPrinter, T) exist
1810+
/// Trait to check if printProperties(OpAsmPrinter, T, ArrayRef<StringRef>)
1811+
/// exist
18091812
template <typename T, typename... Args>
1810-
using has_print_properties = decltype(printProperties(
1811-
std::declval<OpAsmPrinter &>(), std::declval<T>()));
1813+
using has_print_properties =
1814+
decltype(printProperties(std::declval<OpAsmPrinter &>(),
1815+
std::declval<T>(),
1816+
std::declval<ArrayRef<StringRef>>()));
18121817
template <typename T>
18131818
using detect_has_print_properties =
18141819
llvm::is_detected<has_print_properties, T>;
@@ -1974,16 +1979,18 @@ class Op : public OpState, public Traits<ConcreteType>... {
19741979
static void populateDefaultProperties(OperationName opName,
19751980
InferredProperties<T> &properties) {}
19761981

1977-
/// Print the operation properties. Unless overridden, this method will try to
1978-
/// dispatch to a `printProperties` free-function if it exists, and otherwise
1979-
/// by converting the properties to an Attribute.
1982+
/// Print the operation properties with names not included within
1983+
/// 'elidedProps'. Unless overridden, this method will try to dispatch to a
1984+
/// `printProperties` free-function if it exists, and otherwise by converting
1985+
/// the properties to an Attribute.
19801986
template <typename T>
19811987
static void printProperties(MLIRContext *ctx, OpAsmPrinter &p,
1982-
const T &properties) {
1988+
const T &properties,
1989+
ArrayRef<StringRef> elidedProps = {}) {
19831990
if constexpr (detect_has_print_properties<T>::value)
1984-
return printProperties(p, properties);
1985-
genericPrintProperties(p,
1986-
ConcreteType::getPropertiesAsAttr(ctx, properties));
1991+
return printProperties(p, properties, elidedProps);
1992+
genericPrintProperties(
1993+
p, ConcreteType::getPropertiesAsAttr(ctx, properties), elidedProps);
19871994
}
19881995

19891996
/// Parser the properties. Unless overridden, this method will print by

mlir/lib/IR/Operation.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -790,15 +790,33 @@ void OpState::printOpName(Operation *op, OpAsmPrinter &p,
790790
/// Parse properties as a Attribute.
791791
ParseResult OpState::genericParseProperties(OpAsmParser &parser,
792792
Attribute &result) {
793-
if (parser.parseLess() || parser.parseAttribute(result) ||
794-
parser.parseGreater())
795-
return failure();
793+
if (succeeded(parser.parseOptionalLess())) { // The less is optional.
794+
if (parser.parseAttribute(result) || parser.parseGreater())
795+
return failure();
796+
}
796797
return success();
797798
}
798799

799-
/// Print the properties as a Attribute.
800-
void OpState::genericPrintProperties(OpAsmPrinter &p, Attribute properties) {
801-
p << "<" << properties << ">";
800+
/// Print the properties as a Attribute with names not included within
801+
/// 'elidedProps'
802+
void OpState::genericPrintProperties(OpAsmPrinter &p, Attribute properties,
803+
ArrayRef<StringRef> elidedProps) {
804+
auto dictAttr = dyn_cast_or_null<::mlir::DictionaryAttr>(properties);
805+
if (dictAttr && !elidedProps.empty()) {
806+
ArrayRef<NamedAttribute> attrs = dictAttr.getValue();
807+
llvm::SmallDenseSet<StringRef> elidedAttrsSet(elidedProps.begin(),
808+
elidedProps.end());
809+
bool atLeastOneAttr = llvm::any_of(attrs, [&](NamedAttribute attr) {
810+
return !elidedAttrsSet.contains(attr.getName().strref());
811+
});
812+
if (atLeastOneAttr) {
813+
p << "<";
814+
p.printOptionalAttrDict(dictAttr.getValue(), elidedProps);
815+
p << ">";
816+
}
817+
} else {
818+
p << "<" << properties << ">";
819+
}
802820
}
803821

804822
/// Emit an error about fatal conditions with this operation, reporting up to

mlir/test/IR/properties.mlir

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ test.using_property_in_custom [1, 4, 20]
3333
// GENERIC-SAME: second = 4
3434
// GENERIC-SAME: }>
3535
test.using_property_ref_in_custom 1 + 4 = 5
36+
37+
// CHECK: test.with_default_valued_properties {{$}}
38+
// GENERIC: "test.with_default_valued_properties"()
39+
// GENERIC-SAME: <{a = 0 : i32}>
40+
test.with_default_valued_properties <{a = 0 : i32}>

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,7 +2909,8 @@ def TestOpWithNiceProperties : TEST_Op<"with_nice_properties"> {
29092909
);
29102910
let extraClassDeclaration = [{
29112911
void printProperties(::mlir::MLIRContext *ctx, ::mlir::OpAsmPrinter &p,
2912-
const Properties &prop);
2912+
const Properties &prop,
2913+
::mlir::ArrayRef<::llvm::StringRef> elidedProps);
29132914
static ::mlir::ParseResult parseProperties(::mlir::OpAsmParser &parser,
29142915
::mlir::OperationState &result);
29152916
static ::mlir::LogicalResult readFromMlirBytecode(
@@ -2938,7 +2939,8 @@ def TestOpWithNiceProperties : TEST_Op<"with_nice_properties"> {
29382939
writer.writeVarInt(prop.value);
29392940
}
29402941
void TestOpWithNiceProperties::printProperties(::mlir::MLIRContext *ctx,
2941-
::mlir::OpAsmPrinter &p, const Properties &prop) {
2942+
::mlir::OpAsmPrinter &p, const Properties &prop,
2943+
::mlir::ArrayRef<::llvm::StringRef> elidedProps) {
29422944
customPrintProperties(p, prop.prop);
29432945
}
29442946
::mlir::ParseResult TestOpWithNiceProperties::parseProperties(
@@ -2971,7 +2973,8 @@ def TestOpWithVersionedProperties : TEST_Op<"with_versioned_properties"> {
29712973
);
29722974
let extraClassDeclaration = [{
29732975
void printProperties(::mlir::MLIRContext *ctx, ::mlir::OpAsmPrinter &p,
2974-
const Properties &prop);
2976+
const Properties &prop,
2977+
::mlir::ArrayRef<::llvm::StringRef> elidedProps);
29752978
static ::mlir::ParseResult parseProperties(::mlir::OpAsmParser &parser,
29762979
::mlir::OperationState &result);
29772980
static ::mlir::LogicalResult readFromMlirBytecode(
@@ -2983,7 +2986,8 @@ def TestOpWithVersionedProperties : TEST_Op<"with_versioned_properties"> {
29832986
}];
29842987
let extraClassDefinition = [{
29852988
void TestOpWithVersionedProperties::printProperties(::mlir::MLIRContext *ctx,
2986-
::mlir::OpAsmPrinter &p, const Properties &prop) {
2989+
::mlir::OpAsmPrinter &p, const Properties &prop,
2990+
::mlir::ArrayRef<::llvm::StringRef> elidedProps) {
29872991
customPrintProperties(p, prop.prop);
29882992
}
29892993
::mlir::ParseResult TestOpWithVersionedProperties::parseProperties(
@@ -2997,6 +3001,11 @@ def TestOpWithVersionedProperties : TEST_Op<"with_versioned_properties"> {
29973001
}];
29983002
}
29993003

3004+
def TestOpWithDefaultValuedProperties : TEST_Op<"with_default_valued_properties"> {
3005+
let assemblyFormat = "prop-dict attr-dict";
3006+
let arguments = (ins DefaultValuedAttr<I32Attr, "0">:$a);
3007+
}
3008+
30003009
//===----------------------------------------------------------------------===//
30013010
// Test Dataflow
30023011
//===----------------------------------------------------------------------===//

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1775,9 +1775,30 @@ const char *enumAttrBeginPrinterCode = R"(
17751775
/// Generate the printer for the 'prop-dict' directive.
17761776
static void genPropDictPrinter(OperationFormat &fmt, Operator &op,
17771777
MethodBody &body) {
1778+
body << " ::llvm::SmallVector<::llvm::StringRef, 2> elidedProps;\n";
1779+
// Add code to check attributes for equality with the default value
1780+
// for attributes with the elidePrintingDefaultValue bit set.
1781+
for (const NamedAttribute &namedAttr : op.getAttributes()) {
1782+
const Attribute &attr = namedAttr.attr;
1783+
if (!attr.isDerivedAttr() && attr.hasDefaultValue()) {
1784+
const StringRef &name = namedAttr.name;
1785+
FmtContext fctx;
1786+
fctx.withBuilder("odsBuilder");
1787+
std::string defaultValue = std::string(
1788+
tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue()));
1789+
body << " {\n";
1790+
body << " ::mlir::Builder odsBuilder(getContext());\n";
1791+
body << " ::mlir::Attribute attr = " << op.getGetterName(name)
1792+
<< "Attr();\n";
1793+
body << " if(attr && (attr == " << defaultValue << "))\n";
1794+
body << " elidedProps.push_back(\"" << name << "\");\n";
1795+
body << " }\n";
1796+
}
1797+
}
1798+
17781799
body << " _odsPrinter << \" \";\n"
17791800
<< " printProperties(this->getContext(), _odsPrinter, "
1780-
"getProperties());\n";
1801+
"getProperties(), elidedProps);\n";
17811802
}
17821803

17831804
/// Generate the printer for the 'attr-dict' directive.

0 commit comments

Comments
 (0)