-
Notifications
You must be signed in to change notification settings - Fork 116
Fix slow compilation of large zero-initialized arrays; warn on large non-zero fills #586
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
39ali
wants to merge
1
commit into
Rust-GPU:main
Choose a base branch
from
39ali:array-memset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/compiletests/ui/lang/consts/large_array_nonzero_fill.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Tests that large non-zero-initialized arrays emit a compile warning. | ||
| // A `[v; N]` array with non-zero v generates N SPIR-V instructions (either | ||
| // store ops or OpConstantComposite operands), which is inherently slow. | ||
|
|
||
| // build-pass | ||
|
|
||
| #![no_std] | ||
| use spirv_std::glam::Vec4; | ||
| use spirv_std::spirv; | ||
|
|
||
| #[spirv(vertex)] | ||
| pub fn test_vs(#[spirv(push_constant)] index: &u32, #[spirv(position)] out_pos: &mut Vec4) { | ||
| let nonzeroed = [1.0f32; 2048]; | ||
|
|
||
| let i = *index as usize % 2048; | ||
| *out_pos = Vec4::new(nonzeroed[i], nonzeroed[i], 0.0, 1.0); | ||
| } |
8 changes: 8 additions & 0 deletions
8
tests/compiletests/ui/lang/consts/large_array_nonzero_fill.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| warning: large array of 2048 elements with a non-zero fill will generate 2048 SPIR-V store instructions, which may be slow to compile; consider using a storage buffer for large data instead | ||
| --> $DIR/large_array_nonzero_fill.rs:13:21 | ||
| | | ||
| LL | let nonzeroed = [1.0f32; 2048]; | ||
| | ^^^^^^^^^^^^^^ | ||
|
|
||
| warning: 1 warning emitted | ||
|
|
17 changes: 17 additions & 0 deletions
17
tests/compiletests/ui/lang/consts/large_array_zero_fill.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Tests that zero-initialized large arrays compile without warnings. | ||
| // A `[0; N]` array goes through `memset_const_pattern` with fill_byte=0, | ||
| // which should emit a single OpConstantNull | ||
|
|
||
| // build-pass | ||
|
|
||
| #![no_std] | ||
| use spirv_std::glam::Vec4; | ||
| use spirv_std::spirv; | ||
|
|
||
| #[spirv(vertex)] | ||
| pub fn test_vs(#[spirv(push_constant)] index: &u32, #[spirv(position)] out_pos: &mut Vec4) { | ||
| let zeroed = [0.0f32; 2048]; | ||
|
|
||
| let i = *index as usize % 2048; | ||
| *out_pos = Vec4::new(zeroed[i], zeroed[i], 0.0, 1.0); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Context (SPIR-T)
(Future) SPIR-T supports
OpConstantNull(e.g. in Rust-GPU/spirt#45) so this is definitely a good catch and won't cause any issues!I haven't implemented
SPV_EXT_replicated_composites(andVK_EXT_shader_replicated_compositeshas pretty bad adoption - according to https://vulkan.gpuinfo.org/listextensions.php - not to mention we'd need user opt-in anyway).However, SPIR-T could take
OpConstantCompositeReplicateEXTas input, and specifically Rust-GPU/spirt#45 would map it to either undoing the optimization, or optionally re-emittingOpConstantCompositeReplicateEXTon the output side if the extension is enabled etc. (and internally SPIR-T could optimize for this situation, but it doesn't really depend on how SPIR-V does any of this).What else you should/could do here:
OpConstantNullsupports all of these types, you can go as far as makingmemset_const_patternreturnOpConstantNullregardless of the type, and not keeping it specific to arrays (and it would also support e.g.structs)SpirvType::Adtsupport inmemset_const_patternyou just need to iterate over field typesconstant_compositeitself should check for this situation