From 68c8f1cd75848b184e5141caff3dc2dbd1e87f5d Mon Sep 17 00:00:00 2001 From: Joren Dumoulin Date: Mon, 9 Dec 2024 09:28:40 +0100 Subject: [PATCH 1/7] indextype is packable wip indextype wip --- xdsl/dialects/builtin.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/xdsl/dialects/builtin.py b/xdsl/dialects/builtin.py index 15fe476ec0..00f96a5792 100644 --- a/xdsl/dialects/builtin.py +++ b/xdsl/dialects/builtin.py @@ -556,7 +556,7 @@ class LocationAttr(ParametrizedAttribute): @irdl_attr_definition -class IndexType(ParametrizedAttribute): +class IndexType(ParametrizedAttribute, StructPackableType[int]): name = "index" def print_value_without_type(self, value: int, printer: Printer): @@ -565,6 +565,13 @@ def print_value_without_type(self, value: int, printer: Printer): """ printer.print_string(f"{value}") + @property + def format(self) -> str: + return " int: + raise RuntimeError("IndexType bitwidth not known at compile-time") IndexTypeConstr = BaseAttr(IndexType) From 8adbdc463d9ad0df99952c29b9fefacf0b2315cd Mon Sep 17 00:00:00 2001 From: Joren Dumoulin Date: Mon, 9 Dec 2024 09:58:57 +0100 Subject: [PATCH 2/7] add tests --- tests/dialects/test_builtin.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/dialects/test_builtin.py b/tests/dialects/test_builtin.py index cc8f1af224..ff7794d65f 100644 --- a/tests/dialects/test_builtin.py +++ b/tests/dialects/test_builtin.py @@ -19,6 +19,7 @@ Float80Type, Float128Type, FloatAttr, + IndexType, IntAttr, IntegerAttr, IntegerType, @@ -77,6 +78,12 @@ def test_IntegerType_formats(): assert IntegerType(32).format == " Date: Mon, 9 Dec 2024 09:59:05 +0100 Subject: [PATCH 3/7] formatting --- tests/dialects/test_builtin.py | 3 +++ xdsl/dialects/builtin.py | 1 + 2 files changed, 4 insertions(+) diff --git a/tests/dialects/test_builtin.py b/tests/dialects/test_builtin.py index ff7794d65f..7fcb1364eb 100644 --- a/tests/dialects/test_builtin.py +++ b/tests/dialects/test_builtin.py @@ -78,13 +78,16 @@ def test_IntegerType_formats(): assert IntegerType(32).format == " str: def bitwidth(self) -> int: raise RuntimeError("IndexType bitwidth not known at compile-time") + IndexTypeConstr = BaseAttr(IndexType) _IntegerAttrType = TypeVar( From 234d88530390b281aa02e3cfeed356c7d6acf1e0 Mon Sep 17 00:00:00 2001 From: Joren Dumoulin Date: Mon, 9 Dec 2024 15:50:33 +0100 Subject: [PATCH 4/7] change class hierarchy --- xdsl/dialects/builtin.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/xdsl/dialects/builtin.py b/xdsl/dialects/builtin.py index eb149d4c75..15a46d1eca 100644 --- a/xdsl/dialects/builtin.py +++ b/xdsl/dialects/builtin.py @@ -349,7 +349,15 @@ def print_parameter(self, printer: Printer) -> None: raise ValueError(f"Invalid signedness {data}") -class FixedBitwidthType(TypeAttribute, ABC): +class CompileTimeFixedBitwidthType(TypeAttribute, ABC): + """ + A type attribute whose runtime bitwidth is fixed, but may be target-dependent. + """ + + name = "abstract.compile_time_fixed_bitwidth_type" + + +class FixedBitwidthType(CompileTimeFixedBitwidthType, ABC): """ A type attribute whose runtime bitwidth is target-independent. """ @@ -375,7 +383,7 @@ def size(self) -> int: _PyT = TypeVar("_PyT") -class PackableType(Generic[_PyT], FixedBitwidthType, ABC): +class PackableType(Generic[_PyT], CompileTimeFixedBitwidthType, ABC): """ Abstract base class for xDSL types whose values can be encoded and decoded as bytes. """ @@ -441,7 +449,7 @@ def pack(self, values: Sequence[_PyT]) -> bytes: @irdl_attr_definition -class IntegerType(ParametrizedAttribute, StructPackableType[int]): +class IntegerType(ParametrizedAttribute, StructPackableType[int], FixedBitwidthType): name = "integer_type" width: ParameterDef[IntAttr] signedness: ParameterDef[SignednessAttr] @@ -567,6 +575,7 @@ def print_value_without_type(self, value: int, printer: Printer): @property def format(self) -> str: + # index types are always packable as int64 return " int: From 52cb5930d125f200fb1f3e324f4e0b3862c4f6ed Mon Sep 17 00:00:00 2001 From: Joren Dumoulin Date: Mon, 9 Dec 2024 16:14:32 +0100 Subject: [PATCH 5/7] fix ptr problems --- xdsl/dialects/builtin.py | 12 ++++++++++++ xdsl/interpreters/utils/ptr.py | 6 +++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/xdsl/dialects/builtin.py b/xdsl/dialects/builtin.py index 15a46d1eca..4359fd19db 100644 --- a/xdsl/dialects/builtin.py +++ b/xdsl/dialects/builtin.py @@ -416,6 +416,14 @@ def pack(self, values: Sequence[_PyT]) -> bytes: """ raise NotImplementedError() + @property + @abstractmethod + def packed_size(self) -> int: + """ + Contiguous memory footprint of packed value in bytes. + """ + raise NotImplementedError() + class StructPackableType(Generic[_PyT], PackableType[_PyT], ABC): """ @@ -447,6 +455,10 @@ def pack(self, values: Sequence[_PyT]) -> bytes: fmt = self.format[0] + str(len(values)) + self.format[1:] return struct.pack(fmt, *values) + @property + def packed_size(self) -> int: + return struct.calcsize(self.format) + @irdl_attr_definition class IntegerType(ParametrizedAttribute, StructPackableType[int], FixedBitwidthType): diff --git a/xdsl/interpreters/utils/ptr.py b/xdsl/interpreters/utils/ptr.py index c5fb5c5d59..eb102c02b4 100644 --- a/xdsl/interpreters/utils/ptr.py +++ b/xdsl/interpreters/utils/ptr.py @@ -95,7 +95,7 @@ class TypedPtr(Generic[_T]): @property def size(self) -> int: - return self.xtype.size + return self.xtype.packed_size def copy(self) -> Self: return type(self)(self.raw.copy(), xtype=self.xtype) @@ -122,7 +122,7 @@ def __setitem__(self, index: int, value: _T): @staticmethod def zeros(count: int, *, xtype: PackableType[_T]) -> TypedPtr[_T]: - size = xtype.size + size = xtype.packed_size return TypedPtr(RawPtr.zeros(size * count), xtype=xtype) @staticmethod @@ -130,7 +130,7 @@ def new(els: Sequence[_T], *, xtype: PackableType[_T]) -> TypedPtr[_T]: """ Returns a new TypedPtr with the specified els packed into memory. """ - el_size = xtype.size + el_size = xtype.packed_size res = RawPtr.zeros(len(els) * el_size) for i, el in enumerate(els): xtype.pack_into(res.memory, i * el_size, el) From ded4cc80312549be966497c964ec82403a18e084 Mon Sep 17 00:00:00 2001 From: Joren Dumoulin Date: Mon, 9 Dec 2024 16:16:34 +0100 Subject: [PATCH 6/7] remove bitwidth from indextype --- tests/dialects/test_builtin.py | 5 ----- xdsl/dialects/builtin.py | 4 ---- 2 files changed, 9 deletions(-) diff --git a/tests/dialects/test_builtin.py b/tests/dialects/test_builtin.py index 7fcb1364eb..23e56b3317 100644 --- a/tests/dialects/test_builtin.py +++ b/tests/dialects/test_builtin.py @@ -83,11 +83,6 @@ def test_IndexType_formats(): assert IndexType().format == " str: # index types are always packable as int64 return " int: - raise RuntimeError("IndexType bitwidth not known at compile-time") - IndexTypeConstr = BaseAttr(IndexType) From 5df2d508b7ddcf4693a896e883c35898b67d1c14 Mon Sep 17 00:00:00 2001 From: Joren Dumoulin Date: Tue, 10 Dec 2024 09:10:13 +0100 Subject: [PATCH 7/7] change packed_size to compile_time_size --- xdsl/dialects/builtin.py | 18 +++++++++--------- xdsl/interpreters/utils/ptr.py | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/xdsl/dialects/builtin.py b/xdsl/dialects/builtin.py index 4a7a66204f..ed9067c231 100644 --- a/xdsl/dialects/builtin.py +++ b/xdsl/dialects/builtin.py @@ -356,6 +356,14 @@ class CompileTimeFixedBitwidthType(TypeAttribute, ABC): name = "abstract.compile_time_fixed_bitwidth_type" + @property + @abstractmethod + def compile_time_size(self) -> int: + """ + Contiguous memory footprint of the value during compilation. + """ + raise NotImplementedError() + class FixedBitwidthType(CompileTimeFixedBitwidthType, ABC): """ @@ -416,14 +424,6 @@ def pack(self, values: Sequence[_PyT]) -> bytes: """ raise NotImplementedError() - @property - @abstractmethod - def packed_size(self) -> int: - """ - Contiguous memory footprint of packed value in bytes. - """ - raise NotImplementedError() - class StructPackableType(Generic[_PyT], PackableType[_PyT], ABC): """ @@ -456,7 +456,7 @@ def pack(self, values: Sequence[_PyT]) -> bytes: return struct.pack(fmt, *values) @property - def packed_size(self) -> int: + def compile_time_size(self) -> int: return struct.calcsize(self.format) diff --git a/xdsl/interpreters/utils/ptr.py b/xdsl/interpreters/utils/ptr.py index eb102c02b4..3769565236 100644 --- a/xdsl/interpreters/utils/ptr.py +++ b/xdsl/interpreters/utils/ptr.py @@ -95,7 +95,7 @@ class TypedPtr(Generic[_T]): @property def size(self) -> int: - return self.xtype.packed_size + return self.xtype.compile_time_size def copy(self) -> Self: return type(self)(self.raw.copy(), xtype=self.xtype) @@ -122,7 +122,7 @@ def __setitem__(self, index: int, value: _T): @staticmethod def zeros(count: int, *, xtype: PackableType[_T]) -> TypedPtr[_T]: - size = xtype.packed_size + size = xtype.compile_time_size return TypedPtr(RawPtr.zeros(size * count), xtype=xtype) @staticmethod @@ -130,7 +130,7 @@ def new(els: Sequence[_T], *, xtype: PackableType[_T]) -> TypedPtr[_T]: """ Returns a new TypedPtr with the specified els packed into memory. """ - el_size = xtype.packed_size + 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)