-
Notifications
You must be signed in to change notification settings - Fork 977
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
[naga] resolve the size of override-sized arrays in backends #6787
Conversation
d4f5aa3
to
998667d
Compare
In #6788 (comment) I was specifically talking about the issue Jim brought up in #6788; this PR doesn't touch the compactor.
This PR obsoletes #6746. |
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.
Still reviewing.
@cwfitzgerald via Wgpu Users in reply to @junglie85
Wouldn't this mean that it's better to resolve the size of override-sized arrays on the front end to minimize runtime overhead? |
Eh, way more work than the gain - that's not a very common workflow. |
998667d
to
bd02114
Compare
Is this an accurate description of the new /// Replace all overrides in `module` with fully-evaluated constant expressions.
///
/// Given `pipeline_constants`, providing values for all overrides in
/// `module`:
///
/// - Replace all [`Override`] expressions with fully-evaluated
/// constant expressions.
///
/// - Replace all [`Override`][paso] array sizes with [`Expression`]
/// array sizes, referring to fully-evaluated constant expressions.
///
/// - Empty out the `module.overrides` arena.
///
/// Although the above is described in terms of changes to `module`'s
/// contents, this function only actually has shared access to
/// `module`. When changes are needed, this function clones `module`
/// and returns a [`Cow::Owned`] value. If no changes are needed, this
/// function returns a [`Cow::Borrowed`] value that just passes along
/// the original reference.
///
/// [`Override`]: Expression::Override
/// [paso]: crate::PendingArraySize::Override
/// [`Expression`]: crate::PendingArraySize::Expression
|
That does sound more accurate than the current doc. Do you want to push the commit? Or else I can fixup the first commit. |
bd02114
to
b70b3e1
Compare
b70b3e1
to
ed3cb16
Compare
// When `n` is overridden to 14 above, it will generate the type `array<u32, 14 - 2>` which | ||
// already exists in the program because of variable declaration. Ensures naga does not panic | ||
// when this happens. |
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.
Great test case.
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.
Kudos to @kentslaney.
adjusted_global_expressions[size_expr] | ||
let expr = adjusted_global_expressions[size_expr]; | ||
if expr != size_expr { | ||
module.types.replace( |
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.
UniqueArena::replace
is specified to panic if the new value already exists in the arena. The WGSL front end also creates PendingArraySize::Expression
array sizes. Why do we believe that these replace
calls will never panic?
If this isn't a bug, it definitely deserves a comment.
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.
The expression handle in PendingArraySize::Expression
is the only handle to that expression.
I realize that this is not validated though, I will add some validation for this.
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.
The expression handle in
PendingArraySize::Expression
is the only handle to that expression.
This is needed for type equivalency, arrays sized via an override expression should never be equal to other arrays.
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.
The validation is not necessary, I missed the fact that we are working with a UniqueArena
#6787 (comment).
ed3cb16
to
3450d84
Compare
3450d84
to
eed72e6
Compare
@@ -616,6 +614,7 @@ impl Validator { | |||
.into_boxed_slice(), | |||
}; | |||
|
|||
let mut array_override_expr_set = HandleSet::new(); |
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.
There's something about this check that makes me uncomfortable. It seems like either the UniqueArena will ensure the expressions are unique automatically, in which case the check will never fail, or you've got array types with distinct base/stride but the same expression, which doesn't seem necessary to forbid.
Do we have a test case that exercises this check?
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.
You are right, this validation is not needed. When thinking about your comment about UniqueArena::replace
(#6787 (comment)) I thought it was needed.
I will remove it and add a comment instead.
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.
I think we need to find a way to handle this without editing the type arena at all.
const X: array<u32, 2> = array(1, 2);
override Y = X[0u];
override Z = X[0u];
var<workgroup> aY: array<u32, Y>;
var<workgroup> aZ: array<u32, Z>;
This panics because the types aren't unique:
$ d=$HOME/play/naga-array-override && cd ~/rust/wgpu/naga-cli && RUST_BACKTRACE=1 cargo run $d/in.wgsl $d/out.metal
Running `/home/jimb/rust/wgpu/target/debug/naga /home/jimb/play/naga-array-override/in.wgsl /home/jimb/play/naga-array-override/out.metal`
thread 'main' panicked at naga/src/arena/unique_arena.rs:153:9:
assertion failed: added && index == self.set.len() - 1
stack backtrace:
...
3: naga::arena::unique_arena::UniqueArena<T>::replace
at /home/jimb/rust/wgpu/naga/src/arena/unique_arena.rs:153:9
4: naga::back::pipeline_constants::process_pending
at /home/jimb/rust/wgpu/naga/src/back/pipeline_constants.rs:260:21
5: naga::back::pipeline_constants::process_overrides
at /home/jimb/rust/wgpu/naga/src/back/pipeline_constants.rs:210:5
6: naga::write_output
at ./src/bin/naga.rs:717:17
7: naga::run
at ./src/bin/naga.rs:582:9
8: naga::main
at ./src/bin/naga.rs:361:21
Constant evaluation handles AccessIndex { base, index }
by simply returning the index
'th subexpression of base
, so both Y
and Z
end up with Override::init
holding the same Handle<Expression>
. Then process_pending
tries to use that handle for both types.
I have different idea about how to solve this. I'll put together a PR tomorrow. Leaving this open for the time being, since I'm not sure how well my idea will work. |
is it worth taking another look at #6746? |
Connections
Fixes #6722
Closes #6746
Description
Implements Jim's suggestion of resolving the size of override-sized arrays in backends so that we don't have to rebuild the types arena when processing overrides.
Testing
The new and existing tests passed.