Skip to content

Ptr

ptr

RawPtr dataclass

Data structure to help simulate pointers into memory.

Source code in xdsl/interpreters/utils/ptr.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@dataclass
class RawPtr:
    """
    Data structure to help simulate pointers into memory.
    """

    memory: bytearray
    offset: int = field(default=0)

    @property
    def memoryview(self) -> memoryview:
        return memoryview(self.memory)[self.offset :]

    def copy(self) -> RawPtr:
        return RawPtr(bytearray(self.memory), self.offset)

    @staticmethod
    def zeros(count: int) -> RawPtr:
        """
        Returns a new Ptr of size `count` with offset 0.
        """
        return RawPtr(bytearray(count))

    def __add__(self, offset: int) -> RawPtr:
        """
        Aliases the data, so storing into the offset stores for all other references
        to the list.
        """
        return RawPtr(self.memory, self.offset + offset)

    @property
    def int32(self) -> TypedPtr[int]:
        return TypedPtr(self, xtype=i32)

    @property
    def int64(self) -> TypedPtr[int]:
        return TypedPtr(self, xtype=i64)

    def index(self, index_bitwidth: int) -> TypedPtr[int]:
        if index_bitwidth != 32 and index_bitwidth != 64:
            raise ValueError(
                f"Invalid index bitwidth {index_bitwidth} monly 32 or 64 allowed"
            )
        return TypedPtr(self, xtype=index(index_bitwidth))

    @property
    def float32(self) -> TypedPtr[float]:
        return TypedPtr(self, xtype=f32)

    @property
    def float64(self) -> TypedPtr[float]:
        return TypedPtr(self, xtype=f64)

memory: bytearray instance-attribute

offset: int = field(default=0) class-attribute instance-attribute

memoryview: memoryview property

int32: TypedPtr[int] property

int64: TypedPtr[int] property

float32: TypedPtr[float] property

float64: TypedPtr[float] property

__init__(memory: bytearray, offset: int = 0) -> None

copy() -> RawPtr

Source code in xdsl/interpreters/utils/ptr.py
34
35
def copy(self) -> RawPtr:
    return RawPtr(bytearray(self.memory), self.offset)

zeros(count: int) -> RawPtr staticmethod

Returns a new Ptr of size count with offset 0.

Source code in xdsl/interpreters/utils/ptr.py
37
38
39
40
41
42
@staticmethod
def zeros(count: int) -> RawPtr:
    """
    Returns a new Ptr of size `count` with offset 0.
    """
    return RawPtr(bytearray(count))

__add__(offset: int) -> RawPtr

Aliases the data, so storing into the offset stores for all other references to the list.

Source code in xdsl/interpreters/utils/ptr.py
44
45
46
47
48
49
def __add__(self, offset: int) -> RawPtr:
    """
    Aliases the data, so storing into the offset stores for all other references
    to the list.
    """
    return RawPtr(self.memory, self.offset + offset)

index(index_bitwidth: int) -> TypedPtr[int]

Source code in xdsl/interpreters/utils/ptr.py
59
60
61
62
63
64
def index(self, index_bitwidth: int) -> TypedPtr[int]:
    if index_bitwidth != 32 and index_bitwidth != 64:
        raise ValueError(
            f"Invalid index bitwidth {index_bitwidth} monly 32 or 64 allowed"
        )
    return TypedPtr(self, xtype=index(index_bitwidth))

TypedPtr dataclass

Bases: Generic[_T]

A typed pointer into memory, similar to numpy's ndarray, but without the shape.

Source code in xdsl/interpreters/utils/ptr.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
@dataclass
class TypedPtr(Generic[_T]):
    """
    A typed pointer into memory, similar to numpy's ndarray, but without the shape.
    """

    raw: RawPtr
    _: KW_ONLY
    xtype: PackableType[_T]

    @property
    def size(self) -> int:
        return self.xtype.compile_time_size

    def copy(self) -> Self:
        return type(self)(self.raw.copy(), xtype=self.xtype)

    def get_iter(self) -> Iterator[_T]:
        # The memoryview needs to be a multiple of the size of the packed format
        format_size = self.size
        mem_view = self.raw.memoryview
        remainder = len(mem_view) % format_size
        if remainder:
            mem_view = mem_view[:-remainder]
        return self.xtype.iter_unpack(mem_view)

    def get_list(self, count: int) -> list[_T]:
        return list(itertools.islice(self.get_iter(), count))

    def __getitem__(self, index: int) -> _T:
        raw_at_offset = self.raw + index * self.size
        return self.xtype.unpack(raw_at_offset.memoryview[: self.size], 1)[0]

    def __setitem__(self, index: int, value: _T):
        raw_at_offset = self.raw + index * self.size
        self.xtype.pack_into(raw_at_offset.memory, raw_at_offset.offset, value)

    @staticmethod
    def zeros(count: int, *, xtype: PackableType[_T]) -> TypedPtr[_T]:
        size = xtype.compile_time_size
        return TypedPtr(RawPtr.zeros(size * count), xtype=xtype)

    @staticmethod
    def new(els: Sequence[_T], *, xtype: PackableType[_T]) -> TypedPtr[_T]:
        """
        Returns a new TypedPtr with the specified els packed into memory.
        """
        el_size = xtype.compile_time_size
        res = RawPtr.zeros(len(els) * el_size)
        for i, el in enumerate(els):
            xtype.pack_into(res.memory, i * el_size, el)
        return TypedPtr(res, xtype=xtype)

    @staticmethod
    def new_float32(els: Sequence[float]) -> TypedPtr[float]:
        return TypedPtr[float].new(els, xtype=f32)

    @staticmethod
    def new_float64(els: Sequence[float]) -> TypedPtr[float]:
        return TypedPtr[float].new(els, xtype=f64)

    @staticmethod
    def new_int32(els: Sequence[int]) -> TypedPtr[int]:
        return TypedPtr[int].new(els, xtype=i32)

    @staticmethod
    def new_int64(els: Sequence[int]) -> TypedPtr[int]:
        return TypedPtr[int].new(els, xtype=i64)

    @staticmethod
    def new_index(els: Sequence[int], index_bitwidth: int) -> TypedPtr[int]:
        if index_bitwidth != 32 and index_bitwidth != 64:
            raise ValueError(
                f"Invalid index bitwidth {index_bitwidth} monly 32 or 64 allowed"
            )
        return TypedPtr[int].new(els, xtype=index(index_bitwidth))

raw: RawPtr instance-attribute

_: KW_ONLY instance-attribute

xtype: PackableType[_T] instance-attribute

size: int property

__init__(raw: RawPtr, *, xtype: PackableType[_T]) -> None

copy() -> Self

Source code in xdsl/interpreters/utils/ptr.py
93
94
def copy(self) -> Self:
    return type(self)(self.raw.copy(), xtype=self.xtype)

get_iter() -> Iterator[_T]

Source code in xdsl/interpreters/utils/ptr.py
 96
 97
 98
 99
100
101
102
103
def get_iter(self) -> Iterator[_T]:
    # The memoryview needs to be a multiple of the size of the packed format
    format_size = self.size
    mem_view = self.raw.memoryview
    remainder = len(mem_view) % format_size
    if remainder:
        mem_view = mem_view[:-remainder]
    return self.xtype.iter_unpack(mem_view)

get_list(count: int) -> list[_T]

Source code in xdsl/interpreters/utils/ptr.py
105
106
def get_list(self, count: int) -> list[_T]:
    return list(itertools.islice(self.get_iter(), count))

__getitem__(index: int) -> _T

Source code in xdsl/interpreters/utils/ptr.py
108
109
110
def __getitem__(self, index: int) -> _T:
    raw_at_offset = self.raw + index * self.size
    return self.xtype.unpack(raw_at_offset.memoryview[: self.size], 1)[0]

__setitem__(index: int, value: _T)

Source code in xdsl/interpreters/utils/ptr.py
112
113
114
def __setitem__(self, index: int, value: _T):
    raw_at_offset = self.raw + index * self.size
    self.xtype.pack_into(raw_at_offset.memory, raw_at_offset.offset, value)

zeros(count: int, *, xtype: PackableType[_T]) -> TypedPtr[_T] staticmethod

Source code in xdsl/interpreters/utils/ptr.py
116
117
118
119
@staticmethod
def zeros(count: int, *, xtype: PackableType[_T]) -> TypedPtr[_T]:
    size = xtype.compile_time_size
    return TypedPtr(RawPtr.zeros(size * count), xtype=xtype)

new(els: Sequence[_T], *, xtype: PackableType[_T]) -> TypedPtr[_T] staticmethod

Returns a new TypedPtr with the specified els packed into memory.

Source code in xdsl/interpreters/utils/ptr.py
121
122
123
124
125
126
127
128
129
130
@staticmethod
def new(els: Sequence[_T], *, xtype: PackableType[_T]) -> TypedPtr[_T]:
    """
    Returns a new TypedPtr with the specified els packed into memory.
    """
    el_size = xtype.compile_time_size
    res = RawPtr.zeros(len(els) * el_size)
    for i, el in enumerate(els):
        xtype.pack_into(res.memory, i * el_size, el)
    return TypedPtr(res, xtype=xtype)

new_float32(els: Sequence[float]) -> TypedPtr[float] staticmethod

Source code in xdsl/interpreters/utils/ptr.py
132
133
134
@staticmethod
def new_float32(els: Sequence[float]) -> TypedPtr[float]:
    return TypedPtr[float].new(els, xtype=f32)

new_float64(els: Sequence[float]) -> TypedPtr[float] staticmethod

Source code in xdsl/interpreters/utils/ptr.py
136
137
138
@staticmethod
def new_float64(els: Sequence[float]) -> TypedPtr[float]:
    return TypedPtr[float].new(els, xtype=f64)

new_int32(els: Sequence[int]) -> TypedPtr[int] staticmethod

Source code in xdsl/interpreters/utils/ptr.py
140
141
142
@staticmethod
def new_int32(els: Sequence[int]) -> TypedPtr[int]:
    return TypedPtr[int].new(els, xtype=i32)

new_int64(els: Sequence[int]) -> TypedPtr[int] staticmethod

Source code in xdsl/interpreters/utils/ptr.py
144
145
146
@staticmethod
def new_int64(els: Sequence[int]) -> TypedPtr[int]:
    return TypedPtr[int].new(els, xtype=i64)

new_index(els: Sequence[int], index_bitwidth: int) -> TypedPtr[int] staticmethod

Source code in xdsl/interpreters/utils/ptr.py
148
149
150
151
152
153
154
@staticmethod
def new_index(els: Sequence[int], index_bitwidth: int) -> TypedPtr[int]:
    if index_bitwidth != 32 and index_bitwidth != 64:
        raise ValueError(
            f"Invalid index bitwidth {index_bitwidth} monly 32 or 64 allowed"
        )
    return TypedPtr[int].new(els, xtype=index(index_bitwidth))

index(bitwidth: Literal[32, 64]) -> PackableType[int]

Source code in xdsl/interpreters/utils/ptr.py
75
76
def index(bitwidth: Literal[32, 64]) -> PackableType[int]:
    return i32 if bitwidth == 32 else i64