[mlir][AsmPrinter] Fallback to using qualified printer if unqualified output is empty
authorMarkus Böck <markus.boeck02@gmail.com>
Mon, 27 Mar 2023 12:13:44 +0000 (14:13 +0200)
committerMarkus Böck <markus.boeck02@gmail.com>
Mon, 27 Mar 2023 13:48:48 +0000 (15:48 +0200)
commit70074c113432e4065724b5aba79f610d9d7281de
tree5a7f3847235f5cdd84795e0fb6268ce9e0d3a2b2
parentb26970ec4b79590f41f5e1dd7cb56d5f8d4d9301
[mlir][AsmPrinter] Fallback to using qualified printer if unqualified output is empty

The current behaviour of always writing the unqualified form of an attribute or type is problematic for any type or attribute that might output an empty string, making it impossible to parse and therefore roundtrip. This is commonly the case for any types or attributes with optional parameters. One would have to currently woarkaround the issue by either changing ones syntax to not be completetly empty or by explicitly using `qualified` in ALL ops using that type or attribute.

This patch fixes that issue by simply checking whether anything was written to the output. In the case there wasn't, it simply falls back to using the normal printer with the dialect prefix. This also makes the default of unqualified printing always correct and safe. The implementation could theoretically still be tricked if the user were to print just a space or similar. I'd argue this'd be user error and not worth handling.

Fixes https://github.com/llvm/llvm-project/issues/61701

Differential Revision: https://reviews.llvm.org/D146944
mlir/include/mlir/IR/OpImplementation.h
mlir/test/IR/print-empty-attr-or-type.mlir [new file with mode: 0644]
mlir/test/lib/Dialect/Test/TestOps.td
mlir/test/lib/Dialect/Test/TestTypeDefs.td