Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix union field access by representing unions as structs #269

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

Draft
LegNeato wants to merge 1 commit into Rust-GPU:main
base: main
Choose a base branch
Loading
from LegNeato:issue241

Conversation

Copy link
Collaborator

@LegNeato LegNeato commented Jun 3, 2025

When a union has multiple fields, represent it as a struct with all fields at offset 0 instead of just using the largest field. This allows pointer casts between union fields to work correctly by enabling the recover_access_chain_from_offset function to find valid access chains.

Fixes #241.

Copy link
Collaborator Author

LegNeato commented Jun 3, 2025

@simensgreen can you check this fix?

@LegNeato LegNeato changed the title (削除) Fix union field access by representing unions as structs in SPIR-V (削除ここまで) (追記) Fix union field access by representing unions as structs (追記ここまで) Jun 3, 2025
When a union has multiple fields, represent it as a struct with all fields
at offset 0 instead of just using the largest field. This allows pointer
casts between union fields to work correctly by enabling the
recover_access_chain_from_offset function to find valid access chains.
Fixes Rust-GPU#241.
Copy link

@LegNeato Ok, in this simple example it works, but in a more complicated scenario, it will not.

Now I can reproduce both cannot cast between pointer types and cannot memcpy dynamically sized data, you can check it out in my repo

Full error messages:

error: cannot cast between pointer types
 from `*struct DataMatrix { array1d: [f32; 25], array2d: [[f32; 5]; 5], data_array: [struct Data { a: f32, b: [f32; 3], c: f32 }; 5] }`
 to `*[[f32; 5]; 5]`
 --> shader/src/lib.rs:35:77
 |
 35 | pub fn array2d_ref(&self) -> &[[f32; 5]; 5] { unsafe { &self.array2d } }
 | ^
 |
 note: used from within `<shader::DataMatrix>::array2d_ref`
 --> shader/src/lib.rs:35:77
 |
 35 | pub fn array2d_ref(&self) -> &[[f32; 5]; 5] { unsafe { &self.array2d } }
 | ^
 note: called by `shader::main`
 --> shader/src/lib.rs:50:18
 |
 50 | output[id] = input_matrices[id].array2d_ref()[0];
 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: cannot memcpy dynamically sized data
 --> shader/src/lib.rs:34:54
 |
34 | pub fn array2d(self) -> [[f32; 5]; 5] { unsafe { self.array2d } }
 | ^^^^^^^^^^^^
 |
note: used from within `<shader::DataMatrix>::array2d`
 --> shader/src/lib.rs:34:54
 |
34 | pub fn array2d(self) -> [[f32; 5]; 5] { unsafe { self.array2d } }
 | ^^^^^^^^^^^^
note: called by `shader::main`
 --> shader/src/lib.rs:52:18
 |
52 | output[id] = input_matrices[id].array2d()[0];
 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LegNeato reacted with thumbs up emoji

Copy link
Member

@Firestar99 Firestar99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocked by #270 due to adding a test

Copy link
Collaborator Author

LegNeato commented Jun 4, 2025

Ok, this code is a minefield...back to the drawingboard ha.

@LegNeato LegNeato marked this pull request as draft June 4, 2025 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@Firestar99 Firestar99 Firestar99 requested changes

@eddyb eddyb Awaiting requested review from eddyb eddyb is a code owner

@schell schell Awaiting requested review from schell schell is a code owner

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Union cast failed

AltStyle によって変換されたページ (->オリジナル) /