IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
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

AnyType

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:

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:

  • ValueType must conform to DevicePassable or RegisterPassable.
  • ValueType must conform to ImplicitlyCopyable & ImplicitlyDeletable or Copyable & ImplicitlyDeletable.
  • If ValueType is DevicePassable, it must be its own leaf device_type (ValueType._is_convertible_to_device_type[ValueType]()), since a bit-copy only encodes an identity mapping correctly.

Parameters:

  • ValueType (AnyType): The type of value, 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 least size_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 DevicePassable member, recurse into encode_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:

  • StructType must conform to RegisterPassable and be a Mojo struct type.
  • Every field must either conform to DevicePassable, be a composite transitively containing a DevicePassable member, conform to ImplicitlyCopyable & ImplicitlyDeletable, or conform to Copyable & 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 ElementType conforms to DevicePassable, dispatch to its own _to_device_type() so any host-to-device conversion runs.
  • Otherwise, if ElementType is a composite transitively containing a DevicePassable member, recurse into encode_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:

Args:

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 ElementType conforms to DevicePassable, dispatch to its own _to_device_type() so any host-to-device conversion runs.
  • Otherwise, if ElementType is a composite transitively containing a DevicePassable member, recurse into encode_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: