@@ -104,15 +104,21 @@ void printNamedAttribute(OpAsmPrinter &printer, NamedAttribute namedAttr) {
104104 printAttribute (printer, namedAttr.getValue ());
105105}
106106
107- void printOptionalAttrDict (
108- OpAsmPrinter &printer, ArrayRef<NamedAttribute> attrs) {
107+ void printOptionalAttrDict (OpAsmPrinter &printer,
108+ ArrayRef<NamedAttribute> attrs, ArrayRef<StringRef> elidedAttrs = {}) {
109+ SmallVector<NamedAttribute, 4 > filteredAttrs;
110+ for (NamedAttribute attr : attrs) {
111+ if (llvm::is_contained (elidedAttrs, attr.getName ().strref ()))
112+ continue ;
113+ filteredAttrs.emplace_back (attr);
114+ }
109115 // If there are no attributes, then there is nothing to be done.
110- if (attrs .empty ())
116+ if (filteredAttrs .empty ())
111117 return ;
112118
113119 // Otherwise, print them all out in braces.
114120 printer << " {" ;
115- llvm::interleaveComma (attrs , printer.getStream (),
121+ llvm::interleaveComma (filteredAttrs , printer.getStream (),
116122 [&](NamedAttribute attr) { printNamedAttribute (printer, attr); });
117123 printer << ' }' ;
118124}
@@ -156,6 +162,7 @@ void ONNXConstantOp::print(OpAsmPrinter &printer) {
156162 assert (!mlir::isa<SparseElementsAttr>(elements) &&
157163 " ONNXConstantOp value cannot be sparse" );
158164 if (elements.getType () == resultType) {
165+ printOptionalAttrDict (printer, (*this )->getAttrs (), {" value" });
159166 printer << ' ' ;
160167 printAttribute (printer, elements);
161168 return ;
@@ -165,6 +172,7 @@ void ONNXConstantOp::print(OpAsmPrinter &printer) {
165172 // ONNXConstantOp sparse_value must be SparseElementsAttr.
166173 auto sparseElements = mlir::cast<SparseElementsAttr>(*attr);
167174 if (sparseElements.getType () == resultType) {
175+ printOptionalAttrDict (printer, (*this )->getAttrs (), {" sparse_value" });
168176 printer << ' ' ;
169177 printer.printAttribute (sparseElements);
170178 return ;
@@ -182,9 +190,10 @@ ParseResult ONNXConstantOp::parse(OpAsmParser &parser, OperationState &result) {
182190 // First try to parse attribute dictionary.
183191 if (parser.parseOptionalAttrDict (result.attributes ))
184192 return failure ();
185- // If there is no attribute dictionary, the parse above succeeds parsing
186- // nothing. We detect this case by the absence of result attributes.
187- if (result.attributes .empty ()) {
193+ // If value/sparse_value were not in the attributes, parse them from their
194+ // pretty form.
195+ if (!result.attributes .get (" value" ) &&
196+ !result.attributes .get (" sparse_value" )) {
188197 // Try to parse a SparseElementsAttr or or other ElementsAttr.
189198 OptionalParseResult opt = parser.parseOptionalAttribute (attr, type);
190199 if (opt.has_value ()) {
0 commit comments