Skip to content

Commit

Permalink
all basic ptr operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Manainen authored and Max Manainen committed Oct 28, 2024
1 parent 53123ac commit b746cc1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
11 changes: 10 additions & 1 deletion tests/filecheck/dialects/ptr/ops.mlir
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion xdsl/backend/riscv/lowering/convert_memref_to_riscv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 43 additions & 2 deletions xdsl/dialects/ptr.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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,
IRDLOperation,
irdl_attr_definition,
irdl_op_definition,
operand_def,
opt_prop_def,
result_def,
)

Expand All @@ -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])

0 comments on commit b746cc1

Please sign in to comment.