ml_switcheroo.core.mlir.naming¶
Naming Context for MLIR Generator.
This module provides the NamingContext class, which manages variable name resolution during MLIR-to-Python translation. It handles: - Mapping SSA IDs (e.g. %0) to valid Python identifiers (e.g. _0). - Avoiding reserved Python keywords. - Handling hint-based naming (e.g. %flat -> _flat). - Collision resolution for duplicate names in the same scope.
Classes¶
Tracks mapping between MLIR SSA IDs and Python variable names. |
Module Contents¶
- class ml_switcheroo.core.mlir.naming.NamingContext[source]¶
Tracks mapping between MLIR SSA IDs and Python variable names. Ensures generated names are valid identifiers and do not collide.
- register(ssa_name: str, hint: str | None = None) str[source]¶
Assigns a valid Python name to an SSA value.
Naming Strategy:
If hint provided: Use hint (cleaned).
If SSA ID (e.g. %res): Use prefix + ID body (e.g. _res).
Heuristic: Strip trailing numeric counters from SSA hints if base is unique (e.g., %self0 -> self, %x5 -> x).
- Parameters:
ssa_name – The MLIR variable name (e.g. “%0”, “%arg0”).
hint – Optional string to guide naming (e.g. “flatten” from torch.flatten).
- Returns:
The resolved Python identifier string.
- Return type:
str