Change pretty printing of constant so that the attributes precede the value.

This does create an inconsistency between the print formats (e.g., attributes are normally before operands) but fixes an invalid parsing & keeps constant uniform wrt itself (function or int attributes have type at same place). And specifying the specific type for a int/float attribute might get revised shortly.

Also add test to verify that output printed can be parsed again.

PiperOrigin-RevId: 221923893
This commit is contained in:
Jacques Pienaar 2018-11-17 08:24:07 -08:00 committed by jpienaar
parent fff1efbaf5
commit 64c6d3946c
2 changed files with 11 additions and 6 deletions

View File

@ -314,9 +314,12 @@ void ConstantOp::build(Builder *builder, OperationState *result,
}
void ConstantOp::print(OpAsmPrinter *p) const {
*p << "constant " << getValue();
*p << "constant ";
p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"value");
if (getAttrs().size() > 1)
*p << ' ';
*p << getValue();
if (!getValue().isa<FunctionAttr>())
*p << " : " << getType();
}
@ -325,8 +328,8 @@ bool ConstantOp::parse(OpAsmParser *parser, OperationState *result) {
Attribute valueAttr;
Type type;
if (parser->parseAttribute(valueAttr, "value", result->attributes) ||
parser->parseOptionalAttributeDict(result->attributes))
if (parser->parseOptionalAttributeDict(result->attributes) ||
parser->parseAttribute(valueAttr, "value", result->attributes))
return true;
// 'constant' taking a function reference doesn't get a redundant type

View File

@ -1,4 +1,6 @@
// RUN: mlir-opt %s | FileCheck %s
// Verify the printed output can be parsed.
// RUN: mlir-opt %s | mlir-opt | FileCheck %s
// CHECK: #map0 = (d0) -> (d0 + 1)
@ -71,13 +73,13 @@ bb42(%t: tensor<4x4x?xf32>, %f: f32, %i: i32, %idx : index):
%i6 = muli %i2, %i2 : i32
// CHECK: %c42_i32 = constant 42 : i32
%x = "constant"(){value: 42: i32} : () -> i32
%x = "constant"(){value: 42 : i32} : () -> i32
// CHECK: %c42_i32_0 = constant 42 : i32
%7 = constant 42 : i32
// CHECK: %c43 = constant 43 {crazy: "foo"} : index
%8 = constant 43: index {crazy: "foo"}
// CHECK: %c43 = constant {crazy: "foo"} 43 : index
%8 = constant {crazy: "foo"} 43: index
// CHECK: %cst = constant 4.300000e+01 : bf16
%9 = constant 43.0 : bf16