From b746cc14bf2f9eb8c07e6ecae20bd992b1d3008a Mon Sep 17 00:00:00 2001 From: Max Manainen Date: Mon, 28 Oct 2024 18:14:32 +0000 Subject: [PATCH] all basic ptr operations --- tests/filecheck/dialects/ptr/ops.mlir | 11 ++++- .../riscv/lowering/convert_memref_to_riscv.py | 2 +- xdsl/dialects/ptr.py | 45 ++++++++++++++++++- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/tests/filecheck/dialects/ptr/ops.mlir b/tests/filecheck/dialects/ptr/ops.mlir index 5d7edcf572..6f9726c279 100644 --- a/tests/filecheck/dialects/ptr/ops.mlir +++ b/tests/filecheck/dialects/ptr/ops.mlir @@ -1,8 +1,17 @@ // RUN: xdsl-opt %s --parsing-diagnostics --verify-diagnostics --split-input-file | filecheck %s builtin.module { - %p, %idx = "test.op"() : () -> (!ptr.ptr, index) + %p, %idx, %v = "test.op"() : () -> (!ptr.ptr, index, i32) // CHECK: %r0 = ptr.ptradd %p, %idx : (!ptr.ptr, index) -> !ptr.ptr %r0 = ptr.ptradd %p, %idx : (!ptr.ptr, index) -> !ptr.ptr + + // CHECK-NEXT: %r1 = ptr.type_offset %v : i32 -> index + %r1 = ptr.type_offset %v : i32 -> index + + // CHECK-NEXT: ptr.store %v, %p : i32, !ptr.ptr + ptr.store %v, %p : i32, !ptr.ptr + + // CHECK-NEXT: ptr.load %p : !ptr.ptr -> i32 + %r3 = ptr.load %p : !ptr.ptr -> i32 } diff --git a/xdsl/backend/riscv/lowering/convert_memref_to_riscv.py b/xdsl/backend/riscv/lowering/convert_memref_to_riscv.py index 5292a5dfe0..9f2178b993 100644 --- a/xdsl/backend/riscv/lowering/convert_memref_to_riscv.py +++ b/xdsl/backend/riscv/lowering/convert_memref_to_riscv.py @@ -103,7 +103,7 @@ def get_strided_pointer( ops: list[Operation] = [] head: SSAValue | None = None - print("indicies: ", indices, "strides: ", strides) + for index, stride in zip(indices, strides, strict=True): # Calculate the offset that needs to be added through the index of the current # dimension. diff --git a/xdsl/dialects/ptr.py b/xdsl/dialects/ptr.py index 484dfbd424..2a85110613 100644 --- a/xdsl/dialects/ptr.py +++ b/xdsl/dialects/ptr.py @@ -1,4 +1,4 @@ -from xdsl.dialects.builtin import IndexType, IntegerType +from xdsl.dialects.builtin import IndexType, IntegerType, UnitAttr from xdsl.ir import Dialect, ParametrizedAttribute, TypeAttribute from xdsl.irdl import ( AnyOf, @@ -6,6 +6,7 @@ irdl_attr_definition, irdl_op_definition, operand_def, + opt_prop_def, result_def, ) @@ -26,4 +27,44 @@ class PtrAddOp(IRDLOperation): assembly_format = "$addr `,` $offset attr-dict `:` `(` type($addr) `,` type($offset) `)` `->` type($result)" -Ptr = Dialect("ptr", [PtrAddOp], [PtrType]) +# haven't managed to pass a type here yet. so did it with a hack. +@irdl_op_definition +class TypeOffsetOp(IRDLOperation): + name = "ptr.type_offset" + + input_type = operand_def() + offset = result_def(AnyOf([IntegerType, IndexType])) + + assembly_format = "$input_type attr-dict `:` type($input_type) `->` type($offset)" + + +@irdl_op_definition +class StoreOp(IRDLOperation): + name = "ptr.store" + + addr = operand_def(PtrType) + value = operand_def() + + volatile = opt_prop_def(UnitAttr) + syncscope = opt_prop_def(UnitAttr) + ordering = opt_prop_def(UnitAttr) + + assembly_format = "(`volatile` $volatile^)? $value `,` $addr (`atomic` (`syncscope` `(` $syncscope^ `)`)? $ordering^)? attr-dict `:` type($value) `,` type($addr)" + + +@irdl_op_definition +class LoadOp(IRDLOperation): + name = "ptr.load" + + addr = operand_def(PtrType) + res = result_def() + + volatile = opt_prop_def(UnitAttr) + syncscope = opt_prop_def(UnitAttr) + ordering = opt_prop_def(UnitAttr) + invariant = opt_prop_def(UnitAttr) + + assembly_format = "(`volatile` $volatile^)? $addr (`atomic` (`syncscope` `(` $syncscope^ `)`)? $ordering^)? (`invariant` $invariant^)? attr-dict `:` type($addr) `->` type($res)" + + +Ptr = Dialect("ptr", [PtrAddOp, TypeOffsetOp, StoreOp, LoadOp], [PtrType])