Skip to content

[WIP] core: Enable assembly_format on types and attrs#5907

Open
n-io wants to merge 9 commits into
mainfrom
nicolai/attr-asm-fmt-pt8-ref-v2
Open

[WIP] core: Enable assembly_format on types and attrs#5907
n-io wants to merge 9 commits into
mainfrom
nicolai/attr-asm-fmt-pt8-ref-v2

Conversation

@n-io

@n-io n-io commented Apr 21, 2026

Copy link
Copy Markdown
Collaborator

Work-in-progress PR for enabling full support for assembly_format on attributes and types in xDSL.

Declarative assembly_format exists for operations, but currently not for types and attributes. The implementation of existing format directives is tightly wired to operations, so the proposal is instead to have a separate hierararchy based on AttrFormatDirective to keep the systems separate which helped keep the implementation clean. The AttrFormatProgram and AttrFormatParser resemble what is implemented for operations.

Note:
An alternative implementation I have considered was using multiple-inheritance, for instance, class WhitespaceDirective(FormatDirective, AttrFormatDirective). However, this runs into the problem that on the op-side, Directive uses IRDLOperation and FormatDirective uses ParsingState. Conversely, there appears to be very little overhead in having two separate WhitespaceDirective(FormatDirective) and AttrWhitespaceDirective(AttrFormatDirective) classes with shared code between these two, which appears to lead to an overall more elegant and less invasive design.

This is the [WIP] PR for enabling full support for attribute/type assembly_format:

n-io added 8 commits April 21, 2026 12:29
Add the foundational classes for a declarative assembly format system
for ParametrizedAttribute types, mirroring the existing op-side system:
AttrFormatDirective ABC, structural directives (whitespace, punctuation,
keyword), ParameterVariable, AttrFormatProgram, and AttrFormatParser.
…nition

Integrate the attr declarative assembly format into the attribute
definition system. ParametrizedAttribute subclasses can now set
assembly_format = "..." to auto-generate parse_parameters and
print_parameters methods.
Add AttrOptionalGroupDirective and parse_optional_group to support
optional groups in attribute/type declarative assembly formats,
enabling conditional parameter parsing with else branches.
Add QualifiedParameterVariable (no-op in xdsl since print_attribute
always qualifies) and parse_qualified_directive for MLIR format
compatibility.
Add ParamsDirective that captures all parameters of an attribute and
prints them comma-separated. Includes _all_param_variables helper in
AttrFormatParser, with tests for basic, three-param, and optional cases.
Add StructDirective that prints parameters as key=value pairs and
parses them in any order. Supports struct(params) for all parameters
and struct($a, $b) for a subset.
Add AttrCustomDirective ABC and irdl_attr_custom_directive decorator
for user-defined assembly format directives in attribute/type formats,
mirroring the existing CustomDirective system for operations.
Add parse_possible_ref_directive to support ref($var) inside custom
directive arguments, allowing a parameter to be referenced without
re-binding it.
@n-io n-io self-assigned this Apr 21, 2026
@n-io n-io added the core xDSL core (ir, textual format, ...) label Apr 21, 2026
@n-io n-io marked this pull request as ready for review April 21, 2026 12:43
@codecov

codecov Bot commented Apr 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.22%. Comparing base (c9c895c) to head (70dbed0).

Additional details and impacted files
@@                          Coverage Diff                           @@
##           nicolai/attr-asm-fmt-pt7-custom-v2    #5907      +/-   ##
======================================================================
- Coverage                               86.22%   86.22%   -0.01%     
======================================================================
  Files                                     418      418              
  Lines                                   60297    60302       +5     
  Branches                                 7010     7011       +1     
======================================================================
+ Hits                                    51990    51994       +4     
- Misses                                   6695     6696       +1     
  Partials                                 1612     1612              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

params.append(param)
return directive(*params)

def parse_possible_ref_directive(self) -> AttrFormatDirective:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what does "possible" mean here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This mirrors the op-side parser's parse_possible_ref_directive function.

@n-io n-io changed the base branch from nicolai/attr-asm-fmt-pt7-custom-v2 to main April 29, 2026 13:56
@n-io n-io changed the title core: (attr assembly fmt) add ref directive (pt8) [WIP] core: Enable assembly_format on types and attrs Apr 29, 2026
@ed741

ed741 commented May 14, 2026

Copy link
Copy Markdown
Collaborator

Just wanted to add, for the sake of encouragement, that I'm excited to have this feature!

The op-side WhitespaceDirective, PunctuationDirective and KeywordDirective
now delegate their print logic to the shared _print_whitespace /
_print_punctuation / _print_keyword helpers instead of duplicating it,
matching how the attr-side directives already use them.
n-io added a commit that referenced this pull request Jul 9, 2026
Adding foundational classes for a declarative assembly format system,
mirroring the implementations for op assembly format.

The existing format directives are tightly wired to operations, so this
PR proposes instead to have a separate hierarchy based on
AttrFormatDirective to keep the systems separate which helped keep the
implementation clean. This first PR contains only the plumbing — the
`AttrFormatDirective` base class, `AttrFormatProgram`, and an
`AttrFormatParser` skeleton — and currently supports only the empty
format; concrete directives follow in later PRs. The `AttrFormatProgram`
and `AttrFormatParser` resemble what is implemented for operations.

This PR is the first in a stacked series to enable full support for
attribute/type assembly_format. See also:
* #5907
n-io added a commit that referenced this pull request Jul 13, 2026
Adds `AttrWhitespaceDirective` to declarative assembly format for
attributes.

WIP:
* #5907
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core xDSL core (ir, textual format, ...)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants