[naga spv-out] Simplify Writer::get_pointer_id.

Simplify the definition of `naga:🔙:spv::Writer::get_pointer_id`
by using `get_type_id`'s ability to handle `LocalType::Pointer`.

This means that `get_pointer_id` is no longer fallible, and no longer
needs a type arena. Simplify callers, as well as the
`BlockContext::get_pointer_id` convenience function.
This commit is contained in:
Jim Blandy 2024-10-09 11:33:46 -07:00
parent 908e8353a8
commit 5c6b00886e
4 changed files with 15 additions and 44 deletions

View File

@ -381,7 +381,6 @@ impl<'w> BlockContext<'w> {
} }
}; };
let (id, variable) = self.writer.promote_access_expression_to_variable( let (id, variable) = self.writer.promote_access_expression_to_variable(
&self.ir_module.types,
result_type_id, result_type_id,
base_id, base_id,
base_ty, base_ty,

View File

@ -226,7 +226,7 @@ impl<'w> BlockContext<'w> {
let element_type_id = match self.ir_module.types[global.ty].inner { let element_type_id = match self.ir_module.types[global.ty].inner {
crate::TypeInner::BindingArray { base, size: _ } => { crate::TypeInner::BindingArray { base, size: _ } => {
let class = map_storage_class(global.space); let class = map_storage_class(global.space);
self.get_pointer_id(base, class)? self.get_pointer_id(base, class)
} }
_ => return Err(Error::Validation("array length expression case-5")), _ => return Err(Error::Validation("array length expression case-5")),
}; };

View File

@ -667,13 +667,8 @@ impl BlockContext<'_> {
.get_constant_scalar(crate::Literal::I32(scope as _)) .get_constant_scalar(crate::Literal::I32(scope as _))
} }
fn get_pointer_id( fn get_pointer_id(&mut self, handle: Handle<crate::Type>, class: spirv::StorageClass) -> Word {
&mut self, self.writer.get_pointer_id(handle, class)
handle: Handle<crate::Type>,
class: spirv::StorageClass,
) -> Result<Word, Error> {
self.writer
.get_pointer_id(&self.ir_module.types, handle, class)
} }
} }

View File

@ -141,7 +141,6 @@ impl Writer {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub(super) fn promote_access_expression_to_variable( pub(super) fn promote_access_expression_to_variable(
&mut self, &mut self,
ir_types: &UniqueArena<crate::Type>,
result_type_id: Word, result_type_id: Word,
container_id: Word, container_id: Word,
container_ty: Handle<crate::Type>, container_ty: Handle<crate::Type>,
@ -149,8 +148,7 @@ impl Writer {
element_ty: Handle<crate::Type>, element_ty: Handle<crate::Type>,
block: &mut Block, block: &mut Block,
) -> Result<(Word, LocalVariable), Error> { ) -> Result<(Word, LocalVariable), Error> {
let pointer_type_id = let pointer_type_id = self.get_pointer_id(container_ty, spirv::StorageClass::Function);
self.get_pointer_id(ir_types, container_ty, spirv::StorageClass::Function)?;
let variable = { let variable = {
let id = self.id_gen.next(); let id = self.id_gen.next();
@ -170,7 +168,7 @@ impl Writer {
let element_pointer_id = self.id_gen.next(); let element_pointer_id = self.id_gen.next();
let element_pointer_type_id = let element_pointer_type_id =
self.get_pointer_id(ir_types, element_ty, spirv::StorageClass::Function)?; self.get_pointer_id(element_ty, spirv::StorageClass::Function);
block.body.push(Instruction::access_chain( block.body.push(Instruction::access_chain(
element_pointer_type_id, element_pointer_type_id,
element_pointer_id, element_pointer_id,
@ -267,27 +265,13 @@ impl Writer {
pub(super) fn get_pointer_id( pub(super) fn get_pointer_id(
&mut self, &mut self,
arena: &UniqueArena<crate::Type>,
handle: Handle<crate::Type>, handle: Handle<crate::Type>,
class: spirv::StorageClass, class: spirv::StorageClass,
) -> Result<Word, Error> { ) -> Word {
let ty_id = self.get_type_id(LookupType::Handle(handle)); self.get_type_id(LookupType::Local(LocalType::Pointer {
if let crate::TypeInner::Pointer { .. } = arena[handle].inner {
return Ok(ty_id);
}
let lookup_type = LookupType::Local(LocalType::Pointer {
base: handle, base: handle,
class, class,
}); }))
Ok(if let Some(&id) = self.lookup_type.get(&lookup_type) {
id
} else {
let id = self.id_gen.next();
let instruction = Instruction::type_pointer(id, class, ty_id);
instruction.to_words(&mut self.logical_layout.declarations);
self.lookup_type.insert(lookup_type, id);
id
})
} }
pub(super) fn get_uint_type_id(&mut self) -> Word { pub(super) fn get_uint_type_id(&mut self) -> Word {
@ -369,11 +353,7 @@ impl Writer {
let class = spirv::StorageClass::Input; let class = spirv::StorageClass::Input;
let handle_ty = ir_module.types[argument.ty].inner.is_handle(); let handle_ty = ir_module.types[argument.ty].inner.is_handle();
let argument_type_id = match handle_ty { let argument_type_id = match handle_ty {
true => self.get_pointer_id( true => self.get_pointer_id(argument.ty, spirv::StorageClass::UniformConstant),
&ir_module.types,
argument.ty,
spirv::StorageClass::UniformConstant,
)?,
false => self.get_type_id(LookupType::Handle(argument.ty)), false => self.get_type_id(LookupType::Handle(argument.ty)),
}; };
@ -604,8 +584,7 @@ impl Writer {
gv.handle_id = id; gv.handle_id = id;
} else if global_needs_wrapper(ir_module, var) { } else if global_needs_wrapper(ir_module, var) {
let class = map_storage_class(var.space); let class = map_storage_class(var.space);
let pointer_type_id = let pointer_type_id = self.get_pointer_id(var.ty, class);
self.get_pointer_id(&ir_module.types, var.ty, class)?;
let index_id = self.get_index_constant(0); let index_id = self.get_index_constant(0);
let id = self.id_gen.next(); let id = self.id_gen.next();
prelude.body.push(Instruction::access_chain( prelude.body.push(Instruction::access_chain(
@ -664,11 +643,9 @@ impl Writer {
} }
let init_word = variable.init.map(|constant| context.cached[constant]); let init_word = variable.init.map(|constant| context.cached[constant]);
let pointer_type_id = context.writer.get_pointer_id( let pointer_type_id = context
&ir_module.types, .writer
variable.ty, .get_pointer_id(variable.ty, spirv::StorageClass::Function);
spirv::StorageClass::Function,
)?;
let instruction = Instruction::variable( let instruction = Instruction::variable(
pointer_type_id, pointer_type_id,
id, id,
@ -1468,7 +1445,7 @@ impl Writer {
binding: &crate::Binding, binding: &crate::Binding,
) -> Result<Word, Error> { ) -> Result<Word, Error> {
let id = self.id_gen.next(); let id = self.id_gen.next();
let pointer_type_id = self.get_pointer_id(&ir_module.types, ty, class)?; let pointer_type_id = self.get_pointer_id(ty, class);
Instruction::variable(pointer_type_id, id, class, None) Instruction::variable(pointer_type_id, id, class, None)
.to_words(&mut self.logical_layout.declarations); .to_words(&mut self.logical_layout.declarations);
@ -1801,7 +1778,7 @@ impl Writer {
if substitute_inner_type_lookup.is_some() { if substitute_inner_type_lookup.is_some() {
inner_type_id inner_type_id
} else { } else {
self.get_pointer_id(&ir_module.types, global_variable.ty, class)? self.get_pointer_id(global_variable.ty, class)
} }
}; };