diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h --- a/mlir/include/mlir/IR/OpImplementation.h +++ b/mlir/include/mlir/IR/OpImplementation.h @@ -141,7 +141,16 @@ void printStrippedAttrOrType(AttrOrType attrOrType) { if (succeeded(printAlias(attrOrType))) return; + + raw_ostream &os = getStream(); + uint64_t posPrior = os.tell(); attrOrType.print(*this); + if (posPrior != os.tell()) + return; + + // Fallback to printing with prefix if the above failed to write anything + // to the output stream. + *this << attrOrType; } /// Print the provided array of attributes or types in the context of an diff --git a/mlir/test/IR/print-empty-attr-or-type.mlir b/mlir/test/IR/print-empty-attr-or-type.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/IR/print-empty-attr-or-type.mlir @@ -0,0 +1,9 @@ +// RUN: mlir-opt %s | mlir-opt | FileCheck %s + +func.func @test(%arg0 : !test.optional_value_type, %arg1 : !test.optional_value_type<3>) { + // CHECK: test.format_maybe_empty_type %{{.*}} : !test.optional_value_type + test.format_maybe_empty_type %arg0 : !test.optional_value_type + // CHECK: test.format_maybe_empty_type %{{.*}} : <3> + test.format_maybe_empty_type %arg1 : !test.optional_value_type<3> + return +} diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td --- a/mlir/test/lib/Dialect/Test/TestOps.td +++ b/mlir/test/lib/Dialect/Test/TestOps.td @@ -2294,6 +2294,11 @@ let assemblyFormat = "`nested` $nested attr-dict-with-keyword"; } +def FormatMaybeEmptyType : TEST_Op<"format_maybe_empty_type"> { + let arguments = (ins TestTypeOptionalValueType:$in); + let assemblyFormat = "$in `:` type($in) attr-dict"; +} + def FormatQualifiedCompoundAttr : TEST_Op<"format_qual_cpmd_nested_attr"> { let arguments = (ins CompoundNestedOuter:$nested); let assemblyFormat = "`nested` qualified($nested) attr-dict-with-keyword"; diff --git a/mlir/test/lib/Dialect/Test/TestTypeDefs.td b/mlir/test/lib/Dialect/Test/TestTypeDefs.td --- a/mlir/test/lib/Dialect/Test/TestTypeDefs.td +++ b/mlir/test/lib/Dialect/Test/TestTypeDefs.td @@ -312,6 +312,14 @@ let assemblyFormat = "`<` $a `>`"; } +def TestTypeOptionalValueType : Test_Type<"TestTypeOptionalValueType"> { + let parameters = (ins + OptionalParameter<"std::optional">:$value + ); + let mnemonic = "optional_value_type"; + let assemblyFormat = "(`<` $value^ `>`)?"; +} + def TestTypeDefaultValuedType : Test_Type<"TestTypeDefaultValuedType"> { let parameters = (ins DefaultValuedParameter<"mlir::IntegerType",