rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Wrong alignment on vectors

Open ilcheese2 opened this issue 8 months ago • 2 comments

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.

ilcheese2 avatar Apr 09 '25 20:04 ilcheese2

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

https://github.com/rust-lang/rust-bindgen/blob/97ab9152b5edb1fda1ced9bc1604f5e4dc9cfaa9/bindgen-tests/tests/headers/vector.hpp#L1-L3 https://github.com/rust-lang/rust-bindgen/blob/97ab9152b5edb1fda1ced9bc1604f5e4dc9cfaa9/bindgen-tests/tests/headers/opencl_vector.h#L1-L2

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

rich-ayr avatar Apr 10 '25 18:04 rich-ayr

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. https://github.com/rust-lang/rust-bindgen/blob/97ab9152b5edb1fda1ced9bc1604f5e4dc9cfaa9/bindgen/codegen/mod.rs#L4369-L4371 A solution would be to wrap them in a struct to preserve alignment similar to what's being done for opaque arrays https://github.com/rust-lang/rust-bindgen/blob/97ab9152b5edb1fda1ced9bc1604f5e4dc9cfaa9/bindgen/codegen/helpers.rs#L105-L109

ilcheese2 avatar Apr 10 '25 18:04 ilcheese2