diff --git a/naga/src/front/spv/function.rs b/naga/src/front/spv/function.rs index 271b96926b..b780cbe775 100644 --- a/naga/src/front/spv/function.rs +++ b/naga/src/front/spv/function.rs @@ -111,6 +111,9 @@ impl> super::Frontend { } } + // Note the index this function's handle will be assigned, for tracing. + let function_index = module.functions.len(); + // Read body self.function_call_graph.add_node(fun_id); let mut parameters_sampling = @@ -122,14 +125,10 @@ impl> super::Frontend { body_for_label: Default::default(), mergers: Default::default(), bodies: Default::default(), + module, function_id: fun_id, expressions: &mut fun.expressions, local_arena: &mut fun.local_variables, - const_arena: &mut module.constants, - overrides: &mut module.overrides, - global_expressions: &mut module.global_expressions, - type_arena: &module.types, - global_arena: &module.global_variables, arguments: &fun.arguments, parameter_sampling: &mut parameters_sampling, }; @@ -167,7 +166,7 @@ impl> super::Frontend { if let Some(ref prefix) = self.options.block_ctx_dump_prefix { let dump_suffix = match self.lookup_entry_point.get(&fun_id) { Some(ep) => format!("block_ctx.{:?}-{}.txt", ep.stage, ep.name), - None => format!("block_ctx.Fun-{}.txt", module.functions.len()), + None => format!("block_ctx.Fun-{}.txt", function_index), }; let dest = prefix.join(dump_suffix); let dump = format!("{block_ctx:#?}"); @@ -580,10 +579,10 @@ impl> super::Frontend { impl BlockContext<'_> { pub(super) fn gctx(&self) -> crate::proc::GlobalCtx { crate::proc::GlobalCtx { - types: self.type_arena, - constants: self.const_arena, - overrides: self.overrides, - global_expressions: self.global_expressions, + types: &self.module.types, + constants: &self.module.constants, + overrides: &self.module.overrides, + global_expressions: &self.module.global_expressions, } } diff --git a/naga/src/front/spv/image.rs b/naga/src/front/spv/image.rs index e17f19f257..22c1701ed8 100644 --- a/naga/src/front/spv/image.rs +++ b/naga/src/front/spv/image.rs @@ -28,7 +28,9 @@ impl super::BlockContext<'_> { handle: Handle, ) -> Result, Error> { match self.expressions[handle] { - crate::Expression::GlobalVariable(handle) => Ok(self.global_arena[handle].ty), + crate::Expression::GlobalVariable(handle) => { + Ok(self.module.global_variables[handle].ty) + } crate::Expression::FunctionArgument(i) => Ok(self.arguments[i as usize].ty), crate::Expression::Access { base, .. } => Ok(self.get_image_expr_ty(base)?), ref other => Err(Error::InvalidImageExpression(other.clone())), @@ -64,7 +66,7 @@ fn extract_image_coordinates( coordinate_ty: Handle, ctx: &mut super::BlockContext, ) -> (Handle, Option>) { - let (given_size, kind) = match ctx.type_arena[coordinate_ty].inner { + let (given_size, kind) = match ctx.module.types[coordinate_ty].inner { crate::TypeInner::Scalar(Scalar { kind, .. }) => (None, kind), crate::TypeInner::Vector { size, @@ -75,7 +77,8 @@ fn extract_image_coordinates( let required_size = image_dim.required_coordinate_size(); let required_ty = required_size.map(|size| { - ctx.type_arena + ctx.module + .types .get(&crate::Type { name: None, inner: crate::TypeInner::Vector { @@ -287,7 +290,7 @@ impl> super::Frontend { let coord_handle = self.get_expr_handle(coordinate_id, coord_lexp, ctx, emitter, block, body_idx); let coord_type_handle = self.lookup_type.lookup(coord_lexp.type_id)?.handle; - let (coordinate, array_index) = match ctx.type_arena[image_ty].inner { + let (coordinate, array_index) = match ctx.module.types[image_ty].inner { crate::TypeInner::Image { dim, arrayed, @@ -378,7 +381,7 @@ impl> super::Frontend { let coord_handle = self.get_expr_handle(coordinate_id, coord_lexp, ctx, emitter, block, body_idx); let coord_type_handle = self.lookup_type.lookup(coord_lexp.type_id)?.handle; - let (coordinate, array_index, is_depth) = match ctx.type_arena[image_ty].inner { + let (coordinate, array_index, is_depth) = match ctx.module.types[image_ty].inner { crate::TypeInner::Image { dim, arrayed, @@ -414,7 +417,7 @@ impl> super::Frontend { let handle = if is_depth { let result_ty = self.lookup_type.lookup(result_type_id)?; // The return type of `OpImageRead` can be a scalar or vector. - match ctx.type_arena[result_ty.handle].inner { + match ctx.module.types[result_ty.handle].inner { crate::TypeInner::Vector { size, .. } => { let splat_expr = crate::Expression::Splat { size, @@ -493,7 +496,7 @@ impl> super::Frontend { let image_lexp = self.lookup_sampled_image.lookup(sampled_image_id)?; let image_ty = ctx.get_image_expr_ty(image_lexp.image)?; matches!( - ctx.type_arena[image_ty].inner, + ctx.module.types[image_ty].inner, crate::TypeInner::Image { class: crate::ImageClass::Depth { .. }, .. @@ -565,6 +568,7 @@ impl> super::Frontend { .inner .to_expr(); let offset_handle = ctx + .module .global_expressions .append(offset_expr, Default::default()); offset = Some(offset_handle); @@ -599,7 +603,7 @@ impl> super::Frontend { *flags |= sampling_bit; } - ctx.global_arena[handle].ty + ctx.module.global_variables[handle].ty } crate::Expression::FunctionArgument(i) => { @@ -613,7 +617,7 @@ impl> super::Frontend { *flags |= sampling_bit; } - match ctx.type_arena[ctx.global_arena[handle].ty].inner { + match ctx.module.types[ctx.module.global_variables[handle].ty].inner { crate::TypeInner::BindingArray { base, .. } => base, _ => return Err(Error::InvalidGlobalVar(ctx.expressions[base].clone())), } @@ -645,60 +649,60 @@ impl> super::Frontend { ref other => return Err(Error::InvalidGlobalVar(other.clone())), } - let ((coordinate, array_index), depth_ref, is_depth) = match ctx.type_arena[image_ty].inner - { - crate::TypeInner::Image { - dim, - arrayed, - class, - } => ( - extract_image_coordinates( + let ((coordinate, array_index), depth_ref, is_depth) = + match ctx.module.types[image_ty].inner { + crate::TypeInner::Image { dim, - if options.project { - ExtraCoordinate::Projection - } else if arrayed { - ExtraCoordinate::ArrayLayer - } else { - ExtraCoordinate::Garbage + arrayed, + class, + } => ( + extract_image_coordinates( + dim, + if options.project { + ExtraCoordinate::Projection + } else if arrayed { + ExtraCoordinate::ArrayLayer + } else { + ExtraCoordinate::Garbage + }, + coord_handle, + coord_type_handle, + ctx, + ), + { + match dref_id { + Some(id) => { + let expr_lexp = self.lookup_expression.lookup(id)?; + let mut expr = self + .get_expr_handle(id, expr_lexp, ctx, emitter, block, body_idx); + + if options.project { + let required_size = dim.required_coordinate_size(); + let right = ctx.expressions.append( + crate::Expression::AccessIndex { + base: coord_handle, + index: required_size.map_or(1, |size| size as u32), + }, + crate::Span::default(), + ); + expr = ctx.expressions.append( + crate::Expression::Binary { + op: crate::BinaryOperator::Divide, + left: expr, + right, + }, + crate::Span::default(), + ) + }; + Some(expr) + } + None => None, + } }, - coord_handle, - coord_type_handle, - ctx, + class.is_depth(), ), - { - match dref_id { - Some(id) => { - let expr_lexp = self.lookup_expression.lookup(id)?; - let mut expr = - self.get_expr_handle(id, expr_lexp, ctx, emitter, block, body_idx); - - if options.project { - let required_size = dim.required_coordinate_size(); - let right = ctx.expressions.append( - crate::Expression::AccessIndex { - base: coord_handle, - index: required_size.map_or(1, |size| size as u32), - }, - crate::Span::default(), - ); - expr = ctx.expressions.append( - crate::Expression::Binary { - op: crate::BinaryOperator::Divide, - left: expr, - right, - }, - crate::Span::default(), - ) - }; - Some(expr) - } - None => None, - } - }, - class.is_depth(), - ), - _ => return Err(Error::InvalidImage(image_ty)), - }; + _ => return Err(Error::InvalidImage(image_ty)), + }; let expr = crate::Expression::ImageSample { image: si_lexp.image, @@ -764,7 +768,7 @@ impl> super::Frontend { }; let result_type_handle = self.lookup_type.lookup(result_type_id)?.handle; - let maybe_scalar_kind = ctx.type_arena[result_type_handle].inner.scalar_kind(); + let maybe_scalar_kind = ctx.module.types[result_type_handle].inner.scalar_kind(); let expr = if maybe_scalar_kind == Some(crate::ScalarKind::Sint) { crate::Expression::As { @@ -809,7 +813,7 @@ impl> super::Frontend { }; let result_type_handle = self.lookup_type.lookup(result_type_id)?.handle; - let maybe_scalar_kind = ctx.type_arena[result_type_handle].inner.scalar_kind(); + let maybe_scalar_kind = ctx.module.types[result_type_handle].inner.scalar_kind(); let expr = if maybe_scalar_kind == Some(crate::ScalarKind::Sint) { crate::Expression::As { diff --git a/naga/src/front/spv/mod.rs b/naga/src/front/spv/mod.rs index 37a1a1b031..5e43b26a15 100644 --- a/naga/src/front/spv/mod.rs +++ b/naga/src/front/spv/mod.rs @@ -541,20 +541,15 @@ struct BlockContext<'function> { /// The first element is always the function's top-level block. bodies: Vec, + /// The module we're building. + module: &'function mut crate::Module, + /// Id of the function currently being processed function_id: spirv::Word, /// Expression arena of the function currently being processed expressions: &'function mut Arena, /// Local variables arena of the function currently being processed local_arena: &'function mut Arena, - /// Constants arena of the module being processed - const_arena: &'function mut Arena, - overrides: &'function mut Arena, - global_expressions: &'function mut Arena, - /// Type arena of the module being processed - type_arena: &'function UniqueArena, - /// Global arena of the module being processed - global_arena: &'function Arena, /// Arguments of the function currently being processed arguments: &'function [crate::FunctionArgument], /// Metadata about the usage of function parameters as sampling objects @@ -987,7 +982,7 @@ impl> Frontend { let left = self.get_expr_handle(p1_id, p1_lexp, ctx, emitter, block, body_idx); let result_lookup_ty = self.lookup_type.lookup(result_type_id)?; - let kind = ctx.type_arena[result_lookup_ty.handle] + let kind = ctx.module.types[result_lookup_ty.handle] .inner .scalar_kind() .unwrap(); @@ -1053,7 +1048,7 @@ impl> Frontend { SignAnchor::Operand => p1_lexp.type_id, }; let expected_lookup_ty = self.lookup_type.lookup(expected_type_id)?; - let kind = ctx.type_arena[expected_lookup_ty.handle] + let kind = ctx.module.types[expected_lookup_ty.handle] .inner .scalar_kind() .unwrap(); @@ -1121,14 +1116,14 @@ impl> Frontend { let p1_lexp = self.lookup_expression.lookup(p1_id)?; let left = self.get_expr_handle(p1_id, p1_lexp, ctx, emitter, block, body_idx); let p1_lookup_ty = self.lookup_type.lookup(p1_lexp.type_id)?; - let p1_kind = ctx.type_arena[p1_lookup_ty.handle] + let p1_kind = ctx.module.types[p1_lookup_ty.handle] .inner .scalar_kind() .unwrap(); let p2_lexp = self.lookup_expression.lookup(p2_id)?; let right = self.get_expr_handle(p2_id, p2_lexp, ctx, emitter, block, body_idx); let p2_lookup_ty = self.lookup_type.lookup(p2_lexp.type_id)?; - let p2_kind = ctx.type_arena[p2_lookup_ty.handle] + let p2_kind = ctx.module.types[p2_lookup_ty.handle] .inner .scalar_kind() .unwrap(); @@ -1570,7 +1565,7 @@ impl> Frontend { let var_handle = ctx.local_arena.append( crate::LocalVariable { name, - ty: match ctx.type_arena[lookup_ty.handle].inner { + ty: match ctx.module.types[lookup_ty.handle].inner { crate::TypeInner::Pointer { base, .. } => base, _ => lookup_ty.handle, }, @@ -1665,7 +1660,7 @@ impl> Frontend { // This can happen only through `BindingArray`, since // that's the only case where one can obtain a pointer // to an image / sampler, and so let's match on that: - let dereference = match ctx.type_arena[lty.handle].inner { + let dereference = match ctx.module.types[lty.handle].inner { crate::TypeInner::BindingArray { .. } => false, _ => true, }; @@ -1692,7 +1687,7 @@ impl> Frontend { let index_maybe = match *index_expr_data { crate::Expression::Constant(const_handle) => Some( ctx.gctx() - .eval_expr_to_u32(ctx.const_arena[const_handle].init) + .eval_expr_to_u32(ctx.module.constants[const_handle].init) .map_err(|_| { Error::InvalidAccess(crate::Expression::Constant( const_handle, @@ -1704,7 +1699,7 @@ impl> Frontend { log::trace!("\t\t\tlooking up type {:?}", acex.type_id); let type_lookup = self.lookup_type.lookup(acex.type_id)?; - let ty = &ctx.type_arena[type_lookup.handle]; + let ty = &ctx.module.types[type_lookup.handle]; acex = match ty.inner { // can only index a struct with a constant crate::TypeInner::Struct { ref members, .. } => { @@ -1736,7 +1731,7 @@ impl> Frontend { debug_assert!(acex.load_override.is_none()); let sub_type_lookup = self.lookup_type.lookup(lookup_member.type_id)?; - Some(match ctx.type_arena[sub_type_lookup.handle].inner { + Some(match ctx.module.types[sub_type_lookup.handle].inner { // load it transposed, to match column major expectations crate::TypeInner::Matrix { .. } => { let loaded = ctx.expressions.append( @@ -1885,7 +1880,7 @@ impl> Frontend { let index_handle = get_expr_handle!(index_id, index_lexp); let index_type = self.lookup_type.lookup(index_lexp.type_id)?.handle; - let num_components = match ctx.type_arena[root_type_lookup.handle].inner { + let num_components = match ctx.module.types[root_type_lookup.handle].inner { crate::TypeInner::Vector { size, .. } => size as u32, _ => return Err(Error::InvalidVectorType(root_type_lookup.handle)), }; @@ -1964,7 +1959,7 @@ impl> Frontend { let index_handle = get_expr_handle!(index_id, index_lexp); let index_type = self.lookup_type.lookup(index_lexp.type_id)?.handle; - let num_components = match ctx.type_arena[root_type_lookup.handle].inner { + let num_components = match ctx.module.types[root_type_lookup.handle].inner { crate::TypeInner::Vector { size, .. } => size as u32, _ => return Err(Error::InvalidVectorType(root_type_lookup.handle)), }; @@ -2035,7 +2030,7 @@ impl> Frontend { let index = self.next()?; log::trace!("\t\t\tlooking up type {:?}", lexp.type_id); let type_lookup = self.lookup_type.lookup(lexp.type_id)?; - let type_id = match ctx.type_arena[type_lookup.handle].inner { + let type_id = match ctx.module.types[type_lookup.handle].inner { crate::TypeInner::Struct { .. } => { self.lookup_member .get(&(type_lookup.handle, index)) @@ -2095,7 +2090,7 @@ impl> Frontend { result_type_id, object_handle, &selections, - ctx.type_arena, + &ctx.module.types, ctx.expressions, span, )?; @@ -2124,7 +2119,7 @@ impl> Frontend { } let ty = self.lookup_type.lookup(result_type_id)?.handle; let first = components[0]; - let expr = match ctx.type_arena[ty].inner { + let expr = match ctx.module.types[ty].inner { // this is an optimization to detect the splat crate::TypeInner::Vector { size, .. } if components.len() == size as usize @@ -2157,7 +2152,7 @@ impl> Frontend { let base_lexp = self.lookup_expression.lookup(pointer_id)?; let base_handle = get_expr_handle!(pointer_id, base_lexp); let type_lookup = self.lookup_type.lookup(base_lexp.type_id)?; - let handle = match ctx.type_arena[type_lookup.handle].inner { + let handle = match ctx.module.types[type_lookup.handle].inner { crate::TypeInner::Image { .. } | crate::TypeInner::Sampler { .. } => { base_handle } @@ -2303,7 +2298,7 @@ impl> Frontend { ); let result_ty = self.lookup_type.lookup(result_type_id)?; - let inner = &ctx.type_arena[result_ty.handle].inner; + let inner = &ctx.module.types[result_ty.handle].inner; let kind = inner.scalar_kind().unwrap(); let size = inner.size(ctx.gctx()) as u8; @@ -2531,11 +2526,11 @@ impl> Frontend { let count_handle = get_expr_handle!(count_id, count_lexp); let count_lookup_ty = self.lookup_type.lookup(count_lexp.type_id)?; - let offset_kind = ctx.type_arena[offset_lookup_ty.handle] + let offset_kind = ctx.module.types[offset_lookup_ty.handle] .inner .scalar_kind() .unwrap(); - let count_kind = ctx.type_arena[count_lookup_ty.handle] + let count_kind = ctx.module.types[count_lookup_ty.handle] .inner .scalar_kind() .unwrap(); @@ -2599,11 +2594,11 @@ impl> Frontend { let count_handle = get_expr_handle!(count_id, count_lexp); let count_lookup_ty = self.lookup_type.lookup(count_lexp.type_id)?; - let offset_kind = ctx.type_arena[offset_lookup_ty.handle] + let offset_kind = ctx.module.types[offset_lookup_ty.handle] .inner .scalar_kind() .unwrap(); - let count_kind = ctx.type_arena[count_lookup_ty.handle] + let count_kind = ctx.module.types[count_lookup_ty.handle] .inner .scalar_kind() .unwrap(); @@ -2893,14 +2888,14 @@ impl> Frontend { let v1_lexp = self.lookup_expression.lookup(v1_id)?; let v1_lty = self.lookup_type.lookup(v1_lexp.type_id)?; let v1_handle = get_expr_handle!(v1_id, v1_lexp); - let n1 = match ctx.type_arena[v1_lty.handle].inner { + let n1 = match ctx.module.types[v1_lty.handle].inner { crate::TypeInner::Vector { size, .. } => size as u32, _ => return Err(Error::InvalidInnerType(v1_lexp.type_id)), }; let v2_lexp = self.lookup_expression.lookup(v2_id)?; let v2_lty = self.lookup_type.lookup(v2_lexp.type_id)?; let v2_handle = get_expr_handle!(v2_id, v2_lexp); - let n2 = match ctx.type_arena[v2_lty.handle].inner { + let n2 = match ctx.module.types[v2_lty.handle].inner { crate::TypeInner::Vector { size, .. } => size as u32, _ => return Err(Error::InvalidInnerType(v2_lexp.type_id)), }; @@ -2988,7 +2983,7 @@ impl> Frontend { let value_lexp = self.lookup_expression.lookup(value_id)?; let ty_lookup = self.lookup_type.lookup(result_type_id)?; - let scalar = match ctx.type_arena[ty_lookup.handle].inner { + let scalar = match ctx.module.types[ty_lookup.handle].inner { crate::TypeInner::Scalar(scalar) | crate::TypeInner::Vector { scalar, .. } | crate::TypeInner::Matrix { scalar, .. } => scalar, @@ -3514,7 +3509,7 @@ impl> Frontend { let selector_lexp = &self.lookup_expression[&selector]; let selector_lty = self.lookup_type.lookup(selector_lexp.type_id)?; let selector_handle = get_expr_handle!(selector, selector_lexp); - let selector = match ctx.type_arena[selector_lty.handle].inner { + let selector = match ctx.module.types[selector_lty.handle].inner { crate::TypeInner::Scalar(crate::Scalar { kind: crate::ScalarKind::Uint, width: _, @@ -5842,7 +5837,7 @@ fn make_index_literal( ) -> Result, Error> { block.extend(emitter.finish(ctx.expressions)); - let literal = match ctx.type_arena[index_type].inner.scalar_kind() { + let literal = match ctx.module.types[index_type].inner.scalar_kind() { Some(crate::ScalarKind::Uint) => crate::Literal::U32(index), Some(crate::ScalarKind::Sint) => crate::Literal::I32(index as i32), _ => return Err(Error::InvalidIndexType(index_type_id)),