-
Notifications
You must be signed in to change notification settings - Fork 969
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] parse and validate @must_use
attribute
#6801
base: trunk
Are you sure you want to change the base?
Conversation
17da613
to
1331f0f
Compare
@must_use
attribute@must_use
attribute
1331f0f
to
201ce67
Compare
Looking at discussion in the
I expect that many particularities may complicate validation, so tests are provided for slightly complex valid examples. I'm unfamiliar with how naga's AST behaves, and looking through the validation code, it didn't seem obvious where/how to validate that a |
|
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.
some commentary
pub struct FunctionResult<'a> { | ||
pub ty: Handle<Type<'a>>, | ||
pub binding: Option<Binding<'a>>, | ||
pub must_use: bool, | ||
} |
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.
must_use
is a required member of wgsl ast FunctionResult
.
Some(ast::FunctionResult { | ||
ty, | ||
binding, | ||
must_use, | ||
}) |
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.
ast::FunctionResult
creations are updated
naga/src/front/wgsl/parse/mod.rs
Outdated
@@ -2455,6 +2460,7 @@ impl Parser { | |||
let (mut bind_index, mut bind_group) = | |||
(ParsedAttribute::default(), ParsedAttribute::default()); | |||
let mut id = ParsedAttribute::default(); | |||
let mut must_use = ParsedAttribute::default(); |
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.
attribute is handled in global_decl
is there another location in which @must_use
might validly appear?
should invalid locations recognize the attribute at all?
possibly, some global declarations should not have the @must_use
attribute. no checks are performed.
naga/src/front/wgsl/parse/mod.rs
Outdated
@@ -2333,6 +2333,7 @@ impl Parser { | |||
diagnostic_filter_leaf: Option<Handle<DiagnosticFilterNode>>, | |||
out: &mut ast::TranslationUnit<'a>, | |||
dependencies: &mut FastIndexSet<ast::Dependency<'a>>, | |||
must_use: bool, |
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 added a parameter to function_decl
for this attribute.
i'm going to revise this now that i can run conformance tests after #6840 it is not presently ready for review |
201ce67
to
31b3e47
Compare
Signed-off-by: sagudev <[email protected]>
{"fail_fast": false, "matrix": [{"name": "WebGPU CTS", "workflow": "linux", "wpt_layout": "2020", "profile": "production", "unit_tests": false, "bencher": false, "wpt_args": "_webgpu"}]}
Unfortunately am not sure how relevant information from cts_runner actually is, as there is no concept of (bad) expectations (so only selected tests are run) and it uses old CTS version. The most useful results come from browsers running CTS, either Firefox or Servo as both run all test and have expectations properly set, so you know what results become different using your PR. In servo you can simply override wgpu in Cargo.toml (ensuring that your PR bases from same revision of wgpu previous used in servo) then run |
yes, realizing this since i patched things together. i was hoping to have canonical tests for edge cases and ambiguities, that could execute in a nice automated way, but it seems like the validation tests can't run in cts_runner without more api work. maybe this repo should remove cts_runner and the documentation that suggests using it, if it's no longer relevant. thanks for the instructions for executing in servo's ci! this is very useful |
I've ran the CTS without and with this in firefox and it looks good. There are no new failures, and there are a few new passes as expected. There are also quite a few more places where previously we failed due to As you noted above this does have the limitation that it doesn't ensure the result of @turbocrime do you still have changes planned for this, or would you like me to review as-is? Or if you're too busy I'm happy to take this over the line for you. LMK! |
#[test] | ||
#[ignore = "validation can't be tested if parser fails"] | ||
fn must_use_unused_validation() { | ||
check_validation! { | ||
" | ||
@must_use | ||
fn use_me(a: i32) -> i32 { | ||
return 10; | ||
} | ||
|
||
fn useless() -> i32 { | ||
use_me(1); | ||
return 0; | ||
} | ||
": | ||
Err( | ||
naga::valid::ValidationError::Function { | ||
handle: _caller, | ||
name: caller_name, | ||
source: naga::valid::FunctionError::InvalidCall { | ||
function: _callee, | ||
error: naga::valid::CallError::ResultNotUsed(_result), | ||
}, | ||
}, | ||
) | ||
if caller_name == "useless" | ||
} | ||
} | ||
|
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.
not sure how to test validation if the parser will reject the shader. perhaps i could manually create the AST by parsing a shader without the attribute and then modifying it.
i assume the builtins aren't presently 'annotated'. if must_use builtins are expressed correctly in AST, perhaps validation phase can catch that. validation code is present and works if i remove the parser error, but i'm not sure how to test it when the parser throws. i'm not sure if the validation code is actually useful - would the parser know about builtins' attributes at parsing time? will the parsing step ever be skipped? do we care about that validation if the parsing step is skipped?
i had a local commit sitting around that reverted some random junk. pushed that up, ready for you to take a look. i will have way less free time starting monday so feel free to take over. |
6247e79
to
cfe925f
Compare
if you'd like to see the validation phase successfully detect an unused call, see f028d07 where i have disabled the parser failures |
this is covered by the parser, but perhaps the validator should also error if it encounters a call to some |
@must_use
attribute@must_use
attribute
ready for review
to be revised now that i can run conformance testsnot worrying about conformance tests, as cts_runner seems disused.
Connections
initial work on #5186
Description
When the wgsl parser encounters the
@must_use
function attribute, it should not error.This PR will recognize the attribute and emit a new field in function AST. Both the parser and the validator stage will use this attribute.
The parser requires that the annotated function should return a value, and requires that the function call appears in an expression.
The validator requires that return value is referenced or assigned.
A few tests are added covering these behaviors and more.
Testing
Checklist
cargo fmt
.taplo format
.cargo clippy
. If applicable, add:--target wasm32-unknown-unknown
--target wasm32-unknown-emscripten
cargo xtask test
to run tests.CHANGELOG.md
. See simple instructions inside file.