Skip to content

IdTable

IdTable dataclass

IdTable(
    prefix: str = "%",
    table: dict[T, str] = dict(),
    name_count: dict[str, int] = dict(),
    next_id: int = 0,
    prefix_if_none: str = "",
)

Bases: Generic[T]

A table that maps values to "human readable" unique names. This is used for IR printing and code generation of SSA values and basic blocks, or anything else required to have a unique name.

Example

from kirin import ir
from kirin.idtable import IdTable
table = IdTable()
x = ir.TestValue()
table[x] # "%0"
table[x] # "%0"
y = ir.TestValue()
table[y] # "%1"

name_count class-attribute instance-attribute

name_count: dict[str, int] = field(default_factory=dict)

The count of names that have been generated.

next_id class-attribute instance-attribute

next_id: int = 0

The next ID to use for generating names.

prefix class-attribute instance-attribute

prefix: str = '%'

The prefix to use for generated names.

prefix_if_none class-attribute instance-attribute

prefix_if_none: str = ''

An alternate prefix to use when the name is None.

table class-attribute instance-attribute

table: dict[T, str] = field(default_factory=dict)

The table that maps values to names.

add

add(value: T) -> str

Add a value to the table and return the name.

Source code in src/kirin/idtable.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def add(self, value: T) -> str:
    """Add a value to the table and return the name."""
    id = self.next_id
    if (value_name := getattr(value, "name", None)) is not None:
        curr_ind = self.name_count.get(value_name, 0)
        suffix = f"_{curr_ind}" if curr_ind != 0 else ""
        self.name_count[value_name] = curr_ind + 1
        name = self.prefix + value_name + suffix
        self.table[value] = name
    else:
        name = f"{self.prefix}{self.prefix_if_none}{id}"
        self.next_id += 1
        self.table[value] = name
    return name