From f2828ace38432926f0c0a4ddce098babc15b7809 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Fri, 8 Dec 2023 11:56:12 -0800 Subject: [PATCH] [naga wgsl-in] Separate out `convert_to_leaf_scalar`. Abstract out the body of `convert_slice_to_common_leaf_scalar`'s main loop into its own function. Code motion only, no intended change of behavior. --- naga/src/front/wgsl/lower/conversion.rs | 35 ++++++++++++++++--------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/naga/src/front/wgsl/lower/conversion.rs b/naga/src/front/wgsl/lower/conversion.rs index 419b8cbcfc..7378f0f63e 100644 --- a/naga/src/front/wgsl/lower/conversion.rs +++ b/naga/src/front/wgsl/lower/conversion.rs @@ -130,6 +130,28 @@ impl<'source, 'temp, 'out> super::ExpressionContext<'source, 'temp, 'out> { Ok(()) } + /// Convert `expr` to the leaf scalar type `scalar`. + pub fn convert_to_leaf_scalar( + &mut self, + expr: &mut Handle, + goal: crate::Scalar, + ) -> Result<(), super::Error<'source>> { + let inner = super::resolve_inner!(self, *expr); + // Do nothing if `inner` doesn't even have leaf scalars; + // it's a type error that validation will catch. + if inner.scalar() != Some(goal) { + let cast = crate::Expression::As { + expr: *expr, + kind: goal.kind, + convert: Some(goal.width), + }; + let expr_span = self.get_expression_span(*expr); + *expr = self.append_expression(cast, expr_span)?; + } + + Ok(()) + } + /// Convert all expressions in `exprs` to a common scalar type. /// /// Note that the caller is responsible for making sure these @@ -146,18 +168,7 @@ impl<'source, 'temp, 'out> super::ExpressionContext<'source, 'temp, 'out> { goal: crate::Scalar, ) -> Result<(), super::Error<'source>> { for expr in exprs.iter_mut() { - let inner = super::resolve_inner!(self, *expr); - // Do nothing if `inner` doesn't even have leaf scalars; - // it's a type error that validation will catch. - if inner.scalar() != Some(goal) { - let cast = crate::Expression::As { - expr: *expr, - kind: goal.kind, - convert: Some(goal.width), - }; - let expr_span = self.get_expression_span(*expr); - *expr = self.append_expression(cast, expr_span)?; - } + self.convert_to_leaf_scalar(expr, goal)?; } Ok(())