Skip to content

Fold typed-array attribute constraints into static definition emission #129

Description

@jonathanvdc

Goal

Typed-array attribute constraints should no longer have their own attribute-constraint emission kind or per-constraint generated assembly-format shim. Treat them like the other storage-backed attribute constraints: emit a static AttributeConstraintDefinition that points at a reusable runtime assembly format, and rely on the existing global::MLIR.ArrayAttr storage overlays for typed operation properties.

Background

src/MLIR.Generators/Prelude/Extensions/mlir/IR/CommonAttrConstraintsExtensions.td already describes TypedArrayAttrBase-style constraints as global::MLIR.ArrayAttr storage. The specific array constraints refine the public property type and storage conversion through ArrayAttrConstraintHelpers, for example:

  • csharpStorageType = "global::MLIR.ArrayAttr"
  • csharpReturnType = "global::System.Collections.Generic.IReadOnlyList<...>"
  • csharpConvertFromStorage = "global::MLIR.Semantics.Attributes.Collections.ArrayAttrConstraintHelpers.GetItems(...)"
  • csharpConstBuilderCall = "global::MLIR.Semantics.Attributes.Collections.ArrayAttrConstraintHelpers.Create(...)"

So the generated typed-array constraint classes are not needed as semantic storage. Today the remaining special case is only a generated assembly-format class like:

internal sealed class I32ArrayAttrConstraintAttributeValueAssemblyFormat
    : TypedArrayAttributeAssemblyFormat<int>
{
}

The TElement parameter is not used by TypedArrayAttributeAssemblyFormat<TElement> for parsing, binding, or printing: it parses an array literal, binds to ArrayAttr, and rebuilds from ArrayAttr.Value.

Proposed Change

  1. Replace TypedArrayAttributeAssemblyFormat<TElement> with a non-generic reusable runtime format.

    Suggested shape:

    public sealed class TypedArrayAttributeAssemblyFormat : IAttributeAssemblyFormat
    {
        // same parse/bind/print behavior as the current generic base
    }

    Keep the existing behavior:

    • parse [ ... ] into ArrayAttributeValueSyntax
    • bind with ArrayAttrConstraintHelpers.BindFromSyntax(...)
    • print from reusable ArrayAttributeValueSyntax when available
    • otherwise print from ArrayAttr.Value
  2. Make TypedArrayConstraintCodeStrategy use the normal static-definition path.

    It should provide an assembly format expression equivalent to:

    new global::MLIR.Dialects.Attributes.Collections.TypedArrayAttributeAssemblyFormat()

    This should make typed-array constraints look like dictionary/type/unit/elements constraints from the emitter's point of view.

  3. Delete the typed-array-specific emission kind and emitter path.

    Remove:

    • AttributeConstraintEmissionKind.TypedArray
    • AttributeConstraintEmitter.EmitTypedArrayConstraint(...)
    • AttributeConstraintEmitter.GetTypedArrayElementTypeName(...)
    • generated per-constraint ...AssemblyFormat : TypedArrayAttributeAssemblyFormat<TElement> classes
  4. Keep operation property typing and setter/getter behavior driven by AttrModel overlays.

    Do not regress the typed public surface for constraints such as:

    • I32ArrayAttr -> IReadOnlyList<int>
    • StrArrayAttr -> IReadOnlyList<string>
    • TypeArrayAttr -> IReadOnlyList<TypeReference>
    • DictArrayAttr -> IReadOnlyList<NamedAttributeCollection>

    These should continue to use ArrayAttrConstraintHelpers.GetItems(...) and ArrayAttrConstraintHelpers.Create(...) from the TableGen extensions.

Acceptance Criteria

  • Typed-array constraints are emitted as ordinary static constraint-definition holders.
  • No per-typed-array generated assembly format classes are emitted.
  • No generated typed-array AttributeValue subclasses or helper Create/GetItems methods are emitted.
  • The runtime has a single reusable typed-array/array attribute assembly format.
  • Existing typed-array operation properties remain strongly typed through the csharpStorageType, csharpReturnType, csharpConvertFromStorage, and csharpConstBuilderCall overlays.
  • Remove dead typed-array emission code from AttributeConstraintEmitter.

Suggested Validation

Run:

dotnet test tests/MLIR.Generators.Tests/MLIR.Generators.Tests.csproj -m:1
dotnet test tests/DialectTests/DialectTests.csproj -m:1
dotnet test tests/MLIR.Tests/MLIR.Tests.csproj -m:1
dotnet build samples/GeneratedDialectConsumer/GeneratedDialectConsumer.csproj -m:1

Strengthen TypedArrayAttrConstraintsAreGeneratedAsConstraintOnlyStaticClasses so it also asserts that typed-array constraints use the reusable runtime assembly format and do not emit per-constraint typed-array assembly-format classes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions