Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

[wgsl-in] Implement phony assignment #1866

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/front/wgsl/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ impl<'a> Lexer<'a> {

pub(super) fn next_ident_with_span(&mut self) -> Result<(&'a str, Span), Error<'a>> {
match self.next() {
(Token::Word(word), span) if word == "_" => {
Err(Error::InvalidIdentifierUnderscore(span))
}
(Token::Word(word), span) if word.starts_with("__") => {
Err(Error::ReservedIdentifierPrefix(span))
}
Expand Down
21 changes: 21 additions & 0 deletions src/front/wgsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub enum Error<'a> {
InvalidForInitializer(Span),
InvalidGatherComponent(Span, i32),
InvalidConstructorComponentType(Span, i32),
InvalidIdentifierUnderscore(Span),
ReservedIdentifierPrefix(Span),
UnknownAddressSpace(Span),
UnknownAttribute(Span),
Expand Down Expand Up @@ -362,6 +363,11 @@ impl<'a> Error<'a> {
labels: vec![(bad_span.clone(), "invalid component type".into())],
notes: vec![],
},
Error::InvalidIdentifierUnderscore(ref bad_span) => ParseError {
message: "Identifier can't be '_'".to_string(),
labels: vec![(bad_span.clone(), "invalid identifier".into())],
notes: vec!["Use phony assignment instead ('_ =' notice the absence of 'let' or 'var')".to_string()],
},
Error::ReservedIdentifierPrefix(ref bad_span) => ParseError {
message: format!("Identifier starts with a reserved prefix: '{}'", &source[bad_span.clone()]),
labels: vec![(bad_span.clone(), "invalid identifier".into())],
Expand Down Expand Up @@ -3489,6 +3495,21 @@ impl Parser {
(Token::Word(word), _) => {
let mut emitter = super::Emitter::default();
let statement = match word {
"_" => {
let _ = lexer.next();
emitter.start(context.expressions);
lexer.expect(Token::Operation('='))?;
let expr_id = self.parse_general_expression(
lexer,
context.as_expression(block, &mut emitter),
)?;
lexer.expect(Token::Separator(';'))?;
block.extend(emitter.finish(context.expressions));
// TODO: the situation here could be improved but
// for now this is necessary to get the expressions emitted
context.named_expressions.insert(expr_id, "_".to_string());
None
}
"let" => {
let _ = lexer.next();
emitter.start(context.expressions);
Expand Down
16 changes: 8 additions & 8 deletions tests/in/access.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ fn test_matrix_within_struct_accesses() {
idx--;

// loads
var _ = baz.m;
var _ = baz.m[0];
var _ = baz.m[idx];
var _ = baz.m[0][1];
var _ = baz.m[0][idx];
var _ = baz.m[idx][1];
var _ = baz.m[idx][idx];
_ = baz.m;
_ = baz.m[0];
_ = baz.m[idx];
_ = baz.m[0][1];
_ = baz.m[0][idx];
_ = baz.m[idx][1];
_ = baz.m[idx][idx];

var t = Baz(mat3x2<f32>(vec2<f32>(1.0), vec2<f32>(2.0), vec2<f32>(3.0)));

Expand Down Expand Up @@ -83,7 +83,7 @@ fn foo_vert(@builtin(vertex_index) vi: u32) -> @builtin(position) vec4<f32> {
c[vi + 1u] = 42;
let value = c[vi];

var _ = test_arr_as_arg(array<array<f32, 10>, 5>());
_ = test_arr_as_arg(array<array<f32, 10>, 5>());

return vec4<f32>(_matrix * vec4<f32>(vec4<i32>(value)), 2.0);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/in/globals.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ fn test_msl_packed_vec3() {
let data = alignment;

// loads
let _ = data.v3;
let _ = data.v3.zx;
_ = data.v3;
_ = data.v3.zx;
test_msl_packed_vec3_as_arg(data.v3);

// matrix vector multiplication
let _ = data.v3 * mat3x3<f32>();
let _ = mat3x3<f32>() * data.v3;
_ = data.v3 * mat3x3<f32>();
_ = mat3x3<f32>() * data.v3;

// scalar vector multiplication
let _ = data.v3 * 2.0;
let _ = 2.0 * data.v3;
_ = data.v3 * 2.0;
_ = 2.0 * data.v3;
}

@compute @workgroup_size(1)
Expand Down
Loading