Skip to content

Feature request: support Protocol-based ir.Data interface #625

@weinbe58

Description

@weinbe58

Summary

Allow compiler attribute types to satisfy the ir.Data interface via structural typing (Python Protocol) instead of requiring inheritance from a concrete base class.

Motivation

In bloqade-lanes, we have Rust-backed types (via PyO3) that carry device-level data (addresses, lane descriptors, etc.) used as IR attributes. These types need to satisfy Kirin's ir.Data interface to participate in the compiler pipeline.

Since PyO3 #[pyclass] types cannot inherit from Python-defined base classes (a known PyO3 limitation), we currently maintain a separate layer of pure-Python wrapper classes whose sole purpose is to:

  1. Inherit from the Kirin ir.Data / Encoder base class
  2. Delegate all methods to the underlying Rust object via self._inner

This creates a three-layer type stack (Rust → PyO3 binding → Python wrapper) that adds code complexity, maintenance burden, and conversion overhead at every IR boundary.

If ir.Data were defined as a Protocol (PEP 544), any type that implements the required methods would automatically satisfy the interface — no inheritance needed. Rust-backed types could participate directly in the IR without wrappers.

Proposed change

Define the ir.Data interface as a typing.Protocol (or provide a Protocol alternative alongside the current base class for backward compatibility):

from typing import Protocol, runtime_checkable

@runtime_checkable
class Data(Protocol):
    # whatever methods/properties ir.Data requires
    ...

This would allow any type — including PyO3 extension types — to satisfy the interface through structural subtyping rather than nominal inheritance.

Backward compatibility

A Protocol-based approach could coexist with the current base class:

  • Existing code that inherits from ir.Data would continue to work
  • Type checks using isinstance() would work if the Protocol is @runtime_checkable
  • New types (especially FFI types) could satisfy the interface without inheritance

Context

  • PyO3 limitation: #991 — Rust #[pyclass] cannot inherit from Python classes
  • bloqade-lanes uses Kirin for its IR framework and has ~10 address/descriptor types that need the ir.Data interface

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: This is a feature request, i.e: not implemented / a PRarea: IRArea: issues related to the IR module.category: enhancementCategory: this is an enhancement of an existing feature.category: featureCategory: new feature or feature request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions