Skip to main content

struct

DTypePointer

Defines a DTypePointer struct that contains an address of the given dtype.

Parameters

  • type (DType): DType of the underlying data.
  • address_space (AddressSpace): The address space the pointer is in.

Aliases

  • element_type = SIMD[type, 1]:

Fields

  • address (LegacyPointer[SIMD[type, 1], address_space]): The pointed-to address.

Implemented traits

AnyType, Boolable, CollectionElement, Copyable, EqualityComparable, Intable, Movable, Stringable

Methods

__init__

__init__(inout self: Self, /)

Constructs a null DTypePointer from the given type.

__init__(inout self: Self, /, value: pointer<scalar<#lit.struct.extract<:_stdlib::_builtin::_dtype::_DType type, "value">>, #lit.struct.extract<:_stdlib::_builtin::_int::_Int #lit.struct.extract<:_stdlib::_memory::_reference::_AddressSpace address_space, "_value">, "value">>)

Constructs a DTypePointer from a scalar pointer of the same type.

Args:

  • value (pointer<scalar<#lit.struct.extract<:_stdlib::_builtin::_dtype::_DType type, "value">>, #lit.struct.extract<:_stdlib::_builtin::_int::_Int #lit.struct.extract<:_stdlib::_memory::_reference::_AddressSpace address_space, "_value">, "value">>): The scalar pointer.

__init__(inout self: Self, /, value: LegacyPointer[SIMD[type, 1], address_space])

Constructs a DTypePointer from a scalar pointer of the same type.

Args:

  • value (LegacyPointer[SIMD[type, 1], address_space]): The scalar pointer.

__init__(inout self: Self, /, other: UnsafePointer[SIMD[type, 1], address_space])

Constructs a DTypePointer from a scalar pointer of the same type.

Args:

  • other (UnsafePointer[SIMD[type, 1], address_space]): The scalar pointer.

__init__(inout self: Self, /, value: SIMD[address, 1])

Constructs a DTypePointer from the value of scalar address.

Args:

  • value (SIMD[address, 1]): The input pointer index.

__init__(inout self: Self, /, *, address: Int)

Constructs a DTypePointer from an integer address.

Args:

  • address (Int): The input address.

__bool__

__bool__(self: Self) -> Bool

Checks if the DTypePointer is null.

Returns:

Returns False if the DTypePointer is null and True otherwise.

__getitem__

__getitem__[T: Intable](self: Self, offset: T) -> SIMD[type, 1]

Loads a single element (SIMD of size 1) from the pointer at the specified index.

Parameters:

  • T (Intable): The Intable type of the offset.

Args:

  • offset (T): The offset to load from.

Returns:

The loaded value.

__setitem__

__setitem__[T: Intable](self: Self, offset: T, val: SIMD[type, 1])

Stores a single element value at the given offset.

Parameters:

  • T (Intable): The Intable type of the offset.

Args:

  • offset (T): The offset to store to.
  • val (SIMD[type, 1]): The value to store.

__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__[T: Intable](self: Self, rhs: T) -> Self

Returns a new pointer shifted by the specified offset.

Parameters:

  • T (Intable): The Intable type of the offset.

Args:

  • rhs (T): The offset.

Returns:

The new DTypePointer shifted by the offset.

__sub__

__sub__[T: Intable](self: Self, rhs: T) -> Self

Returns a new pointer shifted back by the specified offset.

Parameters:

  • T (Intable): The Intable type of the offset.

Args:

  • rhs (T): The offset.

Returns:

The new DTypePointer shifted by the offset.

__iadd__

__iadd__[T: Intable](inout self: Self, rhs: T)

Shifts the current pointer by the specified offset.

Parameters:

  • T (Intable): The Intable type of the offset.

Args:

  • rhs (T): The offset.

__isub__

__isub__[T: Intable](inout self: Self, rhs: T)

Shifts back the current pointer by the specified offset.

Parameters:

  • T (Intable): The Intable type of the offset.

Args:

  • rhs (T): The offset.

get_null

static get_null() -> Self

Constructs a DTypePointer representing nullptr.

Returns:

Constructed nullptr DTypePointer object.

__str__

__str__(self: Self) -> String

Format this pointer as a hexadecimal string.

Returns:

A String containing the hexadecimal representation of the memory location destination of this pointer.

address_of

static address_of(arg: Reference[SIMD[type, 1], is_mutable, lifetime, address_space]) -> Self

Gets the address of the argument.

Args:

  • arg (Reference[SIMD[type, 1], is_mutable, lifetime, address_space]): The value to get the address of.

Returns:

A DTypePointer struct which contains the address of the argument.

alloc

static alloc(count: Int, /, *, alignment: Int = alignof[stdlib::builtin::dtype::DType,__mlir_type.!kgen.target]()) -> Self

Heap-allocates a number of element of the specified type using the specified alignment.

Args:

  • count (Int): The number of elements to allocate (note that this is not the bytecount).
  • alignment (Int): The alignment used for the allocation.

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 = type, /, address_space: AddressSpace = address_space](self: Self) -> DTypePointer[$0, $1]

Bitcasts DTypePointer to a different dtype.

Parameters:

  • new_type (DType): The target dtype.
  • address_space (AddressSpace): The address space of the result.

Returns:

A new DTypePointer object with the specified dtype and the same address, as the original DTypePointer.

prefetch

prefetch[params: PrefetchOptions](self: Self)

Prefetches memory at the underlying address.

Parameters:

  • params (PrefetchOptions): Prefetch options (see PrefetchOptions for details).

load

load[*, width: Int = 1, alignment: Int = alignof[AnyRegType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1](self: Self) -> SIMD[type, $0]

Loads the value the Pointer object points to.

Constraints:

The width and alignment must be positive integer values.

Parameters:

  • width (Int): The SIMD width.
  • alignment (Int): The minimal alignment of the address.

Returns:

The loaded value.

load[T: Intable, *, width: Int = 1, alignment: Int = alignof[AnyRegType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1](self: Self, offset: T) -> SIMD[type, $1]

Loads the value the Pointer object points to with the given offset.

Constraints:

The width and alignment must be positive integer values.

Parameters:

  • T (Intable): The Intable type of the offset.
  • width (Int): The SIMD width.
  • alignment (Int): The minimal alignment of the address.

Args:

  • offset (T): The offset to load from.

Returns:

The loaded value.

store

store[T: Intable, /, *, width: Int = 1, alignment: Int = alignof[AnyRegType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1](self: Self, offset: T, val: SIMD[type, width])

Stores a single element value at the given offset.

Constraints:

The width and alignment must be positive integer values.

Parameters:

  • T (Intable): The Intable type of the offset.
  • width (Int): The SIMD width.
  • alignment (Int): The minimal alignment of the address.

Args:

  • offset (T): The offset to store to.
  • val (SIMD[type, width]): The value to store.

store[*, width: Int = 1, alignment: Int = alignof[AnyRegType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1](self: Self, val: SIMD[type, width])

Stores a single element value.

Constraints:

The width and alignment must be positive integer values.

Parameters:

  • width (Int): The SIMD width.
  • alignment (Int): The minimal alignment of the address.

Args:

  • val (SIMD[type, width]): The value to store.

simd_nt_store

simd_nt_store[width: Int, T: Intable](self: Self, offset: T, val: SIMD[type, width])

Stores a SIMD vector using non-temporal store.

Parameters:

  • width (Int): The SIMD width.
  • T (Intable): The Intable type of the offset.

Args:

  • offset (T): 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, T: Intable](self: Self, stride: T) -> SIMD[type, $0]

Performs a strided load of the SIMD vector.

Parameters:

  • width (Int): The SIMD width.
  • T (Intable): The Intable type of the stride.

Args:

  • stride (T): The stride between loads.

Returns:

A vector which is stride loaded.

simd_strided_store

simd_strided_store[width: Int, T: Intable](self: Self, val: SIMD[type, width], stride: T)

Performs a strided store of the SIMD vector.

Parameters:

  • width (Int): The SIMD width.
  • T (Intable): The Intable type of the stride.

Args:

  • val (SIMD[type, width]): The SIMD value to store.
  • stride (T): The stride between stores.

gather

gather[*, width: Int = 1, alignment: Int = alignof[AnyRegType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1](self: Self, offset: SIMD[type, width]) -> SIMD[type, $0]

Gathers a SIMD vector from offsets of the current pointer.

This method loads from memory addresses calculated by appropriately shifting the current pointer according to the offset SIMD vector.

Constraints:

The offset type must be an integral type. The alignment must be a power of two integer value.

Parameters:

  • width (Int): The SIMD width.
  • alignment (Int): The minimal alignment of the address.

Args:

  • offset (SIMD[type, width]): The SIMD vector of offsets to gather from.

Returns:

The SIMD vector containing the gathered values.

gather[*, width: Int = 1, alignment: Int = alignof[AnyRegType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1](self: Self, offset: SIMD[type, width], mask: SIMD[bool, width], default: SIMD[type, width]) -> SIMD[type, $0]

Gathers a SIMD vector from offsets of the current pointer.

This method loads from memory addresses calculated by appropriately shifting the current pointer according to the offset SIMD vector, or takes from the default SIMD vector, depending on the values of the mask SIMD vector.

If a mask element is True, the respective result element is given by the current pointer and the offset SIMD vector; otherwise, the result element is taken from the default SIMD vector.

Constraints:

The offset type must be an integral type. The alignment must be a power of two integer value.

Parameters:

  • width (Int): The SIMD width.
  • alignment (Int): The minimal alignment of the address.

Args:

  • offset (SIMD[type, width]): The SIMD vector of offsets to gather from.
  • mask (SIMD[bool, width]): The SIMD vector of boolean values, indicating for each element whether to load from memory or to take from the default SIMD vector.
  • default (SIMD[type, width]): The SIMD vector providing default values to be taken where the mask SIMD vector is False.

Returns:

The SIMD vector containing the gathered values.

scatter

scatter[*, width: Int = 1, alignment: Int = alignof[AnyRegType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1](self: Self, offset: SIMD[type, width], val: SIMD[type, width])

Scatters a SIMD vector into offsets of the current pointer.

This method stores at memory addresses calculated by appropriately shifting the current pointer according to the offset SIMD vector.

If the same offset is targeted multiple times, the values are stored in the order they appear in the val SIMD vector, from the first to the last element.

Constraints:

The offset type must be an integral type. The alignment must be a power of two integer value.

Parameters:

  • width (Int): The SIMD width.
  • alignment (Int): The minimal alignment of the address.

Args:

  • offset (SIMD[type, width]): The SIMD vector of offsets to scatter into.
  • val (SIMD[type, width]): The SIMD vector containing the values to be scattered.

scatter[*, width: Int = 1, alignment: Int = alignof[AnyRegType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1](self: Self, offset: SIMD[type, width], val: SIMD[type, width], mask: SIMD[bool, width])

Scatters a SIMD vector into offsets of the current pointer.

This method stores at memory addresses calculated by appropriately shifting the current pointer according to the offset SIMD vector, depending on the values of the mask SIMD vector.

If a mask element is True, the respective element in the val SIMD vector is stored at the memory address defined by the current pointer and the offset SIMD vector; otherwise, no action is taken for that element in val.

If the same offset is targeted multiple times, the values are stored in the order they appear in the val SIMD vector, from the first to the last element.

Constraints:

The offset type must be an integral type. The alignment must be a power of two integer value.

Parameters:

  • width (Int): The SIMD width.
  • alignment (Int): The minimal alignment of the address.

Args:

  • offset (SIMD[type, width]): The SIMD vector of offsets to scatter into.
  • val (SIMD[type, width]): The SIMD vector containing the values to be scattered.
  • mask (SIMD[bool, width]): The SIMD vector of boolean values, indicating for each element whether to store at memory or not.

__int__

__int__(self: Self) -> Int

Returns the pointer address as an integer.

Returns:

The address of the pointer as an Int.

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[T: Intable](self: Self, idx: T) -> Self

Returns a new pointer shifted by the specified offset.

Parameters:

  • T (Intable): The Intable type of the offset.

Args:

  • idx (T): The offset of the new pointer.

Returns:

The new constructed DTypePointer.