Skip to content

Wrong alignment on vectors #3190

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

Open
ilcheese2 opened this issue Apr 9, 2025 · 2 comments
Open

Wrong alignment on vectors #3190

ilcheese2 opened this issue Apr 9, 2025 · 2 comments

Comments

@ilcheese2
Copy link
Contributor

Since vectors are defined with type aliases they have an incorrect alignment.

For example this has an alignment of 8

typedef float float2 __attribute__((vector_size(2 * sizeof(float))));

The generated bindgen code is

pub type float2 = [f32; 2usize];

which has an alignment of 4 instead of the expected 8.

@rich-ayr
Copy link

rich-ayr commented Apr 10, 2025

__vector_size__/ext_vector_type should work, is vector_size standard/documented syntax?

struct foo {
__attribute__((__vector_size__(1 * sizeof(long long)))) long long mMember;
};

typedef float float4 __attribute__((ext_vector_type(4)));
typedef float float2 __attribute__((ext_vector_type(2)));

Could just be an uncaught variant, some of the work done here is only 2 weeks old.

@ilcheese2
Copy link
Contributor Author

The issue isn't specific to that syntax; using ext_vector_type would result in the same problem.
The current implementation for vectors represents them with an array which have a different alignment than the SIMD types.

TypeKind::Array(item, len) | TypeKind::Vector(item, len) => {
let ty = item.try_to_rust_ty(ctx, &())?;
Ok(syn::parse_quote! { [ #ty ; #len ] })

A solution would be to wrap them in a struct to preserve alignment similar to what's being done for opaque arrays
if ctx.options().enable_cxx_namespaces {
syn::parse_quote! { root::__BindgenOpaqueArray<#ty, #data_len> }
} else {
syn::parse_quote! { __BindgenOpaqueArray<#ty, #data_len> }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants