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.