[spv-out] Creating SPIR-V types is not, in fact, fallible.

There's no need for functions that construct SPIR-V types to be fallible, or for
their callers to check for errors.

As a consequence of this change, there may be other functions that no longer
need to be fallible, but Rust doesn't warn and Clippy doesn't lint, so we'll
have to address them as they come up.
This commit is contained in:
Jim Blandy
2021-08-16 08:50:50 -07:00
committed by Dzmitry Malyshau
parent c6a6afdf37
commit 4b1363e122
4 changed files with 58 additions and 59 deletions

View File

@@ -53,7 +53,7 @@ impl Writer {
kind: crate::ScalarKind::Float,
width: 4,
pointer_class: Some(spirv::StorageClass::Output),
}))?;
}));
let index_y_id = self.get_index_constant(1)?;
body.push(Instruction::access_chain(
float_ptr_type_id,
@@ -68,7 +68,7 @@ impl Writer {
kind: crate::ScalarKind::Float,
width: 4,
pointer_class: None,
}))?;
}));
body.push(Instruction::load(float_type_id, load_id, access_id, None));
let neg_id = self.id_gen.next();
@@ -86,7 +86,7 @@ impl Writer {
}
impl<'w> BlockContext<'w> {
fn get_type_id(&mut self, lookup_type: LookupType) -> Result<Word, Error> {
fn get_type_id(&mut self, lookup_type: LookupType) -> Word {
self.writer.get_type_id(lookup_type)
}
@@ -167,7 +167,7 @@ impl<'w> BlockContext<'w> {
kind: component_kind,
width: 4,
pointer_class: None,
}))?;
}));
let reconciled_id = self.gen_id();
block.body.push(Instruction::unary(
@@ -180,13 +180,12 @@ impl<'w> BlockContext<'w> {
};
// Find the SPIR-V type for the combined coordinates/index vector.
let combined_coordinate_type_id =
self.get_type_id(LookupType::Local(LocalType::Value {
vector_size: Some(result_size),
kind: component_kind,
width: 4,
pointer_class: None,
}))?;
let combined_coordinate_type_id = self.get_type_id(LookupType::Local(LocalType::Value {
vector_size: Some(result_size),
kind: component_kind,
width: 4,
pointer_class: None,
}));
// Schmear the coordinates and index together.
let id = self.gen_id();
@@ -227,7 +226,7 @@ impl<'w> BlockContext<'w> {
expr_handle: Handle<crate::Expression>,
block: &mut Block,
) -> Result<(), Error> {
let result_type_id = self.get_expression_type_id(&self.fun_info[expr_handle].ty)?;
let result_type_id = self.get_expression_type_id(&self.fun_info[expr_handle].ty);
let id = match self.ir_function.expressions[expr_handle] {
crate::Expression::Access { base, index: _ } if self.is_intermediate(base) => {
@@ -619,7 +618,7 @@ impl<'w> BlockContext<'w> {
kind,
width,
pointer_class: None,
}))?;
}));
self.temp_list.clear();
self.temp_list.resize(size as usize, arg2_id);
@@ -826,7 +825,7 @@ impl<'w> BlockContext<'w> {
kind: crate::ScalarKind::Float,
width: 4,
pointer_class: None,
}))?;
}));
Instruction::image_fetch(load_result_type_id, id, image_id, coordinate_id)
}
_ => Instruction::image_fetch(result_type_id, id, image_id, coordinate_id),
@@ -893,15 +892,15 @@ impl<'w> BlockContext<'w> {
kind: crate::ScalarKind::Float,
width: 4,
pointer_class: None,
}))?
}))
} else {
result_type_id
};
// OpTypeSampledImage
let image_type_id = self.get_type_id(LookupType::Handle(image_type))?;
let image_type_id = self.get_type_id(LookupType::Handle(image_type));
let sampled_image_type_id =
self.get_type_id(LookupType::Local(LocalType::SampledImage { image_type_id }))?;
self.get_type_id(LookupType::Local(LocalType::SampledImage { image_type_id }));
let sampler_id = self.get_image_id(sampler);
let coordinate_id =
@@ -1062,7 +1061,7 @@ impl<'w> BlockContext<'w> {
kind: crate::ScalarKind::Bool,
width,
pointer_class: None,
}))?;
}));
let id = self.gen_id();
block.body.push(Instruction::composite_construct(
@@ -1131,7 +1130,7 @@ impl<'w> BlockContext<'w> {
kind: crate::ScalarKind::Sint,
width: 4,
pointer_class: None,
}))?
}))
};
let (query_op, level_id) = match class {
@@ -1200,7 +1199,7 @@ impl<'w> BlockContext<'w> {
kind: crate::ScalarKind::Sint,
width: 4,
pointer_class: None,
}))?;
}));
let id_extended = self.gen_id();
let mut inst = Instruction::image_query(
spirv::Op::ImageQuerySizeLod,
@@ -1272,7 +1271,7 @@ impl<'w> BlockContext<'w> {
TypeResolution::Handle(ty_handle) => LookupType::Handle(ty_handle),
TypeResolution::Value(ref inner) => LookupType::Local(make_local(inner).unwrap()),
};
let result_type_id = self.get_type_id(result_lookup_ty)?;
let result_type_id = self.get_type_id(result_lookup_ty);
// The id of the boolean `and` of all dynamic bounds checks up to this point. If
// `None`, then we haven't done any dynamic bounds checks yet.
@@ -1301,7 +1300,7 @@ impl<'w> BlockContext<'w> {
let combined = self.gen_id();
block.body.push(Instruction::binary(
spirv::Op::LogicalAnd,
self.writer.get_bool_type_id()?,
self.writer.get_bool_type_id(),
combined,
prior_checks,
comparison_id,
@@ -1705,7 +1704,7 @@ impl<'w> BlockContext<'w> {
let type_id = match result {
Some(expr) => {
self.cached[expr] = id;
self.get_expression_type_id(&self.fun_info[expr].ty)?
self.get_expression_type_id(&self.fun_info[expr].ty)
}
None => self.writer.void_type,
};
@@ -1724,7 +1723,7 @@ impl<'w> BlockContext<'w> {
result,
} => {
let id = self.gen_id();
let result_type_id = self.get_expression_type_id(&self.fun_info[result].ty)?;
let result_type_id = self.get_expression_type_id(&self.fun_info[result].ty);
self.cached[result] = id;
@@ -1856,7 +1855,7 @@ impl<'w> BlockContext<'w> {
// Or it may be the end of the self.function.
None => match self.ir_function.result {
Some(ref result) if self.function.entry_point_context.is_none() => {
let type_id = self.get_type_id(LookupType::Handle(result.ty))?;
let type_id = self.get_type_id(LookupType::Handle(result.ty));
let null_id = self.writer.write_constant_null(type_id);
Instruction::return_value(null_id)
}

View File

@@ -77,7 +77,7 @@ impl<'w> BlockContext<'w> {
let length_id = self.gen_id();
block.body.push(Instruction::array_length(
self.writer.get_uint_type_id()?,
self.writer.get_uint_type_id(),
length_id,
structure_id,
last_member_index,
@@ -145,7 +145,7 @@ impl<'w> BlockContext<'w> {
let max_index_id = self.gen_id();
block.body.push(Instruction::binary(
spirv::Op::ISub,
self.writer.get_uint_type_id()?,
self.writer.get_uint_type_id(),
max_index_id,
length_id,
const_one_id,
@@ -207,7 +207,7 @@ impl<'w> BlockContext<'w> {
block.body.push(Instruction::ext_inst(
self.writer.gl450_ext_inst_id,
spirv::GLOp::UMin,
self.writer.get_uint_type_id()?,
self.writer.get_uint_type_id(),
restricted_index_id,
&[index_id, max_index_id],
));
@@ -280,7 +280,7 @@ impl<'w> BlockContext<'w> {
let condition_id = self.gen_id();
block.body.push(Instruction::binary(
spirv::Op::ULessThan,
self.writer.get_bool_type_id()?,
self.writer.get_bool_type_id(),
condition_id,
index_id,
length_id,
@@ -382,7 +382,7 @@ impl<'w> BlockContext<'w> {
index: Handle<crate::Expression>,
block: &mut Block,
) -> Result<Word, Error> {
let result_type_id = self.get_expression_type_id(&self.fun_info[expr_handle].ty)?;
let result_type_id = self.get_expression_type_id(&self.fun_info[expr_handle].ty);
let base_id = self.cached[base];
let index_id = self.cached[index];

View File

@@ -360,7 +360,7 @@ impl BlockContext<'_> {
self.writer.id_gen.next()
}
fn get_expression_type_id(&mut self, tr: &TypeResolution) -> Result<Word, Error> {
fn get_expression_type_id(&mut self, tr: &TypeResolution) -> Word {
self.writer.get_expression_type_id(tr)
}

View File

@@ -154,9 +154,9 @@ impl Writer {
Err(Error::MissingCapabilities(capabilities.to_vec()))
}
pub(super) fn get_type_id(&mut self, lookup_ty: LookupType) -> Result<Word, Error> {
pub(super) fn get_type_id(&mut self, lookup_ty: LookupType) -> Word {
if let Entry::Occupied(e) = self.lookup_type.entry(lookup_ty) {
Ok(*e.get())
*e.get()
} else {
match lookup_ty {
LookupType::Handle(_handle) => unreachable!("Handles are populated at start"),
@@ -165,7 +165,7 @@ impl Writer {
}
}
pub(super) fn get_expression_type_id(&mut self, tr: &TypeResolution) -> Result<Word, Error> {
pub(super) fn get_expression_type_id(&mut self, tr: &TypeResolution) -> Word {
let lookup_ty = match *tr {
TypeResolution::Handle(ty_handle) => LookupType::Handle(ty_handle),
TypeResolution::Value(ref inner) => LookupType::Local(make_local(inner).unwrap()),
@@ -179,7 +179,7 @@ impl Writer {
handle: Handle<crate::Type>,
class: spirv::StorageClass,
) -> Result<Word, Error> {
let ty_id = self.get_type_id(LookupType::Handle(handle))?;
let ty_id = self.get_type_id(LookupType::Handle(handle));
if let crate::TypeInner::Pointer { .. } = arena[handle].inner {
return Ok(ty_id);
}
@@ -198,7 +198,7 @@ impl Writer {
})
}
pub(super) fn get_uint_type_id(&mut self) -> Result<Word, Error> {
pub(super) fn get_uint_type_id(&mut self) -> Word {
let local_type = LocalType::Value {
vector_size: None,
kind: crate::ScalarKind::Uint,
@@ -208,7 +208,7 @@ impl Writer {
self.get_type_id(local_type.into())
}
pub(super) fn get_bool_type_id(&mut self) -> Result<Word, Error> {
pub(super) fn get_bool_type_id(&mut self) -> Word {
let local_type = LocalType::Value {
vector_size: None,
kind: crate::ScalarKind::Bool,
@@ -274,7 +274,7 @@ impl Writer {
argument.ty,
spirv::StorageClass::UniformConstant,
)?,
false => self.get_type_id(LookupType::Handle(argument.ty))?,
false => self.get_type_id(LookupType::Handle(argument.ty)),
};
if let Some(ref mut list) = varying_ids {
let id = if let Some(ref binding) = argument.binding {
@@ -293,7 +293,7 @@ impl Writer {
let struct_id = self.id_gen.next();
let mut constituent_ids = Vec::with_capacity(members.len());
for member in members {
let type_id = self.get_type_id(LookupType::Handle(member.ty))?;
let type_id = self.get_type_id(LookupType::Handle(member.ty));
let name = member.name.as_ref().map(AsRef::as_ref);
let binding = member.binding.as_ref().unwrap();
let varying_id =
@@ -323,7 +323,7 @@ impl Writer {
handle_id: if handle_ty {
let id = self.id_gen.next();
prelude.body.push(Instruction::load(
self.get_type_id(LookupType::Handle(argument.ty))?,
self.get_type_id(LookupType::Handle(argument.ty)),
id,
argument_id,
None,
@@ -342,7 +342,7 @@ impl Writer {
if let Some(ref mut list) = varying_ids {
let class = spirv::StorageClass::Output;
if let Some(ref binding) = result.binding {
let type_id = self.get_type_id(LookupType::Handle(result.ty))?;
let type_id = self.get_type_id(LookupType::Handle(result.ty));
let varying_id =
self.write_varying(ir_module, class, None, result.ty, binding)?;
list.push(varying_id);
@@ -355,7 +355,7 @@ impl Writer {
ir_module.types[result.ty].inner
{
for member in members {
let type_id = self.get_type_id(LookupType::Handle(member.ty))?;
let type_id = self.get_type_id(LookupType::Handle(member.ty));
let name = member.name.as_ref().map(AsRef::as_ref);
let binding = member.binding.as_ref().unwrap();
let varying_id =
@@ -372,7 +372,7 @@ impl Writer {
}
self.void_type
} else {
self.get_type_id(LookupType::Handle(result.ty))?
self.get_type_id(LookupType::Handle(result.ty))
}
}
None => self.void_type,
@@ -412,7 +412,7 @@ impl Writer {
continue;
}
let id = self.id_gen.next();
let result_type_id = self.get_type_id(LookupType::Handle(var.ty))?;
let result_type_id = self.get_type_id(LookupType::Handle(var.ty));
let gv = &mut self.global_variables[handle.index()];
prelude
.body
@@ -567,7 +567,7 @@ impl Writer {
}
}
fn write_type_declaration_local(&mut self, local_ty: LocalType) -> Result<Word, Error> {
fn write_type_declaration_local(&mut self, local_ty: LocalType) -> Word {
let id = self.id_gen.next();
let instruction = match local_ty {
LocalType::Value {
@@ -587,7 +587,7 @@ impl Writer {
kind,
width,
pointer_class: None,
}))?;
}));
Instruction::type_vector(id, scalar_id, size)
}
LocalType::Matrix {
@@ -600,11 +600,11 @@ impl Writer {
kind: crate::ScalarKind::Float,
width,
pointer_class: None,
}))?;
}));
Instruction::type_matrix(id, vector_id, columns)
}
LocalType::Pointer { base, class } => {
let type_id = self.get_type_id(LookupType::Handle(base))?;
let type_id = self.get_type_id(LookupType::Handle(base));
Instruction::type_pointer(id, class, type_id)
}
LocalType::Value {
@@ -618,7 +618,7 @@ impl Writer {
kind,
width,
pointer_class: None,
}))?;
}));
Instruction::type_pointer(id, class, type_id)
}
// all the samplers and image types go through `write_type_declaration_arena`
@@ -630,7 +630,7 @@ impl Writer {
self.lookup_type.insert(LookupType::Local(local_ty), id);
instruction.to_words(&mut self.logical_layout.declarations);
Ok(id)
id
}
fn write_type_declaration_arena(
@@ -678,7 +678,7 @@ impl Writer {
kind,
width,
pointer_class: None,
}))?;
}));
Instruction::type_vector(id, scalar_id, size)
}
crate::TypeInner::Matrix {
@@ -691,7 +691,7 @@ impl Writer {
kind: crate::ScalarKind::Float,
width,
pointer_class: None,
}))?;
}));
Instruction::type_matrix(id, vector_id, columns)
}
crate::TypeInner::Image {
@@ -720,7 +720,7 @@ impl Writer {
};
let dim = map_dim(dim);
self.check(dim.required_capabilities())?;
let type_id = self.get_type_id(LookupType::Local(local_type))?;
let type_id = self.get_type_id(LookupType::Local(local_type));
Instruction::type_image(id, type_id, dim, arrayed, class)
}
crate::TypeInner::Sampler { comparison: _ } => Instruction::type_sampler(id),
@@ -729,7 +729,7 @@ impl Writer {
self.decorate(id, Decoration::ArrayStride, &[stride]);
}
let type_id = self.get_type_id(LookupType::Handle(base))?;
let type_id = self.get_type_id(LookupType::Handle(base));
match size {
crate::ArraySize::Constant(const_handle) => {
let length_id = self.constant_ids[const_handle.index()];
@@ -795,13 +795,13 @@ impl Writer {
));
}
let member_id = self.get_type_id(LookupType::Handle(member.ty))?;
let member_id = self.get_type_id(LookupType::Handle(member.ty));
member_ids.push(member_id);
}
Instruction::type_struct(id, member_ids.as_slice())
}
crate::TypeInner::Pointer { base, class } => {
let type_id = self.get_type_id(LookupType::Handle(base))?;
let type_id = self.get_type_id(LookupType::Handle(base));
let raw_class = map_storage_class(class);
Instruction::type_pointer(id, raw_class, type_id)
}
@@ -817,7 +817,7 @@ impl Writer {
kind,
width,
pointer_class: None,
}))?;
}));
Instruction::type_pointer(id, raw_class, type_id)
}
};
@@ -861,7 +861,7 @@ impl Writer {
kind: value.scalar_kind(),
width,
pointer_class: None,
}))?;
}));
let (solo, pair);
let instruction = match *value {
crate::ScalarValue::Sint(val) => {
@@ -927,7 +927,7 @@ impl Writer {
constituent_ids.push(constituent_id);
}
let type_id = self.get_type_id(LookupType::Handle(ty))?;
let type_id = self.get_type_id(LookupType::Handle(ty));
Instruction::constant_composite(type_id, id, constituent_ids.as_slice())
.to_words(&mut self.logical_layout.declarations);
Ok(())