mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-20 14:31:08 +01:00
refactor(naga): Consolidate some redundant constant "evaluation" helpers
* Eliminates code duplication between the `u32` and `bool` versions. * Rename to `get_const_val` since these aren't really evaluating anything.
This commit is contained in:
1 parent
c4df1af1fd
commit
2a895ea0fb
8 files changed
+98
-113
No files matched your search
@@ -280,7 +280,7 @@ fn process_workgroup_size_override(
|
||||
Some(h) => {
|
||||
ep.workgroup_size[i] = module
|
||||
.to_ctx()
|
||||
.eval_expr_to_u32(adjusted_global_expressions[h])
|
||||
.get_const_val(adjusted_global_expressions[h])
|
||||
.map(|n| {
|
||||
if n == 0 {
|
||||
Err(PipelineConstantError::NegativeWorkgroupSize)
|
||||
@@ -309,13 +309,13 @@ fn process_mesh_shader_overrides(
|
||||
if let Some(r#override) = mesh_info.max_vertices_override {
|
||||
mesh_info.max_vertices = module
|
||||
.to_ctx()
|
||||
.eval_expr_to_u32(adjusted_global_expressions[r#override])
|
||||
.get_const_val(adjusted_global_expressions[r#override])
|
||||
.map_err(|_| PipelineConstantError::NegativeMeshOutputMax)?;
|
||||
}
|
||||
if let Some(r#override) = mesh_info.max_primitives_override {
|
||||
mesh_info.max_primitives = module
|
||||
.to_ctx()
|
||||
.eval_expr_to_u32(adjusted_global_expressions[r#override])
|
||||
.get_const_val(adjusted_global_expressions[r#override])
|
||||
.map_err(|_| PipelineConstantError::NegativeMeshOutputMax)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -561,7 +561,7 @@ impl<'a> Context<'a> {
|
||||
_ => self
|
||||
.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_u32_from(index, &self.expressions)
|
||||
.get_const_val_from(index, &self.expressions)
|
||||
.ok(),
|
||||
};
|
||||
|
||||
|
||||
@@ -211,7 +211,7 @@ impl<'source> ParsingContext<'source> {
|
||||
ctx.global_expression_kind_tracker,
|
||||
)?;
|
||||
|
||||
let res = ctx.module.to_ctx().eval_expr_to_u32(const_expr);
|
||||
let res = ctx.module.to_ctx().get_const_val(const_expr);
|
||||
|
||||
let int = match res {
|
||||
Ok(value) => Ok(value),
|
||||
@@ -219,7 +219,7 @@ impl<'source> ParsingContext<'source> {
|
||||
kind: ErrorKind::SemanticError("int constant overflows".into()),
|
||||
meta,
|
||||
}),
|
||||
Err(ConstValueError::NonConst) => Err(Error {
|
||||
Err(ConstValueError::NonConst | ConstValueError::InvalidType) => Err(Error {
|
||||
kind: ErrorKind::SemanticError("Expected a uint constant".into()),
|
||||
meta,
|
||||
}),
|
||||
|
||||
@@ -277,7 +277,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
let index_maybe = match *index_expr_data {
|
||||
crate::Expression::Constant(const_handle) => Some(
|
||||
ctx.gctx()
|
||||
.eval_expr_to_u32(ctx.module.constants[const_handle].init)
|
||||
.get_const_val(ctx.module.constants[const_handle].init)
|
||||
.map_err(|_| {
|
||||
Error::InvalidAccess(crate::Expression::Constant(
|
||||
const_handle,
|
||||
|
||||
@@ -535,10 +535,10 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
|
||||
.map_err(|e| Box::new(Error::ConstantEvaluatorError(e.into(), span)))
|
||||
}
|
||||
|
||||
fn const_eval_expr_to_u32(
|
||||
fn get_const_val<T: TryFrom<crate::Literal, Error = proc::ConstValueError>>(
|
||||
&self,
|
||||
handle: Handle<ir::Expression>,
|
||||
) -> core::result::Result<u32, proc::ConstValueError> {
|
||||
) -> core::result::Result<T, proc::ConstValueError> {
|
||||
match self.expr_type {
|
||||
ExpressionContextType::Runtime(ref ctx) => {
|
||||
if !ctx.local_expression_kind_tracker.is_const(handle) {
|
||||
@@ -547,41 +547,19 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
|
||||
|
||||
self.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_u32_from(handle, &ctx.function.expressions)
|
||||
.get_const_val_from(handle, &ctx.function.expressions)
|
||||
}
|
||||
ExpressionContextType::Constant(Some(ref ctx)) => {
|
||||
assert!(ctx.local_expression_kind_tracker.is_const(handle));
|
||||
self.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_u32_from(handle, &ctx.function.expressions)
|
||||
.get_const_val_from(handle, &ctx.function.expressions)
|
||||
}
|
||||
ExpressionContextType::Constant(None) => self.module.to_ctx().eval_expr_to_u32(handle),
|
||||
ExpressionContextType::Constant(None) => self.module.to_ctx().get_const_val(handle),
|
||||
ExpressionContextType::Override => Err(proc::ConstValueError::NonConst),
|
||||
}
|
||||
}
|
||||
|
||||
fn const_eval_expr_to_bool(&self, handle: Handle<ir::Expression>) -> Option<bool> {
|
||||
match self.expr_type {
|
||||
ExpressionContextType::Runtime(ref ctx) => {
|
||||
if !ctx.local_expression_kind_tracker.is_const(handle) {
|
||||
return None;
|
||||
}
|
||||
|
||||
self.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_bool_from(handle, &ctx.function.expressions)
|
||||
}
|
||||
ExpressionContextType::Constant(Some(ref ctx)) => {
|
||||
assert!(ctx.local_expression_kind_tracker.is_const(handle));
|
||||
self.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_bool_from(handle, &ctx.function.expressions)
|
||||
}
|
||||
ExpressionContextType::Constant(None) => self.module.to_ctx().eval_expr_to_bool(handle),
|
||||
ExpressionContextType::Override => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return `true` if `handle` is a constant expression.
|
||||
fn is_const(&self, handle: Handle<ir::Expression>) -> bool {
|
||||
use ExpressionContextType as Ect;
|
||||
@@ -716,9 +694,9 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
|
||||
let index = self
|
||||
.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_u32_from(expr, &rctx.function.expressions)
|
||||
.get_const_val_from::<u32, _>(expr, &rctx.function.expressions)
|
||||
.map_err(|err| match err {
|
||||
proc::ConstValueError::NonConst => {
|
||||
proc::ConstValueError::NonConst | proc::ConstValueError::InvalidType => {
|
||||
Error::ExpectedConstExprConcreteIntegerScalar(component_span)
|
||||
}
|
||||
proc::ConstValueError::Negative => {
|
||||
@@ -1419,11 +1397,14 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
match ctx
|
||||
.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_bool_from(condition, &ctx.module.global_expressions)
|
||||
.get_const_val_from(condition, &ctx.module.global_expressions)
|
||||
{
|
||||
Some(true) => Ok(()),
|
||||
Some(false) => Err(Error::ConstAssertFailed(span)),
|
||||
_ => Err(Error::NotBool(span)),
|
||||
Ok(true) => Ok(()),
|
||||
Ok(false) => Err(Error::ConstAssertFailed(span)),
|
||||
Err(proc::ConstValueError::NonConst | proc::ConstValueError::Negative) => {
|
||||
unreachable!()
|
||||
}
|
||||
Err(proc::ConstValueError::InvalidType) => Err(Error::NotBool(span)),
|
||||
}?;
|
||||
}
|
||||
}
|
||||
@@ -1942,14 +1923,10 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
match ctx
|
||||
.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_literal_from(expr, &ctx.function.expressions)
|
||||
.get_const_val_from(expr, &ctx.function.expressions)
|
||||
{
|
||||
Some(ir::Literal::I32(value)) => {
|
||||
ir::SwitchValue::I32(value)
|
||||
}
|
||||
Some(ir::Literal::U32(value)) => {
|
||||
ir::SwitchValue::U32(value)
|
||||
}
|
||||
Ok(ir::Literal::I32(value)) => ir::SwitchValue::I32(value),
|
||||
Ok(ir::Literal::U32(value)) => ir::SwitchValue::U32(value),
|
||||
_ => {
|
||||
return Err(Box::new(Error::InvalidSwitchCase {
|
||||
span,
|
||||
@@ -2170,11 +2147,14 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
match ctx
|
||||
.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_bool_from(condition, &ctx.function.expressions)
|
||||
.get_const_val_from(condition, &ctx.function.expressions)
|
||||
{
|
||||
Some(true) => Ok(()),
|
||||
Some(false) => Err(Error::ConstAssertFailed(span)),
|
||||
_ => Err(Error::NotBool(span)),
|
||||
Ok(true) => Ok(()),
|
||||
Ok(false) => Err(Error::ConstAssertFailed(span)),
|
||||
Err(proc::ConstValueError::NonConst | proc::ConstValueError::Negative) => {
|
||||
unreachable!()
|
||||
}
|
||||
Err(proc::ConstValueError::InvalidType) => Err(Error::NotBool(span)),
|
||||
}?;
|
||||
|
||||
block.extend(emitter.finish(&ctx.function.expressions));
|
||||
@@ -2372,7 +2352,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
}
|
||||
}
|
||||
|
||||
lowered_base.try_map(|base| match ctx.const_eval_expr_to_u32(index).ok() {
|
||||
lowered_base.try_map(|base| match ctx.get_const_val(index).ok() {
|
||||
Some(index) => Ok::<_, Box<Error>>(ir::Expression::AccessIndex { base, index }),
|
||||
None => {
|
||||
// When an abstract array value e is indexed by an expression
|
||||
@@ -2567,7 +2547,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
result_var,
|
||||
)))
|
||||
} else {
|
||||
let left_val = ctx.const_eval_expr_to_bool(left);
|
||||
let left_val: Option<bool> = ctx.get_const_val(left).ok();
|
||||
|
||||
if left_val.is_some_and(|left_val| {
|
||||
op == crate::BinaryOperator::LogicalAnd && !left_val
|
||||
@@ -4203,9 +4183,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
let value = ctx
|
||||
.module
|
||||
.to_ctx()
|
||||
.eval_expr_to_u32(expr)
|
||||
.get_const_val(expr)
|
||||
.map_err(|err| match err {
|
||||
proc::ConstValueError::NonConst => Error::ExpectedConstExprConcreteIntegerScalar(span),
|
||||
proc::ConstValueError::NonConst | proc::ConstValueError::InvalidType => {
|
||||
Error::ExpectedConstExprConcreteIntegerScalar(span)
|
||||
}
|
||||
proc::ConstValueError::Negative => Error::ExpectedNonNegative(span),
|
||||
})?;
|
||||
Ok((value, span))
|
||||
@@ -4222,9 +4204,10 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
let const_expr = self.expression(expr, &mut ctx.as_const());
|
||||
match const_expr {
|
||||
Ok(value) => {
|
||||
let len = ctx.const_eval_expr_to_u32(value).map_err(|err| {
|
||||
let len = ctx.get_const_val(value).map_err(|err| {
|
||||
Box::new(match err {
|
||||
proc::ConstValueError::NonConst => {
|
||||
proc::ConstValueError::NonConst
|
||||
| proc::ConstValueError::InvalidType => {
|
||||
Error::ExpectedConstExprConcreteIntegerScalar(span)
|
||||
}
|
||||
proc::ConstValueError::Negative => {
|
||||
|
||||
@@ -483,7 +483,7 @@ impl GuardedIndex {
|
||||
expressions: &crate::Arena<crate::Expression>,
|
||||
module: &crate::Module,
|
||||
) -> Self {
|
||||
match module.to_ctx().eval_expr_to_u32_from(expr, expressions) {
|
||||
match module.to_ctx().get_const_val_from(expr, expressions) {
|
||||
Ok(value) => Self::Known(value),
|
||||
Err(_) => Self::Expression(expr),
|
||||
}
|
||||
|
||||
+53
-54
@@ -172,6 +172,29 @@ impl crate::Literal {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<crate::Literal> for u32 {
|
||||
type Error = ConstValueError;
|
||||
|
||||
fn try_from(value: crate::Literal) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
crate::Literal::U32(value) => Ok(value),
|
||||
crate::Literal::I32(value) => value.try_into().map_err(|_| ConstValueError::Negative),
|
||||
_ => Err(ConstValueError::InvalidType),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<crate::Literal> for bool {
|
||||
type Error = ConstValueError;
|
||||
|
||||
fn try_from(value: crate::Literal) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
crate::Literal::Bool(value) => Ok(value),
|
||||
_ => Err(ConstValueError::InvalidType),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl super::AddressSpace {
|
||||
pub fn access(self) -> crate::StorageAccess {
|
||||
use crate::StorageAccess as Sa;
|
||||
@@ -425,9 +448,16 @@ impl crate::Module {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) enum ConstValueError {
|
||||
pub enum ConstValueError {
|
||||
NonConst,
|
||||
Negative,
|
||||
InvalidType,
|
||||
}
|
||||
|
||||
impl From<core::convert::Infallible> for ConstValueError {
|
||||
fn from(_: core::convert::Infallible) -> Self {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -452,63 +482,26 @@ impl GlobalCtx<'_> {
|
||||
)),
|
||||
allow(dead_code)
|
||||
)]
|
||||
pub(super) fn eval_expr_to_u32(
|
||||
pub(super) fn get_const_val<T, E>(
|
||||
&self,
|
||||
handle: crate::Handle<crate::Expression>,
|
||||
) -> Result<u32, ConstValueError> {
|
||||
self.eval_expr_to_u32_from(handle, self.global_expressions)
|
||||
) -> Result<T, ConstValueError>
|
||||
where
|
||||
T: TryFrom<crate::Literal, Error = E>,
|
||||
E: Into<ConstValueError>,
|
||||
{
|
||||
self.get_const_val_from(handle, self.global_expressions)
|
||||
}
|
||||
|
||||
/// Try to evaluate the expression in the `arena` using its `handle` and return it as a `u32`.
|
||||
pub(super) fn eval_expr_to_u32_from(
|
||||
pub(super) fn get_const_val_from<T, E>(
|
||||
&self,
|
||||
handle: crate::Handle<crate::Expression>,
|
||||
arena: &crate::Arena<crate::Expression>,
|
||||
) -> Result<u32, ConstValueError> {
|
||||
match self.eval_expr_to_literal_from(handle, arena) {
|
||||
Some(crate::Literal::U32(value)) => Ok(value),
|
||||
Some(crate::Literal::I32(value)) => {
|
||||
value.try_into().map_err(|_| ConstValueError::Negative)
|
||||
}
|
||||
_ => Err(ConstValueError::NonConst),
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to evaluate the expression in `self.global_expressions` using its `handle` and return it as a `bool`.
|
||||
#[cfg_attr(not(feature = "wgsl-in"), allow(dead_code))]
|
||||
pub(super) fn eval_expr_to_bool(
|
||||
&self,
|
||||
handle: crate::Handle<crate::Expression>,
|
||||
) -> Option<bool> {
|
||||
self.eval_expr_to_bool_from(handle, self.global_expressions)
|
||||
}
|
||||
|
||||
/// Try to evaluate the expression in the `arena` using its `handle` and return it as a `bool`.
|
||||
#[cfg_attr(not(feature = "wgsl-in"), allow(dead_code))]
|
||||
pub(super) fn eval_expr_to_bool_from(
|
||||
&self,
|
||||
handle: crate::Handle<crate::Expression>,
|
||||
arena: &crate::Arena<crate::Expression>,
|
||||
) -> Option<bool> {
|
||||
match self.eval_expr_to_literal_from(handle, arena) {
|
||||
Some(crate::Literal::Bool(value)) => Some(value),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(dead_code)]
|
||||
pub(crate) fn eval_expr_to_literal(
|
||||
&self,
|
||||
handle: crate::Handle<crate::Expression>,
|
||||
) -> Option<crate::Literal> {
|
||||
self.eval_expr_to_literal_from(handle, self.global_expressions)
|
||||
}
|
||||
|
||||
pub(super) fn eval_expr_to_literal_from(
|
||||
&self,
|
||||
handle: crate::Handle<crate::Expression>,
|
||||
arena: &crate::Arena<crate::Expression>,
|
||||
) -> Option<crate::Literal> {
|
||||
) -> Result<T, ConstValueError>
|
||||
where
|
||||
T: TryFrom<crate::Literal, Error = E>,
|
||||
E: Into<ConstValueError>,
|
||||
{
|
||||
fn get(
|
||||
gctx: GlobalCtx,
|
||||
handle: crate::Handle<crate::Expression>,
|
||||
@@ -523,11 +516,15 @@ impl GlobalCtx<'_> {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
match arena[handle] {
|
||||
let value = match arena[handle] {
|
||||
crate::Expression::Constant(c) => {
|
||||
get(*self, self.constants[c].init, self.global_expressions)
|
||||
}
|
||||
_ => get(*self, handle, arena),
|
||||
};
|
||||
match value {
|
||||
Some(v) => v.try_into().map_err(Into::into),
|
||||
None => Err(ConstValueError::NonConst),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -561,9 +558,11 @@ impl crate::ArraySize {
|
||||
let Some(expr) = gctx.overrides[handle].init else {
|
||||
return Err(ResolveArraySizeError::NonConstArrayLength);
|
||||
};
|
||||
let length = gctx.eval_expr_to_u32(expr).map_err(|err| match err {
|
||||
let length = gctx.get_const_val(expr).map_err(|err| match err {
|
||||
ConstValueError::NonConst => ResolveArraySizeError::NonConstArrayLength,
|
||||
ConstValueError::Negative => ResolveArraySizeError::ExpectedPositiveArrayLength,
|
||||
ConstValueError::Negative | ConstValueError::InvalidType => {
|
||||
ResolveArraySizeError::ExpectedPositiveArrayLength
|
||||
}
|
||||
})?;
|
||||
|
||||
if length == 0 {
|
||||
|
||||
@@ -292,7 +292,7 @@ impl super::Validator {
|
||||
// If index is const we can do check for non-negative index
|
||||
match module
|
||||
.to_ctx()
|
||||
.eval_expr_to_u32_from(index, &function.expressions)
|
||||
.get_const_val_from(index, &function.expressions)
|
||||
{
|
||||
Ok(value) => {
|
||||
let length = if self.overrides_resolved {
|
||||
@@ -312,6 +312,9 @@ impl super::Validator {
|
||||
return Err(ExpressionError::NegativeIndex(base))
|
||||
}
|
||||
Err(crate::proc::ConstValueError::NonConst) => {}
|
||||
Err(crate::proc::ConstValueError::InvalidType) => {
|
||||
return Err(ExpressionError::InvalidIndexType(index))
|
||||
}
|
||||
}
|
||||
|
||||
ShaderStages::all()
|
||||
|
||||
Reference in new issue
Block a user