| 1 |
use alloc::{ |
| 2 |
format, |
| 3 |
string::{String, ToString}, |
| 4 |
vec, |
| 5 |
vec::Vec, |
| 6 |
}; |
| 7 |
use core::iter; |
| 8 |
|
| 9 |
use arrayvec::ArrayVec; |
| 10 |
use half::f16; |
| 11 |
use num_traits::{real::Real, FromPrimitive, One, ToPrimitive, Zero}; |
| 12 |
|
| 13 |
use crate::{ |
| 14 |
arena::{Arena, Handle, HandleVec, UniqueArena}, |
| 15 |
ArraySize, BinaryOperator, Constant, Expression, Literal, Override, RelationalFunction, |
| 16 |
ScalarKind, Span, Type, TypeInner, UnaryOperator, |
| 17 |
}; |
| 18 |
|
| 19 |
#[cfg(feature = "wgsl-in")] |
| 20 |
use crate::common::wgsl::TryToWgsl; |
| 21 |
|
| 22 |
/// A macro that allows dollar signs (`$`) to be emitted by other macros. Useful for generating |
| 23 |
/// `macro_rules!` items that, in turn, emit their own `macro_rules!` items. |
| 24 |
/// |
| 25 |
/// Technique stolen directly from |
| 26 |
/// <https://github.com/rust-lang/rust/issues/35853#issuecomment-415993963>. |
| 27 |
macro_rules! with_dollar_sign { |
| 28 |
($($body:tt)*) => { |
| 29 |
macro_rules! __with_dollar_sign { $($body)* } |
| 30 |
__with_dollar_sign!($); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
macro_rules! gen_component_wise_extractor { |
| 35 |
( |
| 36 |
$ident:ident -> $target:ident, |
| 37 |
literals: [$( $literal:ident => $mapping:ident: $ty:ident ),+ $(,)?], |
| 38 |
scalar_kinds: [$( $scalar_kind:ident ),* $(,)?], |
| 39 |
) => { |
| 40 |
/// A subset of [`Literal`]s intended to be used for implementing numeric built-ins. |
| 41 |
#[derive(Debug)] |
| 42 |
#[cfg_attr(test, derive(PartialEq))] |
| 43 |
enum $target<const N: usize> { |
| 44 |
$( |
| 45 |
#[doc = concat!( |
| 46 |
"Maps to [`Literal::", |
| 47 |
stringify!($literal), |
| 48 |
"`]", |
| 49 |
)] |
| 50 |
$mapping([$ty; N]), |
| 51 |
)+ |
| 52 |
} |
| 53 |
|
| 54 |
impl From<$target<1>> for Expression { |
| 55 |
fn from(value: $target<1>) -> Self { |
| 56 |
match value { |
| 57 |
$( |
| 58 |
$target::$mapping([value]) => { |
| 59 |
Expression::Literal(Literal::$literal(value)) |
| 60 |
} |
| 61 |
)+ |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[doc = concat!( |
| 67 |
"Attempts to evaluate multiple `exprs` as a combined [`", |
| 68 |
stringify!($target), |
| 69 |
"`] to pass to `handler`. ", |
| 70 |
)] |
| 71 |
/// If `exprs` are vectors of the same length, `handler` is called for each corresponding |
| 72 |
/// component of each vector. |
| 73 |
/// |
| 74 |
/// `handler`'s output is registered as a new expression. If `exprs` are vectors of the |
| 75 |
/// same length, a new vector expression is registered, composed of each component emitted |
| 76 |
/// by `handler`. |
| 77 |
fn $ident<const N: usize, const M: usize, F>( |
| 78 |
eval: &mut ConstantEvaluator<'_>, |
| 79 |
span: Span, |
| 80 |
exprs: [Handle<Expression>; N], |
| 81 |
mut handler: F, |
| 82 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> |
| 83 |
where |
| 84 |
$target<M>: Into<Expression>, |
| 85 |
F: FnMut($target<N>) -> Result<$target<M>, ConstantEvaluatorError> + Clone, |
| 86 |
{ |
| 87 |
assert!(N > 0); |
| 88 |
let err = ConstantEvaluatorError::InvalidMathArg; |
| 89 |
let mut exprs = exprs.into_iter(); |
| 90 |
|
| 91 |
macro_rules! sanitize { |
| 92 |
($expr:expr) => { |
| 93 |
eval.eval_zero_value_and_splat($expr, span) |
| 94 |
.map(|expr| &eval.expressions[expr]) |
| 95 |
}; |
| 96 |
} |
| 97 |
|
| 98 |
let new_expr = match sanitize!(exprs.next().unwrap())? { |
| 99 |
$( |
| 100 |
&Expression::Literal(Literal::$literal(x)) => iter::once(Ok(x)) |
| 101 |
.chain(exprs.map(|expr| { |
| 102 |
sanitize!(expr).and_then(|expr| match expr { |
| 103 |
&Expression::Literal(Literal::$literal(x)) => Ok(x), |
| 104 |
_ => Err(err.clone()), |
| 105 |
}) |
| 106 |
})) |
| 107 |
.collect::<Result<ArrayVec<_, N>, _>>() |
| 108 |
.map(|a| a.into_inner().unwrap()) |
| 109 |
.map($target::$mapping) |
| 110 |
.and_then(|comps| Ok(handler(comps)?.into())), |
| 111 |
)+ |
| 112 |
&Expression::Compose { ty, ref components } => match &eval.types[ty].inner { |
| 113 |
&TypeInner::Vector { size, scalar } => match scalar.kind { |
| 114 |
$(ScalarKind::$scalar_kind)|* => { |
| 115 |
let first_ty = ty; |
| 116 |
let mut component_groups = |
| 117 |
ArrayVec::<ArrayVec<_, { crate::VectorSize::MAX }>, N>::new(); |
| 118 |
component_groups.push(crate::proc::flatten_compose( |
| 119 |
first_ty, |
| 120 |
components, |
| 121 |
eval.expressions, |
| 122 |
eval.types, |
| 123 |
).collect()); |
| 124 |
component_groups.extend( |
| 125 |
exprs |
| 126 |
.map(|expr| { |
| 127 |
sanitize!(expr).and_then(|expr| match expr { |
| 128 |
&Expression::Compose { ty, ref components } |
| 129 |
if &eval.types[ty].inner |
| 130 |
== &eval.types[first_ty].inner => |
| 131 |
{ |
| 132 |
Ok(crate::proc::flatten_compose( |
| 133 |
ty, |
| 134 |
components, |
| 135 |
eval.expressions, |
| 136 |
eval.types, |
| 137 |
).collect()) |
| 138 |
} |
| 139 |
_ => Err(err.clone()), |
| 140 |
}) |
| 141 |
}) |
| 142 |
.collect::<Result<ArrayVec<_, { crate::VectorSize::MAX }>, _>>( |
| 143 |
)?, |
| 144 |
); |
| 145 |
let component_groups = component_groups.into_inner().unwrap(); |
| 146 |
let mut new_components = |
| 147 |
ArrayVec::<_, { crate::VectorSize::MAX }>::new(); |
| 148 |
for idx in 0..(size as u8).into() { |
| 149 |
let group = component_groups |
| 150 |
.iter() |
| 151 |
.map(|cs| cs.get(idx).cloned().ok_or(err.clone())) |
| 152 |
.collect::<Result<ArrayVec<_, N>, _>>()? |
| 153 |
.into_inner() |
| 154 |
.unwrap(); |
| 155 |
new_components.push($ident( |
| 156 |
eval, |
| 157 |
span, |
| 158 |
group, |
| 159 |
handler.clone(), |
| 160 |
)?); |
| 161 |
} |
| 162 |
Ok(Expression::Compose { |
| 163 |
ty: first_ty, |
| 164 |
components: new_components.into_iter().collect(), |
| 165 |
}) |
| 166 |
} |
| 167 |
_ => return Err(err), |
| 168 |
}, |
| 169 |
_ => return Err(err), |
| 170 |
}, |
| 171 |
_ => return Err(err), |
| 172 |
}?; |
| 173 |
eval.register_evaluated_expr(new_expr, span) |
| 174 |
} |
| 175 |
|
| 176 |
with_dollar_sign! { |
| 177 |
($d:tt) => { |
| 178 |
#[allow(unused)] |
| 179 |
#[doc = concat!( |
| 180 |
"A convenience macro for using the same RHS for each [`", |
| 181 |
stringify!($target), |
| 182 |
"`] variant in a call to [`", |
| 183 |
stringify!($ident), |
| 184 |
"`].", |
| 185 |
)] |
| 186 |
macro_rules! $ident { |
| 187 |
( |
| 188 |
$eval:expr, |
| 189 |
$span:expr, |
| 190 |
[$d ($d expr:expr),+ $d (,)?], |
| 191 |
|$d ($d arg:ident),+| $d tt:tt |
| 192 |
) => { |
| 193 |
$ident($eval, $span, [$d ($d expr),+], |args| match args { |
| 194 |
$( |
| 195 |
$target::$mapping([$d ($d arg),+]) => { |
| 196 |
let res = $d tt; |
| 197 |
Result::map(res, $target::$mapping) |
| 198 |
}, |
| 199 |
)+ |
| 200 |
}) |
| 201 |
}; |
| 202 |
} |
| 203 |
}; |
| 204 |
} |
| 205 |
}; |
| 206 |
} |
| 207 |
|
| 208 |
gen_component_wise_extractor! { |
| 209 |
component_wise_scalar -> Scalar, |
| 210 |
literals: [ |
| 211 |
AbstractFloat => AbstractFloat: f64, |
| 212 |
F32 => F32: f32, |
| 213 |
F16 => F16: f16, |
| 214 |
AbstractInt => AbstractInt: i64, |
| 215 |
U32 => U32: u32, |
| 216 |
I32 => I32: i32, |
| 217 |
U64 => U64: u64, |
| 218 |
I64 => I64: i64, |
| 219 |
], |
| 220 |
scalar_kinds: [ |
| 221 |
Float, |
| 222 |
AbstractFloat, |
| 223 |
Sint, |
| 224 |
Uint, |
| 225 |
AbstractInt, |
| 226 |
], |
| 227 |
} |
| 228 |
|
| 229 |
gen_component_wise_extractor! { |
| 230 |
component_wise_float -> Float, |
| 231 |
literals: [ |
| 232 |
AbstractFloat => Abstract: f64, |
| 233 |
F32 => F32: f32, |
| 234 |
F16 => F16: f16, |
| 235 |
], |
| 236 |
scalar_kinds: [ |
| 237 |
Float, |
| 238 |
AbstractFloat, |
| 239 |
], |
| 240 |
} |
| 241 |
|
| 242 |
gen_component_wise_extractor! { |
| 243 |
component_wise_concrete_int -> ConcreteInt, |
| 244 |
literals: [ |
| 245 |
U32 => U32: u32, |
| 246 |
I32 => I32: i32, |
| 247 |
], |
| 248 |
scalar_kinds: [ |
| 249 |
Sint, |
| 250 |
Uint, |
| 251 |
], |
| 252 |
} |
| 253 |
|
| 254 |
gen_component_wise_extractor! { |
| 255 |
component_wise_signed -> Signed, |
| 256 |
literals: [ |
| 257 |
AbstractFloat => AbstractFloat: f64, |
| 258 |
AbstractInt => AbstractInt: i64, |
| 259 |
F32 => F32: f32, |
| 260 |
F16 => F16: f16, |
| 261 |
I32 => I32: i32, |
| 262 |
], |
| 263 |
scalar_kinds: [ |
| 264 |
Sint, |
| 265 |
AbstractInt, |
| 266 |
Float, |
| 267 |
AbstractFloat, |
| 268 |
], |
| 269 |
} |
| 270 |
|
| 271 |
#[derive(Debug)] |
| 272 |
enum Behavior<'a> { |
| 273 |
Wgsl(WgslRestrictions<'a>), |
| 274 |
Glsl(GlslRestrictions<'a>), |
| 275 |
} |
| 276 |
|
| 277 |
impl Behavior<'_> { |
| 278 |
/// Returns `true` if the inner WGSL/GLSL restrictions are runtime restrictions. |
| 279 |
const fn has_runtime_restrictions(&self) -> bool { |
| 280 |
matches!( |
| 281 |
self, |
| 282 |
&Behavior::Wgsl(WgslRestrictions::Runtime(_)) |
| 283 |
| &Behavior::Glsl(GlslRestrictions::Runtime(_)) |
| 284 |
) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/// A context for evaluating constant expressions. |
| 289 |
/// |
| 290 |
/// A `ConstantEvaluator` points at an expression arena to which it can append |
| 291 |
/// newly evaluated expressions: you pass [`try_eval_and_append`] whatever kind |
| 292 |
/// of Naga [`Expression`] you like, and if its value can be computed at compile |
| 293 |
/// time, `try_eval_and_append` appends an expression representing the computed |
| 294 |
/// value - a tree of [`Literal`], [`Compose`], [`ZeroValue`], and [`Swizzle`] |
| 295 |
/// expressions - to the arena. See the [`try_eval_and_append`] method for details. |
| 296 |
/// |
| 297 |
/// A `ConstantEvaluator` also holds whatever information we need to carry out |
| 298 |
/// that evaluation: types, other constants, and so on. |
| 299 |
/// |
| 300 |
/// [`try_eval_and_append`]: ConstantEvaluator::try_eval_and_append |
| 301 |
/// [`Compose`]: Expression::Compose |
| 302 |
/// [`ZeroValue`]: Expression::ZeroValue |
| 303 |
/// [`Literal`]: Expression::Literal |
| 304 |
/// [`Swizzle`]: Expression::Swizzle |
| 305 |
#[derive(Debug)] |
| 306 |
pub struct ConstantEvaluator<'a> { |
| 307 |
/// Which language's evaluation rules we should follow. |
| 308 |
behavior: Behavior<'a>, |
| 309 |
|
| 310 |
/// The module's type arena. |
| 311 |
/// |
| 312 |
/// Because expressions like [`Splat`] contain type handles, we need to be |
| 313 |
/// able to add new types to produce those expressions. |
| 314 |
/// |
| 315 |
/// [`Splat`]: Expression::Splat |
| 316 |
types: &'a mut UniqueArena<Type>, |
| 317 |
|
| 318 |
/// The module's constant arena. |
| 319 |
constants: &'a Arena<Constant>, |
| 320 |
|
| 321 |
/// The module's override arena. |
| 322 |
overrides: &'a Arena<Override>, |
| 323 |
|
| 324 |
/// The arena to which we are contributing expressions. |
| 325 |
expressions: &'a mut Arena<Expression>, |
| 326 |
|
| 327 |
/// Tracks the constness of expressions residing in [`Self::expressions`] |
| 328 |
expression_kind_tracker: &'a mut ExpressionKindTracker, |
| 329 |
|
| 330 |
layouter: &'a mut crate::proc::Layouter, |
| 331 |
} |
| 332 |
|
| 333 |
#[derive(Debug)] |
| 334 |
enum WgslRestrictions<'a> { |
| 335 |
/// - const-expressions will be evaluated and inserted in the arena |
| 336 |
Const(Option<FunctionLocalData<'a>>), |
| 337 |
/// - const-expressions will be evaluated and inserted in the arena |
| 338 |
/// - override-expressions will be inserted in the arena |
| 339 |
Override, |
| 340 |
/// - const-expressions will be evaluated and inserted in the arena |
| 341 |
/// - override-expressions will be inserted in the arena |
| 342 |
/// - runtime-expressions will be inserted in the arena |
| 343 |
Runtime(FunctionLocalData<'a>), |
| 344 |
} |
| 345 |
|
| 346 |
#[derive(Debug)] |
| 347 |
enum GlslRestrictions<'a> { |
| 348 |
/// - const-expressions will be evaluated and inserted in the arena |
| 349 |
Const, |
| 350 |
/// - const-expressions will be evaluated and inserted in the arena |
| 351 |
/// - override-expressions will be inserted in the arena |
| 352 |
/// - runtime-expressions will be inserted in the arena |
| 353 |
Runtime(FunctionLocalData<'a>), |
| 354 |
} |
| 355 |
|
| 356 |
#[derive(Debug)] |
| 357 |
struct FunctionLocalData<'a> { |
| 358 |
/// Global constant expressions |
| 359 |
global_expressions: &'a Arena<Expression>, |
| 360 |
emitter: &'a mut super::Emitter, |
| 361 |
block: &'a mut crate::Block, |
| 362 |
} |
| 363 |
|
| 364 |
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] |
| 365 |
pub enum ExpressionKind { |
| 366 |
Const, |
| 367 |
Override, |
| 368 |
Runtime, |
| 369 |
} |
| 370 |
|
| 371 |
#[derive(Debug)] |
| 372 |
pub struct ExpressionKindTracker { |
| 373 |
inner: HandleVec<Expression, ExpressionKind>, |
| 374 |
} |
| 375 |
|
| 376 |
impl ExpressionKindTracker { |
| 377 |
pub const fn new() -> Self { |
| 378 |
Self { |
| 379 |
inner: HandleVec::new(), |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/// Forces the the expression to not be const |
| 384 |
pub fn force_non_const(&mut self, value: Handle<Expression>) { |
| 385 |
self.inner[value] = ExpressionKind::Runtime; |
| 386 |
} |
| 387 |
|
| 388 |
pub fn insert(&mut self, value: Handle<Expression>, expr_type: ExpressionKind) { |
| 389 |
self.inner.insert(value, expr_type); |
| 390 |
} |
| 391 |
|
| 392 |
pub fn is_const(&self, h: Handle<Expression>) -> bool { |
| 393 |
matches!(self.type_of(h), ExpressionKind::Const) |
| 394 |
} |
| 395 |
|
| 396 |
pub fn is_const_or_override(&self, h: Handle<Expression>) -> bool { |
| 397 |
matches!( |
| 398 |
self.type_of(h), |
| 399 |
ExpressionKind::Const | ExpressionKind::Override |
| 400 |
) |
| 401 |
} |
| 402 |
|
| 403 |
fn type_of(&self, value: Handle<Expression>) -> ExpressionKind { |
| 404 |
self.inner[value] |
| 405 |
} |
| 406 |
|
| 407 |
pub fn from_arena(arena: &Arena<Expression>) -> Self { |
| 408 |
let mut tracker = Self { |
| 409 |
inner: HandleVec::with_capacity(arena.len()), |
| 410 |
}; |
| 411 |
for (handle, expr) in arena.iter() { |
| 412 |
tracker |
| 413 |
.inner |
| 414 |
.insert(handle, tracker.type_of_with_expr(expr)); |
| 415 |
} |
| 416 |
tracker |
| 417 |
} |
| 418 |
|
| 419 |
fn type_of_with_expr(&self, expr: &Expression) -> ExpressionKind { |
| 420 |
match *expr { |
| 421 |
Expression::Literal(_) | Expression::ZeroValue(_) | Expression::Constant(_) => { |
| 422 |
ExpressionKind::Const |
| 423 |
} |
| 424 |
Expression::Override(_) => ExpressionKind::Override, |
| 425 |
Expression::Compose { ref components, .. } => { |
| 426 |
let mut expr_type = ExpressionKind::Const; |
| 427 |
for component in components { |
| 428 |
expr_type = expr_type.max(self.type_of(*component)) |
| 429 |
} |
| 430 |
expr_type |
| 431 |
} |
| 432 |
Expression::Splat { value, .. } => self.type_of(value), |
| 433 |
Expression::AccessIndex { base, .. } => self.type_of(base), |
| 434 |
Expression::Access { base, index } => self.type_of(base).max(self.type_of(index)), |
| 435 |
Expression::Swizzle { vector, .. } => self.type_of(vector), |
| 436 |
Expression::Unary { expr, .. } => self.type_of(expr), |
| 437 |
Expression::Binary { left, right, .. } => self.type_of(left).max(self.type_of(right)), |
| 438 |
Expression::Math { |
| 439 |
arg, |
| 440 |
arg1, |
| 441 |
arg2, |
| 442 |
arg3, |
| 443 |
.. |
| 444 |
} => self |
| 445 |
.type_of(arg) |
| 446 |
.max( |
| 447 |
arg1.map(|arg| self.type_of(arg)) |
| 448 |
.unwrap_or(ExpressionKind::Const), |
| 449 |
) |
| 450 |
.max( |
| 451 |
arg2.map(|arg| self.type_of(arg)) |
| 452 |
.unwrap_or(ExpressionKind::Const), |
| 453 |
) |
| 454 |
.max( |
| 455 |
arg3.map(|arg| self.type_of(arg)) |
| 456 |
.unwrap_or(ExpressionKind::Const), |
| 457 |
), |
| 458 |
Expression::As { expr, .. } => self.type_of(expr), |
| 459 |
Expression::Select { |
| 460 |
condition, |
| 461 |
accept, |
| 462 |
reject, |
| 463 |
} => self |
| 464 |
.type_of(condition) |
| 465 |
.max(self.type_of(accept)) |
| 466 |
.max(self.type_of(reject)), |
| 467 |
Expression::Relational { argument, .. } => self.type_of(argument), |
| 468 |
Expression::ArrayLength(expr) => self.type_of(expr), |
| 469 |
_ => ExpressionKind::Runtime, |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
#[derive(Clone, Debug, thiserror::Error)] |
| 475 |
#[cfg_attr(test, derive(PartialEq))] |
| 476 |
pub enum ConstantEvaluatorError { |
| 477 |
#[error("Constants cannot access function arguments")] |
| 478 |
FunctionArg, |
| 479 |
#[error("Constants cannot access global variables")] |
| 480 |
GlobalVariable, |
| 481 |
#[error("Constants cannot access local variables")] |
| 482 |
LocalVariable, |
| 483 |
#[error("Cannot get the array length of a non array type")] |
| 484 |
InvalidArrayLengthArg, |
| 485 |
#[error("Constants cannot get the array length of a dynamically sized array")] |
| 486 |
ArrayLengthDynamic, |
| 487 |
#[error("Cannot call arrayLength on array sized by override-expression")] |
| 488 |
ArrayLengthOverridden, |
| 489 |
#[error("Constants cannot call functions")] |
| 490 |
Call, |
| 491 |
#[error("Constants don't support workGroupUniformLoad")] |
| 492 |
WorkGroupUniformLoadResult, |
| 493 |
#[error("Constants don't support atomic functions")] |
| 494 |
Atomic, |
| 495 |
#[error("Constants don't support derivative functions")] |
| 496 |
Derivative, |
| 497 |
#[error("Constants don't support load expressions")] |
| 498 |
Load, |
| 499 |
#[error("Constants don't support image expressions")] |
| 500 |
ImageExpression, |
| 501 |
#[error("Constants don't support ray query expressions")] |
| 502 |
RayQueryExpression, |
| 503 |
#[error("Constants don't support subgroup expressions")] |
| 504 |
SubgroupExpression, |
| 505 |
#[error("Cannot access the type")] |
| 506 |
InvalidAccessBase, |
| 507 |
#[error("Cannot access at the index")] |
| 508 |
InvalidAccessIndex, |
| 509 |
#[error("Cannot access with index of type")] |
| 510 |
InvalidAccessIndexTy, |
| 511 |
#[error("Constants don't support array length expressions")] |
| 512 |
ArrayLength, |
| 513 |
#[error("Cannot cast scalar components of expression `{from}` to type `{to}`")] |
| 514 |
InvalidCastArg { from: String, to: String }, |
| 515 |
#[error("Cannot apply the unary op to the argument")] |
| 516 |
InvalidUnaryOpArg, |
| 517 |
#[error("Cannot apply the binary op to the arguments")] |
| 518 |
InvalidBinaryOpArgs, |
| 519 |
#[error("Cannot apply math function to type")] |
| 520 |
InvalidMathArg, |
| 521 |
#[error("{0:?} built-in function expects {1:?} arguments but {2:?} were supplied")] |
| 522 |
InvalidMathArgCount(crate::MathFunction, usize, usize), |
| 523 |
#[error("Cannot apply relational function to type")] |
| 524 |
InvalidRelationalArg(RelationalFunction), |
| 525 |
#[error("value of `low` is greater than `high` for clamp built-in function")] |
| 526 |
InvalidClamp, |
| 527 |
#[error("Constructor expects {expected} components, found {actual}")] |
| 528 |
InvalidVectorComposeLength { expected: usize, actual: usize }, |
| 529 |
#[error("Constructor must only contain vector or scalar arguments")] |
| 530 |
InvalidVectorComposeComponent, |
| 531 |
#[error("Splat is defined only on scalar values")] |
| 532 |
SplatScalarOnly, |
| 533 |
#[error("Can only swizzle vector constants")] |
| 534 |
SwizzleVectorOnly, |
| 535 |
#[error("swizzle component not present in source expression")] |
| 536 |
SwizzleOutOfBounds, |
| 537 |
#[error("Type is not constructible")] |
| 538 |
TypeNotConstructible, |
| 539 |
#[error("Subexpression(s) are not constant")] |
| 540 |
SubexpressionsAreNotConstant, |
| 541 |
#[error("Not implemented as constant expression: {0}")] |
| 542 |
NotImplemented(String), |
| 543 |
#[error("{0} operation overflowed")] |
| 544 |
Overflow(String), |
| 545 |
#[error( |
| 546 |
"the concrete type `{to_type}` cannot represent the abstract value `{value}` accurately" |
| 547 |
)] |
| 548 |
AutomaticConversionLossy { |
| 549 |
value: String, |
| 550 |
to_type: &'static str, |
| 551 |
}, |
| 552 |
#[error("Division by zero")] |
| 553 |
DivisionByZero, |
| 554 |
#[error("Remainder by zero")] |
| 555 |
RemainderByZero, |
| 556 |
#[error("RHS of shift operation is greater than or equal to 32")] |
| 557 |
ShiftedMoreThan32Bits, |
| 558 |
#[error(transparent)] |
| 559 |
Literal(#[from] crate::valid::LiteralError), |
| 560 |
#[error("Can't use pipeline-overridable constants in const-expressions")] |
| 561 |
Override, |
| 562 |
#[error("Unexpected runtime-expression")] |
| 563 |
RuntimeExpr, |
| 564 |
#[error("Unexpected override-expression")] |
| 565 |
OverrideExpr, |
| 566 |
} |
| 567 |
|
| 568 |
impl<'a> ConstantEvaluator<'a> { |
| 569 |
/// Return a [`ConstantEvaluator`] that will add expressions to `module`'s |
| 570 |
/// constant expression arena. |
| 571 |
/// |
| 572 |
/// Report errors according to WGSL's rules for constant evaluation. |
| 573 |
pub fn for_wgsl_module( |
| 574 |
module: &'a mut crate::Module, |
| 575 |
global_expression_kind_tracker: &'a mut ExpressionKindTracker, |
| 576 |
layouter: &'a mut crate::proc::Layouter, |
| 577 |
in_override_ctx: bool, |
| 578 |
) -> Self { |
| 579 |
Self::for_module( |
| 580 |
Behavior::Wgsl(if in_override_ctx { |
| 581 |
WgslRestrictions::Override |
| 582 |
} else { |
| 583 |
WgslRestrictions::Const(None) |
| 584 |
}), |
| 585 |
module, |
| 586 |
global_expression_kind_tracker, |
| 587 |
layouter, |
| 588 |
) |
| 589 |
} |
| 590 |
|
| 591 |
/// Return a [`ConstantEvaluator`] that will add expressions to `module`'s |
| 592 |
/// constant expression arena. |
| 593 |
/// |
| 594 |
/// Report errors according to GLSL's rules for constant evaluation. |
| 595 |
pub fn for_glsl_module( |
| 596 |
module: &'a mut crate::Module, |
| 597 |
global_expression_kind_tracker: &'a mut ExpressionKindTracker, |
| 598 |
layouter: &'a mut crate::proc::Layouter, |
| 599 |
) -> Self { |
| 600 |
Self::for_module( |
| 601 |
Behavior::Glsl(GlslRestrictions::Const), |
| 602 |
module, |
| 603 |
global_expression_kind_tracker, |
| 604 |
layouter, |
| 605 |
) |
| 606 |
} |
| 607 |
|
| 608 |
fn for_module( |
| 609 |
behavior: Behavior<'a>, |
| 610 |
module: &'a mut crate::Module, |
| 611 |
global_expression_kind_tracker: &'a mut ExpressionKindTracker, |
| 612 |
layouter: &'a mut crate::proc::Layouter, |
| 613 |
) -> Self { |
| 614 |
Self { |
| 615 |
behavior, |
| 616 |
types: &mut module.types, |
| 617 |
constants: &module.constants, |
| 618 |
overrides: &module.overrides, |
| 619 |
expressions: &mut module.global_expressions, |
| 620 |
expression_kind_tracker: global_expression_kind_tracker, |
| 621 |
layouter, |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
/// Return a [`ConstantEvaluator`] that will add expressions to `function`'s |
| 626 |
/// expression arena. |
| 627 |
/// |
| 628 |
/// Report errors according to WGSL's rules for constant evaluation. |
| 629 |
pub fn for_wgsl_function( |
| 630 |
module: &'a mut crate::Module, |
| 631 |
expressions: &'a mut Arena<Expression>, |
| 632 |
local_expression_kind_tracker: &'a mut ExpressionKindTracker, |
| 633 |
layouter: &'a mut crate::proc::Layouter, |
| 634 |
emitter: &'a mut super::Emitter, |
| 635 |
block: &'a mut crate::Block, |
| 636 |
is_const: bool, |
| 637 |
) -> Self { |
| 638 |
let local_data = FunctionLocalData { |
| 639 |
global_expressions: &module.global_expressions, |
| 640 |
emitter, |
| 641 |
block, |
| 642 |
}; |
| 643 |
Self { |
| 644 |
behavior: Behavior::Wgsl(if is_const { |
| 645 |
WgslRestrictions::Const(Some(local_data)) |
| 646 |
} else { |
| 647 |
WgslRestrictions::Runtime(local_data) |
| 648 |
}), |
| 649 |
types: &mut module.types, |
| 650 |
constants: &module.constants, |
| 651 |
overrides: &module.overrides, |
| 652 |
expressions, |
| 653 |
expression_kind_tracker: local_expression_kind_tracker, |
| 654 |
layouter, |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
/// Return a [`ConstantEvaluator`] that will add expressions to `function`'s |
| 659 |
/// expression arena. |
| 660 |
/// |
| 661 |
/// Report errors according to GLSL's rules for constant evaluation. |
| 662 |
pub fn for_glsl_function( |
| 663 |
module: &'a mut crate::Module, |
| 664 |
expressions: &'a mut Arena<Expression>, |
| 665 |
local_expression_kind_tracker: &'a mut ExpressionKindTracker, |
| 666 |
layouter: &'a mut crate::proc::Layouter, |
| 667 |
emitter: &'a mut super::Emitter, |
| 668 |
block: &'a mut crate::Block, |
| 669 |
) -> Self { |
| 670 |
Self { |
| 671 |
behavior: Behavior::Glsl(GlslRestrictions::Runtime(FunctionLocalData { |
| 672 |
global_expressions: &module.global_expressions, |
| 673 |
emitter, |
| 674 |
block, |
| 675 |
})), |
| 676 |
types: &mut module.types, |
| 677 |
constants: &module.constants, |
| 678 |
overrides: &module.overrides, |
| 679 |
expressions, |
| 680 |
expression_kind_tracker: local_expression_kind_tracker, |
| 681 |
layouter, |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
pub fn to_ctx(&self) -> crate::proc::GlobalCtx { |
| 686 |
crate::proc::GlobalCtx { |
| 687 |
types: self.types, |
| 688 |
constants: self.constants, |
| 689 |
overrides: self.overrides, |
| 690 |
global_expressions: match self.function_local_data() { |
| 691 |
Some(data) => data.global_expressions, |
| 692 |
None => self.expressions, |
| 693 |
}, |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
fn check(&self, expr: Handle<Expression>) -> Result<(), ConstantEvaluatorError> { |
| 698 |
if !self.expression_kind_tracker.is_const(expr) { |
| 699 |
log::debug!("check: SubexpressionsAreNotConstant"); |
| 700 |
return Err(ConstantEvaluatorError::SubexpressionsAreNotConstant); |
| 701 |
} |
| 702 |
Ok(()) |
| 703 |
} |
| 704 |
|
| 705 |
fn check_and_get( |
| 706 |
&mut self, |
| 707 |
expr: Handle<Expression>, |
| 708 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 709 |
match self.expressions[expr] { |
| 710 |
Expression::Constant(c) => { |
| 711 |
// Are we working in a function's expression arena, or the |
| 712 |
// module's constant expression arena? |
| 713 |
if let Some(function_local_data) = self.function_local_data() { |
| 714 |
// Deep-copy the constant's value into our arena. |
| 715 |
self.copy_from( |
| 716 |
self.constants[c].init, |
| 717 |
function_local_data.global_expressions, |
| 718 |
) |
| 719 |
} else { |
| 720 |
// "See through" the constant and use its initializer. |
| 721 |
Ok(self.constants[c].init) |
| 722 |
} |
| 723 |
} |
| 724 |
_ => { |
| 725 |
self.check(expr)?; |
| 726 |
Ok(expr) |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
/// Try to evaluate `expr` at compile time. |
| 732 |
/// |
| 733 |
/// The `expr` argument can be any sort of Naga [`Expression`] you like. If |
| 734 |
/// we can determine its value at compile time, we append an expression |
| 735 |
/// representing its value - a tree of [`Literal`], [`Compose`], |
| 736 |
/// [`ZeroValue`], and [`Swizzle`] expressions - to the expression arena |
| 737 |
/// `self` contributes to. |
| 738 |
/// |
| 739 |
/// If `expr`'s value cannot be determined at compile time, and `self` is |
| 740 |
/// contributing to some function's expression arena, then append `expr` to |
| 741 |
/// that arena unchanged (and thus unevaluated). Otherwise, `self` must be |
| 742 |
/// contributing to the module's constant expression arena; since `expr`'s |
| 743 |
/// value is not a constant, return an error. |
| 744 |
/// |
| 745 |
/// We only consider `expr` itself, without recursing into its operands. Its |
| 746 |
/// operands must all have been produced by prior calls to |
| 747 |
/// `try_eval_and_append`, to ensure that they have already been reduced to |
| 748 |
/// an evaluated form if possible. |
| 749 |
/// |
| 750 |
/// [`Literal`]: Expression::Literal |
| 751 |
/// [`Compose`]: Expression::Compose |
| 752 |
/// [`ZeroValue`]: Expression::ZeroValue |
| 753 |
/// [`Swizzle`]: Expression::Swizzle |
| 754 |
pub fn try_eval_and_append( |
| 755 |
&mut self, |
| 756 |
expr: Expression, |
| 757 |
span: Span, |
| 758 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 759 |
match self.expression_kind_tracker.type_of_with_expr(&expr) { |
| 760 |
ExpressionKind::Const => { |
| 761 |
let eval_result = self.try_eval_and_append_impl(&expr, span); |
| 762 |
// We should be able to evaluate `Const` expressions at this |
| 763 |
// point. If we failed to, then that probably means we just |
| 764 |
// haven't implemented that part of constant evaluation. Work |
| 765 |
// around this by simply emitting it as a run-time expression. |
| 766 |
if self.behavior.has_runtime_restrictions() |
| 767 |
&& matches!( |
| 768 |
eval_result, |
| 769 |
Err(ConstantEvaluatorError::NotImplemented(_) |
| 770 |
| ConstantEvaluatorError::InvalidBinaryOpArgs,) |
| 771 |
) |
| 772 |
{ |
| 773 |
Ok(self.append_expr(expr, span, ExpressionKind::Runtime)) |
| 774 |
} else { |
| 775 |
eval_result |
| 776 |
} |
| 777 |
} |
| 778 |
ExpressionKind::Override => match self.behavior { |
| 779 |
Behavior::Wgsl(WgslRestrictions::Override | WgslRestrictions::Runtime(_)) => { |
| 780 |
Ok(self.append_expr(expr, span, ExpressionKind::Override)) |
| 781 |
} |
| 782 |
Behavior::Wgsl(WgslRestrictions::Const(_)) => { |
| 783 |
Err(ConstantEvaluatorError::OverrideExpr) |
| 784 |
} |
| 785 |
Behavior::Glsl(_) => { |
| 786 |
unreachable!() |
| 787 |
} |
| 788 |
}, |
| 789 |
ExpressionKind::Runtime => { |
| 790 |
if self.behavior.has_runtime_restrictions() { |
| 791 |
Ok(self.append_expr(expr, span, ExpressionKind::Runtime)) |
| 792 |
} else { |
| 793 |
Err(ConstantEvaluatorError::RuntimeExpr) |
| 794 |
} |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
/// Is the [`Self::expressions`] arena the global module expression arena? |
| 800 |
const fn is_global_arena(&self) -> bool { |
| 801 |
matches!( |
| 802 |
self.behavior, |
| 803 |
Behavior::Wgsl(WgslRestrictions::Const(None) | WgslRestrictions::Override) |
| 804 |
| Behavior::Glsl(GlslRestrictions::Const) |
| 805 |
) |
| 806 |
} |
| 807 |
|
| 808 |
const fn function_local_data(&self) -> Option<&FunctionLocalData<'a>> { |
| 809 |
match self.behavior { |
| 810 |
Behavior::Wgsl( |
| 811 |
WgslRestrictions::Runtime(ref function_local_data) |
| 812 |
| WgslRestrictions::Const(Some(ref function_local_data)), |
| 813 |
) |
| 814 |
| Behavior::Glsl(GlslRestrictions::Runtime(ref function_local_data)) => { |
| 815 |
Some(function_local_data) |
| 816 |
} |
| 817 |
_ => None, |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
fn try_eval_and_append_impl( |
| 822 |
&mut self, |
| 823 |
expr: &Expression, |
| 824 |
span: Span, |
| 825 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 826 |
log::trace!("try_eval_and_append: {:?}", expr); |
| 827 |
match *expr { |
| 828 |
Expression::Constant(c) if self.is_global_arena() => { |
| 829 |
// "See through" the constant and use its initializer. |
| 830 |
// This is mainly done to avoid having constants pointing to other constants. |
| 831 |
Ok(self.constants[c].init) |
| 832 |
} |
| 833 |
Expression::Override(_) => Err(ConstantEvaluatorError::Override), |
| 834 |
Expression::Literal(_) | Expression::ZeroValue(_) | Expression::Constant(_) => { |
| 835 |
self.register_evaluated_expr(expr.clone(), span) |
| 836 |
} |
| 837 |
Expression::Compose { ty, ref components } => { |
| 838 |
let components = components |
| 839 |
.iter() |
| 840 |
.map(|component| self.check_and_get(*component)) |
| 841 |
.collect::<Result<Vec<_>, _>>()?; |
| 842 |
self.register_evaluated_expr(Expression::Compose { ty, components }, span) |
| 843 |
} |
| 844 |
Expression::Splat { size, value } => { |
| 845 |
let value = self.check_and_get(value)?; |
| 846 |
self.register_evaluated_expr(Expression::Splat { size, value }, span) |
| 847 |
} |
| 848 |
Expression::AccessIndex { base, index } => { |
| 849 |
let base = self.check_and_get(base)?; |
| 850 |
|
| 851 |
self.access(base, index as usize, span) |
| 852 |
} |
| 853 |
Expression::Access { base, index } => { |
| 854 |
let base = self.check_and_get(base)?; |
| 855 |
let index = self.check_and_get(index)?; |
| 856 |
|
| 857 |
self.access(base, self.constant_index(index)?, span) |
| 858 |
} |
| 859 |
Expression::Swizzle { |
| 860 |
size, |
| 861 |
vector, |
| 862 |
pattern, |
| 863 |
} => { |
| 864 |
let vector = self.check_and_get(vector)?; |
| 865 |
|
| 866 |
self.swizzle(size, span, vector, pattern) |
| 867 |
} |
| 868 |
Expression::Unary { expr, op } => { |
| 869 |
let expr = self.check_and_get(expr)?; |
| 870 |
|
| 871 |
self.unary_op(op, expr, span) |
| 872 |
} |
| 873 |
Expression::Binary { left, right, op } => { |
| 874 |
let left = self.check_and_get(left)?; |
| 875 |
let right = self.check_and_get(right)?; |
| 876 |
|
| 877 |
self.binary_op(op, left, right, span) |
| 878 |
} |
| 879 |
Expression::Math { |
| 880 |
fun, |
| 881 |
arg, |
| 882 |
arg1, |
| 883 |
arg2, |
| 884 |
arg3, |
| 885 |
} => { |
| 886 |
let arg = self.check_and_get(arg)?; |
| 887 |
let arg1 = arg1.map(|arg| self.check_and_get(arg)).transpose()?; |
| 888 |
let arg2 = arg2.map(|arg| self.check_and_get(arg)).transpose()?; |
| 889 |
let arg3 = arg3.map(|arg| self.check_and_get(arg)).transpose()?; |
| 890 |
|
| 891 |
self.math(arg, arg1, arg2, arg3, fun, span) |
| 892 |
} |
| 893 |
Expression::As { |
| 894 |
convert, |
| 895 |
expr, |
| 896 |
kind, |
| 897 |
} => { |
| 898 |
let expr = self.check_and_get(expr)?; |
| 899 |
|
| 900 |
match convert { |
| 901 |
Some(width) => self.cast(expr, crate::Scalar { kind, width }, span), |
| 902 |
None => Err(ConstantEvaluatorError::NotImplemented( |
| 903 |
"bitcast built-in function".into(), |
| 904 |
)), |
| 905 |
} |
| 906 |
} |
| 907 |
Expression::Select { .. } => Err(ConstantEvaluatorError::NotImplemented( |
| 908 |
"select built-in function".into(), |
| 909 |
)), |
| 910 |
Expression::Relational { fun, argument } => { |
| 911 |
let argument = self.check_and_get(argument)?; |
| 912 |
self.relational(fun, argument, span) |
| 913 |
} |
| 914 |
Expression::ArrayLength(expr) => match self.behavior { |
| 915 |
Behavior::Wgsl(_) => Err(ConstantEvaluatorError::ArrayLength), |
| 916 |
Behavior::Glsl(_) => { |
| 917 |
let expr = self.check_and_get(expr)?; |
| 918 |
self.array_length(expr, span) |
| 919 |
} |
| 920 |
}, |
| 921 |
Expression::Load { .. } => Err(ConstantEvaluatorError::Load), |
| 922 |
Expression::LocalVariable(_) => Err(ConstantEvaluatorError::LocalVariable), |
| 923 |
Expression::Derivative { .. } => Err(ConstantEvaluatorError::Derivative), |
| 924 |
Expression::CallResult { .. } => Err(ConstantEvaluatorError::Call), |
| 925 |
Expression::WorkGroupUniformLoadResult { .. } => { |
| 926 |
Err(ConstantEvaluatorError::WorkGroupUniformLoadResult) |
| 927 |
} |
| 928 |
Expression::AtomicResult { .. } => Err(ConstantEvaluatorError::Atomic), |
| 929 |
Expression::FunctionArgument(_) => Err(ConstantEvaluatorError::FunctionArg), |
| 930 |
Expression::GlobalVariable(_) => Err(ConstantEvaluatorError::GlobalVariable), |
| 931 |
Expression::ImageSample { .. } |
| 932 |
| Expression::ImageLoad { .. } |
| 933 |
| Expression::ImageQuery { .. } => Err(ConstantEvaluatorError::ImageExpression), |
| 934 |
Expression::RayQueryProceedResult |
| 935 |
| Expression::RayQueryGetIntersection { .. } |
| 936 |
| Expression::RayQueryVertexPositions { .. } => { |
| 937 |
Err(ConstantEvaluatorError::RayQueryExpression) |
| 938 |
} |
| 939 |
Expression::SubgroupBallotResult => Err(ConstantEvaluatorError::SubgroupExpression), |
| 940 |
Expression::SubgroupOperationResult { .. } => { |
| 941 |
Err(ConstantEvaluatorError::SubgroupExpression) |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
/// Splat `value` to `size`, without using [`Splat`] expressions. |
| 947 |
/// |
| 948 |
/// This constructs [`Compose`] or [`ZeroValue`] expressions to |
| 949 |
/// build a vector with the given `size` whose components are all |
| 950 |
/// `value`. |
| 951 |
/// |
| 952 |
/// Use `span` as the span of the inserted expressions and |
| 953 |
/// resulting types. |
| 954 |
/// |
| 955 |
/// [`Splat`]: Expression::Splat |
| 956 |
/// [`Compose`]: Expression::Compose |
| 957 |
/// [`ZeroValue`]: Expression::ZeroValue |
| 958 |
fn splat( |
| 959 |
&mut self, |
| 960 |
value: Handle<Expression>, |
| 961 |
size: crate::VectorSize, |
| 962 |
span: Span, |
| 963 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 964 |
match self.expressions[value] { |
| 965 |
Expression::Literal(literal) => { |
| 966 |
let scalar = literal.scalar(); |
| 967 |
let ty = self.types.insert( |
| 968 |
Type { |
| 969 |
name: None, |
| 970 |
inner: TypeInner::Vector { size, scalar }, |
| 971 |
}, |
| 972 |
span, |
| 973 |
); |
| 974 |
let expr = Expression::Compose { |
| 975 |
ty, |
| 976 |
components: vec![value; size as usize], |
| 977 |
}; |
| 978 |
self.register_evaluated_expr(expr, span) |
| 979 |
} |
| 980 |
Expression::ZeroValue(ty) => { |
| 981 |
let inner = match self.types[ty].inner { |
| 982 |
TypeInner::Scalar(scalar) => TypeInner::Vector { size, scalar }, |
| 983 |
_ => return Err(ConstantEvaluatorError::SplatScalarOnly), |
| 984 |
}; |
| 985 |
let res_ty = self.types.insert(Type { name: None, inner }, span); |
| 986 |
let expr = Expression::ZeroValue(res_ty); |
| 987 |
self.register_evaluated_expr(expr, span) |
| 988 |
} |
| 989 |
_ => Err(ConstantEvaluatorError::SplatScalarOnly), |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
fn swizzle( |
| 994 |
&mut self, |
| 995 |
size: crate::VectorSize, |
| 996 |
span: Span, |
| 997 |
src_constant: Handle<Expression>, |
| 998 |
pattern: [crate::SwizzleComponent; 4], |
| 999 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1000 |
let mut get_dst_ty = |ty| match self.types[ty].inner { |
| 1001 |
TypeInner::Vector { size: _, scalar } => Ok(self.types.insert( |
| 1002 |
Type { |
| 1003 |
name: None, |
| 1004 |
inner: TypeInner::Vector { size, scalar }, |
| 1005 |
}, |
| 1006 |
span, |
| 1007 |
)), |
| 1008 |
_ => Err(ConstantEvaluatorError::SwizzleVectorOnly), |
| 1009 |
}; |
| 1010 |
|
| 1011 |
match self.expressions[src_constant] { |
| 1012 |
Expression::ZeroValue(ty) => { |
| 1013 |
let dst_ty = get_dst_ty(ty)?; |
| 1014 |
let expr = Expression::ZeroValue(dst_ty); |
| 1015 |
self.register_evaluated_expr(expr, span) |
| 1016 |
} |
| 1017 |
Expression::Splat { value, .. } => { |
| 1018 |
let expr = Expression::Splat { size, value }; |
| 1019 |
self.register_evaluated_expr(expr, span) |
| 1020 |
} |
| 1021 |
Expression::Compose { ty, ref components } => { |
| 1022 |
let dst_ty = get_dst_ty(ty)?; |
| 1023 |
|
| 1024 |
let mut flattened = [src_constant; 4]; // dummy value |
| 1025 |
let len = |
| 1026 |
crate::proc::flatten_compose(ty, components, self.expressions, self.types) |
| 1027 |
.zip(flattened.iter_mut()) |
| 1028 |
.map(|(component, elt)| *elt = component) |
| 1029 |
.count(); |
| 1030 |
let flattened = &flattened[..len]; |
| 1031 |
|
| 1032 |
let swizzled_components = pattern[..size as usize] |
| 1033 |
.iter() |
| 1034 |
.map(|&sc| { |
| 1035 |
let sc = sc as usize; |
| 1036 |
if let Some(elt) = flattened.get(sc) { |
| 1037 |
Ok(*elt) |
| 1038 |
} else { |
| 1039 |
Err(ConstantEvaluatorError::SwizzleOutOfBounds) |
| 1040 |
} |
| 1041 |
}) |
| 1042 |
.collect::<Result<Vec<Handle<Expression>>, _>>()?; |
| 1043 |
let expr = Expression::Compose { |
| 1044 |
ty: dst_ty, |
| 1045 |
components: swizzled_components, |
| 1046 |
}; |
| 1047 |
self.register_evaluated_expr(expr, span) |
| 1048 |
} |
| 1049 |
_ => Err(ConstantEvaluatorError::SwizzleVectorOnly), |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
fn math( |
| 1054 |
&mut self, |
| 1055 |
arg: Handle<Expression>, |
| 1056 |
arg1: Option<Handle<Expression>>, |
| 1057 |
arg2: Option<Handle<Expression>>, |
| 1058 |
arg3: Option<Handle<Expression>>, |
| 1059 |
fun: crate::MathFunction, |
| 1060 |
span: Span, |
| 1061 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1062 |
let expected = fun.argument_count(); |
| 1063 |
let given = Some(arg) |
| 1064 |
.into_iter() |
| 1065 |
.chain(arg1) |
| 1066 |
.chain(arg2) |
| 1067 |
.chain(arg3) |
| 1068 |
.count(); |
| 1069 |
if expected != given { |
| 1070 |
return Err(ConstantEvaluatorError::InvalidMathArgCount( |
| 1071 |
fun, expected, given, |
| 1072 |
)); |
| 1073 |
} |
| 1074 |
|
| 1075 |
// NOTE: We try to match the declaration order of `MathFunction` here. |
| 1076 |
match fun { |
| 1077 |
// comparison |
| 1078 |
crate::MathFunction::Abs => { |
| 1079 |
component_wise_scalar(self, span, [arg], |args| match args { |
| 1080 |
Scalar::AbstractFloat([e]) => Ok(Scalar::AbstractFloat([e.abs()])), |
| 1081 |
Scalar::F32([e]) => Ok(Scalar::F32([e.abs()])), |
| 1082 |
Scalar::F16([e]) => Ok(Scalar::F16([e.abs()])), |
| 1083 |
Scalar::AbstractInt([e]) => Ok(Scalar::AbstractInt([e.wrapping_abs()])), |
| 1084 |
Scalar::I32([e]) => Ok(Scalar::I32([e.wrapping_abs()])), |
| 1085 |
Scalar::U32([e]) => Ok(Scalar::U32([e])), // TODO: just re-use the expression, ezpz |
| 1086 |
Scalar::I64([e]) => Ok(Scalar::I64([e.wrapping_abs()])), |
| 1087 |
Scalar::U64([e]) => Ok(Scalar::U64([e])), |
| 1088 |
}) |
| 1089 |
} |
| 1090 |
crate::MathFunction::Min => { |
| 1091 |
component_wise_scalar!(self, span, [arg, arg1.unwrap()], |e1, e2| { |
| 1092 |
Ok([e1.min(e2)]) |
| 1093 |
}) |
| 1094 |
} |
| 1095 |
crate::MathFunction::Max => { |
| 1096 |
component_wise_scalar!(self, span, [arg, arg1.unwrap()], |e1, e2| { |
| 1097 |
Ok([e1.max(e2)]) |
| 1098 |
}) |
| 1099 |
} |
| 1100 |
crate::MathFunction::Clamp => { |
| 1101 |
component_wise_scalar!( |
| 1102 |
self, |
| 1103 |
span, |
| 1104 |
[arg, arg1.unwrap(), arg2.unwrap()], |
| 1105 |
|e, low, high| { |
| 1106 |
if low > high { |
| 1107 |
Err(ConstantEvaluatorError::InvalidClamp) |
| 1108 |
} else { |
| 1109 |
Ok([e.clamp(low, high)]) |
| 1110 |
} |
| 1111 |
} |
| 1112 |
) |
| 1113 |
} |
| 1114 |
crate::MathFunction::Saturate => component_wise_float(self, span, [arg], |e| match e { |
| 1115 |
Float::F16([e]) => Ok(Float::F16( |
| 1116 |
[e.clamp(f16::from_f32(0.0), f16::from_f32(1.0))], |
| 1117 |
)), |
| 1118 |
Float::F32([e]) => Ok(Float::F32([e.clamp(0., 1.)])), |
| 1119 |
Float::Abstract([e]) => Ok(Float::Abstract([e.clamp(0., 1.)])), |
| 1120 |
}), |
| 1121 |
|
| 1122 |
// trigonometry |
| 1123 |
crate::MathFunction::Cos => { |
| 1124 |
component_wise_float!(self, span, [arg], |e| { Ok([e.cos()]) }) |
| 1125 |
} |
| 1126 |
crate::MathFunction::Cosh => { |
| 1127 |
component_wise_float!(self, span, [arg], |e| { Ok([e.cosh()]) }) |
| 1128 |
} |
| 1129 |
crate::MathFunction::Sin => { |
| 1130 |
component_wise_float!(self, span, [arg], |e| { Ok([e.sin()]) }) |
| 1131 |
} |
| 1132 |
crate::MathFunction::Sinh => { |
| 1133 |
component_wise_float!(self, span, [arg], |e| { Ok([e.sinh()]) }) |
| 1134 |
} |
| 1135 |
crate::MathFunction::Tan => { |
| 1136 |
component_wise_float!(self, span, [arg], |e| { Ok([e.tan()]) }) |
| 1137 |
} |
| 1138 |
crate::MathFunction::Tanh => { |
| 1139 |
component_wise_float!(self, span, [arg], |e| { Ok([e.tanh()]) }) |
| 1140 |
} |
| 1141 |
crate::MathFunction::Acos => { |
| 1142 |
component_wise_float!(self, span, [arg], |e| { Ok([e.acos()]) }) |
| 1143 |
} |
| 1144 |
crate::MathFunction::Asin => { |
| 1145 |
component_wise_float!(self, span, [arg], |e| { Ok([e.asin()]) }) |
| 1146 |
} |
| 1147 |
crate::MathFunction::Atan => { |
| 1148 |
component_wise_float!(self, span, [arg], |e| { Ok([e.atan()]) }) |
| 1149 |
} |
| 1150 |
crate::MathFunction::Asinh => { |
| 1151 |
component_wise_float!(self, span, [arg], |e| { Ok([e.asinh()]) }) |
| 1152 |
} |
| 1153 |
crate::MathFunction::Acosh => { |
| 1154 |
component_wise_float!(self, span, [arg], |e| { Ok([e.acosh()]) }) |
| 1155 |
} |
| 1156 |
crate::MathFunction::Atanh => { |
| 1157 |
component_wise_float!(self, span, [arg], |e| { Ok([e.atanh()]) }) |
| 1158 |
} |
| 1159 |
crate::MathFunction::Radians => { |
| 1160 |
component_wise_float!(self, span, [arg], |e1| { Ok([e1.to_radians()]) }) |
| 1161 |
} |
| 1162 |
crate::MathFunction::Degrees => { |
| 1163 |
component_wise_float!(self, span, [arg], |e| { Ok([e.to_degrees()]) }) |
| 1164 |
} |
| 1165 |
|
| 1166 |
// decomposition |
| 1167 |
crate::MathFunction::Ceil => { |
| 1168 |
component_wise_float!(self, span, [arg], |e| { Ok([e.ceil()]) }) |
| 1169 |
} |
| 1170 |
crate::MathFunction::Floor => { |
| 1171 |
component_wise_float!(self, span, [arg], |e| { Ok([e.floor()]) }) |
| 1172 |
} |
| 1173 |
crate::MathFunction::Round => { |
| 1174 |
component_wise_float(self, span, [arg], |e| match e { |
| 1175 |
Float::Abstract([e]) => Ok(Float::Abstract([libm::rint(e)])), |
| 1176 |
Float::F32([e]) => Ok(Float::F32([libm::rintf(e)])), |
| 1177 |
Float::F16([e]) => { |
| 1178 |
// TODO: `round_ties_even` is not available on `half::f16` yet. |
| 1179 |
// |
| 1180 |
// This polyfill is shamelessly [~~stolen from~~ inspired by `ndarray-image`][polyfill source], |
| 1181 |
// which has licensing compatible with ours. See also |
| 1182 |
// <https://github.com/rust-lang/rust/issues/96710>. |
| 1183 |
// |
| 1184 |
// [polyfill source]: https://github.com/imeka/ndarray-ndimage/blob/8b14b4d6ecfbc96a8a052f802e342a7049c68d8f/src/lib.rs#L98 |
| 1185 |
fn round_ties_even(x: f64) -> f64 { |
| 1186 |
let i = x as i64; |
| 1187 |
let f = (x - i as f64).abs(); |
| 1188 |
if f == 0.5 { |
| 1189 |
if i & 1 == 1 { |
| 1190 |
// -1.5, 1.5, 3.5, ... |
| 1191 |
(x.abs() + 0.5).copysign(x) |
| 1192 |
} else { |
| 1193 |
(x.abs() - 0.5).copysign(x) |
| 1194 |
} |
| 1195 |
} else { |
| 1196 |
x.round() |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
Ok(Float::F16([(f16::from_f64(round_ties_even(f64::from(e))))])) |
| 1201 |
} |
| 1202 |
}) |
| 1203 |
} |
| 1204 |
crate::MathFunction::Fract => { |
| 1205 |
component_wise_float!(self, span, [arg], |e| { |
| 1206 |
// N.B., Rust's definition of `fract` is `e - e.trunc()`, so we can't use that |
| 1207 |
// here. |
| 1208 |
Ok([e - e.floor()]) |
| 1209 |
}) |
| 1210 |
} |
| 1211 |
crate::MathFunction::Trunc => { |
| 1212 |
component_wise_float!(self, span, [arg], |e| { Ok([e.trunc()]) }) |
| 1213 |
} |
| 1214 |
|
| 1215 |
// exponent |
| 1216 |
crate::MathFunction::Exp => { |
| 1217 |
component_wise_float!(self, span, [arg], |e| { Ok([e.exp()]) }) |
| 1218 |
} |
| 1219 |
crate::MathFunction::Exp2 => { |
| 1220 |
component_wise_float!(self, span, [arg], |e| { Ok([e.exp2()]) }) |
| 1221 |
} |
| 1222 |
crate::MathFunction::Log => { |
| 1223 |
component_wise_float!(self, span, [arg], |e| { Ok([e.ln()]) }) |
| 1224 |
} |
| 1225 |
crate::MathFunction::Log2 => { |
| 1226 |
component_wise_float!(self, span, [arg], |e| { Ok([e.log2()]) }) |
| 1227 |
} |
| 1228 |
crate::MathFunction::Pow => { |
| 1229 |
component_wise_float!(self, span, [arg, arg1.unwrap()], |e1, e2| { |
| 1230 |
Ok([e1.powf(e2)]) |
| 1231 |
}) |
| 1232 |
} |
| 1233 |
|
| 1234 |
// computational |
| 1235 |
crate::MathFunction::Sign => { |
| 1236 |
component_wise_signed!(self, span, [arg], |e| { Ok([e.signum()]) }) |
| 1237 |
} |
| 1238 |
crate::MathFunction::Fma => { |
| 1239 |
component_wise_float!( |
| 1240 |
self, |
| 1241 |
span, |
| 1242 |
[arg, arg1.unwrap(), arg2.unwrap()], |
| 1243 |
|e1, e2, e3| { Ok([e1.mul_add(e2, e3)]) } |
| 1244 |
) |
| 1245 |
} |
| 1246 |
crate::MathFunction::Step => { |
| 1247 |
component_wise_float(self, span, [arg, arg1.unwrap()], |x| match x { |
| 1248 |
Float::Abstract([edge, x]) => { |
| 1249 |
Ok(Float::Abstract([if edge <= x { 1.0 } else { 0.0 }])) |
| 1250 |
} |
| 1251 |
Float::F32([edge, x]) => Ok(Float::F32([if edge <= x { 1.0 } else { 0.0 }])), |
| 1252 |
Float::F16([edge, x]) => Ok(Float::F16([if edge <= x { |
| 1253 |
f16::one() |
| 1254 |
} else { |
| 1255 |
f16::zero() |
| 1256 |
}])), |
| 1257 |
}) |
| 1258 |
} |
| 1259 |
crate::MathFunction::Sqrt => { |
| 1260 |
component_wise_float!(self, span, [arg], |e| { Ok([e.sqrt()]) }) |
| 1261 |
} |
| 1262 |
crate::MathFunction::InverseSqrt => { |
| 1263 |
component_wise_float(self, span, [arg], |e| match e { |
| 1264 |
Float::Abstract([e]) => Ok(Float::Abstract([1. / e.sqrt()])), |
| 1265 |
Float::F32([e]) => Ok(Float::F32([1. / e.sqrt()])), |
| 1266 |
Float::F16([e]) => Ok(Float::F16([f16::from_f32(1. / f32::from(e).sqrt())])), |
| 1267 |
}) |
| 1268 |
} |
| 1269 |
|
| 1270 |
// bits |
| 1271 |
crate::MathFunction::CountTrailingZeros => { |
| 1272 |
component_wise_concrete_int!(self, span, [arg], |e| { |
| 1273 |
#[allow(clippy::useless_conversion)] |
| 1274 |
Ok([e |
| 1275 |
.trailing_zeros() |
| 1276 |
.try_into() |
| 1277 |
.expect("bit count overflowed 32 bits, somehow!?")]) |
| 1278 |
}) |
| 1279 |
} |
| 1280 |
crate::MathFunction::CountLeadingZeros => { |
| 1281 |
component_wise_concrete_int!(self, span, [arg], |e| { |
| 1282 |
#[allow(clippy::useless_conversion)] |
| 1283 |
Ok([e |
| 1284 |
.leading_zeros() |
| 1285 |
.try_into() |
| 1286 |
.expect("bit count overflowed 32 bits, somehow!?")]) |
| 1287 |
}) |
| 1288 |
} |
| 1289 |
crate::MathFunction::CountOneBits => { |
| 1290 |
component_wise_concrete_int!(self, span, [arg], |e| { |
| 1291 |
#[allow(clippy::useless_conversion)] |
| 1292 |
Ok([e |
| 1293 |
.count_ones() |
| 1294 |
.try_into() |
| 1295 |
.expect("bit count overflowed 32 bits, somehow!?")]) |
| 1296 |
}) |
| 1297 |
} |
| 1298 |
crate::MathFunction::ReverseBits => { |
| 1299 |
component_wise_concrete_int!(self, span, [arg], |e| { Ok([e.reverse_bits()]) }) |
| 1300 |
} |
| 1301 |
crate::MathFunction::FirstTrailingBit => { |
| 1302 |
component_wise_concrete_int(self, span, [arg], |ci| Ok(first_trailing_bit(ci))) |
| 1303 |
} |
| 1304 |
crate::MathFunction::FirstLeadingBit => { |
| 1305 |
component_wise_concrete_int(self, span, [arg], |ci| Ok(first_leading_bit(ci))) |
| 1306 |
} |
| 1307 |
|
| 1308 |
// vector |
| 1309 |
crate::MathFunction::Dot4I8Packed => { |
| 1310 |
self.packed_dot_product(arg, arg1.unwrap(), span, true) |
| 1311 |
} |
| 1312 |
crate::MathFunction::Dot4U8Packed => { |
| 1313 |
self.packed_dot_product(arg, arg1.unwrap(), span, false) |
| 1314 |
} |
| 1315 |
crate::MathFunction::Cross => self.cross_product(arg, arg1.unwrap(), span), |
| 1316 |
|
| 1317 |
// unimplemented |
| 1318 |
crate::MathFunction::Atan2 |
| 1319 |
| crate::MathFunction::Modf |
| 1320 |
| crate::MathFunction::Frexp |
| 1321 |
| crate::MathFunction::Ldexp |
| 1322 |
| crate::MathFunction::Dot |
| 1323 |
| crate::MathFunction::Outer |
| 1324 |
| crate::MathFunction::Distance |
| 1325 |
| crate::MathFunction::Length |
| 1326 |
| crate::MathFunction::Normalize |
| 1327 |
| crate::MathFunction::FaceForward |
| 1328 |
| crate::MathFunction::Reflect |
| 1329 |
| crate::MathFunction::Refract |
| 1330 |
| crate::MathFunction::Mix |
| 1331 |
| crate::MathFunction::SmoothStep |
| 1332 |
| crate::MathFunction::Inverse |
| 1333 |
| crate::MathFunction::Transpose |
| 1334 |
| crate::MathFunction::Determinant |
| 1335 |
| crate::MathFunction::QuantizeToF16 |
| 1336 |
| crate::MathFunction::ExtractBits |
| 1337 |
| crate::MathFunction::InsertBits |
| 1338 |
| crate::MathFunction::Pack4x8snorm |
| 1339 |
| crate::MathFunction::Pack4x8unorm |
| 1340 |
| crate::MathFunction::Pack2x16snorm |
| 1341 |
| crate::MathFunction::Pack2x16unorm |
| 1342 |
| crate::MathFunction::Pack2x16float |
| 1343 |
| crate::MathFunction::Pack4xI8 |
| 1344 |
| crate::MathFunction::Pack4xU8 |
| 1345 |
| crate::MathFunction::Pack4xI8Clamp |
| 1346 |
| crate::MathFunction::Pack4xU8Clamp |
| 1347 |
| crate::MathFunction::Unpack4x8snorm |
| 1348 |
| crate::MathFunction::Unpack4x8unorm |
| 1349 |
| crate::MathFunction::Unpack2x16snorm |
| 1350 |
| crate::MathFunction::Unpack2x16unorm |
| 1351 |
| crate::MathFunction::Unpack2x16float |
| 1352 |
| crate::MathFunction::Unpack4xI8 |
| 1353 |
| crate::MathFunction::Unpack4xU8 => Err(ConstantEvaluatorError::NotImplemented( |
| 1354 |
format!("{fun:?} built-in function"), |
| 1355 |
)), |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
/// Dot product of two packed vectors (`dot4I8Packed` and `dot4U8Packed`) |
| 1360 |
fn packed_dot_product( |
| 1361 |
&mut self, |
| 1362 |
a: Handle<Expression>, |
| 1363 |
b: Handle<Expression>, |
| 1364 |
span: Span, |
| 1365 |
signed: bool, |
| 1366 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1367 |
let Expression::Literal(Literal::U32(a)) = self.expressions[a] else { |
| 1368 |
return Err(ConstantEvaluatorError::InvalidMathArg); |
| 1369 |
}; |
| 1370 |
let Expression::Literal(Literal::U32(b)) = self.expressions[b] else { |
| 1371 |
return Err(ConstantEvaluatorError::InvalidMathArg); |
| 1372 |
}; |
| 1373 |
|
| 1374 |
let result = if signed { |
| 1375 |
Literal::I32( |
| 1376 |
(a & 0xFF) as i8 as i32 * (b & 0xFF) as i8 as i32 |
| 1377 |
+ ((a >> 8) & 0xFF) as i8 as i32 * ((b >> 8) & 0xFF) as i8 as i32 |
| 1378 |
+ ((a >> 16) & 0xFF) as i8 as i32 * ((b >> 16) & 0xFF) as i8 as i32 |
| 1379 |
+ ((a >> 24) & 0xFF) as i8 as i32 * ((b >> 24) & 0xFF) as i8 as i32, |
| 1380 |
) |
| 1381 |
} else { |
| 1382 |
Literal::U32( |
| 1383 |
(a & 0xFF) * (b & 0xFF) |
| 1384 |
+ ((a >> 8) & 0xFF) * ((b >> 8) & 0xFF) |
| 1385 |
+ ((a >> 16) & 0xFF) * ((b >> 16) & 0xFF) |
| 1386 |
+ ((a >> 24) & 0xFF) * ((b >> 24) & 0xFF), |
| 1387 |
) |
| 1388 |
}; |
| 1389 |
|
| 1390 |
self.register_evaluated_expr(Expression::Literal(result), span) |
| 1391 |
} |
| 1392 |
|
| 1393 |
/// Vector cross product. |
| 1394 |
fn cross_product( |
| 1395 |
&mut self, |
| 1396 |
a: Handle<Expression>, |
| 1397 |
b: Handle<Expression>, |
| 1398 |
span: Span, |
| 1399 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1400 |
use Literal as Li; |
| 1401 |
|
| 1402 |
let (a, ty) = self.extract_vec::<3>(a)?; |
| 1403 |
let (b, _) = self.extract_vec::<3>(b)?; |
| 1404 |
|
| 1405 |
let product = match (a, b) { |
| 1406 |
( |
| 1407 |
[Li::AbstractInt(a0), Li::AbstractInt(a1), Li::AbstractInt(a2)], |
| 1408 |
[Li::AbstractInt(b0), Li::AbstractInt(b1), Li::AbstractInt(b2)], |
| 1409 |
) => { |
| 1410 |
// `cross` has no overload for AbstractInt, so AbstractInt |
| 1411 |
// arguments are automatically converted to AbstractFloat. Since |
| 1412 |
// `f64` has a much wider range than `i64`, there's no danger of |
| 1413 |
// overflow here. |
| 1414 |
let p = cross_product( |
| 1415 |
[a0 as f64, a1 as f64, a2 as f64], |
| 1416 |
[b0 as f64, b1 as f64, b2 as f64], |
| 1417 |
); |
| 1418 |
[ |
| 1419 |
Li::AbstractFloat(p[0]), |
| 1420 |
Li::AbstractFloat(p[1]), |
| 1421 |
Li::AbstractFloat(p[2]), |
| 1422 |
] |
| 1423 |
} |
| 1424 |
( |
| 1425 |
[Li::AbstractFloat(a0), Li::AbstractFloat(a1), Li::AbstractFloat(a2)], |
| 1426 |
[Li::AbstractFloat(b0), Li::AbstractFloat(b1), Li::AbstractFloat(b2)], |
| 1427 |
) => { |
| 1428 |
let p = cross_product([a0, a1, a2], [b0, b1, b2]); |
| 1429 |
[ |
| 1430 |
Li::AbstractFloat(p[0]), |
| 1431 |
Li::AbstractFloat(p[1]), |
| 1432 |
Li::AbstractFloat(p[2]), |
| 1433 |
] |
| 1434 |
} |
| 1435 |
([Li::F16(a0), Li::F16(a1), Li::F16(a2)], [Li::F16(b0), Li::F16(b1), Li::F16(b2)]) => { |
| 1436 |
let p = cross_product([a0, a1, a2], [b0, b1, b2]); |
| 1437 |
[Li::F16(p[0]), Li::F16(p[1]), Li::F16(p[2])] |
| 1438 |
} |
| 1439 |
([Li::F32(a0), Li::F32(a1), Li::F32(a2)], [Li::F32(b0), Li::F32(b1), Li::F32(b2)]) => { |
| 1440 |
let p = cross_product([a0, a1, a2], [b0, b1, b2]); |
| 1441 |
[Li::F32(p[0]), Li::F32(p[1]), Li::F32(p[2])] |
| 1442 |
} |
| 1443 |
([Li::F64(a0), Li::F64(a1), Li::F64(a2)], [Li::F64(b0), Li::F64(b1), Li::F64(b2)]) => { |
| 1444 |
let p = cross_product([a0, a1, a2], [b0, b1, b2]); |
| 1445 |
[Li::F64(p[0]), Li::F64(p[1]), Li::F64(p[2])] |
| 1446 |
} |
| 1447 |
_ => return Err(ConstantEvaluatorError::InvalidMathArg), |
| 1448 |
}; |
| 1449 |
|
| 1450 |
let p0 = self.register_evaluated_expr(Expression::Literal(product[0]), span)?; |
| 1451 |
let p1 = self.register_evaluated_expr(Expression::Literal(product[1]), span)?; |
| 1452 |
let p2 = self.register_evaluated_expr(Expression::Literal(product[2]), span)?; |
| 1453 |
|
| 1454 |
self.register_evaluated_expr( |
| 1455 |
Expression::Compose { |
| 1456 |
ty, |
| 1457 |
components: vec![p0, p1, p2], |
| 1458 |
}, |
| 1459 |
span, |
| 1460 |
) |
| 1461 |
} |
| 1462 |
|
| 1463 |
/// Extract the values of a `vecN` from `expr`. |
| 1464 |
/// |
| 1465 |
/// Return the value of `expr`, whose type is `vecN<S>` for some |
| 1466 |
/// vector size `N` and scalar `S`, as an array of `N` [`Literal`] |
| 1467 |
/// values. |
| 1468 |
/// |
| 1469 |
/// Also return the type handle from the `Compose` expression. |
| 1470 |
fn extract_vec<const N: usize>( |
| 1471 |
&mut self, |
| 1472 |
expr: Handle<Expression>, |
| 1473 |
) -> Result<([Literal; N], Handle<Type>), ConstantEvaluatorError> { |
| 1474 |
let span = self.expressions.get_span(expr); |
| 1475 |
let expr = self.eval_zero_value_and_splat(expr, span)?; |
| 1476 |
let Expression::Compose { ty, ref components } = self.expressions[expr] else { |
| 1477 |
return Err(ConstantEvaluatorError::InvalidMathArg); |
| 1478 |
}; |
| 1479 |
|
| 1480 |
let mut value = [Literal::Bool(false); N]; |
| 1481 |
for (component, elt) in |
| 1482 |
crate::proc::flatten_compose(ty, components, self.expressions, self.types) |
| 1483 |
.zip(value.iter_mut()) |
| 1484 |
{ |
| 1485 |
let Expression::Literal(literal) = self.expressions[component] else { |
| 1486 |
return Err(ConstantEvaluatorError::InvalidMathArg); |
| 1487 |
}; |
| 1488 |
*elt = literal; |
| 1489 |
} |
| 1490 |
|
| 1491 |
Ok((value, ty)) |
| 1492 |
} |
| 1493 |
|
| 1494 |
fn array_length( |
| 1495 |
&mut self, |
| 1496 |
array: Handle<Expression>, |
| 1497 |
span: Span, |
| 1498 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1499 |
match self.expressions[array] { |
| 1500 |
Expression::ZeroValue(ty) | Expression::Compose { ty, .. } => { |
| 1501 |
match self.types[ty].inner { |
| 1502 |
TypeInner::Array { size, .. } => match size { |
| 1503 |
ArraySize::Constant(len) => { |
| 1504 |
let expr = Expression::Literal(Literal::U32(len.get())); |
| 1505 |
self.register_evaluated_expr(expr, span) |
| 1506 |
} |
| 1507 |
ArraySize::Pending(_) => Err(ConstantEvaluatorError::ArrayLengthOverridden), |
| 1508 |
ArraySize::Dynamic => Err(ConstantEvaluatorError::ArrayLengthDynamic), |
| 1509 |
}, |
| 1510 |
_ => Err(ConstantEvaluatorError::InvalidArrayLengthArg), |
| 1511 |
} |
| 1512 |
} |
| 1513 |
_ => Err(ConstantEvaluatorError::InvalidArrayLengthArg), |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
fn access( |
| 1518 |
&mut self, |
| 1519 |
base: Handle<Expression>, |
| 1520 |
index: usize, |
| 1521 |
span: Span, |
| 1522 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1523 |
match self.expressions[base] { |
| 1524 |
Expression::ZeroValue(ty) => { |
| 1525 |
let ty_inner = &self.types[ty].inner; |
| 1526 |
let components = ty_inner |
| 1527 |
.components() |
| 1528 |
.ok_or(ConstantEvaluatorError::InvalidAccessBase)?; |
| 1529 |
|
| 1530 |
if index >= components as usize { |
| 1531 |
Err(ConstantEvaluatorError::InvalidAccessBase) |
| 1532 |
} else { |
| 1533 |
let ty_res = ty_inner |
| 1534 |
.component_type(index) |
| 1535 |
.ok_or(ConstantEvaluatorError::InvalidAccessIndex)?; |
| 1536 |
let ty = match ty_res { |
| 1537 |
crate::proc::TypeResolution::Handle(ty) => ty, |
| 1538 |
crate::proc::TypeResolution::Value(inner) => { |
| 1539 |
self.types.insert(Type { name: None, inner }, span) |
| 1540 |
} |
| 1541 |
}; |
| 1542 |
self.register_evaluated_expr(Expression::ZeroValue(ty), span) |
| 1543 |
} |
| 1544 |
} |
| 1545 |
Expression::Splat { size, value } => { |
| 1546 |
if index >= size as usize { |
| 1547 |
Err(ConstantEvaluatorError::InvalidAccessBase) |
| 1548 |
} else { |
| 1549 |
Ok(value) |
| 1550 |
} |
| 1551 |
} |
| 1552 |
Expression::Compose { ty, ref components } => { |
| 1553 |
let _ = self.types[ty] |
| 1554 |
.inner |
| 1555 |
.components() |
| 1556 |
.ok_or(ConstantEvaluatorError::InvalidAccessBase)?; |
| 1557 |
|
| 1558 |
crate::proc::flatten_compose(ty, components, self.expressions, self.types) |
| 1559 |
.nth(index) |
| 1560 |
.ok_or(ConstantEvaluatorError::InvalidAccessIndex) |
| 1561 |
} |
| 1562 |
_ => Err(ConstantEvaluatorError::InvalidAccessBase), |
| 1563 |
} |
| 1564 |
} |
| 1565 |
|
| 1566 |
fn constant_index(&self, expr: Handle<Expression>) -> Result<usize, ConstantEvaluatorError> { |
| 1567 |
match self.expressions[expr] { |
| 1568 |
Expression::ZeroValue(ty) |
| 1569 |
if matches!( |
| 1570 |
self.types[ty].inner, |
| 1571 |
TypeInner::Scalar(crate::Scalar { |
| 1572 |
kind: ScalarKind::Uint, |
| 1573 |
.. |
| 1574 |
}) |
| 1575 |
) => |
| 1576 |
{ |
| 1577 |
Ok(0) |
| 1578 |
} |
| 1579 |
Expression::Literal(Literal::U32(index)) => Ok(index as usize), |
| 1580 |
_ => Err(ConstantEvaluatorError::InvalidAccessIndexTy), |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
/// Lower [`ZeroValue`] and [`Splat`] expressions to [`Literal`] and [`Compose`] expressions. |
| 1585 |
/// |
| 1586 |
/// [`ZeroValue`]: Expression::ZeroValue |
| 1587 |
/// [`Splat`]: Expression::Splat |
| 1588 |
/// [`Literal`]: Expression::Literal |
| 1589 |
/// [`Compose`]: Expression::Compose |
| 1590 |
fn eval_zero_value_and_splat( |
| 1591 |
&mut self, |
| 1592 |
mut expr: Handle<Expression>, |
| 1593 |
span: Span, |
| 1594 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1595 |
// If expr is a Compose expression, eliminate ZeroValue and Splat expressions for |
| 1596 |
// each of its components. |
| 1597 |
if let Expression::Compose { ty, ref components } = self.expressions[expr] { |
| 1598 |
let components = components |
| 1599 |
.clone() |
| 1600 |
.iter() |
| 1601 |
.map(|component| self.eval_zero_value_and_splat(*component, span)) |
| 1602 |
.collect::<Result<_, _>>()?; |
| 1603 |
expr = self.register_evaluated_expr(Expression::Compose { ty, components }, span)?; |
| 1604 |
} |
| 1605 |
|
| 1606 |
// The result of the splat() for a Splat of a scalar ZeroValue is a |
| 1607 |
// vector ZeroValue, so we must call eval_zero_value_impl() after |
| 1608 |
// splat() in order to ensure we have no ZeroValues remaining. |
| 1609 |
if let Expression::Splat { size, value } = self.expressions[expr] { |
| 1610 |
expr = self.splat(value, size, span)?; |
| 1611 |
} |
| 1612 |
if let Expression::ZeroValue(ty) = self.expressions[expr] { |
| 1613 |
expr = self.eval_zero_value_impl(ty, span)?; |
| 1614 |
} |
| 1615 |
Ok(expr) |
| 1616 |
} |
| 1617 |
|
| 1618 |
/// Lower [`ZeroValue`] expressions to [`Literal`] and [`Compose`] expressions. |
| 1619 |
/// |
| 1620 |
/// [`ZeroValue`]: Expression::ZeroValue |
| 1621 |
/// [`Literal`]: Expression::Literal |
| 1622 |
/// [`Compose`]: Expression::Compose |
| 1623 |
fn eval_zero_value( |
| 1624 |
&mut self, |
| 1625 |
expr: Handle<Expression>, |
| 1626 |
span: Span, |
| 1627 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1628 |
match self.expressions[expr] { |
| 1629 |
Expression::ZeroValue(ty) => self.eval_zero_value_impl(ty, span), |
| 1630 |
_ => Ok(expr), |
| 1631 |
} |
| 1632 |
} |
| 1633 |
|
| 1634 |
/// Lower [`ZeroValue`] expressions to [`Literal`] and [`Compose`] expressions. |
| 1635 |
/// |
| 1636 |
/// [`ZeroValue`]: Expression::ZeroValue |
| 1637 |
/// [`Literal`]: Expression::Literal |
| 1638 |
/// [`Compose`]: Expression::Compose |
| 1639 |
fn eval_zero_value_impl( |
| 1640 |
&mut self, |
| 1641 |
ty: Handle<Type>, |
| 1642 |
span: Span, |
| 1643 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1644 |
match self.types[ty].inner { |
| 1645 |
TypeInner::Scalar(scalar) => { |
| 1646 |
let expr = Expression::Literal( |
| 1647 |
Literal::zero(scalar).ok_or(ConstantEvaluatorError::TypeNotConstructible)?, |
| 1648 |
); |
| 1649 |
self.register_evaluated_expr(expr, span) |
| 1650 |
} |
| 1651 |
TypeInner::Vector { size, scalar } => { |
| 1652 |
let scalar_ty = self.types.insert( |
| 1653 |
Type { |
| 1654 |
name: None, |
| 1655 |
inner: TypeInner::Scalar(scalar), |
| 1656 |
}, |
| 1657 |
span, |
| 1658 |
); |
| 1659 |
let el = self.eval_zero_value_impl(scalar_ty, span)?; |
| 1660 |
let expr = Expression::Compose { |
| 1661 |
ty, |
| 1662 |
components: vec![el; size as usize], |
| 1663 |
}; |
| 1664 |
self.register_evaluated_expr(expr, span) |
| 1665 |
} |
| 1666 |
TypeInner::Matrix { |
| 1667 |
columns, |
| 1668 |
rows, |
| 1669 |
scalar, |
| 1670 |
} => { |
| 1671 |
let vec_ty = self.types.insert( |
| 1672 |
Type { |
| 1673 |
name: None, |
| 1674 |
inner: TypeInner::Vector { size: rows, scalar }, |
| 1675 |
}, |
| 1676 |
span, |
| 1677 |
); |
| 1678 |
let el = self.eval_zero_value_impl(vec_ty, span)?; |
| 1679 |
let expr = Expression::Compose { |
| 1680 |
ty, |
| 1681 |
components: vec![el; columns as usize], |
| 1682 |
}; |
| 1683 |
self.register_evaluated_expr(expr, span) |
| 1684 |
} |
| 1685 |
TypeInner::Array { |
| 1686 |
base, |
| 1687 |
size: ArraySize::Constant(size), |
| 1688 |
.. |
| 1689 |
} => { |
| 1690 |
let el = self.eval_zero_value_impl(base, span)?; |
| 1691 |
let expr = Expression::Compose { |
| 1692 |
ty, |
| 1693 |
components: vec![el; size.get() as usize], |
| 1694 |
}; |
| 1695 |
self.register_evaluated_expr(expr, span) |
| 1696 |
} |
| 1697 |
TypeInner::Struct { ref members, .. } => { |
| 1698 |
let types: Vec<_> = members.iter().map(|m| m.ty).collect(); |
| 1699 |
let mut components = Vec::with_capacity(members.len()); |
| 1700 |
for ty in types { |
| 1701 |
components.push(self.eval_zero_value_impl(ty, span)?); |
| 1702 |
} |
| 1703 |
let expr = Expression::Compose { ty, components }; |
| 1704 |
self.register_evaluated_expr(expr, span) |
| 1705 |
} |
| 1706 |
_ => Err(ConstantEvaluatorError::TypeNotConstructible), |
| 1707 |
} |
| 1708 |
} |
| 1709 |
|
| 1710 |
/// Convert the scalar components of `expr` to `target`. |
| 1711 |
/// |
| 1712 |
/// Treat `span` as the location of the resulting expression. |
| 1713 |
pub fn cast( |
| 1714 |
&mut self, |
| 1715 |
expr: Handle<Expression>, |
| 1716 |
target: crate::Scalar, |
| 1717 |
span: Span, |
| 1718 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1719 |
use crate::Scalar as Sc; |
| 1720 |
|
| 1721 |
let expr = self.eval_zero_value(expr, span)?; |
| 1722 |
|
| 1723 |
let make_error = || -> Result<_, ConstantEvaluatorError> { |
| 1724 |
let from = format!("{:?} {:?}", expr, self.expressions[expr]); |
| 1725 |
|
| 1726 |
#[cfg(feature = "wgsl-in")] |
| 1727 |
let to = target.to_wgsl_for_diagnostics(); |
| 1728 |
|
| 1729 |
#[cfg(not(feature = "wgsl-in"))] |
| 1730 |
let to = format!("{target:?}"); |
| 1731 |
|
| 1732 |
Err(ConstantEvaluatorError::InvalidCastArg { from, to }) |
| 1733 |
}; |
| 1734 |
|
| 1735 |
use crate::proc::type_methods::IntFloatLimits; |
| 1736 |
|
| 1737 |
let expr = match self.expressions[expr] { |
| 1738 |
Expression::Literal(literal) => { |
| 1739 |
let literal = match target { |
| 1740 |
Sc::I32 => Literal::I32(match literal { |
| 1741 |
Literal::I32(v) => v, |
| 1742 |
Literal::U32(v) => v as i32, |
| 1743 |
Literal::F32(v) => v.clamp(i32::min_float(), i32::max_float()) as i32, |
| 1744 |
Literal::F16(v) => f16::to_i32(&v).unwrap(), //Only None on NaN or Inf |
| 1745 |
Literal::Bool(v) => v as i32, |
| 1746 |
Literal::F64(_) | Literal::I64(_) | Literal::U64(_) => { |
| 1747 |
return make_error(); |
| 1748 |
} |
| 1749 |
Literal::AbstractInt(v) => i32::try_from_abstract(v)?, |
| 1750 |
Literal::AbstractFloat(v) => i32::try_from_abstract(v)?, |
| 1751 |
}), |
| 1752 |
Sc::U32 => Literal::U32(match literal { |
| 1753 |
Literal::I32(v) => v as u32, |
| 1754 |
Literal::U32(v) => v, |
| 1755 |
Literal::F32(v) => v.clamp(u32::min_float(), u32::max_float()) as u32, |
| 1756 |
// max(0) avoids None due to negative, therefore only None on NaN or Inf |
| 1757 |
Literal::F16(v) => f16::to_u32(&v.max(f16::ZERO)).unwrap(), |
| 1758 |
Literal::Bool(v) => v as u32, |
| 1759 |
Literal::F64(_) | Literal::I64(_) | Literal::U64(_) => { |
| 1760 |
return make_error(); |
| 1761 |
} |
| 1762 |
Literal::AbstractInt(v) => u32::try_from_abstract(v)?, |
| 1763 |
Literal::AbstractFloat(v) => u32::try_from_abstract(v)?, |
| 1764 |
}), |
| 1765 |
Sc::I64 => Literal::I64(match literal { |
| 1766 |
Literal::I32(v) => v as i64, |
| 1767 |
Literal::U32(v) => v as i64, |
| 1768 |
Literal::F32(v) => v.clamp(i64::min_float(), i64::max_float()) as i64, |
| 1769 |
Literal::Bool(v) => v as i64, |
| 1770 |
Literal::F64(v) => v.clamp(i64::min_float(), i64::max_float()) as i64, |
| 1771 |
Literal::I64(v) => v, |
| 1772 |
Literal::U64(v) => v as i64, |
| 1773 |
Literal::F16(v) => f16::to_i64(&v).unwrap(), //Only None on NaN or Inf |
| 1774 |
Literal::AbstractInt(v) => i64::try_from_abstract(v)?, |
| 1775 |
Literal::AbstractFloat(v) => i64::try_from_abstract(v)?, |
| 1776 |
}), |
| 1777 |
Sc::U64 => Literal::U64(match literal { |
| 1778 |
Literal::I32(v) => v as u64, |
| 1779 |
Literal::U32(v) => v as u64, |
| 1780 |
Literal::F32(v) => v.clamp(u64::min_float(), u64::max_float()) as u64, |
| 1781 |
Literal::Bool(v) => v as u64, |
| 1782 |
Literal::F64(v) => v.clamp(u64::min_float(), u64::max_float()) as u64, |
| 1783 |
Literal::I64(v) => v as u64, |
| 1784 |
Literal::U64(v) => v, |
| 1785 |
// max(0) avoids None due to negative, therefore only None on NaN or Inf |
| 1786 |
Literal::F16(v) => f16::to_u64(&v.max(f16::ZERO)).unwrap(), |
| 1787 |
Literal::AbstractInt(v) => u64::try_from_abstract(v)?, |
| 1788 |
Literal::AbstractFloat(v) => u64::try_from_abstract(v)?, |
| 1789 |
}), |
| 1790 |
Sc::F16 => Literal::F16(match literal { |
| 1791 |
Literal::F16(v) => v, |
| 1792 |
Literal::F32(v) => f16::from_f32(v), |
| 1793 |
Literal::F64(v) => f16::from_f64(v), |
| 1794 |
Literal::Bool(v) => f16::from_u32(v as u32).unwrap(), |
| 1795 |
Literal::I64(v) => f16::from_i64(v).unwrap(), |
| 1796 |
Literal::U64(v) => f16::from_u64(v).unwrap(), |
| 1797 |
Literal::I32(v) => f16::from_i32(v).unwrap(), |
| 1798 |
Literal::U32(v) => f16::from_u32(v).unwrap(), |
| 1799 |
Literal::AbstractFloat(v) => f16::try_from_abstract(v)?, |
| 1800 |
Literal::AbstractInt(v) => f16::try_from_abstract(v)?, |
| 1801 |
}), |
| 1802 |
Sc::F32 => Literal::F32(match literal { |
| 1803 |
Literal::I32(v) => v as f32, |
| 1804 |
Literal::U32(v) => v as f32, |
| 1805 |
Literal::F32(v) => v, |
| 1806 |
Literal::Bool(v) => v as u32 as f32, |
| 1807 |
Literal::F64(_) | Literal::I64(_) | Literal::U64(_) => { |
| 1808 |
return make_error(); |
| 1809 |
} |
| 1810 |
Literal::F16(v) => f16::to_f32(v), |
| 1811 |
Literal::AbstractInt(v) => f32::try_from_abstract(v)?, |
| 1812 |
Literal::AbstractFloat(v) => f32::try_from_abstract(v)?, |
| 1813 |
}), |
| 1814 |
Sc::F64 => Literal::F64(match literal { |
| 1815 |
Literal::I32(v) => v as f64, |
| 1816 |
Literal::U32(v) => v as f64, |
| 1817 |
Literal::F16(v) => f16::to_f64(v), |
| 1818 |
Literal::F32(v) => v as f64, |
| 1819 |
Literal::F64(v) => v, |
| 1820 |
Literal::Bool(v) => v as u32 as f64, |
| 1821 |
Literal::I64(_) | Literal::U64(_) => return make_error(), |
| 1822 |
Literal::AbstractInt(v) => f64::try_from_abstract(v)?, |
| 1823 |
Literal::AbstractFloat(v) => f64::try_from_abstract(v)?, |
| 1824 |
}), |
| 1825 |
Sc::BOOL => Literal::Bool(match literal { |
| 1826 |
Literal::I32(v) => v != 0, |
| 1827 |
Literal::U32(v) => v != 0, |
| 1828 |
Literal::F32(v) => v != 0.0, |
| 1829 |
Literal::F16(v) => v != f16::zero(), |
| 1830 |
Literal::Bool(v) => v, |
| 1831 |
Literal::AbstractInt(v) => v != 0, |
| 1832 |
Literal::AbstractFloat(v) => v != 0.0, |
| 1833 |
Literal::F64(_) | Literal::I64(_) | Literal::U64(_) => { |
| 1834 |
return make_error(); |
| 1835 |
} |
| 1836 |
}), |
| 1837 |
Sc::ABSTRACT_FLOAT => Literal::AbstractFloat(match literal { |
| 1838 |
Literal::AbstractInt(v) => { |
| 1839 |
// Overflow is forbidden, but inexact conversions |
| 1840 |
// are fine. The range of f64 is far larger than |
| 1841 |
// that of i64, so we don't have to check anything |
| 1842 |
// here. |
| 1843 |
v as f64 |
| 1844 |
} |
| 1845 |
Literal::AbstractFloat(v) => v, |
| 1846 |
_ => return make_error(), |
| 1847 |
}), |
| 1848 |
_ => { |
| 1849 |
log::debug!("Constant evaluator refused to convert value to {target:?}"); |
| 1850 |
return make_error(); |
| 1851 |
} |
| 1852 |
}; |
| 1853 |
Expression::Literal(literal) |
| 1854 |
} |
| 1855 |
Expression::Compose { |
| 1856 |
ty, |
| 1857 |
components: ref src_components, |
| 1858 |
} => { |
| 1859 |
let ty_inner = match self.types[ty].inner { |
| 1860 |
TypeInner::Vector { size, .. } => TypeInner::Vector { |
| 1861 |
size, |
| 1862 |
scalar: target, |
| 1863 |
}, |
| 1864 |
TypeInner::Matrix { columns, rows, .. } => TypeInner::Matrix { |
| 1865 |
columns, |
| 1866 |
rows, |
| 1867 |
scalar: target, |
| 1868 |
}, |
| 1869 |
_ => return make_error(), |
| 1870 |
}; |
| 1871 |
|
| 1872 |
let mut components = src_components.clone(); |
| 1873 |
for component in &mut components { |
| 1874 |
*component = self.cast(*component, target, span)?; |
| 1875 |
} |
| 1876 |
|
| 1877 |
let ty = self.types.insert( |
| 1878 |
Type { |
| 1879 |
name: None, |
| 1880 |
inner: ty_inner, |
| 1881 |
}, |
| 1882 |
span, |
| 1883 |
); |
| 1884 |
|
| 1885 |
Expression::Compose { ty, components } |
| 1886 |
} |
| 1887 |
Expression::Splat { size, value } => { |
| 1888 |
let value_span = self.expressions.get_span(value); |
| 1889 |
let cast_value = self.cast(value, target, value_span)?; |
| 1890 |
Expression::Splat { |
| 1891 |
size, |
| 1892 |
value: cast_value, |
| 1893 |
} |
| 1894 |
} |
| 1895 |
_ => return make_error(), |
| 1896 |
}; |
| 1897 |
|
| 1898 |
self.register_evaluated_expr(expr, span) |
| 1899 |
} |
| 1900 |
|
| 1901 |
/// Convert the scalar leaves of `expr` to `target`, handling arrays. |
| 1902 |
/// |
| 1903 |
/// `expr` must be a `Compose` expression whose type is a scalar, vector, |
| 1904 |
/// matrix, or nested arrays of such. |
| 1905 |
/// |
| 1906 |
/// This is basically the same as the [`cast`] method, except that that |
| 1907 |
/// should only handle Naga [`As`] expressions, which cannot convert arrays. |
| 1908 |
/// |
| 1909 |
/// Treat `span` as the location of the resulting expression. |
| 1910 |
/// |
| 1911 |
/// [`cast`]: ConstantEvaluator::cast |
| 1912 |
/// [`As`]: crate::Expression::As |
| 1913 |
pub fn cast_array( |
| 1914 |
&mut self, |
| 1915 |
expr: Handle<Expression>, |
| 1916 |
target: crate::Scalar, |
| 1917 |
span: Span, |
| 1918 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1919 |
let expr = self.check_and_get(expr)?; |
| 1920 |
|
| 1921 |
let Expression::Compose { ty, ref components } = self.expressions[expr] else { |
| 1922 |
return self.cast(expr, target, span); |
| 1923 |
}; |
| 1924 |
|
| 1925 |
let TypeInner::Array { |
| 1926 |
base: _, |
| 1927 |
size, |
| 1928 |
stride: _, |
| 1929 |
} = self.types[ty].inner |
| 1930 |
else { |
| 1931 |
return self.cast(expr, target, span); |
| 1932 |
}; |
| 1933 |
|
| 1934 |
let mut components = components.clone(); |
| 1935 |
for component in &mut components { |
| 1936 |
*component = self.cast_array(*component, target, span)?; |
| 1937 |
} |
| 1938 |
|
| 1939 |
let first = components.first().unwrap(); |
| 1940 |
let new_base = match self.resolve_type(*first)? { |
| 1941 |
crate::proc::TypeResolution::Handle(ty) => ty, |
| 1942 |
crate::proc::TypeResolution::Value(inner) => { |
| 1943 |
self.types.insert(Type { name: None, inner }, span) |
| 1944 |
} |
| 1945 |
}; |
| 1946 |
let mut layouter = core::mem::take(self.layouter); |
| 1947 |
layouter.update(self.to_ctx()).unwrap(); |
| 1948 |
*self.layouter = layouter; |
| 1949 |
|
| 1950 |
let new_base_stride = self.layouter[new_base].to_stride(); |
| 1951 |
let new_array_ty = self.types.insert( |
| 1952 |
Type { |
| 1953 |
name: None, |
| 1954 |
inner: TypeInner::Array { |
| 1955 |
base: new_base, |
| 1956 |
size, |
| 1957 |
stride: new_base_stride, |
| 1958 |
}, |
| 1959 |
}, |
| 1960 |
span, |
| 1961 |
); |
| 1962 |
|
| 1963 |
let compose = Expression::Compose { |
| 1964 |
ty: new_array_ty, |
| 1965 |
components, |
| 1966 |
}; |
| 1967 |
self.register_evaluated_expr(compose, span) |
| 1968 |
} |
| 1969 |
|
| 1970 |
fn unary_op( |
| 1971 |
&mut self, |
| 1972 |
op: UnaryOperator, |
| 1973 |
expr: Handle<Expression>, |
| 1974 |
span: Span, |
| 1975 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 1976 |
let expr = self.eval_zero_value_and_splat(expr, span)?; |
| 1977 |
|
| 1978 |
let expr = match self.expressions[expr] { |
| 1979 |
Expression::Literal(value) => Expression::Literal(match op { |
| 1980 |
UnaryOperator::Negate => match value { |
| 1981 |
Literal::I32(v) => Literal::I32(v.wrapping_neg()), |
| 1982 |
Literal::I64(v) => Literal::I64(v.wrapping_neg()), |
| 1983 |
Literal::F32(v) => Literal::F32(-v), |
| 1984 |
Literal::F16(v) => Literal::F16(-v), |
| 1985 |
Literal::F64(v) => Literal::F64(-v), |
| 1986 |
Literal::AbstractInt(v) => Literal::AbstractInt(v.wrapping_neg()), |
| 1987 |
Literal::AbstractFloat(v) => Literal::AbstractFloat(-v), |
| 1988 |
_ => return Err(ConstantEvaluatorError::InvalidUnaryOpArg), |
| 1989 |
}, |
| 1990 |
UnaryOperator::LogicalNot => match value { |
| 1991 |
Literal::Bool(v) => Literal::Bool(!v), |
| 1992 |
_ => return Err(ConstantEvaluatorError::InvalidUnaryOpArg), |
| 1993 |
}, |
| 1994 |
UnaryOperator::BitwiseNot => match value { |
| 1995 |
Literal::I32(v) => Literal::I32(!v), |
| 1996 |
Literal::I64(v) => Literal::I64(!v), |
| 1997 |
Literal::U32(v) => Literal::U32(!v), |
| 1998 |
Literal::U64(v) => Literal::U64(!v), |
| 1999 |
Literal::AbstractInt(v) => Literal::AbstractInt(!v), |
| 2000 |
_ => return Err(ConstantEvaluatorError::InvalidUnaryOpArg), |
| 2001 |
}, |
| 2002 |
}), |
| 2003 |
Expression::Compose { |
| 2004 |
ty, |
| 2005 |
components: ref src_components, |
| 2006 |
} => { |
| 2007 |
match self.types[ty].inner { |
| 2008 |
TypeInner::Vector { .. } | TypeInner::Matrix { .. } => (), |
| 2009 |
_ => return Err(ConstantEvaluatorError::InvalidUnaryOpArg), |
| 2010 |
} |
| 2011 |
|
| 2012 |
let mut components = src_components.clone(); |
| 2013 |
for component in &mut components { |
| 2014 |
*component = self.unary_op(op, *component, span)?; |
| 2015 |
} |
| 2016 |
|
| 2017 |
Expression::Compose { ty, components } |
| 2018 |
} |
| 2019 |
_ => return Err(ConstantEvaluatorError::InvalidUnaryOpArg), |
| 2020 |
}; |
| 2021 |
|
| 2022 |
self.register_evaluated_expr(expr, span) |
| 2023 |
} |
| 2024 |
|
| 2025 |
fn binary_op( |
| 2026 |
&mut self, |
| 2027 |
op: BinaryOperator, |
| 2028 |
left: Handle<Expression>, |
| 2029 |
right: Handle<Expression>, |
| 2030 |
span: Span, |
| 2031 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 2032 |
let left = self.eval_zero_value_and_splat(left, span)?; |
| 2033 |
let right = self.eval_zero_value_and_splat(right, span)?; |
| 2034 |
|
| 2035 |
let expr = match (&self.expressions[left], &self.expressions[right]) { |
| 2036 |
(&Expression::Literal(left_value), &Expression::Literal(right_value)) => { |
| 2037 |
let literal = match op { |
| 2038 |
BinaryOperator::Equal => Literal::Bool(left_value == right_value), |
| 2039 |
BinaryOperator::NotEqual => Literal::Bool(left_value != right_value), |
| 2040 |
BinaryOperator::Less => Literal::Bool(left_value < right_value), |
| 2041 |
BinaryOperator::LessEqual => Literal::Bool(left_value <= right_value), |
| 2042 |
BinaryOperator::Greater => Literal::Bool(left_value > right_value), |
| 2043 |
BinaryOperator::GreaterEqual => Literal::Bool(left_value >= right_value), |
| 2044 |
|
| 2045 |
_ => match (left_value, right_value) { |
| 2046 |
(Literal::I32(a), Literal::I32(b)) => Literal::I32(match op { |
| 2047 |
BinaryOperator::Add => a.wrapping_add(b), |
| 2048 |
BinaryOperator::Subtract => a.wrapping_sub(b), |
| 2049 |
BinaryOperator::Multiply => a.wrapping_mul(b), |
| 2050 |
BinaryOperator::Divide => { |
| 2051 |
if b == 0 { |
| 2052 |
return Err(ConstantEvaluatorError::DivisionByZero); |
| 2053 |
} else { |
| 2054 |
a.wrapping_div(b) |
| 2055 |
} |
| 2056 |
} |
| 2057 |
BinaryOperator::Modulo => { |
| 2058 |
if b == 0 { |
| 2059 |
return Err(ConstantEvaluatorError::RemainderByZero); |
| 2060 |
} else { |
| 2061 |
a.wrapping_rem(b) |
| 2062 |
} |
| 2063 |
} |
| 2064 |
BinaryOperator::And => a & b, |
| 2065 |
BinaryOperator::ExclusiveOr => a ^ b, |
| 2066 |
BinaryOperator::InclusiveOr => a | b, |
| 2067 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2068 |
}), |
| 2069 |
(Literal::I32(a), Literal::U32(b)) => Literal::I32(match op { |
| 2070 |
BinaryOperator::ShiftLeft => { |
| 2071 |
if (if a.is_negative() { !a } else { a }).leading_zeros() <= b { |
| 2072 |
return Err(ConstantEvaluatorError::Overflow("<<".to_string())); |
| 2073 |
} |
| 2074 |
a.checked_shl(b) |
| 2075 |
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)? |
| 2076 |
} |
| 2077 |
BinaryOperator::ShiftRight => a |
| 2078 |
.checked_shr(b) |
| 2079 |
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)?, |
| 2080 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2081 |
}), |
| 2082 |
(Literal::U32(a), Literal::U32(b)) => Literal::U32(match op { |
| 2083 |
BinaryOperator::Add => a.checked_add(b).ok_or_else(|| { |
| 2084 |
ConstantEvaluatorError::Overflow("addition".into()) |
| 2085 |
})?, |
| 2086 |
BinaryOperator::Subtract => a.checked_sub(b).ok_or_else(|| { |
| 2087 |
ConstantEvaluatorError::Overflow("subtraction".into()) |
| 2088 |
})?, |
| 2089 |
BinaryOperator::Multiply => a.checked_mul(b).ok_or_else(|| { |
| 2090 |
ConstantEvaluatorError::Overflow("multiplication".into()) |
| 2091 |
})?, |
| 2092 |
BinaryOperator::Divide => a |
| 2093 |
.checked_div(b) |
| 2094 |
.ok_or(ConstantEvaluatorError::DivisionByZero)?, |
| 2095 |
BinaryOperator::Modulo => a |
| 2096 |
.checked_rem(b) |
| 2097 |
.ok_or(ConstantEvaluatorError::RemainderByZero)?, |
| 2098 |
BinaryOperator::And => a & b, |
| 2099 |
BinaryOperator::ExclusiveOr => a ^ b, |
| 2100 |
BinaryOperator::InclusiveOr => a | b, |
| 2101 |
BinaryOperator::ShiftLeft => a |
| 2102 |
.checked_mul( |
| 2103 |
1u32.checked_shl(b) |
| 2104 |
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)?, |
| 2105 |
) |
| 2106 |
.ok_or(ConstantEvaluatorError::Overflow("<<".to_string()))?, |
| 2107 |
BinaryOperator::ShiftRight => a |
| 2108 |
.checked_shr(b) |
| 2109 |
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)?, |
| 2110 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2111 |
}), |
| 2112 |
(Literal::F32(a), Literal::F32(b)) => Literal::F32(match op { |
| 2113 |
BinaryOperator::Add => a + b, |
| 2114 |
BinaryOperator::Subtract => a - b, |
| 2115 |
BinaryOperator::Multiply => a * b, |
| 2116 |
BinaryOperator::Divide => a / b, |
| 2117 |
BinaryOperator::Modulo => a % b, |
| 2118 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2119 |
}), |
| 2120 |
(Literal::AbstractInt(a), Literal::U32(b)) => { |
| 2121 |
Literal::AbstractInt(match op { |
| 2122 |
BinaryOperator::ShiftLeft => { |
| 2123 |
if (if a.is_negative() { !a } else { a }).leading_zeros() <= b { |
| 2124 |
return Err(ConstantEvaluatorError::Overflow( |
| 2125 |
"<<".to_string(), |
| 2126 |
)); |
| 2127 |
} |
| 2128 |
a.checked_shl(b).unwrap_or(0) |
| 2129 |
} |
| 2130 |
BinaryOperator::ShiftRight => a.checked_shr(b).unwrap_or(0), |
| 2131 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2132 |
}) |
| 2133 |
} |
| 2134 |
(Literal::F16(a), Literal::F16(b)) => Literal::F16(match op { |
| 2135 |
BinaryOperator::Add => a + b, |
| 2136 |
BinaryOperator::Subtract => a - b, |
| 2137 |
BinaryOperator::Multiply => a * b, |
| 2138 |
BinaryOperator::Divide => a / b, |
| 2139 |
BinaryOperator::Modulo => a % b, |
| 2140 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2141 |
}), |
| 2142 |
(Literal::AbstractInt(a), Literal::AbstractInt(b)) => { |
| 2143 |
Literal::AbstractInt(match op { |
| 2144 |
BinaryOperator::Add => a.checked_add(b).ok_or_else(|| { |
| 2145 |
ConstantEvaluatorError::Overflow("addition".into()) |
| 2146 |
})?, |
| 2147 |
BinaryOperator::Subtract => a.checked_sub(b).ok_or_else(|| { |
| 2148 |
ConstantEvaluatorError::Overflow("subtraction".into()) |
| 2149 |
})?, |
| 2150 |
BinaryOperator::Multiply => a.checked_mul(b).ok_or_else(|| { |
| 2151 |
ConstantEvaluatorError::Overflow("multiplication".into()) |
| 2152 |
})?, |
| 2153 |
BinaryOperator::Divide => a.checked_div(b).ok_or_else(|| { |
| 2154 |
if b == 0 { |
| 2155 |
ConstantEvaluatorError::DivisionByZero |
| 2156 |
} else { |
| 2157 |
ConstantEvaluatorError::Overflow("division".into()) |
| 2158 |
} |
| 2159 |
})?, |
| 2160 |
BinaryOperator::Modulo => a.checked_rem(b).ok_or_else(|| { |
| 2161 |
if b == 0 { |
| 2162 |
ConstantEvaluatorError::RemainderByZero |
| 2163 |
} else { |
| 2164 |
ConstantEvaluatorError::Overflow("remainder".into()) |
| 2165 |
} |
| 2166 |
})?, |
| 2167 |
BinaryOperator::And => a & b, |
| 2168 |
BinaryOperator::ExclusiveOr => a ^ b, |
| 2169 |
BinaryOperator::InclusiveOr => a | b, |
| 2170 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2171 |
}) |
| 2172 |
} |
| 2173 |
(Literal::AbstractFloat(a), Literal::AbstractFloat(b)) => { |
| 2174 |
Literal::AbstractFloat(match op { |
| 2175 |
BinaryOperator::Add => a + b, |
| 2176 |
BinaryOperator::Subtract => a - b, |
| 2177 |
BinaryOperator::Multiply => a * b, |
| 2178 |
BinaryOperator::Divide => a / b, |
| 2179 |
BinaryOperator::Modulo => a % b, |
| 2180 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2181 |
}) |
| 2182 |
} |
| 2183 |
(Literal::Bool(a), Literal::Bool(b)) => Literal::Bool(match op { |
| 2184 |
BinaryOperator::LogicalAnd => a && b, |
| 2185 |
BinaryOperator::LogicalOr => a || b, |
| 2186 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2187 |
}), |
| 2188 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2189 |
}, |
| 2190 |
}; |
| 2191 |
Expression::Literal(literal) |
| 2192 |
} |
| 2193 |
( |
| 2194 |
&Expression::Compose { |
| 2195 |
components: ref src_components, |
| 2196 |
ty, |
| 2197 |
}, |
| 2198 |
&Expression::Literal(_), |
| 2199 |
) => { |
| 2200 |
let mut components = src_components.clone(); |
| 2201 |
for component in &mut components { |
| 2202 |
*component = self.binary_op(op, *component, right, span)?; |
| 2203 |
} |
| 2204 |
Expression::Compose { ty, components } |
| 2205 |
} |
| 2206 |
( |
| 2207 |
&Expression::Literal(_), |
| 2208 |
&Expression::Compose { |
| 2209 |
components: ref src_components, |
| 2210 |
ty, |
| 2211 |
}, |
| 2212 |
) => { |
| 2213 |
let mut components = src_components.clone(); |
| 2214 |
for component in &mut components { |
| 2215 |
*component = self.binary_op(op, left, *component, span)?; |
| 2216 |
} |
| 2217 |
Expression::Compose { ty, components } |
| 2218 |
} |
| 2219 |
( |
| 2220 |
&Expression::Compose { |
| 2221 |
components: ref left_components, |
| 2222 |
ty: left_ty, |
| 2223 |
}, |
| 2224 |
&Expression::Compose { |
| 2225 |
components: ref right_components, |
| 2226 |
ty: right_ty, |
| 2227 |
}, |
| 2228 |
) => { |
| 2229 |
// We have to make a copy of the component lists, because the |
| 2230 |
// call to `binary_op_vector` needs `&mut self`, but `self` owns |
| 2231 |
// the component lists. |
| 2232 |
let left_flattened = crate::proc::flatten_compose( |
| 2233 |
left_ty, |
| 2234 |
left_components, |
| 2235 |
self.expressions, |
| 2236 |
self.types, |
| 2237 |
); |
| 2238 |
let right_flattened = crate::proc::flatten_compose( |
| 2239 |
right_ty, |
| 2240 |
right_components, |
| 2241 |
self.expressions, |
| 2242 |
self.types, |
| 2243 |
); |
| 2244 |
|
| 2245 |
// `flatten_compose` doesn't return an `ExactSizeIterator`, so |
| 2246 |
// make a reasonable guess of the capacity we'll need. |
| 2247 |
let mut flattened = Vec::with_capacity(left_components.len()); |
| 2248 |
flattened.extend(left_flattened.zip(right_flattened)); |
| 2249 |
|
| 2250 |
match (&self.types[left_ty].inner, &self.types[right_ty].inner) { |
| 2251 |
( |
| 2252 |
&TypeInner::Vector { |
| 2253 |
size: left_size, .. |
| 2254 |
}, |
| 2255 |
&TypeInner::Vector { |
| 2256 |
size: right_size, .. |
| 2257 |
}, |
| 2258 |
) if left_size == right_size => { |
| 2259 |
self.binary_op_vector(op, left_size, &flattened, left_ty, span)? |
| 2260 |
} |
| 2261 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2262 |
} |
| 2263 |
} |
| 2264 |
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs), |
| 2265 |
}; |
| 2266 |
|
| 2267 |
self.register_evaluated_expr(expr, span) |
| 2268 |
} |
| 2269 |
|
| 2270 |
fn binary_op_vector( |
| 2271 |
&mut self, |
| 2272 |
op: BinaryOperator, |
| 2273 |
size: crate::VectorSize, |
| 2274 |
components: &[(Handle<Expression>, Handle<Expression>)], |
| 2275 |
left_ty: Handle<Type>, |
| 2276 |
span: Span, |
| 2277 |
) -> Result<Expression, ConstantEvaluatorError> { |
| 2278 |
let ty = match op { |
| 2279 |
// Relational operators produce vectors of booleans. |
| 2280 |
BinaryOperator::Equal |
| 2281 |
| BinaryOperator::NotEqual |
| 2282 |
| BinaryOperator::Less |
| 2283 |
| BinaryOperator::LessEqual |
| 2284 |
| BinaryOperator::Greater |
| 2285 |
| BinaryOperator::GreaterEqual => self.types.insert( |
| 2286 |
Type { |
| 2287 |
name: None, |
| 2288 |
inner: TypeInner::Vector { |
| 2289 |
size, |
| 2290 |
scalar: crate::Scalar::BOOL, |
| 2291 |
}, |
| 2292 |
}, |
| 2293 |
span, |
| 2294 |
), |
| 2295 |
|
| 2296 |
// Other operators produce the same type as their left |
| 2297 |
// operand. |
| 2298 |
BinaryOperator::Add |
| 2299 |
| BinaryOperator::Subtract |
| 2300 |
| BinaryOperator::Multiply |
| 2301 |
| BinaryOperator::Divide |
| 2302 |
| BinaryOperator::Modulo |
| 2303 |
| BinaryOperator::And |
| 2304 |
| BinaryOperator::ExclusiveOr |
| 2305 |
| BinaryOperator::InclusiveOr |
| 2306 |
| BinaryOperator::ShiftLeft |
| 2307 |
| BinaryOperator::ShiftRight => left_ty, |
| 2308 |
|
| 2309 |
BinaryOperator::LogicalAnd | BinaryOperator::LogicalOr => { |
| 2310 |
// Not supported on vectors |
| 2311 |
return Err(ConstantEvaluatorError::InvalidBinaryOpArgs); |
| 2312 |
} |
| 2313 |
}; |
| 2314 |
|
| 2315 |
let components = components |
| 2316 |
.iter() |
| 2317 |
.map(|&(left, right)| self.binary_op(op, left, right, span)) |
| 2318 |
.collect::<Result<Vec<_>, _>>()?; |
| 2319 |
|
| 2320 |
Ok(Expression::Compose { ty, components }) |
| 2321 |
} |
| 2322 |
|
| 2323 |
fn relational( |
| 2324 |
&mut self, |
| 2325 |
fun: RelationalFunction, |
| 2326 |
arg: Handle<Expression>, |
| 2327 |
span: Span, |
| 2328 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 2329 |
let arg = self.eval_zero_value_and_splat(arg, span)?; |
| 2330 |
match fun { |
| 2331 |
RelationalFunction::All | RelationalFunction::Any => match self.expressions[arg] { |
| 2332 |
Expression::Literal(Literal::Bool(_)) => Ok(arg), |
| 2333 |
Expression::Compose { ty, ref components } |
| 2334 |
if matches!(self.types[ty].inner, TypeInner::Vector { .. }) => |
| 2335 |
{ |
| 2336 |
let components = |
| 2337 |
crate::proc::flatten_compose(ty, components, self.expressions, self.types) |
| 2338 |
.map(|component| match self.expressions[component] { |
| 2339 |
Expression::Literal(Literal::Bool(val)) => Ok(val), |
| 2340 |
_ => Err(ConstantEvaluatorError::InvalidRelationalArg(fun)), |
| 2341 |
}) |
| 2342 |
.collect::<Result<ArrayVec<bool, { crate::VectorSize::MAX }>, _>>()?; |
| 2343 |
let result = match fun { |
| 2344 |
RelationalFunction::All => components.iter().all(|c| *c), |
| 2345 |
RelationalFunction::Any => components.iter().any(|c| *c), |
| 2346 |
_ => unreachable!(), |
| 2347 |
}; |
| 2348 |
self.register_evaluated_expr(Expression::Literal(Literal::Bool(result)), span) |
| 2349 |
} |
| 2350 |
_ => Err(ConstantEvaluatorError::InvalidRelationalArg(fun)), |
| 2351 |
}, |
| 2352 |
_ => Err(ConstantEvaluatorError::NotImplemented(format!( |
| 2353 |
"{fun:?} built-in function" |
| 2354 |
))), |
| 2355 |
} |
| 2356 |
} |
| 2357 |
|
| 2358 |
/// Deep copy `expr` from `expressions` into `self.expressions`. |
| 2359 |
/// |
| 2360 |
/// Return the root of the new copy. |
| 2361 |
/// |
| 2362 |
/// This is used when we're evaluating expressions in a function's |
| 2363 |
/// expression arena that refer to a constant: we need to copy the |
| 2364 |
/// constant's value into the function's arena so we can operate on it. |
| 2365 |
fn copy_from( |
| 2366 |
&mut self, |
| 2367 |
expr: Handle<Expression>, |
| 2368 |
expressions: &Arena<Expression>, |
| 2369 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 2370 |
let span = expressions.get_span(expr); |
| 2371 |
match expressions[expr] { |
| 2372 |
ref expr @ (Expression::Literal(_) |
| 2373 |
| Expression::Constant(_) |
| 2374 |
| Expression::ZeroValue(_)) => self.register_evaluated_expr(expr.clone(), span), |
| 2375 |
Expression::Compose { ty, ref components } => { |
| 2376 |
let mut components = components.clone(); |
| 2377 |
for component in &mut components { |
| 2378 |
*component = self.copy_from(*component, expressions)?; |
| 2379 |
} |
| 2380 |
self.register_evaluated_expr(Expression::Compose { ty, components }, span) |
| 2381 |
} |
| 2382 |
Expression::Splat { size, value } => { |
| 2383 |
let value = self.copy_from(value, expressions)?; |
| 2384 |
self.register_evaluated_expr(Expression::Splat { size, value }, span) |
| 2385 |
} |
| 2386 |
_ => { |
| 2387 |
log::debug!("copy_from: SubexpressionsAreNotConstant"); |
| 2388 |
Err(ConstantEvaluatorError::SubexpressionsAreNotConstant) |
| 2389 |
} |
| 2390 |
} |
| 2391 |
} |
| 2392 |
|
| 2393 |
/// Returns the total number of components, after flattening, of a vector compose expression. |
| 2394 |
fn vector_compose_flattened_size( |
| 2395 |
&self, |
| 2396 |
components: &[Handle<Expression>], |
| 2397 |
) -> Result<usize, ConstantEvaluatorError> { |
| 2398 |
components |
| 2399 |
.iter() |
| 2400 |
.try_fold(0, |acc, c| -> Result<_, ConstantEvaluatorError> { |
| 2401 |
let size = match *self.resolve_type(*c)?.inner_with(self.types) { |
| 2402 |
TypeInner::Scalar(_) => 1, |
| 2403 |
// We trust that the vector size of `component` is correct, |
| 2404 |
// as it will have already been validated when `component` |
| 2405 |
// was registered. |
| 2406 |
TypeInner::Vector { size, .. } => size as usize, |
| 2407 |
_ => return Err(ConstantEvaluatorError::InvalidVectorComposeComponent), |
| 2408 |
}; |
| 2409 |
Ok(acc + size) |
| 2410 |
}) |
| 2411 |
} |
| 2412 |
|
| 2413 |
fn register_evaluated_expr( |
| 2414 |
&mut self, |
| 2415 |
expr: Expression, |
| 2416 |
span: Span, |
| 2417 |
) -> Result<Handle<Expression>, ConstantEvaluatorError> { |
| 2418 |
// It suffices to only check_literal_value() for `Literal` expressions, |
| 2419 |
// since we only register one expression at a time, `Compose` |
| 2420 |
// expressions can only refer to other expressions, and `ZeroValue` |
| 2421 |
// expressions are always okay. |
| 2422 |
if let Expression::Literal(literal) = expr { |
| 2423 |
crate::valid::check_literal_value(literal)?; |
| 2424 |
} |
| 2425 |
|
| 2426 |
// Ensure vector composes contain the correct number of components. We |
| 2427 |
// do so here when each compose is registered to avoid having to deal |
| 2428 |
// with the mess each time the compose is used in another expression. |
| 2429 |
if let Expression::Compose { ty, ref components } = expr { |
| 2430 |
if let TypeInner::Vector { size, scalar: _ } = self.types[ty].inner { |
| 2431 |
let expected = size as usize; |
| 2432 |
let actual = self.vector_compose_flattened_size(components)?; |
| 2433 |
if expected != actual { |
| 2434 |
return Err(ConstantEvaluatorError::InvalidVectorComposeLength { |
| 2435 |
expected, |
| 2436 |
actual, |
| 2437 |
}); |
| 2438 |
} |
| 2439 |
} |
| 2440 |
} |
| 2441 |
|
| 2442 |
Ok(self.append_expr(expr, span, ExpressionKind::Const)) |
| 2443 |
} |
| 2444 |
|
| 2445 |
fn append_expr( |
| 2446 |
&mut self, |
| 2447 |
expr: Expression, |
| 2448 |
span: Span, |
| 2449 |
expr_type: ExpressionKind, |
| 2450 |
) -> Handle<Expression> { |
| 2451 |
let h = match self.behavior { |
| 2452 |
Behavior::Wgsl( |
| 2453 |
WgslRestrictions::Runtime(ref mut function_local_data) |
| 2454 |
| WgslRestrictions::Const(Some(ref mut function_local_data)), |
| 2455 |
) |
| 2456 |
| Behavior::Glsl(GlslRestrictions::Runtime(ref mut function_local_data)) => { |
| 2457 |
let is_running = function_local_data.emitter.is_running(); |
| 2458 |
let needs_pre_emit = expr.needs_pre_emit(); |
| 2459 |
if is_running && needs_pre_emit { |
| 2460 |
function_local_data |
| 2461 |
.block |
| 2462 |
.extend(function_local_data.emitter.finish(self.expressions)); |
| 2463 |
let h = self.expressions.append(expr, span); |
| 2464 |
function_local_data.emitter.start(self.expressions); |
| 2465 |
h |
| 2466 |
} else { |
| 2467 |
self.expressions.append(expr, span) |
| 2468 |
} |
| 2469 |
} |
| 2470 |
_ => self.expressions.append(expr, span), |
| 2471 |
}; |
| 2472 |
self.expression_kind_tracker.insert(h, expr_type); |
| 2473 |
h |
| 2474 |
} |
| 2475 |
|
| 2476 |
fn resolve_type( |
| 2477 |
&self, |
| 2478 |
expr: Handle<Expression>, |
| 2479 |
) -> Result<crate::proc::TypeResolution, ConstantEvaluatorError> { |
| 2480 |
use crate::proc::TypeResolution as Tr; |
| 2481 |
use crate::Expression as Ex; |
| 2482 |
let resolution = match self.expressions[expr] { |
| 2483 |
Ex::Literal(ref literal) => Tr::Value(literal.ty_inner()), |
| 2484 |
Ex::Constant(c) => Tr::Handle(self.constants[c].ty), |
| 2485 |
Ex::ZeroValue(ty) | Ex::Compose { ty, .. } => Tr::Handle(ty), |
| 2486 |
Ex::Splat { size, value } => { |
| 2487 |
let Tr::Value(TypeInner::Scalar(scalar)) = self.resolve_type(value)? else { |
| 2488 |
return Err(ConstantEvaluatorError::SplatScalarOnly); |
| 2489 |
}; |
| 2490 |
Tr::Value(TypeInner::Vector { scalar, size }) |
| 2491 |
} |
| 2492 |
_ => { |
| 2493 |
log::debug!("resolve_type: SubexpressionsAreNotConstant"); |
| 2494 |
return Err(ConstantEvaluatorError::SubexpressionsAreNotConstant); |
| 2495 |
} |
| 2496 |
}; |
| 2497 |
|
| 2498 |
Ok(resolution) |
| 2499 |
} |
| 2500 |
} |
| 2501 |
|
| 2502 |
fn first_trailing_bit(concrete_int: ConcreteInt<1>) -> ConcreteInt<1> { |
| 2503 |
// NOTE: Bit indices for this built-in start at 0 at the "right" (or LSB). For example, a value |
| 2504 |
// of 1 means the least significant bit is set. Therefore, an input of `0x[80 00…]` would |
| 2505 |
// return a right-to-left bit index of 0. |
| 2506 |
let trailing_zeros_to_bit_idx = |e: u32| -> u32 { |
| 2507 |
match e { |
| 2508 |
idx @ 0..=31 => idx, |
| 2509 |
32 => u32::MAX, |
| 2510 |
_ => unreachable!(), |
| 2511 |
} |
| 2512 |
}; |
| 2513 |
match concrete_int { |
| 2514 |
ConcreteInt::U32([e]) => ConcreteInt::U32([trailing_zeros_to_bit_idx(e.trailing_zeros())]), |
| 2515 |
ConcreteInt::I32([e]) => { |
| 2516 |
ConcreteInt::I32([trailing_zeros_to_bit_idx(e.trailing_zeros()) as i32]) |
| 2517 |
} |
| 2518 |
} |
| 2519 |
} |
| 2520 |
|
| 2521 |
#[test] |
| 2522 |
fn first_trailing_bit_smoke() { |
| 2523 |
assert_eq!( |
| 2524 |
first_trailing_bit(ConcreteInt::I32([0])), |
| 2525 |
ConcreteInt::I32([-1]) |
| 2526 |
); |
| 2527 |
assert_eq!( |
| 2528 |
first_trailing_bit(ConcreteInt::I32([1])), |
| 2529 |
ConcreteInt::I32([0]) |
| 2530 |
); |
| 2531 |
assert_eq!( |
| 2532 |
first_trailing_bit(ConcreteInt::I32([2])), |
| 2533 |
ConcreteInt::I32([1]) |
| 2534 |
); |
| 2535 |
assert_eq!( |
| 2536 |
first_trailing_bit(ConcreteInt::I32([-1])), |
| 2537 |
ConcreteInt::I32([0]), |
| 2538 |
); |
| 2539 |
assert_eq!( |
| 2540 |
first_trailing_bit(ConcreteInt::I32([i32::MIN])), |
| 2541 |
ConcreteInt::I32([31]), |
| 2542 |
); |
| 2543 |
assert_eq!( |
| 2544 |
first_trailing_bit(ConcreteInt::I32([i32::MAX])), |
| 2545 |
ConcreteInt::I32([0]), |
| 2546 |
); |
| 2547 |
for idx in 0..32 { |
| 2548 |
assert_eq!( |
| 2549 |
first_trailing_bit(ConcreteInt::I32([1 << idx])), |
| 2550 |
ConcreteInt::I32([idx]) |
| 2551 |
) |
| 2552 |
} |
| 2553 |
|
| 2554 |
assert_eq!( |
| 2555 |
first_trailing_bit(ConcreteInt::U32([0])), |
| 2556 |
ConcreteInt::U32([u32::MAX]) |
| 2557 |
); |
| 2558 |
assert_eq!( |
| 2559 |
first_trailing_bit(ConcreteInt::U32([1])), |
| 2560 |
ConcreteInt::U32([0]) |
| 2561 |
); |
| 2562 |
assert_eq!( |
| 2563 |
first_trailing_bit(ConcreteInt::U32([2])), |
| 2564 |
ConcreteInt::U32([1]) |
| 2565 |
); |
| 2566 |
assert_eq!( |
| 2567 |
first_trailing_bit(ConcreteInt::U32([1 << 31])), |
| 2568 |
ConcreteInt::U32([31]), |
| 2569 |
); |
| 2570 |
assert_eq!( |
| 2571 |
first_trailing_bit(ConcreteInt::U32([u32::MAX])), |
| 2572 |
ConcreteInt::U32([0]), |
| 2573 |
); |
| 2574 |
for idx in 0..32 { |
| 2575 |
assert_eq!( |
| 2576 |
first_trailing_bit(ConcreteInt::U32([1 << idx])), |
| 2577 |
ConcreteInt::U32([idx]) |
| 2578 |
) |
| 2579 |
} |
| 2580 |
} |
| 2581 |
|
| 2582 |
fn first_leading_bit(concrete_int: ConcreteInt<1>) -> ConcreteInt<1> { |
| 2583 |
// NOTE: Bit indices for this built-in start at 0 at the "right" (or LSB). For example, 1 means |
| 2584 |
// the least significant bit is set. Therefore, an input of 1 would return a right-to-left bit |
| 2585 |
// index of 0. |
| 2586 |
let rtl_to_ltr_bit_idx = |e: u32| -> u32 { |
| 2587 |
match e { |
| 2588 |
idx @ 0..=31 => 31 - idx, |
| 2589 |
32 => u32::MAX, |
| 2590 |
_ => unreachable!(), |
| 2591 |
} |
| 2592 |
}; |
| 2593 |
match concrete_int { |
| 2594 |
ConcreteInt::I32([e]) => ConcreteInt::I32([{ |
| 2595 |
let rtl_bit_index = if e.is_negative() { |
| 2596 |
e.leading_ones() |
| 2597 |
} else { |
| 2598 |
e.leading_zeros() |
| 2599 |
}; |
| 2600 |
rtl_to_ltr_bit_idx(rtl_bit_index) as i32 |
| 2601 |
}]), |
| 2602 |
ConcreteInt::U32([e]) => ConcreteInt::U32([rtl_to_ltr_bit_idx(e.leading_zeros())]), |
| 2603 |
} |
| 2604 |
} |
| 2605 |
|
| 2606 |
#[test] |
| 2607 |
fn first_leading_bit_smoke() { |
| 2608 |
assert_eq!( |
| 2609 |
first_leading_bit(ConcreteInt::I32([-1])), |
| 2610 |
ConcreteInt::I32([-1]) |
| 2611 |
); |
| 2612 |
assert_eq!( |
| 2613 |
first_leading_bit(ConcreteInt::I32([0])), |
| 2614 |
ConcreteInt::I32([-1]) |
| 2615 |
); |
| 2616 |
assert_eq!( |
| 2617 |
first_leading_bit(ConcreteInt::I32([1])), |
| 2618 |
ConcreteInt::I32([0]) |
| 2619 |
); |
| 2620 |
assert_eq!( |
| 2621 |
first_leading_bit(ConcreteInt::I32([-2])), |
| 2622 |
ConcreteInt::I32([0]) |
| 2623 |
); |
| 2624 |
assert_eq!( |
| 2625 |
first_leading_bit(ConcreteInt::I32([1234 + 4567])), |
| 2626 |
ConcreteInt::I32([12]) |
| 2627 |
); |
| 2628 |
assert_eq!( |
| 2629 |
first_leading_bit(ConcreteInt::I32([i32::MAX])), |
| 2630 |
ConcreteInt::I32([30]) |
| 2631 |
); |
| 2632 |
assert_eq!( |
| 2633 |
first_leading_bit(ConcreteInt::I32([i32::MIN])), |
| 2634 |
ConcreteInt::I32([30]) |
| 2635 |
); |
| 2636 |
// NOTE: Ignore the sign bit, which is a separate (above) case. |
| 2637 |
for idx in 0..(32 - 1) { |
| 2638 |
assert_eq!( |
| 2639 |
first_leading_bit(ConcreteInt::I32([1 << idx])), |
| 2640 |
ConcreteInt::I32([idx]) |
| 2641 |
); |
| 2642 |
} |
| 2643 |
for idx in 1..(32 - 1) { |
| 2644 |
assert_eq!( |
| 2645 |
first_leading_bit(ConcreteInt::I32([-(1 << idx)])), |
| 2646 |
ConcreteInt::I32([idx - 1]) |
| 2647 |
); |
| 2648 |
} |
| 2649 |
|
| 2650 |
assert_eq!( |
| 2651 |
first_leading_bit(ConcreteInt::U32([0])), |
| 2652 |
ConcreteInt::U32([u32::MAX]) |
| 2653 |
); |
| 2654 |
assert_eq!( |
| 2655 |
first_leading_bit(ConcreteInt::U32([1])), |
| 2656 |
ConcreteInt::U32([0]) |
| 2657 |
); |
| 2658 |
assert_eq!( |
| 2659 |
first_leading_bit(ConcreteInt::U32([u32::MAX])), |
| 2660 |
ConcreteInt::U32([31]) |
| 2661 |
); |
| 2662 |
for idx in 0..32 { |
| 2663 |
assert_eq!( |
| 2664 |
first_leading_bit(ConcreteInt::U32([1 << idx])), |
| 2665 |
ConcreteInt::U32([idx]) |
| 2666 |
) |
| 2667 |
} |
| 2668 |
} |
| 2669 |
|
| 2670 |
/// Trait for conversions of abstract values to concrete types. |
| 2671 |
trait TryFromAbstract<T>: Sized { |
| 2672 |
/// Convert an abstract literal `value` to `Self`. |
| 2673 |
/// |
| 2674 |
/// Since Naga's `AbstractInt` and `AbstractFloat` exist to support |
| 2675 |
/// WGSL, we follow WGSL's conversion rules here: |
| 2676 |
/// |
| 2677 |
/// - WGSL §6.1.2. Conversion Rank says that automatic conversions |
| 2678 |
/// from `AbstractInt` to an integer type are either lossless or an |
| 2679 |
/// error. |
| 2680 |
/// |
| 2681 |
/// - WGSL §15.7.6 Floating Point Conversion says that conversions |
| 2682 |
/// to floating point in constant expressions and override |
| 2683 |
/// expressions are errors if the value is out of range for the |
| 2684 |
/// destination type, but rounding is okay. |
| 2685 |
/// |
| 2686 |
/// - WGSL §17.1.2 i32()/u32() constructors treat AbstractFloat as any |
| 2687 |
/// other floating point type, following the scalar floating point to |
| 2688 |
/// integral conversion algorithm (§15.7.6). There is no automatic |
| 2689 |
/// conversion from AbstractFloat to integer types. |
| 2690 |
/// |
| 2691 |
/// [`AbstractInt`]: crate::Literal::AbstractInt |
| 2692 |
/// [`Float`]: crate::Literal::Float |
| 2693 |
fn try_from_abstract(value: T) -> Result<Self, ConstantEvaluatorError>; |
| 2694 |
} |
| 2695 |
|
| 2696 |
impl TryFromAbstract<i64> for i32 { |
| 2697 |
fn try_from_abstract(value: i64) -> Result<i32, ConstantEvaluatorError> { |
| 2698 |
i32::try_from(value).map_err(|_| ConstantEvaluatorError::AutomaticConversionLossy { |
| 2699 |
value: format!("{value:?}"), |
| 2700 |
to_type: "i32", |
| 2701 |
}) |
| 2702 |
} |
| 2703 |
} |
| 2704 |
|
| 2705 |
impl TryFromAbstract<i64> for u32 { |
| 2706 |
fn try_from_abstract(value: i64) -> Result<u32, ConstantEvaluatorError> { |
| 2707 |
u32::try_from(value).map_err(|_| ConstantEvaluatorError::AutomaticConversionLossy { |
| 2708 |
value: format!("{value:?}"), |
| 2709 |
to_type: "u32", |
| 2710 |
}) |
| 2711 |
} |
| 2712 |
} |
| 2713 |
|
| 2714 |
impl TryFromAbstract<i64> for u64 { |
| 2715 |
fn try_from_abstract(value: i64) -> Result<u64, ConstantEvaluatorError> { |
| 2716 |
u64::try_from(value).map_err(|_| ConstantEvaluatorError::AutomaticConversionLossy { |
| 2717 |
value: format!("{value:?}"), |
| 2718 |
to_type: "u64", |
| 2719 |
}) |
| 2720 |
} |
| 2721 |
} |
| 2722 |
|
| 2723 |
impl TryFromAbstract<i64> for i64 { |
| 2724 |
fn try_from_abstract(value: i64) -> Result<i64, ConstantEvaluatorError> { |
| 2725 |
Ok(value) |
| 2726 |
} |
| 2727 |
} |
| 2728 |
|
| 2729 |
impl TryFromAbstract<i64> for f32 { |
| 2730 |
fn try_from_abstract(value: i64) -> Result<Self, ConstantEvaluatorError> { |
| 2731 |
let f = value as f32; |
| 2732 |
// The range of `i64` is roughly ±18 × 10¹⁸, whereas the range of |
| 2733 |
// `f32` is roughly ±3.4 × 10³⁸, so there's no opportunity for |
| 2734 |
// overflow here. |
| 2735 |
Ok(f) |
| 2736 |
} |
| 2737 |
} |
| 2738 |
|
| 2739 |
impl TryFromAbstract<f64> for f32 { |
| 2740 |
fn try_from_abstract(value: f64) -> Result<f32, ConstantEvaluatorError> { |
| 2741 |
let f = value as f32; |
| 2742 |
if f.is_infinite() { |
| 2743 |
return Err(ConstantEvaluatorError::AutomaticConversionLossy { |
| 2744 |
value: format!("{value:?}"), |
| 2745 |
to_type: "f32", |
| 2746 |
}); |
| 2747 |
} |
| 2748 |
Ok(f) |
| 2749 |
} |
| 2750 |
} |
| 2751 |
|
| 2752 |
impl TryFromAbstract<i64> for f64 { |
| 2753 |
fn try_from_abstract(value: i64) -> Result<Self, ConstantEvaluatorError> { |
| 2754 |
let f = value as f64; |
| 2755 |
// The range of `i64` is roughly ±18 × 10¹⁸, whereas the range of |
| 2756 |
// `f64` is roughly ±1.8 × 10³⁰⁸, so there's no opportunity for |
| 2757 |
// overflow here. |
| 2758 |
Ok(f) |
| 2759 |
} |
| 2760 |
} |
| 2761 |
|
| 2762 |
impl TryFromAbstract<f64> for f64 { |
| 2763 |
fn try_from_abstract(value: f64) -> Result<f64, ConstantEvaluatorError> { |
| 2764 |
Ok(value) |
| 2765 |
} |
| 2766 |
} |
| 2767 |
|
| 2768 |
impl TryFromAbstract<f64> for i32 { |
| 2769 |
fn try_from_abstract(value: f64) -> Result<Self, ConstantEvaluatorError> { |
| 2770 |
// https://www.w3.org/TR/WGSL/#floating-point-conversion |
| 2771 |
// To convert a floating point scalar value X to an integer scalar type T: |
| 2772 |
// * If X is a NaN, the result is an indeterminate value in T. |
| 2773 |
// * If X is exactly representable in the target type T, then the |
| 2774 |
// result is that value. |
| 2775 |
// * Otherwise, the result is the value in T closest to truncate(X) and |
| 2776 |
// also exactly representable in the original floating point type. |
| 2777 |
// |
| 2778 |
// A rust cast satisfies these requirements apart from "the result |
| 2779 |
// is... exactly representable in the original floating point type". |
| 2780 |
// However, i32::MIN and i32::MAX are exactly representable by f64, so |
| 2781 |
// we're all good. |
| 2782 |
Ok(value as i32) |
| 2783 |
} |
| 2784 |
} |
| 2785 |
|
| 2786 |
impl TryFromAbstract<f64> for u32 { |
| 2787 |
fn try_from_abstract(value: f64) -> Result<Self, ConstantEvaluatorError> { |
| 2788 |
// As above, u32::MIN and u32::MAX are exactly representable by f64, |
| 2789 |
// so a simple rust cast is sufficient. |
| 2790 |
Ok(value as u32) |
| 2791 |
} |
| 2792 |
} |
| 2793 |
|
| 2794 |
impl TryFromAbstract<f64> for i64 { |
| 2795 |
fn try_from_abstract(value: f64) -> Result<Self, ConstantEvaluatorError> { |
| 2796 |
// As above, except we clamp to the minimum and maximum values |
| 2797 |
// representable by both f64 and i64. |
| 2798 |
use crate::proc::type_methods::IntFloatLimits; |
| 2799 |
Ok(value.clamp(i64::min_float(), i64::max_float()) as i64) |
| 2800 |
} |
| 2801 |
} |
| 2802 |
|
| 2803 |
impl TryFromAbstract<f64> for u64 { |
| 2804 |
fn try_from_abstract(value: f64) -> Result<Self, ConstantEvaluatorError> { |
| 2805 |
// As above, this time clamping to the minimum and maximum values |
| 2806 |
// representable by both f64 and u64. |
| 2807 |
use crate::proc::type_methods::IntFloatLimits; |
| 2808 |
Ok(value.clamp(u64::min_float(), u64::max_float()) as u64) |
| 2809 |
} |
| 2810 |
} |
| 2811 |
|
| 2812 |
impl TryFromAbstract<f64> for f16 { |
| 2813 |
fn try_from_abstract(value: f64) -> Result<f16, ConstantEvaluatorError> { |
| 2814 |
let f = f16::from_f64(value); |
| 2815 |
if f.is_infinite() { |
| 2816 |
return Err(ConstantEvaluatorError::AutomaticConversionLossy { |
| 2817 |
value: format!("{value:?}"), |
| 2818 |
to_type: "f16", |
| 2819 |
}); |
| 2820 |
} |
| 2821 |
Ok(f) |
| 2822 |
} |
| 2823 |
} |
| 2824 |
|
| 2825 |
impl TryFromAbstract<i64> for f16 { |
| 2826 |
fn try_from_abstract(value: i64) -> Result<f16, ConstantEvaluatorError> { |
| 2827 |
let f = f16::from_i64(value); |
| 2828 |
if f.is_none() { |
| 2829 |
return Err(ConstantEvaluatorError::AutomaticConversionLossy { |
| 2830 |
value: format!("{value:?}"), |
| 2831 |
to_type: "f16", |
| 2832 |
}); |
| 2833 |
} |
| 2834 |
Ok(f.unwrap()) |
| 2835 |
} |
| 2836 |
} |
| 2837 |
|
| 2838 |
fn cross_product<T>(a: [T; 3], b: [T; 3]) -> [T; 3] |
| 2839 |
where |
| 2840 |
T: Copy, |
| 2841 |
T: core::ops::Mul<T, Output = T>, |
| 2842 |
T: core::ops::Sub<T, Output = T>, |
| 2843 |
{ |
| 2844 |
[ |
| 2845 |
a[1] * b[2] - a[2] * b[1], |
| 2846 |
a[2] * b[0] - a[0] * b[2], |
| 2847 |
a[0] * b[1] - a[1] * b[0], |
| 2848 |
] |
| 2849 |
} |
| 2850 |
|
| 2851 |
#[cfg(test)] |
| 2852 |
mod tests { |
| 2853 |
use alloc::{vec, vec::Vec}; |
| 2854 |
|
| 2855 |
use crate::{ |
| 2856 |
Arena, Constant, Expression, Literal, ScalarKind, Type, TypeInner, UnaryOperator, |
| 2857 |
UniqueArena, VectorSize, |
| 2858 |
}; |
| 2859 |
|
| 2860 |
use super::{Behavior, ConstantEvaluator, ExpressionKindTracker, WgslRestrictions}; |
| 2861 |
|
| 2862 |
#[test] |
| 2863 |
fn unary_op() { |
| 2864 |
let mut types = UniqueArena::new(); |
| 2865 |
let mut constants = Arena::new(); |
| 2866 |
let overrides = Arena::new(); |
| 2867 |
let mut global_expressions = Arena::new(); |
| 2868 |
|
| 2869 |
let scalar_ty = types.insert( |
| 2870 |
Type { |
| 2871 |
name: None, |
| 2872 |
inner: TypeInner::Scalar(crate::Scalar::I32), |
| 2873 |
}, |
| 2874 |
Default::default(), |
| 2875 |
); |
| 2876 |
|
| 2877 |
let vec_ty = types.insert( |
| 2878 |
Type { |
| 2879 |
name: None, |
| 2880 |
inner: TypeInner::Vector { |
| 2881 |
size: VectorSize::Bi, |
| 2882 |
scalar: crate::Scalar::I32, |
| 2883 |
}, |
| 2884 |
}, |
| 2885 |
Default::default(), |
| 2886 |
); |
| 2887 |
|
| 2888 |
let h = constants.append( |
| 2889 |
Constant { |
| 2890 |
name: None, |
| 2891 |
ty: scalar_ty, |
| 2892 |
init: global_expressions |
| 2893 |
.append(Expression::Literal(Literal::I32(4)), Default::default()), |
| 2894 |
}, |
| 2895 |
Default::default(), |
| 2896 |
); |
| 2897 |
|
| 2898 |
let h1 = constants.append( |
| 2899 |
Constant { |
| 2900 |
name: None, |
| 2901 |
ty: scalar_ty, |
| 2902 |
init: global_expressions |
| 2903 |
.append(Expression::Literal(Literal::I32(8)), Default::default()), |
| 2904 |
}, |
| 2905 |
Default::default(), |
| 2906 |
); |
| 2907 |
|
| 2908 |
let vec_h = constants.append( |
| 2909 |
Constant { |
| 2910 |
name: None, |
| 2911 |
ty: vec_ty, |
| 2912 |
init: global_expressions.append( |
| 2913 |
Expression::Compose { |
| 2914 |
ty: vec_ty, |
| 2915 |
components: vec![constants[h].init, constants[h1].init], |
| 2916 |
}, |
| 2917 |
Default::default(), |
| 2918 |
), |
| 2919 |
}, |
| 2920 |
Default::default(), |
| 2921 |
); |
| 2922 |
|
| 2923 |
let expr = global_expressions.append(Expression::Constant(h), Default::default()); |
| 2924 |
let expr1 = global_expressions.append(Expression::Constant(vec_h), Default::default()); |
| 2925 |
|
| 2926 |
let expr2 = Expression::Unary { |
| 2927 |
op: UnaryOperator::Negate, |
| 2928 |
expr, |
| 2929 |
}; |
| 2930 |
|
| 2931 |
let expr3 = Expression::Unary { |
| 2932 |
op: UnaryOperator::BitwiseNot, |
| 2933 |
expr, |
| 2934 |
}; |
| 2935 |
|
| 2936 |
let expr4 = Expression::Unary { |
| 2937 |
op: UnaryOperator::BitwiseNot, |
| 2938 |
expr: expr1, |
| 2939 |
}; |
| 2940 |
|
| 2941 |
let expression_kind_tracker = &mut ExpressionKindTracker::from_arena(&global_expressions); |
| 2942 |
let mut solver = ConstantEvaluator { |
| 2943 |
behavior: Behavior::Wgsl(WgslRestrictions::Const(None)), |
| 2944 |
types: &mut types, |
| 2945 |
constants: &constants, |
| 2946 |
overrides: &overrides, |
| 2947 |
expressions: &mut global_expressions, |
| 2948 |
expression_kind_tracker, |
| 2949 |
layouter: &mut crate::proc::Layouter::default(), |
| 2950 |
}; |
| 2951 |
|
| 2952 |
let res1 = solver |
| 2953 |
.try_eval_and_append(expr2, Default::default()) |
| 2954 |
.unwrap(); |
| 2955 |
let res2 = solver |
| 2956 |
.try_eval_and_append(expr3, Default::default()) |
| 2957 |
.unwrap(); |
| 2958 |
let res3 = solver |
| 2959 |
.try_eval_and_append(expr4, Default::default()) |
| 2960 |
.unwrap(); |
| 2961 |
|
| 2962 |
assert_eq!( |
| 2963 |
global_expressions[res1], |
| 2964 |
Expression::Literal(Literal::I32(-4)) |
| 2965 |
); |
| 2966 |
|
| 2967 |
assert_eq!( |
| 2968 |
global_expressions[res2], |
| 2969 |
Expression::Literal(Literal::I32(!4)) |
| 2970 |
); |
| 2971 |
|
| 2972 |
let res3_inner = &global_expressions[res3]; |
| 2973 |
|
| 2974 |
match *res3_inner { |
| 2975 |
Expression::Compose { |
| 2976 |
ref ty, |
| 2977 |
ref components, |
| 2978 |
} => { |
| 2979 |
assert_eq!(*ty, vec_ty); |
| 2980 |
let mut components_iter = components.iter().copied(); |
| 2981 |
assert_eq!( |
| 2982 |
global_expressions[components_iter.next().unwrap()], |
| 2983 |
Expression::Literal(Literal::I32(!4)) |
| 2984 |
); |
| 2985 |
assert_eq!( |
| 2986 |
global_expressions[components_iter.next().unwrap()], |
| 2987 |
Expression::Literal(Literal::I32(!8)) |
| 2988 |
); |
| 2989 |
assert!(components_iter.next().is_none()); |
| 2990 |
} |
| 2991 |
_ => panic!("Expected vector"), |
| 2992 |
} |
| 2993 |
} |
| 2994 |
|
| 2995 |
#[test] |
| 2996 |
fn cast() { |
| 2997 |
let mut types = UniqueArena::new(); |
| 2998 |
let mut constants = Arena::new(); |
| 2999 |
let overrides = Arena::new(); |
| 3000 |
let mut global_expressions = Arena::new(); |
| 3001 |
|
| 3002 |
let scalar_ty = types.insert( |
| 3003 |
Type { |
| 3004 |
name: None, |
| 3005 |
inner: TypeInner::Scalar(crate::Scalar::I32), |
| 3006 |
}, |
| 3007 |
Default::default(), |
| 3008 |
); |
| 3009 |
|
| 3010 |
let h = constants.append( |
| 3011 |
Constant { |
| 3012 |
name: None, |
| 3013 |
ty: scalar_ty, |
| 3014 |
init: global_expressions |
| 3015 |
.append(Expression::Literal(Literal::I32(4)), Default::default()), |
| 3016 |
}, |
| 3017 |
Default::default(), |
| 3018 |
); |
| 3019 |
|
| 3020 |
let expr = global_expressions.append(Expression::Constant(h), Default::default()); |
| 3021 |
|
| 3022 |
let root = Expression::As { |
| 3023 |
expr, |
| 3024 |
kind: ScalarKind::Bool, |
| 3025 |
convert: Some(crate::BOOL_WIDTH), |
| 3026 |
}; |
| 3027 |
|
| 3028 |
let expression_kind_tracker = &mut ExpressionKindTracker::from_arena(&global_expressions); |
| 3029 |
let mut solver = ConstantEvaluator { |
| 3030 |
behavior: Behavior::Wgsl(WgslRestrictions::Const(None)), |
| 3031 |
types: &mut types, |
| 3032 |
constants: &constants, |
| 3033 |
overrides: &overrides, |
| 3034 |
expressions: &mut global_expressions, |
| 3035 |
expression_kind_tracker, |
| 3036 |
layouter: &mut crate::proc::Layouter::default(), |
| 3037 |
}; |
| 3038 |
|
| 3039 |
let res = solver |
| 3040 |
.try_eval_and_append(root, Default::default()) |
| 3041 |
.unwrap(); |
| 3042 |
|
| 3043 |
assert_eq!( |
| 3044 |
global_expressions[res], |
| 3045 |
Expression::Literal(Literal::Bool(true)) |
| 3046 |
); |
| 3047 |
} |
| 3048 |
|
| 3049 |
#[test] |
| 3050 |
fn access() { |
| 3051 |
let mut types = UniqueArena::new(); |
| 3052 |
let mut constants = Arena::new(); |
| 3053 |
let overrides = Arena::new(); |
| 3054 |
let mut global_expressions = Arena::new(); |
| 3055 |
|
| 3056 |
let matrix_ty = types.insert( |
| 3057 |
Type { |
| 3058 |
name: None, |
| 3059 |
inner: TypeInner::Matrix { |
| 3060 |
columns: VectorSize::Bi, |
| 3061 |
rows: VectorSize::Tri, |
| 3062 |
scalar: crate::Scalar::F32, |
| 3063 |
}, |
| 3064 |
}, |
| 3065 |
Default::default(), |
| 3066 |
); |
| 3067 |
|
| 3068 |
let vec_ty = types.insert( |
| 3069 |
Type { |
| 3070 |
name: None, |
| 3071 |
inner: TypeInner::Vector { |
| 3072 |
size: VectorSize::Tri, |
| 3073 |
scalar: crate::Scalar::F32, |
| 3074 |
}, |
| 3075 |
}, |
| 3076 |
Default::default(), |
| 3077 |
); |
| 3078 |
|
| 3079 |
let mut vec1_components = Vec::with_capacity(3); |
| 3080 |
let mut vec2_components = Vec::with_capacity(3); |
| 3081 |
|
| 3082 |
for i in 0..3 { |
| 3083 |
let h = global_expressions.append( |
| 3084 |
Expression::Literal(Literal::F32(i as f32)), |
| 3085 |
Default::default(), |
| 3086 |
); |
| 3087 |
|
| 3088 |
vec1_components.push(h) |
| 3089 |
} |
| 3090 |
|
| 3091 |
for i in 3..6 { |
| 3092 |
let h = global_expressions.append( |
| 3093 |
Expression::Literal(Literal::F32(i as f32)), |
| 3094 |
Default::default(), |
| 3095 |
); |
| 3096 |
|
| 3097 |
vec2_components.push(h) |
| 3098 |
} |
| 3099 |
|
| 3100 |
let vec1 = constants.append( |
| 3101 |
Constant { |
| 3102 |
name: None, |
| 3103 |
ty: vec_ty, |
| 3104 |
init: global_expressions.append( |
| 3105 |
Expression::Compose { |
| 3106 |
ty: vec_ty, |
| 3107 |
components: vec1_components, |
| 3108 |
}, |
| 3109 |
Default::default(), |
| 3110 |
), |
| 3111 |
}, |
| 3112 |
Default::default(), |
| 3113 |
); |
| 3114 |
|
| 3115 |
let vec2 = constants.append( |
| 3116 |
Constant { |
| 3117 |
name: None, |
| 3118 |
ty: vec_ty, |
| 3119 |
init: global_expressions.append( |
| 3120 |
Expression::Compose { |
| 3121 |
ty: vec_ty, |
| 3122 |
components: vec2_components, |
| 3123 |
}, |
| 3124 |
Default::default(), |
| 3125 |
), |
| 3126 |
}, |
| 3127 |
Default::default(), |
| 3128 |
); |
| 3129 |
|
| 3130 |
let h = constants.append( |
| 3131 |
Constant { |
| 3132 |
name: None, |
| 3133 |
ty: matrix_ty, |
| 3134 |
init: global_expressions.append( |
| 3135 |
Expression::Compose { |
| 3136 |
ty: matrix_ty, |
| 3137 |
components: vec![constants[vec1].init, constants[vec2].init], |
| 3138 |
}, |
| 3139 |
Default::default(), |
| 3140 |
), |
| 3141 |
}, |
| 3142 |
Default::default(), |
| 3143 |
); |
| 3144 |
|
| 3145 |
let base = global_expressions.append(Expression::Constant(h), Default::default()); |
| 3146 |
|
| 3147 |
let expression_kind_tracker = &mut ExpressionKindTracker::from_arena(&global_expressions); |
| 3148 |
let mut solver = ConstantEvaluator { |
| 3149 |
behavior: Behavior::Wgsl(WgslRestrictions::Const(None)), |
| 3150 |
types: &mut types, |
| 3151 |
constants: &constants, |
| 3152 |
overrides: &overrides, |
| 3153 |
expressions: &mut global_expressions, |
| 3154 |
expression_kind_tracker, |
| 3155 |
layouter: &mut crate::proc::Layouter::default(), |
| 3156 |
}; |
| 3157 |
|
| 3158 |
let root1 = Expression::AccessIndex { base, index: 1 }; |
| 3159 |
|
| 3160 |
let res1 = solver |
| 3161 |
.try_eval_and_append(root1, Default::default()) |
| 3162 |
.unwrap(); |
| 3163 |
|
| 3164 |
let root2 = Expression::AccessIndex { |
| 3165 |
base: res1, |
| 3166 |
index: 2, |
| 3167 |
}; |
| 3168 |
|
| 3169 |
let res2 = solver |
| 3170 |
.try_eval_and_append(root2, Default::default()) |
| 3171 |
.unwrap(); |
| 3172 |
|
| 3173 |
match global_expressions[res1] { |
| 3174 |
Expression::Compose { |
| 3175 |
ref ty, |
| 3176 |
ref components, |
| 3177 |
} => { |
| 3178 |
assert_eq!(*ty, vec_ty); |
| 3179 |
let mut components_iter = components.iter().copied(); |
| 3180 |
assert_eq!( |
| 3181 |
global_expressions[components_iter.next().unwrap()], |
| 3182 |
Expression::Literal(Literal::F32(3.)) |
| 3183 |
); |
| 3184 |
assert_eq!( |
| 3185 |
global_expressions[components_iter.next().unwrap()], |
| 3186 |
Expression::Literal(Literal::F32(4.)) |
| 3187 |
); |
| 3188 |
assert_eq!( |
| 3189 |
global_expressions[components_iter.next().unwrap()], |
| 3190 |
Expression::Literal(Literal::F32(5.)) |
| 3191 |
); |
| 3192 |
assert!(components_iter.next().is_none()); |
| 3193 |
} |
| 3194 |
_ => panic!("Expected vector"), |
| 3195 |
} |
| 3196 |
|
| 3197 |
assert_eq!( |
| 3198 |
global_expressions[res2], |
| 3199 |
Expression::Literal(Literal::F32(5.)) |
| 3200 |
); |
| 3201 |
} |
| 3202 |
|
| 3203 |
#[test] |
| 3204 |
fn compose_of_constants() { |
| 3205 |
let mut types = UniqueArena::new(); |
| 3206 |
let mut constants = Arena::new(); |
| 3207 |
let overrides = Arena::new(); |
| 3208 |
let mut global_expressions = Arena::new(); |
| 3209 |
|
| 3210 |
let i32_ty = types.insert( |
| 3211 |
Type { |
| 3212 |
name: None, |
| 3213 |
inner: TypeInner::Scalar(crate::Scalar::I32), |
| 3214 |
}, |
| 3215 |
Default::default(), |
| 3216 |
); |
| 3217 |
|
| 3218 |
let vec2_i32_ty = types.insert( |
| 3219 |
Type { |
| 3220 |
name: None, |
| 3221 |
inner: TypeInner::Vector { |
| 3222 |
size: VectorSize::Bi, |
| 3223 |
scalar: crate::Scalar::I32, |
| 3224 |
}, |
| 3225 |
}, |
| 3226 |
Default::default(), |
| 3227 |
); |
| 3228 |
|
| 3229 |
let h = constants.append( |
| 3230 |
Constant { |
| 3231 |
name: None, |
| 3232 |
ty: i32_ty, |
| 3233 |
init: global_expressions |
| 3234 |
.append(Expression::Literal(Literal::I32(4)), Default::default()), |
| 3235 |
}, |
| 3236 |
Default::default(), |
| 3237 |
); |
| 3238 |
|
| 3239 |
let h_expr = global_expressions.append(Expression::Constant(h), Default::default()); |
| 3240 |
|
| 3241 |
let expression_kind_tracker = &mut ExpressionKindTracker::from_arena(&global_expressions); |
| 3242 |
let mut solver = ConstantEvaluator { |
| 3243 |
behavior: Behavior::Wgsl(WgslRestrictions::Const(None)), |
| 3244 |
types: &mut types, |
| 3245 |
constants: &constants, |
| 3246 |
overrides: &overrides, |
| 3247 |
expressions: &mut global_expressions, |
| 3248 |
expression_kind_tracker, |
| 3249 |
layouter: &mut crate::proc::Layouter::default(), |
| 3250 |
}; |
| 3251 |
|
| 3252 |
let solved_compose = solver |
| 3253 |
.try_eval_and_append( |
| 3254 |
Expression::Compose { |
| 3255 |
ty: vec2_i32_ty, |
| 3256 |
components: vec![h_expr, h_expr], |
| 3257 |
}, |
| 3258 |
Default::default(), |
| 3259 |
) |
| 3260 |
.unwrap(); |
| 3261 |
let solved_negate = solver |
| 3262 |
.try_eval_and_append( |
| 3263 |
Expression::Unary { |
| 3264 |
op: UnaryOperator::Negate, |
| 3265 |
expr: solved_compose, |
| 3266 |
}, |
| 3267 |
Default::default(), |
| 3268 |
) |
| 3269 |
.unwrap(); |
| 3270 |
|
| 3271 |
let pass = match global_expressions[solved_negate] { |
| 3272 |
Expression::Compose { ty, ref components } => { |
| 3273 |
ty == vec2_i32_ty |
| 3274 |
&& components.iter().all(|&component| { |
| 3275 |
let component = &global_expressions[component]; |
| 3276 |
matches!(*component, Expression::Literal(Literal::I32(-4))) |
| 3277 |
}) |
| 3278 |
} |
| 3279 |
_ => false, |
| 3280 |
}; |
| 3281 |
if !pass { |
| 3282 |
panic!("unexpected evaluation result") |
| 3283 |
} |
| 3284 |
} |
| 3285 |
|
| 3286 |
#[test] |
| 3287 |
fn splat_of_constant() { |
| 3288 |
let mut types = UniqueArena::new(); |
| 3289 |
let mut constants = Arena::new(); |
| 3290 |
let overrides = Arena::new(); |
| 3291 |
let mut global_expressions = Arena::new(); |
| 3292 |
|
| 3293 |
let i32_ty = types.insert( |
| 3294 |
Type { |
| 3295 |
name: None, |
| 3296 |
inner: TypeInner::Scalar(crate::Scalar::I32), |
| 3297 |
}, |
| 3298 |
Default::default(), |
| 3299 |
); |
| 3300 |
|
| 3301 |
let vec2_i32_ty = types.insert( |
| 3302 |
Type { |
| 3303 |
name: None, |
| 3304 |
inner: TypeInner::Vector { |
| 3305 |
size: VectorSize::Bi, |
| 3306 |
scalar: crate::Scalar::I32, |
| 3307 |
}, |
| 3308 |
}, |
| 3309 |
Default::default(), |
| 3310 |
); |
| 3311 |
|
| 3312 |
let h = constants.append( |
| 3313 |
Constant { |
| 3314 |
name: None, |
| 3315 |
ty: i32_ty, |
| 3316 |
init: global_expressions |
| 3317 |
.append(Expression::Literal(Literal::I32(4)), Default::default()), |
| 3318 |
}, |
| 3319 |
Default::default(), |
| 3320 |
); |
| 3321 |
|
| 3322 |
let h_expr = global_expressions.append(Expression::Constant(h), Default::default()); |
| 3323 |
|
| 3324 |
let expression_kind_tracker = &mut ExpressionKindTracker::from_arena(&global_expressions); |
| 3325 |
let mut solver = ConstantEvaluator { |
| 3326 |
behavior: Behavior::Wgsl(WgslRestrictions::Const(None)), |
| 3327 |
types: &mut types, |
| 3328 |
constants: &constants, |
| 3329 |
overrides: &overrides, |
| 3330 |
expressions: &mut global_expressions, |
| 3331 |
expression_kind_tracker, |
| 3332 |
layouter: &mut crate::proc::Layouter::default(), |
| 3333 |
}; |
| 3334 |
|
| 3335 |
let solved_compose = solver |
| 3336 |
.try_eval_and_append( |
| 3337 |
Expression::Splat { |
| 3338 |
size: VectorSize::Bi, |
| 3339 |
value: h_expr, |
| 3340 |
}, |
| 3341 |
Default::default(), |
| 3342 |
) |
| 3343 |
.unwrap(); |
| 3344 |
let solved_negate = solver |
| 3345 |
.try_eval_and_append( |
| 3346 |
Expression::Unary { |
| 3347 |
op: UnaryOperator::Negate, |
| 3348 |
expr: solved_compose, |
| 3349 |
}, |
| 3350 |
Default::default(), |
| 3351 |
) |
| 3352 |
.unwrap(); |
| 3353 |
|
| 3354 |
let pass = match global_expressions[solved_negate] { |
| 3355 |
Expression::Compose { ty, ref components } => { |
| 3356 |
ty == vec2_i32_ty |
| 3357 |
&& components.iter().all(|&component| { |
| 3358 |
let component = &global_expressions[component]; |
| 3359 |
matches!(*component, Expression::Literal(Literal::I32(-4))) |
| 3360 |
}) |
| 3361 |
} |
| 3362 |
_ => false, |
| 3363 |
}; |
| 3364 |
if !pass { |
| 3365 |
panic!("unexpected evaluation result") |
| 3366 |
} |
| 3367 |
} |
| 3368 |
|
| 3369 |
#[test] |
| 3370 |
fn splat_of_zero_value() { |
| 3371 |
let mut types = UniqueArena::new(); |
| 3372 |
let constants = Arena::new(); |
| 3373 |
let overrides = Arena::new(); |
| 3374 |
let mut global_expressions = Arena::new(); |
| 3375 |
|
| 3376 |
let f32_ty = types.insert( |
| 3377 |
Type { |
| 3378 |
name: None, |
| 3379 |
inner: TypeInner::Scalar(crate::Scalar::F32), |
| 3380 |
}, |
| 3381 |
Default::default(), |
| 3382 |
); |
| 3383 |
|
| 3384 |
let vec2_f32_ty = types.insert( |
| 3385 |
Type { |
| 3386 |
name: None, |
| 3387 |
inner: TypeInner::Vector { |
| 3388 |
size: VectorSize::Bi, |
| 3389 |
scalar: crate::Scalar::F32, |
| 3390 |
}, |
| 3391 |
}, |
| 3392 |
Default::default(), |
| 3393 |
); |
| 3394 |
|
| 3395 |
let five = |
| 3396 |
global_expressions.append(Expression::Literal(Literal::F32(5.0)), Default::default()); |
| 3397 |
let five_splat = global_expressions.append( |
| 3398 |
Expression::Splat { |
| 3399 |
size: VectorSize::Bi, |
| 3400 |
value: five, |
| 3401 |
}, |
| 3402 |
Default::default(), |
| 3403 |
); |
| 3404 |
let zero = global_expressions.append(Expression::ZeroValue(f32_ty), Default::default()); |
| 3405 |
let zero_splat = global_expressions.append( |
| 3406 |
Expression::Splat { |
| 3407 |
size: VectorSize::Bi, |
| 3408 |
value: zero, |
| 3409 |
}, |
| 3410 |
Default::default(), |
| 3411 |
); |
| 3412 |
|
| 3413 |
let expression_kind_tracker = &mut ExpressionKindTracker::from_arena(&global_expressions); |
| 3414 |
let mut solver = ConstantEvaluator { |
| 3415 |
behavior: Behavior::Wgsl(WgslRestrictions::Const(None)), |
| 3416 |
types: &mut types, |
| 3417 |
constants: &constants, |
| 3418 |
overrides: &overrides, |
| 3419 |
expressions: &mut global_expressions, |
| 3420 |
expression_kind_tracker, |
| 3421 |
layouter: &mut crate::proc::Layouter::default(), |
| 3422 |
}; |
| 3423 |
|
| 3424 |
let solved_add = solver |
| 3425 |
.try_eval_and_append( |
| 3426 |
Expression::Binary { |
| 3427 |
op: crate::BinaryOperator::Add, |
| 3428 |
left: zero_splat, |
| 3429 |
right: five_splat, |
| 3430 |
}, |
| 3431 |
Default::default(), |
| 3432 |
) |
| 3433 |
.unwrap(); |
| 3434 |
|
| 3435 |
let pass = match global_expressions[solved_add] { |
| 3436 |
Expression::Compose { ty, ref components } => { |
| 3437 |
ty == vec2_f32_ty |
| 3438 |
&& components.iter().all(|&component| { |
| 3439 |
let component = &global_expressions[component]; |
| 3440 |
matches!(*component, Expression::Literal(Literal::F32(5.0))) |
| 3441 |
}) |
| 3442 |
} |
| 3443 |
_ => false, |
| 3444 |
}; |
| 3445 |
if !pass { |
| 3446 |
panic!("unexpected evaluation result") |
| 3447 |
} |
| 3448 |
} |
| 3449 |
} |