From 4f22f8d7b6e78ecf7a14e4f96ce3d471eb82af00 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Tue, 24 Dec 2024 12:03:51 -0800 Subject: [PATCH] [naga wgsl-in] Apply automatic conversions to values being assigned. Apply automatic conversions to the right-hand sides of assignment and compound assignment statements. Fix `try_automatic_conversion_for_leaf_scalar` to handle arrays, not just scalars, vectors, and matrices. Previously this was only used for converting the right-hand sides of bit shift operators to `u32`, and bit shift operators don't support arrays. But now we're using `try_automatic_conversion_for_leaf_scalar` for ordinary assignments, and you can assign arrays. Update a test of propagating source code spans through compaction. This test needs validation to report an error, but this commit's fix causes the error in the test to be detected in the front end, which is too early to exercise compaction. Use a new error that the front end still won't notice. --- naga/src/front/wgsl/lower/conversion.rs | 28 ++++++++++++++++- naga/src/front/wgsl/lower/mod.rs | 41 +++++++++++++++++-------- naga/tests/wgsl_errors.rs | 11 ++++--- 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/naga/src/front/wgsl/lower/conversion.rs b/naga/src/front/wgsl/lower/conversion.rs index 37bb696f0..b7a649fd1 100644 --- a/naga/src/front/wgsl/lower/conversion.rs +++ b/naga/src/front/wgsl/lower/conversion.rs @@ -93,7 +93,7 @@ impl<'source> super::ExpressionContext<'source, '_, '_> { })) }; - let expr_scalar = match expr_inner.scalar() { + let expr_scalar = match expr_inner.automatically_convertible_scalar(&self.module.types) { Some(scalar) => scalar, None => return Err(make_error()), }; @@ -436,6 +436,32 @@ impl crate::TypeInner { | Ti::BindingArray { .. } => None, } } + + /// Return the leaf scalar type of `pointer`. + /// + /// `pointer` must be a `TypeInner` representing a pointer type. + pub fn pointer_automatically_convertible_scalar( + &self, + types: &crate::UniqueArena, + ) -> Option { + use crate::TypeInner as Ti; + match *self { + Ti::Scalar(scalar) | Ti::Vector { scalar, .. } | Ti::Matrix { scalar, .. } => { + Some(scalar) + } + Ti::Atomic(_) => None, + Ti::Pointer { base, .. } | Ti::Array { base, .. } => { + types[base].inner.automatically_convertible_scalar(types) + } + Ti::ValuePointer { scalar, .. } => Some(scalar), + Ti::Struct { .. } + | Ti::Image { .. } + | Ti::Sampler { .. } + | Ti::AccelerationStructure + | Ti::RayQuery + | Ti::BindingArray { .. } => None, + } + } } impl crate::Scalar { diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index fc31e43ec..50d012c4e 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -1700,31 +1700,48 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { } => { let mut emitter = Emitter::default(); emitter.start(&ctx.function.expressions); + let target_span = ctx.ast_expressions.get_span(ast_target); - let target = self.expression_for_reference( - ast_target, - &mut ctx.as_expression(block, &mut emitter), - )?; - let mut value = - self.expression(value, &mut ctx.as_expression(block, &mut emitter))?; - + let mut ectx = ctx.as_expression(block, &mut emitter); + let target = self.expression_for_reference(ast_target, &mut ectx)?; let target_handle = match target { Typed::Reference(handle) => handle, Typed::Plain(handle) => { let ty = ctx.invalid_assignment_type(handle); return Err(Error::InvalidAssignment { - span: ctx.ast_expressions.get_span(ast_target), + span: target_span, ty, }); } }; + // Usually the value needs to be converted to match the type of + // the memory view you're assigning it to. The bit shift + // operators are exceptions, in that the right operand is always + // a `u32` or `vecN`. + let target_scalar = match op { + Some(crate::BinaryOperator::ShiftLeft | crate::BinaryOperator::ShiftRight) => { + Some(crate::Scalar::U32) + } + _ => resolve_inner!(ectx, target_handle) + .pointer_automatically_convertible_scalar(&ectx.module.types), + }; + + let value = self.expression_for_abstract(value, &mut ectx)?; + let mut value = match target_scalar { + Some(target_scalar) => ectx.try_automatic_conversion_for_leaf_scalar( + value, + target_scalar, + target_span, + )?, + None => value, + }; + let value = match op { Some(op) => { - let mut ctx = ctx.as_expression(block, &mut emitter); - let mut left = ctx.apply_load_rule(target)?; - ctx.binary_op_splat(op, &mut left, &mut value)?; - ctx.append_expression( + let mut left = ectx.apply_load_rule(target)?; + ectx.binary_op_splat(op, &mut left, &mut value)?; + ectx.append_expression( crate::Expression::Binary { op, left, diff --git a/naga/tests/wgsl_errors.rs b/naga/tests/wgsl_errors.rs index 2d1669ada..a9c7344ec 100644 --- a/naga/tests/wgsl_errors.rs +++ b/naga/tests/wgsl_errors.rs @@ -2108,10 +2108,10 @@ fn compaction_preserves_spans() { let source = r#" fn f() { var a: i32 = -(-(-(-42i))); - var x: i32; - x = 42u; + var x: array; + var y = x[1.0]; } - "#; // ^^^ correct error span: 95..98 + "#; // ^^^ correct error span: 108..114 let mut module = naga::front::wgsl::parse_str(source).expect("source ought to parse"); naga::compact::compact(&mut module); let err = naga::valid::Validator::new( @@ -2135,7 +2135,10 @@ fn compaction_preserves_spans() { .0; if !matches!( dest_span.to_range(), - Some(std::ops::Range { start: 95, end: 98 }) + Some(std::ops::Range { + start: 108, + end: 114 + }) ) { panic!("Error message has wrong span:\n\n{err:#?}"); }