[wgsl-in] Only splat 'scalar op vec', not '_ op vec' (#2444)

Change binary_op_splat() from splatting:

        vec op scalar
        _ op vec

to only splat:

        vec op scalar
        scalar op vec
This commit is contained in:
Fredrik Fornwall 2023-08-18 15:16:44 +02:00 committed by GitHub
parent 3da9355125
commit b001313947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 8 deletions

View File

@ -525,13 +525,8 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
) {
self.grow_types(*left)?.grow_types(*right)?;
let left_size = match *self.resolved_inner(*left) {
crate::TypeInner::Vector { size, .. } => Some(size),
_ => None,
};
match (left_size, self.resolved_inner(*right)) {
(Some(size), &crate::TypeInner::Scalar { .. }) => {
match (self.resolved_inner(*left), self.resolved_inner(*right)) {
(&crate::TypeInner::Vector { size, .. }, &crate::TypeInner::Scalar { .. }) => {
*right = self.append_expression(
crate::Expression::Splat {
size,
@ -540,7 +535,7 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
self.get_expression_span(*right),
);
}
(None, &crate::TypeInner::Vector { size, .. }) => {
(&crate::TypeInner::Scalar { .. }, &crate::TypeInner::Vector { size, .. }) => {
*left = self.append_expression(
crate::Expression::Splat { size, value: *left },
self.get_expression_span(*left),

View File

@ -433,6 +433,24 @@ fn binary_expression_mixed_scalar_and_vector_operands() {
assert_eq!(found_expressions, 1);
}
let module = parse_str(
"@fragment
fn main(mat: mat3x3<f32>) {
let vec = vec3<f32>(1.0, 1.0, 1.0);
let result = mat / vec;
}",
)
.unwrap();
let expressions = &&module.entry_points[0].function.expressions;
let found_splat = expressions.iter().any(|(_, e)| {
if let crate::Expression::Binary { left, .. } = *e {
matches!(&expressions[left], &crate::Expression::Splat { .. })
} else {
false
}
});
assert!(!found_splat, "'mat / vec' should not be splatted");
}
#[test]