Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow aligned allocations in NIFs #6665

Open
hauleth opened this issue Jan 13, 2023 · 6 comments
Open

Allow aligned allocations in NIFs #6665

hauleth opened this issue Jan 13, 2023 · 6 comments
Assignees
Labels
enhancement team:VM Assigned to OTP team VM

Comments

@hauleth
Copy link
Contributor

hauleth commented Jan 13, 2023

Is your feature request related to a problem? Please describe.

When using Rust (via Rustler) it would be handy to have a way to use Rust GlobalAlloc with enif_alloc function. The problem is that enif_alloc do not have counterpart that allows passing alignment, which would be needed there. This can sometimes lead to SIGBUS errors.

Describe the solution you'd like

Have another allocation function (for example void *enif_alloc_aligned(size_t size, size_t alignment)) that would allow to explicitly set the alignment. It will then pick the max(erlang_alignment, alignment) and will use that as the target alignment (AFAIK we can always expand alignment, but never contract it).

With such hypothetical function we could then implement Rust GlobalAlloc as:

struct ErlGlobalAlloc;

unsafe impl GlobalAlloc for ErlGlobalAlloc {
    fn alloc(&self, layout: Layout) -> *mut u8 {
        enif_alloc_aligned(layout.size, layout.align)
    }

    // Rest of the functions
}

Describe alternatives you've considered

I was thinking about implementing this as try and error or by doing some extra magic with allocating more memory than needed and then storing required metadata there, but all of that is just set of hacks, that could be instead implemented directly in OTP.

Additional context

I believe that it would be useful to have such function, as it may help with having Rust global allocator that will allow introspection and observability of memory usage in Rustler NIFs.

@rickard-green rickard-green added the team:VM Assigned to OTP team VM label Jan 16, 2023
@hauleth
Copy link
Contributor Author

hauleth commented Feb 20, 2023

I see that there already is such function in ERTS, but I am not experienced enough right now to come up with an idea how to expose such function there.

@sverker
Copy link
Contributor

sverker commented Feb 22, 2023

I see that there already is such function in ERTS

What function are you referring to?

@hauleth
Copy link
Contributor Author

hauleth commented Feb 22, 2023

erts_sys_aligned_alloc in erts/emulator/sys/unix/sys.c and erts/emulator/sys/win32/sys.c

@PastMoments
Copy link

I'm also in need of this (or something similar). For me, I am attempting to use enif_alloc_resource with SIMD intrinsics. From what I'm seeing, __m256d (4 packed doubles) requires a 32 byte alignment, but the address given by enif_alloc_resource is often not properly aligned, causing a crash.

@filmor
Copy link
Contributor

filmor commented Aug 30, 2024

@hauleth I added GlobalAlloc support for Rustler a while ago in rusterlium/rustler#580. A custom alignment for enif_alloc would still be highly appreciated, less code is always better :)

I'll try to create a PR for this.

@ityonemo
Copy link

currently tracking it in zigler:

E-xyza/zigler#488

though here we use a custom allocator for wide alignments:

https://github.com/E-xyza/zigler/blob/93f9526e477867770e5f075343b1f396e000d704/priv/beam/allocator.zig#L86-L132

If there's a better internally supported choice, that would be amazing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement team:VM Assigned to OTP team VM
Projects
None yet
Development

No branches or pull requests

6 participants