unsafe
Module
Implements classes for working with unsafe pointers.
You can import these APIs from the memory
package. For example:
from memory.unsafe import Pointer
Pointer
Defines a Pointer struct that contains an address of any mlirtype.
Parameters:
- type (
AnyType
): Type of the underlying data.
Aliases:
pointer_type = pointer<*"type">
Fields:
- address (
pointer<*"type">
): The pointed-to address.
Functions:
__init__
__init__() -> Self
Constructs a null Pointer from the value of pop.pointer type.
Returns:
Constructed Pointer object.
__init__(address: pointer<*"type">) -> Self
Constructs a Pointer from the address.
Args:
- address (
pointer<*"type">
): The input pointer address.
Returns:
Constructed Pointer object.
__init__(value: SIMD[address, 1]) -> Self
Constructs a Pointer from the value of scalar address.
Args:
- value (
SIMD[address, 1]
): The input pointer index.
Returns:
Constructed Pointer object.
__bool__
__bool__(self: Self) -> Bool
Checks if the pointer is null.
Returns:
Returns False if the pointer is null and True otherwise.
__getitem__
__getitem__(self: Self, offset: Int) -> *"type"
Loads the value the Pointer object points to with the given offset.
Args:
- offset (
Int
): The offset to load from.
Returns:
The loaded value.
__eq__
__eq__(self: Self, rhs: Self) -> Bool
Returns True if the two pointers are equal.
Args:
- rhs (
Self
): The value of the other pointer.
Returns:
True if the two pointers are equal and False otherwise.
__ne__
__ne__(self: Self, rhs: Self) -> Bool
Returns True if the two pointers are not equal.
Args:
- rhs (
Self
): The value of the other pointer.
Returns:
True if the two pointers are not equal and False otherwise.
__add__
__add__(self: Self, rhs: Int) -> Self
Returns a new pointer shifted by the specified offset.
Args:
- rhs (
Int
): The offset.
Returns:
The new Pointer shifted by the offset.
__sub__
__sub__(self: Self, rhs: Int) -> Self
Returns a new pointer shifted back by the specified offset.
Args:
- rhs (
Int
): The offset.
Returns:
The new Pointer shifted back by the offset.
__iadd__
__iadd__(inout self: Self, rhs: Int)
Shifts the current pointer by the specified offset.
Args:
- rhs (
Int
): The offset.
__isub__
__isub__(inout self: Self, rhs: Int)
Shifts back the current pointer by the specified offset.
Args:
- rhs (
Int
): The offset.
get_null
get_null() -> Self
Constructs a Pointer representing nullptr.
Returns:
Constructed nullptr Pointer object.
address_of
address_of(inout *arg: "type") -> Self
Gets the address of the argument.
Args:
- arg (
*"type"
): The value to get the address of.
Returns:
A pointer struct which contains the address of the argument.
load
load(self: Self, offset: Int) -> *"type"
Loads the value the Pointer object points to with the given offset.
Args:
- offset (
Int
): The offset to load from.
Returns:
The loaded value.
load(self: Self) -> *"type"
Loads the value the Pointer object points to.
Returns:
The loaded value.
store
store(self: Self, offset: Int, *value: "type")
Stores the specified value to the location the Pointer object points to with the given offset.
Args:
- offset (
Int
): The offset to load from. - value (
*"type"
): The value to store.
store(self: Self, *value: "type")
Stores the specified value to the location the Pointer object points to.
Args:
- value (
*"type"
): The value to store.
alloc
alloc(count: Int) -> Self
Heap-allocates a number of element of the specified type.
Args:
- count (
Int
): The number of elements to allocate (note that this is not the bytecount).
Returns:
A new Pointer object which has been allocated on the heap.
aligned_alloc
aligned_alloc(alignment: Int, count: Int) -> Self
Heap-allocates a number of element of the specified type using the specified alignment.
Args:
- alignment (
Int
): The alignment used for the allocation. - count (
Int
): The number of elements to allocate (note that this is not the bytecount).
Returns:
A new Pointer object which has been allocated on the heap.
free
free(self: Self)
Frees the heap allocated memory.
bitcast
bitcast[new_type: AnyType](self: Self) -> Pointer[new_type]
Bitcasts a Pointer to a different type.
Parameters:
- new_type (
AnyType
): The target type.
Returns:
A new Pointer object with the specified type and the same address, as the original Pointer.
offset
offset(self: Self, idx: Int) -> Self
Returns a new pointer shifted by the specified offset.
Args:
- idx (
Int
): The offset.
Returns:
The new Pointer shifted by the offset.
DTypePointer
Defines a DTypePointer
struct that contains an address of the given dtype.
Parameters:
- type (
DType
): DType of the underlying data.
Aliases:
element_type = scalar<#lit.struct.extract<:!kgen.declref<@"$builtin"::@"$dtype"::@DType> type, "value">>
pointer_type = pointer<scalar<#lit.struct.extract<:!kgen.declref<@"$builtin"::@"$dtype"::@DType> type, "value">>>
Fields:
- address (
pointer<scalar<#lit.struct.extract<:!kgen.declref<@"$builtin"::@"$dtype"::@DType> type, "value">>>
): The pointed-to address.
Functions:
__init__
__init__() -> Self
Constructs a null DTypePointer
from the given type.
Returns:
Constructed DTypePointer
object.
__init__(address: pointer<scalar<#lit.struct.extract<:!kgen.declref<_"$builtin"::_"$dtype"::_DType> type, "value">>>) -> Self
Constructs a DTypePointer
from the given address.
Args:
- address (
pointer<scalar<#lit.struct.extract<:!kgen.declref<_"$builtin"::_"$dtype"::_DType> type, "value">>>
): The input pointer.
Returns:
Constructed DTypePointer
object.
__init__(value: Pointer[SIMD[type, 1]]) -> Self
Constructs a DTypePointer
from a scalar pointer of the same type.
Args:
- value (
Pointer[SIMD[type, 1]]
): The scalar pointer.
Returns:
Constructed DTypePointer
.
__init__(value: SIMD[address, 1]) -> Self
Constructs a DTypePointer
from the value of scalar address.
Args:
- value (
SIMD[address, 1]
): The input pointer index.
Returns:
Constructed DTypePointer
object.
__bool__
__bool__(self: Self) -> Bool
Checks if the pointer is null.
Returns:
Returns False if the pointer is null and True otherwise.
__lt__
__lt__(self: Self, rhs: Self) -> Bool
Returns True if this pointer represents a lower address than rhs.
Args:
- rhs (
Self
): The value of the other pointer.
Returns:
True if this pointer represents a lower address and False otherwise.
__eq__
__eq__(self: Self, rhs: Self) -> Bool
Returns True if the two pointers are equal.
Args:
- rhs (
Self
): The value of the other pointer.
Returns:
True if the two pointers are equal and False otherwise.
__ne__
__ne__(self: Self, rhs: Self) -> Bool
Returns True if the two pointers are not equal.
Args:
- rhs (
Self
): The value of the other pointer.
Returns:
True if the two pointers are not equal and False otherwise.
__add__
__add__(self: Self, rhs: Int) -> Self
Returns a new pointer shifted by the specified offset.
Args:
- rhs (
Int
): The offset.
Returns:
The new DTypePointer shifted by the offset.
__sub__
__sub__(self: Self, rhs: Int) -> Self
Returns a new pointer shifted back by the specified offset.
Args:
- rhs (
Int
): The offset.
Returns:
The new DTypePointer shifted by the offset.
__iadd__
__iadd__(inout self: Self, rhs: Int)
Shifts the current pointer by the specified offset.
Args:
- rhs (
Int
): The offset.
__isub__
__isub__(inout self: Self, rhs: Int)
Shifts back the current pointer by the specified offset.
Args:
- rhs (
Int
): The offset.
get_null
get_null() -> Self
Constructs a DTypePointer
representing nullptr.
Returns:
Constructed nullptr DTypePointer
object.
address_of
address_of(inout arg: scalar<#lit.struct.extract<:!kgen.declref<_"$builtin"::_"$dtype"::_DType> type, "value">>) -> Self
Gets the address of the argument.
Args:
- arg (
scalar<#lit.struct.extract<:!kgen.declref<_"$builtin"::_"$dtype"::_DType> type, "value">>
): The value to get the address of.
Returns:
A pointer struct which contains the address of the argument.
alloc
alloc(count: Int) -> Self
Heap-allocates a number of element of the specified type.
Args:
- count (
Int
): The number of elements to allocate (note that this is not the bytecount).
Returns:
A new DTypePointer
object which has been allocated on the heap.
aligned_alloc
aligned_alloc(alignment: Int, count: Int) -> Self
Heap-allocates a number of element of the specified type using the specified alignment.
Args:
- alignment (
Int
): The alignment used for the allocation. - count (
Int
): The number of elements to allocate (note that this is not the bytecount).
Returns:
A new DTypePointer
object which has been allocated on the heap.
free
free(self: Self)
Frees the heap allocates memory.
bitcast
bitcast[new_type: DType](self: Self) -> DTypePointer[new_type]
Bitcasts DTypePointer
to a different dtype.
Parameters:
- new_type (
DType
): The target dtype.
Returns:
A new DTypePointer
object with the specified dtype and the same address, as the original DTypePointer
.
as_scalar_pointer
as_scalar_pointer(self: Self) -> Pointer[SIMD[type, 1]]
Converts the DTypePointer
to a scalar pointer of the same dtype.
Returns:
A Pointer
to a scalar of the same dtype.
load
load(self: Self, offset: Int) -> SIMD[type, 1]
Loads a single element (SIMD of size 1) from the pointer at the specified index.
Args:
- offset (
Int
): The offset to load from.
Returns:
The loaded value.
load(self: Self) -> SIMD[type, 1]
Loads a single element (SIMD of size 1) from the pointer.
Returns:
The loaded value.
prefetch
prefetch[params: PrefetchOptions](self: Self)
Prefetches memory at the underlying address.
Parameters:
- params (
PrefetchOptions
): Prefetch options (seePrefetchOptions
for details).
simd_load
simd_load[width: Int](self: Self, offset: Int) -> SIMD[type, width]
Loads a SIMD vector of elements from the pointer at the specified offset.
Parameters:
- width (
Int
): The SIMD width.
Args:
- offset (
Int
): The offset to load from.
Returns:
The loaded value.
simd_load[width: Int](self: Self) -> SIMD[type, width]
Loads a SIMD vector of elements from the pointer.
Parameters:
- width (
Int
): The SIMD width.
Returns:
The loaded SIMD value.
aligned_simd_load
aligned_simd_load[width: Int, alignment: Int](self: Self, offset: Int) -> SIMD[type, width]
Loads a SIMD vector of elements from the pointer at the specified offset with the guaranteed specified alignment.
Parameters:
- width (
Int
): The SIMD width. - alignment (
Int
): The minimal alignment of the address.
Args:
- offset (
Int
): The offset to load from.
Returns:
The loaded SIMD value.
aligned_simd_load[width: Int, alignment: Int](self: Self) -> SIMD[type, width]
Loads a SIMD vector of elements from the pointer with the guaranteed specified alignment.
Parameters:
- width (
Int
): The SIMD width. - alignment (
Int
): The minimal alignment of the address.
Returns:
The loaded SIMD value.
store
store(self: Self, offset: Int, val: SIMD[type, 1])
Stores a single element value at the given offset.
Args:
- offset (
Int
): The offset to store to. - val (
SIMD[type, 1]
): The value to store.
store(self: Self, val: SIMD[type, 1])
Stores a single element value.
Args:
- val (
SIMD[type, 1]
): The value to store.
simd_store
simd_store[width: Int](self: Self, offset: Int, val: SIMD[type, width])
Stores a SIMD vector at the given offset.
Parameters:
- width (
Int
): The SIMD width.
Args:
- offset (
Int
): The offset to store to. - val (
SIMD[type, width]
): The SIMD value to store.
simd_store[width: Int](self: Self, val: SIMD[type, width])
Stores a SIMD vector.
Parameters:
- width (
Int
): The SIMD width.
Args:
- val (
SIMD[type, width]
): The SIMD value to store.
simd_nt_store
simd_nt_store[width: Int](self: Self, offset: Int, val: SIMD[type, width])
Stores a SIMD vector using non-temporal store.
Parameters:
- width (
Int
): The SIMD width.
Args:
- offset (
Int
): The offset to store to. - val (
SIMD[type, width]
): The SIMD value to store.
simd_nt_store[width: Int](self: Self, val: SIMD[type, width])
Stores a SIMD vector using non-temporal store.
The address must be properly aligned, 64B for avx512, 32B for avx2, and 16B for avx.
Parameters:
- width (
Int
): The SIMD width.
Args:
- val (
SIMD[type, width]
): The SIMD value to store.
simd_strided_load
simd_strided_load[width: Int](self: Self, stride: Int) -> SIMD[type, width]
Performs a strided load of the SIMD vector.
Parameters:
- width (
Int
): The SIMD width.
Args:
- stride (
Int
): The stride between loads.
Returns:
A vector which is stride loaded.
simd_strided_store
simd_strided_store[width: Int](self: Self, val: SIMD[type, width], stride: Int)
Performs a strided store of the SIMD vector.
Parameters:
- width (
Int
): The SIMD width.
Args:
- val (
SIMD[type, width]
): The SIMD value to store. - stride (
Int
): The stride between stores.
aligned_simd_store
aligned_simd_store[width: Int, alignment: Int](self: Self, offset: Int, val: SIMD[type, width])
Stores a SIMD vector at the given offset with a guaranteed alignment.
Parameters:
- width (
Int
): The SIMD width. - alignment (
Int
): The minimal alignment of the address.
Args:
- offset (
Int
): The offset to store to. - val (
SIMD[type, width]
): The SIMD value to store.
aligned_simd_store[width: Int, alignment: Int](self: Self, val: SIMD[type, width])
Stores a SIMD vector with a guaranteed alignment.
Parameters:
- width (
Int
): The SIMD width. - alignment (
Int
): The minimal alignment of the address.
Args:
- val (
SIMD[type, width]
): The SIMD value to store.
is_aligned
is_aligned[alignment: Int](self: Self) -> Bool
Checks if the pointer is aligned.
Parameters:
- alignment (
Int
): The minimal desired alignment.
Returns:
True
if the pointer is at least alignment
-aligned or False
otherwise.
offset
offset(self: Self, idx: Int) -> Self
Returns a new pointer shifted by the specified offset.
Args:
- idx (
Int
): The offset of the new pointer.
Returns:
The new constructed DTypePointer.