Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions naga/src/front/wgsl/lower/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,25 @@ impl<'source> Lowerer<'source, '_> {

let base = ctx.register_type(components[0])?;

ctx.layouter.update(ctx.module.to_ctx()).map_err(|err| {
let crate::proc::LayoutErrorInner::TooLarge = err.inner else {
unreachable!("unexpected layout error: {err:?}");
};
// This error could be for any type that was pending layout. Lots of
// type definitions don't get spans, so the error message may not be
// very useful.
Box::new(Error::TypeTooLarge {
span: ctx.module.types.get_span(err.ty),
})
})?;
let stride = ctx.layouter[base].to_stride();

let inner = crate::TypeInner::Array {
base,
size: crate::ArraySize::Constant(
NonZeroU32::new(u32::try_from(components.len()).unwrap()).unwrap(),
),
stride: {
ctx.layouter.update(ctx.module.to_ctx()).unwrap();
ctx.layouter[base].to_stride()
},
stride,
};
let ty = ctx.ensure_type_exists(inner);

Expand Down
7 changes: 7 additions & 0 deletions naga/src/front/wgsl/lower/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
.cast_array(expr, *concrete_scalar, expr_span)
{
Ok(expr) => return Ok(expr),
Err(crate::proc::ConstantEvaluatorError::TypeTooLarge(ty)) => {
// Special case where the error is not actually related to the
// particular scalar we tried to concretize.
return Err(Box::new(super::Error::TypeTooLarge {
span: self.module.types.get_span(ty),
}));
}
Err(err) => {
errors.push((concrete_scalar.to_wgsl_for_diagnostics(), err));
}
Expand Down
11 changes: 10 additions & 1 deletion naga/src/proc/constant_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,8 @@ pub enum ConstantEvaluatorError {
SelectAcceptRejectTypeMismatch,
#[error("Cooperative operations can't be constant")]
CooperativeOperation,
#[error("Type is too large")]
TypeTooLarge(Handle<Type>),
}

impl<'a> ConstantEvaluator<'a> {
Expand Down Expand Up @@ -2652,7 +2654,14 @@ impl<'a> ConstantEvaluator<'a> {
}
};
let mut layouter = core::mem::take(self.layouter);
layouter.update(self.to_ctx()).unwrap();
layouter.update(self.to_ctx()).map_err(|err| {
// The layouter operates lazily, so the error
// could be for any pending type.
let crate::proc::LayoutErrorInner::TooLarge = err.inner else {
unreachable!("unexpected layout error: {err:?}");
};
ConstantEvaluatorError::TypeTooLarge(err.ty)
})?;
*self.layouter = layouter;

let new_base_stride = self.layouter[new_base].to_stride();
Expand Down
44 changes: 44 additions & 0 deletions naga/tests/naga/wgsl_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4714,6 +4714,50 @@ fn max_type_size_array_of_structs() {
}
}

#[test]
fn max_type_size_array_constructor_with_oversize_type() {
// An `array(...)` constructor expression invokes the layouter to compute
// the stride of the constructed array. If a previously declared type is
// oversize, the layouter encounters it and the error must be reported
// rather than panicking.
//
// Regression test for <https://github.com/gfx-rs/wgpu/issues/9440>.
check(
r#"
var<workgroup> big: array<u32, 1 << 29>;
const A = array(1);
"#,
r#"error: type is too large
= note: the maximum size is 2147483647 bytes

"#,
);
}

#[test]
fn max_type_size_concretize_with_oversize_type() {
// Concretizing an abstract array (here, indexing it with a non-constant
// index forces concretization to a concrete element type) invokes the
// layouter to compute the new array's stride. If a previously declared
// type is oversize, the layouter encounters it and the error must be
// reported rather than panicking.
//
// Regression test for <https://github.com/gfx-rs/wgpu/issues/9440>.
check(
r#"
const a = array(0.);
var<workgroup> big: array<u32, 1 << 29>;
fn main(i: u32) {
let x = a[i];
}
"#,
r#"error: type is too large
= note: the maximum size is 2147483647 bytes

"#,
);
}

#[test]
fn source_with_control_char() {
check(
Expand Down
Loading