Skip to content

core: add alignof<T>(), the alignment of a type, beside sizeof<T>() #4575

Backlog

Description

@hiraditya

Vx has sizeof<T>() but no alignof<T>(). Code that needs the alignment of a type must measure it.

PR #4565 adds std::alloc::heap_align<T>() for Vec, Box and the other containers. A <4 x f64> needs an address that is a multiple of 32, and malloc gives 16. heap_align measures the alignment of T with a helper struct:

struct AlignProbe<T> { byte : u8, value : T, }
// value starts at the first multiple of the alignment of T after byte.
let align = sizeof<AlignProbe<T>>() - sizeof<T>();

This works for every type that sizeof knows, but it is indirect. core::mem has no align_of for the same reason (see the list of missing functions in stdlib/core/mem.vx). docs/implementation_plans/heap_memory_and_c_functions.md lists "alignof<T>() for Vec" as open.

Request

Add an alignof<T>() intrinsic beside sizeof<T>(), in both code generators. It gives the alignment from the data layout, as sizeof gives the size. Then:

  • heap_align<T>() uses it instead of AlignProbe.
  • core::mem gets align_of<T>().

A test checks alignof for a number, a <4 x f64>, a struct with a vector field and a struct of bytes, with both code generators.

Generated With Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions