-
Notifications
You must be signed in to change notification settings - Fork 5
Feature request: support Protocol-based ir.Data interface #625
Description
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:
- Inherit from the Kirin
ir.Data/Encoderbase class - 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.Datawould 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.Datainterface