For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
DeviceTypeEncoder
This trait marks types as capable of encoding device types.
Used in DevicePassable._to_device_type() to enable target specific
encoding of device types at the boundary where functions are enqueued for
execution on an accelerator device.
Implemented traits
Required methods
target
static def target() -> __mlir_type.`!kgen.target`
Returns the target architecture this encoder is encoding for.
Layout-sensitive queries (size_of, align_of,
reflect[T].field_offset[...]) inside encode /
encode_device_ptr and inside DevicePassable._to_device_type
implementations should pass target=Self.target() so that the
device's data layout — not the host's — is consulted.
Returns:
__mlir_type.`!kgen.target`: The target architecture this encoder is encoding for.
encode_device_ptr
def encode_device_ptr(mut self: _Self, value: DevicePointer, dst: UnsafePointer[NoneType])
Encodes a DevicePointer into dst.
Args:
- value (
DevicePointer): TheDevicePointerinstance to encode intodst. - dst (
UnsafePointer[NoneType]): The opaque destination pointer to encode into.
Provided methods
encode
def encode[ValueType: AnyType](mut self: _Self, value: ValueType, dst: UnsafePointer[NoneType])
Encodes value into dst by copying its bits.
This is the default device encoding for a type whose
DevicePassable.device_type is Self.
Constraints:
ValueTypemust conform toDevicePassableorRegisterPassable.ValueTypemust conform toImplicitlyCopyable & ImplicitlyDeletableorCopyable & ImplicitlyDeletable.- If
ValueTypeisDevicePassable, it must be its own leafdevice_type(ValueType._is_convertible_to_device_type[ValueType]()), since a bit-copy only encodes an identity mapping correctly.
Parameters:
- ValueType (
AnyType): The type ofvalue, see constraints.
Args:
- value (
ValueType): The variable to encode. - dst (
UnsafePointer[NoneType]): The opaque destination pointer to encode into. Must point to uninitialized storage at leastsize_of[ValueType, target=Self.target()]()bytes wide.
encode_fields
def encode_fields[StructType: AnyType](mut self: _Self, value: StructType, dst: UnsafePointer[NoneType])
Encodes each field of value into dst at its device offset.
For each field of StructType:
- If it conforms to
DevicePassable, dispatch to its own_to_device_type(). - Otherwise, if it is a composite transitively containing a
DevicePassablemember, recurse intoencode_fields. - Otherwise, delegate to
encode(a bit-copy for a register-passable field; any other type is rejected there at compile time).
This is the building block composite types use to encode their members,
including compiler-synthesized unified-closure wrappers whose
closure-state type does not itself conform to DevicePassable. Field
offsets use the encoder's target data layout (Self.target()) rather
than the host's, so each field lands at the offset the device expects.
Constraints:
StructTypemust conform toRegisterPassableand be a Mojo struct type.- Every field must either conform to
DevicePassable, be a composite transitively containing aDevicePassablemember, conform toImplicitlyCopyable & ImplicitlyDeletable, or conform toCopyable & ImplicitlyDeletable.
Parameters:
- StructType (
AnyType): The composite host-side type whose fields are being encoded.
Args:
- value (
StructType): The composite host-side value to encode. - dst (
UnsafePointer[NoneType]): The opaque destination pointer that receives the encoded fields.
encode_static_tuple
def encode_static_tuple[ElementType: ImplicitlyCopyable & ImplicitlyDeletable & RegisterPassable, size: Int, //](mut self: _Self, value: StaticTuple[ElementType, size], dst: UnsafePointer[NoneType])
Encodes each element of a StaticTuple into dst element-wise.
StaticTuple's !pop.array storage is opaque to field reflection
(see MOCO-4018), so encode_fields cannot iterate it. This encodes the
tuple by element instead, applying the same dispatch encode_fields
uses per field:
- If
ElementTypeconforms toDevicePassable, dispatch to its own_to_device_type()so any host-to-device conversion runs. - Otherwise, if
ElementTypeis a composite transitively containing aDevicePassablemember, recurse intoencode_fields. - Otherwise, bit-copy via
encode.
Elements are placed at i * size_of[device-element-type] in the
encoder's target data layout (Self.target()), matching the device
layout of StaticTuple.device_type.
Parameters:
- ElementType (
ImplicitlyCopyable&ImplicitlyDeletable&RegisterPassable): The tuple's element type (inferred). - size (
Int): The number of elements (inferred).
Args:
- value (
StaticTuple[ElementType, size]): TheStaticTupleto encode. - dst (
UnsafePointer[NoneType]): The opaque destination pointer that receives the encoded elements.
encode_inline_array
def encode_inline_array[ElementType: Movable, size: Int, //](mut self: _Self, value: InlineArray[ElementType, size], dst: UnsafePointer[NoneType])
Encodes each element of an InlineArray into dst element-wise.
Like encode_static_tuple, but for InlineArray. The array's
!pop.array storage is opaque to field reflection (see MOCO-4018), so
this encodes by element, applying the same dispatch encode_fields
uses per field:
- If
ElementTypeconforms toDevicePassable, dispatch to its own_to_device_type()so any host-to-device conversion runs. - Otherwise, if
ElementTypeis a composite transitively containing aDevicePassablemember, recurse intoencode_fields. - Otherwise, bit-copy via
encode.
Elements are placed at i * size_of[device-element-type] in the
encoder's target data layout (Self.target()), matching the device
layout of InlineArray.device_type.
Parameters:
- ElementType (
Movable): The array's element type (inferred). - size (
Int): The number of elements (inferred).
Args:
- value (
InlineArray[ElementType, size]): TheInlineArrayto encode. - dst (
UnsafePointer[NoneType]): The opaque destination pointer that receives the encoded elements.