From d034c4b4282eab7e44425304a1133117d52918cf Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Wed, 9 Oct 2024 14:08:15 -0700 Subject: [PATCH] [naga spv-out] Move code to load a pointer into its own function. Introduce a new function, `naga::back::spv::BlockContext::write_checked_load`, that does the work of `Expression::Load`. This change is just code motion, and should have no effect on behavior. The new function will be used in later commits. --- naga/src/back/spv/block.rs | 114 ++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 52 deletions(-) diff --git a/naga/src/back/spv/block.rs b/naga/src/back/spv/block.rs index 6161fa089..2e342295e 100644 --- a/naga/src/back/spv/block.rs +++ b/naga/src/back/spv/block.rs @@ -1355,58 +1355,7 @@ impl<'w> BlockContext<'w> { } crate::Expression::LocalVariable(variable) => self.function.variables[&variable].id, crate::Expression::Load { pointer } => { - match self.write_expression_pointer(pointer, block, None)? { - ExpressionPointer::Ready { pointer_id } => { - let id = self.gen_id(); - let atomic_space = - match *self.fun_info[pointer].ty.inner_with(&self.ir_module.types) { - crate::TypeInner::Pointer { base, space } => { - match self.ir_module.types[base].inner { - crate::TypeInner::Atomic { .. } => Some(space), - _ => None, - } - } - _ => None, - }; - let instruction = if let Some(space) = atomic_space { - let (semantics, scope) = space.to_spirv_semantics_and_scope(); - let scope_constant_id = self.get_scope_constant(scope as u32); - let semantics_id = self.get_index_constant(semantics.bits()); - Instruction::atomic_load( - result_type_id, - id, - pointer_id, - scope_constant_id, - semantics_id, - ) - } else { - Instruction::load(result_type_id, id, pointer_id, None) - }; - block.body.push(instruction); - id - } - ExpressionPointer::Conditional { condition, access } => { - //TODO: support atomics? - self.write_conditional_indexed_load( - result_type_id, - condition, - block, - move |id_gen, block| { - // The in-bounds path. Perform the access and the load. - let pointer_id = access.result_id.unwrap(); - let value_id = id_gen.next(); - block.body.push(access); - block.body.push(Instruction::load( - result_type_id, - value_id, - pointer_id, - None, - )); - value_id - }, - ) - } - } + self.write_checked_load(pointer, block, result_type_id)? } crate::Expression::FunctionArgument(index) => self.function.parameter_id(index), crate::Expression::CallResult(_) @@ -1947,6 +1896,67 @@ impl<'w> BlockContext<'w> { } } + fn write_checked_load( + &mut self, + pointer: Handle, + block: &mut Block, + result_type_id: Word, + ) -> Result { + match self.write_expression_pointer(pointer, block, None)? { + ExpressionPointer::Ready { pointer_id } => { + let id = self.gen_id(); + let atomic_space = + match *self.fun_info[pointer].ty.inner_with(&self.ir_module.types) { + crate::TypeInner::Pointer { base, space } => { + match self.ir_module.types[base].inner { + crate::TypeInner::Atomic { .. } => Some(space), + _ => None, + } + } + _ => None, + }; + let instruction = if let Some(space) = atomic_space { + let (semantics, scope) = space.to_spirv_semantics_and_scope(); + let scope_constant_id = self.get_scope_constant(scope as u32); + let semantics_id = self.get_index_constant(semantics.bits()); + Instruction::atomic_load( + result_type_id, + id, + pointer_id, + scope_constant_id, + semantics_id, + ) + } else { + Instruction::load(result_type_id, id, pointer_id, None) + }; + block.body.push(instruction); + Ok(id) + } + ExpressionPointer::Conditional { condition, access } => { + //TODO: support atomics? + let value = self.write_conditional_indexed_load( + result_type_id, + condition, + block, + move |id_gen, block| { + // The in-bounds path. Perform the access and the load. + let pointer_id = access.result_id.unwrap(); + let value_id = id_gen.next(); + block.body.push(access); + block.body.push(Instruction::load( + result_type_id, + value_id, + pointer_id, + None, + )); + value_id + }, + ); + Ok(value) + } + } + } + /// Build the instructions for matrix - matrix column operations #[allow(clippy::too_many_arguments)] fn write_matrix_matrix_column_op(