Introduce GlobalCtx

This commit is contained in:
teoxoy authored and Jim Blandy committed 2023-05-11 09:52:40 -07:00
1 parent d763fb31e2
commit d842d6688d
20 files changed
+138 -170

No files matched your search

+12 -16
View File
@@ -1,8 +1,5 @@
#[cfg(feature = "validate")]
use crate::{
arena::{Arena, UniqueArena},
proc::TypeResolution,
};
use crate::proc::TypeResolution;
use crate::arena::Handle;
@@ -20,18 +17,17 @@ pub enum ComposeError {
#[cfg(feature = "validate")]
pub fn validate_compose(
self_ty_handle: Handle<crate::Type>,
constant_arena: &Arena<crate::Constant>,
type_arena: &UniqueArena<crate::Type>,
gctx: crate::proc::GlobalCtx,
component_resolutions: impl ExactSizeIterator<Item = TypeResolution>,
) -> Result<(), ComposeError> {
use crate::TypeInner as Ti;
match type_arena[self_ty_handle].inner {
match gctx.types[self_ty_handle].inner {
// vectors are composed from scalars or other vectors
Ti::Vector { size, kind, width } => {
let mut total = 0;
for (index, comp_res) in component_resolutions.enumerate() {
total += match *comp_res.inner_with(type_arena) {
total += match *comp_res.inner_with(gctx.types) {
Ti::Scalar {
kind: comp_kind,
width: comp_width,
@@ -74,7 +70,7 @@ pub fn validate_compose(
});
}
for (index, comp_res) in component_resolutions.enumerate() {
if comp_res.inner_with(type_arena) != &inner {
if comp_res.inner_with(gctx.types) != &inner {
log::error!("Matrix component[{}] type {:?}", index, comp_res);
return Err(ComposeError::ComponentType {
index: index as u32,
@@ -87,7 +83,7 @@ pub fn validate_compose(
size: crate::ArraySize::Constant(handle),
stride: _,
} => {
let count = constant_arena[handle].to_array_length().unwrap();
let count = gctx.constants[handle].to_array_length().unwrap();
if count as usize != component_resolutions.len() {
return Err(ComposeError::ComponentCount {
expected: count,
@@ -95,11 +91,11 @@ pub fn validate_compose(
});
}
for (index, comp_res) in component_resolutions.enumerate() {
let base_inner = &type_arena[base].inner;
let comp_res_inner = comp_res.inner_with(type_arena);
let base_inner = &gctx.types[base].inner;
let comp_res_inner = comp_res.inner_with(gctx.types);
// We don't support arrays of pointers, but it seems best not to
// embed that assumption here, so use `TypeInner::equivalent`.
if !base_inner.equivalent(comp_res_inner, type_arena) {
if !base_inner.equivalent(comp_res_inner, gctx.types) {
log::error!("Array component[{}] type {:?}", index, comp_res);
return Err(ComposeError::ComponentType {
index: index as u32,
@@ -116,11 +112,11 @@ pub fn validate_compose(
}
for (index, (member, comp_res)) in members.iter().zip(component_resolutions).enumerate()
{
let member_inner = &type_arena[member.ty].inner;
let comp_res_inner = comp_res.inner_with(type_arena);
let member_inner = &gctx.types[member.ty].inner;
let comp_res_inner = comp_res.inner_with(gctx.types);
// We don't support pointers in structs, but it seems best not to embed
// that assumption here, so use `TypeInner::equivalent`.
if !comp_res_inner.equivalent(member_inner, type_arena) {
if !comp_res_inner.equivalent(member_inner, gctx.types) {
log::error!("Struct component[{}] type {:?}", index, comp_res);
return Err(ComposeError::ComponentType {
index: index as u32,
+1 -2
View File
@@ -301,8 +301,7 @@ impl super::Validator {
E::Compose { ref components, ty } => {
validate_compose(
ty,
&module.constants,
&module.types,
module.to_ctx(),
components.iter().map(|&handle| info[handle].ty.clone()),
)?;
ShaderStages::all()
+4 -5
View File
@@ -894,8 +894,7 @@ impl super::Validator {
fn validate_local_var(
&self,
var: &crate::LocalVariable,
types: &UniqueArena<crate::Type>,
constants: &Arena<crate::Constant>,
gctx: crate::proc::GlobalCtx,
) -> Result<(), LocalVariableError> {
log::debug!("var {:?}", var);
let type_info = self
@@ -910,13 +909,13 @@ impl super::Validator {
}
if let Some(const_handle) = var.init {
match constants[const_handle].inner {
match gctx.constants[const_handle].inner {
crate::ConstantInner::Scalar { width, ref value } => {
let ty_inner = crate::TypeInner::Scalar {
width,
kind: value.scalar_kind(),
};
if types[var.ty].inner != ty_inner {
if gctx.types[var.ty].inner != ty_inner {
return Err(LocalVariableError::InitializerType);
}
}
@@ -942,7 +941,7 @@ impl super::Validator {
#[cfg(feature = "validate")]
for (var_handle, var) in fun.local_variables.iter() {
self.validate_local_var(var, &module.types, &module.constants)
self.validate_local_var(var, module.to_ctx())
.map_err(|source| {
FunctionError::LocalVariable {
handle: var_handle,
+3 -3
View File
@@ -395,12 +395,12 @@ impl super::Validator {
pub(super) fn validate_global_var(
&self,
var: &crate::GlobalVariable,
types: &UniqueArena<crate::Type>,
gctx: crate::proc::GlobalCtx,
) -> Result<(), GlobalVariableError> {
use super::TypeFlags;
log::debug!("var {:?}", var);
let inner_ty = match types[var.ty].inner {
let inner_ty = match gctx.types[var.ty].inner {
// A binding array is (mostly) supposed to behave the same as a
// series of individually bound resources, so we can (mostly)
// validate a `binding_array<T>` as if it were just a plain `T`.
@@ -444,7 +444,7 @@ impl super::Validator {
)
}
crate::AddressSpace::Handle => {
match types[inner_ty].inner {
match gctx.types[inner_ty].inner {
crate::TypeInner::Image { class, .. } => match class {
crate::ImageClass::Storage {
format:
+12 -16
View File
@@ -11,7 +11,7 @@ mod interface;
mod r#type;
#[cfg(feature = "validate")]
use crate::arena::{Arena, UniqueArena};
use crate::arena::UniqueArena;
use crate::{
arena::Handle,
@@ -296,10 +296,9 @@ impl Validator {
fn validate_constant(
&self,
handle: Handle<crate::Constant>,
constants: &Arena<crate::Constant>,
types: &UniqueArena<crate::Type>,
gctx: crate::proc::GlobalCtx,
) -> Result<(), ConstantError> {
let con = &constants[handle];
let con = &gctx.constants[handle];
match con.inner {
crate::ConstantInner::Scalar { width, ref value } => {
if self.check_width(value.scalar_kind(), width).is_err() {
@@ -309,11 +308,10 @@ impl Validator {
crate::ConstantInner::Composite { ty, ref components } => {
compose::validate_compose(
ty,
constants,
types,
gctx,
components
.iter()
.map(|&component| constants[component].inner.resolve_type()),
.map(|&component| gctx.constants[component].inner.resolve_type()),
)?;
}
}
@@ -331,17 +329,15 @@ impl Validator {
#[cfg(feature = "validate")]
Self::validate_module_handles(module).map_err(|e| e.with_span())?;
self.layouter
.update(&module.types, &module.constants)
.map_err(|e| {
let handle = e.ty;
ValidationError::from(e).with_span_handle(handle, &module.types)
})?;
self.layouter.update(module.to_ctx()).map_err(|e| {
let handle = e.ty;
ValidationError::from(e).with_span_handle(handle, &module.types)
})?;
#[cfg(feature = "validate")]
if self.flags.contains(ValidationFlags::CONSTANTS) {
for (handle, constant) in module.constants.iter() {
self.validate_constant(handle, &module.constants, &module.types)
self.validate_constant(handle, module.to_ctx())
.map_err(|source| {
ValidationError::Constant {
handle,
@@ -361,7 +357,7 @@ impl Validator {
for (handle, ty) in module.types.iter() {
let ty_info = self
.validate_type(handle, &module.types, &module.constants)
.validate_type(handle, module.to_ctx())
.map_err(|source| {
ValidationError::Type {
handle,
@@ -376,7 +372,7 @@ impl Validator {
#[cfg(feature = "validate")]
for (var_handle, var) in module.global_variables.iter() {
self.validate_global_var(var, &module.types)
self.validate_global_var(var, module.to_ctx())
.map_err(|source| {
ValidationError::GlobalVariable {
handle: var_handle,
+8 -12
View File
@@ -1,8 +1,5 @@
use super::Capabilities;
use crate::{
arena::{Arena, Handle, UniqueArena},
proc::Alignment,
};
use crate::{arena::Handle, proc::Alignment};
bitflags::bitflags! {
/// Flags associated with [`Type`]s by [`Validator`].
@@ -246,11 +243,10 @@ impl super::Validator {
pub(super) fn validate_type(
&self,
handle: Handle<crate::Type>,
types: &UniqueArena<crate::Type>,
constants: &Arena<crate::Constant>,
gctx: crate::proc::GlobalCtx,
) -> Result<TypeInfo, TypeError> {
use crate::TypeInner as Ti;
Ok(match types[handle].inner {
Ok(match gctx.types[handle].inner {
Ti::Scalar { kind, width } => {
self.check_width(kind, width)?;
let shareable = if kind.is_numeric() {
@@ -419,7 +415,7 @@ impl super::Validator {
let type_info_mask = match size {
crate::ArraySize::Constant(const_handle) => {
let constant = &constants[const_handle];
let constant = &gctx.constants[const_handle];
let length_is_positive = match *constant {
crate::Constant {
specialization: Some(_),
@@ -535,7 +531,7 @@ impl super::Validator {
}
}
let base_size = types[member.ty].inner.size(constants);
let base_size = gctx.types[member.ty].inner.size(gctx);
min_offset = member.offset + base_size;
if min_offset > span {
return Err(TypeError::MemberOutOfBounds {
@@ -579,14 +575,14 @@ impl super::Validator {
}
};
prev_struct_data = match types[member.ty].inner {
prev_struct_data = match gctx.types[member.ty].inner {
crate::TypeInner::Struct { span, .. } => Some((span, member.offset)),
_ => None,
};
// The last field may be an unsized array.
if !base_info.flags.contains(TypeFlags::SIZED) {
let is_array = match types[member.ty].inner {
let is_array = match gctx.types[member.ty].inner {
crate::TypeInner::Array { .. } => true,
_ => false,
};
@@ -635,7 +631,7 @@ impl super::Validator {
if base_info.flags.contains(TypeFlags::DATA) {
// Currently Naga only supports binding arrays of structs for non-handle types.
match types[base].inner {
match gctx.types[base].inner {
crate::TypeInner::Struct { .. } => {}
_ => return Err(TypeError::BindingArrayBaseTypeNotStruct(base)),
};