Skip to content

[mlir] Add helper method to print and parse cyclic attributes and types #65210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2023

Conversation

zero9178
Copy link
Member

@zero9178 zero9178 commented Sep 2, 2023

Printing cyclic attributes and types currently has no first-class support within the AsmPrinter and AsmParser. The workaround for this issue used in all mutable attributes and types upstream has been to create a thread_local static SetVector keeping track of currently parsed and printed attributes.

This solution is not ideal readability wise due to the use of globals and keeping track of state. Worst of all, this pattern had to be reimplemented for every mutable attribute and type.

This patch therefore adds support for this pattern in AsmPrinter and AsmParser replacing the use of this pattern. By calling tryStartCyclingPrint/Parse, the mutable attribute or type are registered in an internal stack. All subsequent calls to the function with the same attribute or type will lead to returning failure. This way the nesting can be detected and a short form printed or parsed instead.
Through the resetter returned by the call, the cyclic printing or parsing region automatically ends on return.

@zero9178 zero9178 requested review from a team as code owners September 2, 2023 15:39
@mshockwave mshockwave self-requested a review September 2, 2023 18:35
Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but probably worth waiting for @ftynse to have a look as well

[loc] { return emitError(loc); }, loc.getContext(), name);
if (succeeded(parser.tryStartCyclicParse(type)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: braces here plz. Having a separate return nullptr line could also improve readability.

Printing cyclic attributes and types currently has no first-class support within the AsmPrinter and AsmParser. The workaround for this issue used in all mutable attributes and types upstream has been to create a `thread_local static SetVector` keeping track of currently parsed and printed attributes.

This solution is not ideal readability wise due to the use of globals and keeping track of state. Worst of all, this pattern had to be reimplemented for every mutable attribute and type.

This patch therefore adds support for this pattern in `AsmPrinter` and `AsmParser` replacing the use of this pattern.
By calling `tryStartCyclingPrint/Parse`, the mutable attribute or type are registered in an internal stack. All subsequent calls to the function with the same attribute or type will lead to returning failure. This way the nesting can be detected and a short form printed or parsed instead.

Through the resetter returned by the call, the cyclic printing or parsing region automatically ends on return.
@zero9178 zero9178 force-pushed the cyclic-mutable-print-parse branch from 071c622 to 44f1f72 Compare September 4, 2023 16:10
@zero9178 zero9178 merged commit b121c26 into llvm:main Sep 4, 2023
@zero9178 zero9178 deleted the cyclic-mutable-print-parse branch September 4, 2023 16:19
avillega pushed a commit to avillega/llvm-project that referenced this pull request Sep 11, 2023
…es (llvm#65210)

Printing cyclic attributes and types currently has no first-class
support within the AsmPrinter and AsmParser. The workaround for this
issue used in all mutable attributes and types upstream has been to
create a `thread_local static SetVector` keeping track of currently
parsed and printed attributes.

This solution is not ideal readability wise due to the use of globals
and keeping track of state. Worst of all, this pattern had to be
reimplemented for every mutable attribute and type.

This patch therefore adds support for this pattern in `AsmPrinter` and
`AsmParser` replacing the use of this pattern. By calling
`tryStartCyclingPrint/Parse`, the mutable attribute or type are
registered in an internal stack. All subsequent calls to the function
with the same attribute or type will lead to returning failure. This way
the nesting can be detected and a short form printed or parsed instead.
Through the resetter returned by the call, the cyclic printing or
parsing region automatically ends on return.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants