Skip to content

add an is_aligned intrinsic to rust #42561

@oli-obk

Description

@oli-obk

This intrinsic takes a raw pointer and an alignment and checks whether the pointer is aligned wrt the given alignment. Implementations like miri can use this to always return false, if the requested alignment is bigger than the alignment given at creation of the allocation that the pointer points into.

In the future platforms which don't require alignment can return true unconditionally, which will probably allow more optimizations to trigger on those platforms.

The signature of the intrinsic can either be fn<T>(* const T, usize) or just fn<T>(* const T) where the latter obtains the alignment to be checked from the pointee type.

Most platforms should simply generate (ptr as usize) & ((1 << (align - 1) - 1) == 0

An example of such a check in the stdlib is

let align = (ptr as usize + index) & (usize_bytes - 1);
which is part of utf8 checking. Without such an intrinsic, miri can never do utf8 checking.

Does a change like this require an rfc?

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