Type#

struct Type#

Types in the halide type system.

They can be ints, unsigned ints, or floats of various bit-widths (the ‘bits’ field). They can also be vectors of the same (by setting the ‘lanes’ field to something larger than one). Front-end code shouldn’t use vector types. Instead vectorize a function.

Public Functions

int bytes() const#

The number of bytes required to store a single scalar value of this type.

Ignores vector lanes. For a struct type, this is the packed size of the whole struct.

inline bool is_struct() const#

Is this type a struct type, as constructed by Type::Struct?

inline const StructTypeInfo *struct_type() const#

If this is a struct type, its field list/offsets.

Null otherwise. Backed by an intern index rather than an inline pointer (like handle_type()), so this is an accessor rather than a public field.

bool same_struct_type(const Type &other) const#

Check that the struct layout of two struct types matches structurally (same field names, types, order, and array extents), not just by pointer identity of the interned StructTypeInfo.

inline Type(halide_type_code_t code, int bits, int lanes, const halide_handle_cplusplus_type *handle_type = nullptr)#

Construct a runtime representation of a Halide type from: code: The fundamental type from an enum.

bits: The bit size of one element. lanes: The number of vector elements in the type.

Type(const Type &that) = default#

Trivial copy constructor.

Type &operator=(const Type &that) = default#

Trivial copy assignment operator.

inline Type(halide_type_t that, const halide_handle_cplusplus_type *handle_type = nullptr)#

Construct a (scalar) language Type from an ABI element type.

A struct ABI type carries only its packed byte size, not its field layout (just as a buffer of handles forgets its C++ pointee type), so it rebuilds a faithful-sized but field-opaque struct — enough for bytes()/to_abi() to round-trip and for buffer-bind compatibility, but not for field access.

inline halide_type_t to_abi() const#

Erase this language Type to the ABI/runtime halide_type_t for use in runtime calls, buffer/argument metadata, etc.

This is a checked erasure: the ABI element type has no lanes, so a vector type (lanes >= 2) has no image here and asserts rather than silently dropping its lanes. A struct type erases to code halide_type_struct carrying its packed byte size in the info field, so the aggregate’s true size survives the boundary (an ordinary numeric type erases with info == 0).

inline halide_type_code_t code() const#

Return the underlying data type of an element as an enum value.

inline int bits() const#

Return the bit size of a single element of this type.

Not meaningful for a struct type (whose real size is a byte count that need not be a whole number of machine words, and can exceed what this field holds) — use bytes() for a struct’s size.

inline int lanes() const#

Return the number of vector elements in this type.

inline uint32_t hash() const#

A cheap hash of the type, for use in the hashes of Expr nodes that embed a Type (see Expr.h).

Just the bits of type_code, type_bits, and type_lanes (which happen to pack into 32 bits), ignoring handle_index_.

inline Type with_code(halide_type_code_t new_code) const#

Return Type with same number of bits and lanes, but new_code for a type code.

inline Type with_bits(int new_bits) const#

Return Type with same type code and lanes, but new_bits for the number of bits.

inline Type with_lanes(int new_lanes) const#

Return Type with same type code and number of bits, but new_lanes for the number of vector lanes.

inline Type widen() const#

Return Type with the same type code and number of lanes, but with at least twice as many bits.

inline Type narrow() const#

Return Type with the same type code and number of lanes, but with at most half as many bits.

inline const halide_handle_cplusplus_type *handle_type() const#

The externally-owned C++ type metadata for a handle type (null for a plain void * handle or a non-handle type).

Backed by a 4-byte intern index rather than stored inline, so this is now an accessor rather than a public field.

inline bool is_bool() const#

Is this type boolean (represented as UInt(1))?

inline bool is_vector() const#

Is this type a vector type?

(lanes() != 1). TODO(abadams): Decide what to do for lanes() == 0.

inline bool is_scalar() const#

Is this type a scalar type?

(lanes() == 1). TODO(abadams): Decide what to do for lanes() == 0.

inline bool is_float() const#

Is this type a floating point type (float or double).

inline bool is_bfloat() const#

Is this type a floating point type (float or double).

inline bool is_int() const#

Is this type a signed integer type?

inline bool is_uint() const#

Is this type an unsigned integer type?

A struct type has its own honest code (halide_type_struct), so this reports false for it — struct types no longer masquerade as UInt(8), and numeric reasoning is naturally excluded without a separate is_struct() guard.

inline bool is_int_or_uint() const#

Is this type an integer type of any sort?

inline bool is_handle() const#

Is this type an opaque handle type (void *).

bool same_handle_type(const Type &other) const#

Check that the type name of two handles matches.

inline bool operator==(const Type &other) const#

Compare two types for equality.

inline bool operator!=(const Type &other) const#

Compare two types for inequality.

inline bool matches_buffer_abi_type(const Type &buffer_type) const#

Is buffer_type an acceptable type for a halide_buffer_t bound to a Parameter/ImageParam declared with this Type?

This mirrors the runtime type check emitted by AddImageChecks: the buffer’s ABI element type must equal this type’s. For a struct that means the same struct code and packed byte size — the buffer only carries the ABI halide_type_t, which forgets a struct’s field layout (like a buffer of handles forgets its pointee type), so two structs of equal size are ABI-compatible.

inline bool operator==(const halide_type_t &other) const#

Compare a language type to an ABI element type.

Equal iff this type is a single element (not a vector) with the same code and bits.

inline bool operator!=(const halide_type_t &other) const#

Compare two types for inequality.

bool operator<(const Type &other) const#

Compare ordering of two types so they can be used in certain containers and algorithms.

Consistent with operator==: two struct types that compare equal (same field names/types/order/extents, per same_struct_type) always compare as neither-less-than-the-other, regardless of whether they share a StructTypeInfo pointer.

inline Type element_of() const#

Produce the scalar type (that of a single element) of this vector type.

bool can_represent(Type other) const#

Can this type represent all values of another type?

bool can_represent(const Internal::ConstantInterval &in) const#

Can this type represent exactly all integer values of some constant integer range?

bool can_represent(double x) const#

Can this type represent a particular constant?

bool is_max(uint64_t) const#

Check if an integer constant value is the maximum or minimum representable value for this type.

Expr max() const#

Return an expression which is the maximum value of this type.

Returns infinity for types which can represent it.

Expr min() const#

Return an expression which is the minimum value of this type.

Returns -infinity for types which can represent it.

Public Static Functions

static Type Struct(const std::vector<StructField> &fields)#

Construct a packed, named-field aggregate type, matching a C struct with no alignment padding.

The resulting Type has its own honest type code (Type::Struct); its packed byte size is recorded so it survives erasure to the ABI (see to_abi()).

Public Static Attributes

static halide_type_code_t Int = halide_type_int#

Aliases for halide_type_code_t values for legacy compatibility and to match the Halide internal C++ style.

static int kLanesBits = 8 * sizeof(type_lanes)#

Exposed so code that needs to reason about the maximum representable lanes count (e.g.

overflow checks when combining vectors) can derive it from Type itself instead of hardcoding a width.