Skip to content

Optimize constant casts to avoid unnecessary capabilities #302

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

Merged
merged 3 commits into from
Jun 30, 2025

Conversation

LegNeato
Copy link
Collaborator

Add constant folding for widening casts (u8->u32, f32->f64) to avoid creating intermediate types. Remove capability checks during type creation and add bidirectional validation in linker to remove unused type capabilities.

Fixes #300

@LegNeato
Copy link
Collaborator Author

LegNeato commented Jun 27, 2025

I think this is safe to do...everywhere we widen a const, just replace with a wider const, after everything is done look to see what types we use and match the capabilities used. But I haven't really thought through all the cases. One thing that can happen if there are a ton of constant casts we could be creating a ton of constants rather than a ton of cast instructions...not sure if that is better or worse from a perf perspective.

Add constant folding for widening casts (u8->u32, f32->f64) to avoid creating
intermediate types. Remove capability checks during type creation and add
bidirectional validation in linker to remove unused type capabilities.

Fixes Rust-GPU#300
@nazar-pc
Copy link
Contributor

Will this handle a case like u32::from(K)? To rustc/LLVM it is basically the same thing, but likely requires inlining to happen first.

@LegNeato
Copy link
Collaborator Author

LegNeato commented Jun 28, 2025

No, this only does as casts.

@LegNeato
Copy link
Collaborator Author

Pushed another commit to support From::from

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.

Very nice!

I'd love to have a remove_unused_type_capabilities for all capabilities in the future, so instead of pre-declaring all needed capabilities, each shader would just have the capabilities it needs, and end users can use standard features instead of relying on target_features to mask out certain capabilities.

@Firestar99 Firestar99 added this pull request to the merge queue Jun 30, 2025
Merged via the queue into Rust-GPU:main with commit 598682c Jun 30, 2025
13 checks passed
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

Successfully merging this pull request may close these issues.

Capability is required unexpectedly for constant cast
3 participants