[mlir] Printing types in dialect attributes

I can write a custom dialect attribute printer for the following:

#foo.bar<0> : i32

The operation parser will look ahead and optionally parse the colon-type. But if my dialect attribute printer is just

printer << '<' << getValue() << '>';

The output is

#foo.bar<0>

I have to manually print the type, but there’s no way to print it the same way it’s parsed, because

printer << '<' << getValue() << "> : " << getType();

Results in

#foo<"bar<0> : i32">

Is this an oversight or an intentional design decision? The dialect attribute parser supports parsing ahead to get an optional type, but the printer does not.

The attribute isn’t in charge of printing the type, the framework is. The AsmPrinter code looks buggy:
https://github.com/llvm/llvm-project/blob/cb4a5eae1eb3fb8be8c0cf73aa9b1e6f9279eda6/mlir/lib/IR/AsmPrinter.cpp#L1761

We shouldn’t be returning early there, because it skips type handling.

1 Like

OK, that’s what I thought. Thanks for clarifying. I’ll fix that up in my patch for https://github.com/llvm/llvm-project/issues/55324