| 1 |
mod expressions; |
| 2 |
mod functions; |
| 3 |
mod handle_set_map; |
| 4 |
mod statements; |
| 5 |
mod types; |
| 6 |
|
| 7 |
use alloc::vec::Vec; |
| 8 |
|
| 9 |
use crate::arena::HandleSet; |
| 10 |
use crate::{arena, compact::functions::FunctionTracer}; |
| 11 |
use handle_set_map::HandleMap; |
| 12 |
|
| 13 |
#[cfg(test)] |
| 14 |
use alloc::{format, string::ToString}; |
| 15 |
|
| 16 |
/// Remove unused types, expressions, and constants from `module`. |
| 17 |
/// |
| 18 |
/// Assume that the following are used by definition: |
| 19 |
/// - global variables |
| 20 |
/// - named constants and overrides |
| 21 |
/// - special types |
| 22 |
/// - functions and entry points, and within those: |
| 23 |
/// - arguments |
| 24 |
/// - local variables |
| 25 |
/// - named expressions |
| 26 |
/// |
| 27 |
/// Given those assumptions, determine which types, constants, |
| 28 |
/// overrides, and expressions (both function-local and global |
| 29 |
/// constant expressions) are actually used, and remove the rest, |
| 30 |
/// adjusting all handles as necessary. The result should always be a |
| 31 |
/// module functionally identical to the original. |
| 32 |
/// |
| 33 |
/// This may be useful to apply to modules generated in the snapshot |
| 34 |
/// tests. Our backends often generate temporary names based on handle |
| 35 |
/// indices, which means that adding or removing unused arena entries |
| 36 |
/// can affect the output even though they have no semantic effect. |
| 37 |
/// Such meaningless changes add noise to snapshot diffs, making |
| 38 |
/// accurate patch review difficult. Compacting the modules before |
| 39 |
/// generating snapshots makes the output independent of unused arena |
| 40 |
/// entries. |
| 41 |
/// |
| 42 |
/// # Panics |
| 43 |
/// |
| 44 |
/// If `module` would not pass validation, this may panic. |
| 45 |
pub fn compact(module: &mut crate::Module) { |
| 46 |
// The trickiest part of compaction is determining what is used and what is |
| 47 |
// not. Once we have computed that correctly, it's easy enough to call |
| 48 |
// `retain_mut` on each arena, drop unused elements, and fix up the handles |
| 49 |
// in what's left. |
| 50 |
// |
| 51 |
// Some arenas' contents are considered used by definition, like |
| 52 |
// `Module::global_variables` and `Module::functions`, so those are never |
| 53 |
// compacted. |
| 54 |
// |
| 55 |
// But for every compactable arena in a `Module`, whether global to the |
| 56 |
// `Module` or local to a function or entry point, the `ModuleTracer` type |
| 57 |
// holds a bitmap indicating which elements of that arena are used. Our task |
| 58 |
// is to populate those bitmaps correctly. |
| 59 |
// |
| 60 |
// First, we mark everything that is considered used by definition, as |
| 61 |
// described in this function's documentation. |
| 62 |
// |
| 63 |
// Since functions and entry points are considered used by definition, we |
| 64 |
// traverse their statement trees, and mark the referents of all handles |
| 65 |
// appearing in those statements as used. |
| 66 |
// |
| 67 |
// Once we've marked which elements of an arena are referred to directly by |
| 68 |
// handles elsewhere (for example, which of a function's expressions are |
| 69 |
// referred to by handles in its body statements), we can mark all the other |
| 70 |
// arena elements that are used indirectly in a single pass, traversing the |
| 71 |
// arena from back to front. Since Naga allows arena elements to refer only |
| 72 |
// to prior elements, we know that by the time we reach an element, all |
| 73 |
// other elements that could possibly refer to it have already been visited. |
| 74 |
// Thus, if the present element has not been marked as used, then it is |
| 75 |
// definitely unused, and compaction can remove it. Otherwise, the element |
| 76 |
// is used and must be retained, so we must mark everything it refers to. |
| 77 |
// |
| 78 |
// The final step is to mark the global expressions and types, which must be |
| 79 |
// traversed simultaneously; see `ModuleTracer::type_expression_tandem`'s |
| 80 |
// documentation for details. |
| 81 |
// |
| 82 |
// # A definition and a rule of thumb |
| 83 |
// |
| 84 |
// In this module, to "trace" something is to mark everything else it refers |
| 85 |
// to as used, on the assumption that the thing itself is used. For example, |
| 86 |
// to trace an `Expression` is to mark its subexpressions as used, as well |
| 87 |
// as any types, constants, overrides, etc. that it refers to. This is what |
| 88 |
// `ExpressionTracer::trace_expression` does. |
| 89 |
// |
| 90 |
// Given that we we want to visit each thing only once (to keep compaction |
| 91 |
// linear in the size of the module), this definition of "trace" implies |
| 92 |
// that things that are not "used by definition" must be marked as used |
| 93 |
// *before* we trace them. |
| 94 |
// |
| 95 |
// Thus, whenever you are marking something as used, it's a good idea to ask |
| 96 |
// yourself how you know that thing will be traced in the future. If you're |
| 97 |
// not sure, then you could be marking it too late to be noticed. The thing |
| 98 |
// itself will be retained by compaction, but since it will not be traced, |
| 99 |
// anything it refers to could be compacted away. |
| 100 |
let mut module_tracer = ModuleTracer::new(module); |
| 101 |
|
| 102 |
// We treat all globals as used by definition. |
| 103 |
log::trace!("tracing global variables"); |
| 104 |
{ |
| 105 |
for (_, global) in module.global_variables.iter() { |
| 106 |
log::trace!("tracing global {:?}", global.name); |
| 107 |
module_tracer.types_used.insert(global.ty); |
| 108 |
if let Some(init) = global.init { |
| 109 |
module_tracer.global_expressions_used.insert(init); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// We treat all special types as used by definition. |
| 115 |
log::trace!("tracing special types"); |
| 116 |
module_tracer.trace_special_types(&module.special_types); |
| 117 |
|
| 118 |
// We treat all named constants as used by definition, unless they have an |
| 119 |
// abstract type as we do not want those reaching the validator. |
| 120 |
log::trace!("tracing named constants"); |
| 121 |
for (handle, constant) in module.constants.iter() { |
| 122 |
if constant.name.is_none() || module.types[constant.ty].inner.is_abstract(&module.types) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
log::trace!("tracing constant {:?}", constant.name.as_ref().unwrap()); |
| 127 |
module_tracer.constants_used.insert(handle); |
| 128 |
module_tracer.types_used.insert(constant.ty); |
| 129 |
module_tracer.global_expressions_used.insert(constant.init); |
| 130 |
} |
| 131 |
|
| 132 |
// We treat all named overrides as used by definition. |
| 133 |
log::trace!("tracing named overrides"); |
| 134 |
for (handle, r#override) in module.overrides.iter() { |
| 135 |
if r#override.name.is_some() { |
| 136 |
log::trace!("tracing override {:?}", r#override.name.as_ref().unwrap()); |
| 137 |
module_tracer.overrides_used.insert(handle); |
| 138 |
module_tracer.types_used.insert(r#override.ty); |
| 139 |
if let Some(init) = r#override.init { |
| 140 |
module_tracer.global_expressions_used.insert(init); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// We assume that all functions are used. |
| 146 |
// |
| 147 |
// Observe which types, constant expressions, constants, and |
| 148 |
// expressions each function uses, and produce maps for each |
| 149 |
// function from pre-compaction to post-compaction expression |
| 150 |
// handles. |
| 151 |
log::trace!("tracing functions"); |
| 152 |
let function_maps: Vec<FunctionMap> = module |
| 153 |
.functions |
| 154 |
.iter() |
| 155 |
.map(|(_, f)| { |
| 156 |
log::trace!("tracing function {:?}", f.name); |
| 157 |
let mut function_tracer = module_tracer.as_function(f); |
| 158 |
function_tracer.trace(); |
| 159 |
FunctionMap::from(function_tracer) |
| 160 |
}) |
| 161 |
.collect(); |
| 162 |
|
| 163 |
// Similarly, observe what each entry point actually uses. |
| 164 |
log::trace!("tracing entry points"); |
| 165 |
let entry_point_maps: Vec<FunctionMap> = module |
| 166 |
.entry_points |
| 167 |
.iter() |
| 168 |
.map(|e| { |
| 169 |
log::trace!("tracing entry point {:?}", e.function.name); |
| 170 |
|
| 171 |
if let Some(sizes) = e.workgroup_size_overrides { |
| 172 |
for size in sizes.iter().filter_map(|x| *x) { |
| 173 |
module_tracer.global_expressions_used.insert(size); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
let mut used = module_tracer.as_function(&e.function); |
| 178 |
used.trace(); |
| 179 |
FunctionMap::from(used) |
| 180 |
}) |
| 181 |
.collect(); |
| 182 |
|
| 183 |
// Treat all named types as used. |
| 184 |
for (handle, ty) in module.types.iter() { |
| 185 |
log::trace!("tracing type {:?}, name {:?}", handle, ty.name); |
| 186 |
if ty.name.is_some() { |
| 187 |
module_tracer.types_used.insert(handle); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
module_tracer.type_expression_tandem(); |
| 192 |
|
| 193 |
// Now that we know what is used and what is never touched, |
| 194 |
// produce maps from the `Handle`s that appear in `module` now to |
| 195 |
// the corresponding `Handle`s that will refer to the same items |
| 196 |
// in the compacted module. |
| 197 |
let module_map = ModuleMap::from(module_tracer); |
| 198 |
|
| 199 |
// Drop unused types from the type arena. |
| 200 |
// |
| 201 |
// `FastIndexSet`s don't have an underlying Vec<T> that we can |
| 202 |
// steal, compact in place, and then rebuild the `FastIndexSet` |
| 203 |
// from. So we have to rebuild the type arena from scratch. |
| 204 |
log::trace!("compacting types"); |
| 205 |
let mut new_types = arena::UniqueArena::new(); |
| 206 |
for (old_handle, mut ty, span) in module.types.drain_all() { |
| 207 |
if let Some(expected_new_handle) = module_map.types.try_adjust(old_handle) { |
| 208 |
module_map.adjust_type(&mut ty); |
| 209 |
let actual_new_handle = new_types.insert(ty, span); |
| 210 |
assert_eq!(actual_new_handle, expected_new_handle); |
| 211 |
} |
| 212 |
} |
| 213 |
module.types = new_types; |
| 214 |
log::trace!("adjusting special types"); |
| 215 |
module_map.adjust_special_types(&mut module.special_types); |
| 216 |
|
| 217 |
// Drop unused constant expressions, reusing existing storage. |
| 218 |
log::trace!("adjusting constant expressions"); |
| 219 |
module.global_expressions.retain_mut(|handle, expr| { |
| 220 |
if module_map.global_expressions.used(handle) { |
| 221 |
module_map.adjust_expression(expr, &module_map.global_expressions); |
| 222 |
true |
| 223 |
} else { |
| 224 |
false |
| 225 |
} |
| 226 |
}); |
| 227 |
|
| 228 |
// Drop unused constants in place, reusing existing storage. |
| 229 |
log::trace!("adjusting constants"); |
| 230 |
module.constants.retain_mut(|handle, constant| { |
| 231 |
if module_map.constants.used(handle) { |
| 232 |
module_map.types.adjust(&mut constant.ty); |
| 233 |
module_map.global_expressions.adjust(&mut constant.init); |
| 234 |
true |
| 235 |
} else { |
| 236 |
false |
| 237 |
} |
| 238 |
}); |
| 239 |
|
| 240 |
// Drop unused overrides in place, reusing existing storage. |
| 241 |
log::trace!("adjusting overrides"); |
| 242 |
module.overrides.retain_mut(|handle, r#override| { |
| 243 |
if module_map.overrides.used(handle) { |
| 244 |
module_map.types.adjust(&mut r#override.ty); |
| 245 |
if let Some(ref mut init) = r#override.init { |
| 246 |
module_map.global_expressions.adjust(init); |
| 247 |
} |
| 248 |
true |
| 249 |
} else { |
| 250 |
false |
| 251 |
} |
| 252 |
}); |
| 253 |
|
| 254 |
// Adjust workgroup_size_overrides |
| 255 |
log::trace!("adjusting workgroup_size_overrides"); |
| 256 |
for e in module.entry_points.iter_mut() { |
| 257 |
if let Some(sizes) = e.workgroup_size_overrides.as_mut() { |
| 258 |
for size in sizes.iter_mut() { |
| 259 |
if let Some(expr) = size.as_mut() { |
| 260 |
module_map.global_expressions.adjust(expr); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// Adjust global variables' types and initializers. |
| 267 |
log::trace!("adjusting global variables"); |
| 268 |
for (_, global) in module.global_variables.iter_mut() { |
| 269 |
log::trace!("adjusting global {:?}", global.name); |
| 270 |
module_map.types.adjust(&mut global.ty); |
| 271 |
if let Some(ref mut init) = global.init { |
| 272 |
module_map.global_expressions.adjust(init); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
// Temporary storage to help us reuse allocations of existing |
| 277 |
// named expression tables. |
| 278 |
let mut reused_named_expressions = crate::NamedExpressions::default(); |
| 279 |
|
| 280 |
// Compact each function. |
| 281 |
for ((_, function), map) in module.functions.iter_mut().zip(function_maps.iter()) { |
| 282 |
log::trace!("compacting function {:?}", function.name); |
| 283 |
map.compact(function, &module_map, &mut reused_named_expressions); |
| 284 |
} |
| 285 |
|
| 286 |
// Compact each entry point. |
| 287 |
for (entry, map) in module.entry_points.iter_mut().zip(entry_point_maps.iter()) { |
| 288 |
log::trace!("compacting entry point {:?}", entry.function.name); |
| 289 |
map.compact( |
| 290 |
&mut entry.function, |
| 291 |
&module_map, |
| 292 |
&mut reused_named_expressions, |
| 293 |
); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
struct ModuleTracer<'module> { |
| 298 |
module: &'module crate::Module, |
| 299 |
types_used: HandleSet<crate::Type>, |
| 300 |
constants_used: HandleSet<crate::Constant>, |
| 301 |
overrides_used: HandleSet<crate::Override>, |
| 302 |
global_expressions_used: HandleSet<crate::Expression>, |
| 303 |
} |
| 304 |
|
| 305 |
impl<'module> ModuleTracer<'module> { |
| 306 |
fn new(module: &'module crate::Module) -> Self { |
| 307 |
Self { |
| 308 |
module, |
| 309 |
types_used: HandleSet::for_arena(&module.types), |
| 310 |
constants_used: HandleSet::for_arena(&module.constants), |
| 311 |
overrides_used: HandleSet::for_arena(&module.overrides), |
| 312 |
global_expressions_used: HandleSet::for_arena(&module.global_expressions), |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
fn trace_special_types(&mut self, special_types: &crate::SpecialTypes) { |
| 317 |
let crate::SpecialTypes { |
| 318 |
ref ray_desc, |
| 319 |
ref ray_intersection, |
| 320 |
ref ray_vertex_return, |
| 321 |
ref predeclared_types, |
| 322 |
} = *special_types; |
| 323 |
|
| 324 |
if let Some(ray_desc) = *ray_desc { |
| 325 |
self.types_used.insert(ray_desc); |
| 326 |
} |
| 327 |
if let Some(ray_intersection) = *ray_intersection { |
| 328 |
self.types_used.insert(ray_intersection); |
| 329 |
} |
| 330 |
if let Some(ray_vertex_return) = *ray_vertex_return { |
| 331 |
self.types_used.insert(ray_vertex_return); |
| 332 |
} |
| 333 |
for (_, &handle) in predeclared_types { |
| 334 |
self.types_used.insert(handle); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/// Traverse types and global expressions in tandem to determine which are used. |
| 339 |
/// |
| 340 |
/// Assuming that all types and global expressions used by other parts of |
| 341 |
/// the module have been added to [`types_used`] and |
| 342 |
/// [`global_expressions_used`], expand those sets to include all types and |
| 343 |
/// global expressions reachable from those. |
| 344 |
/// |
| 345 |
/// [`types_used`]: ModuleTracer::types_used |
| 346 |
/// [`global_expressions_used`]: ModuleTracer::global_expressions_used |
| 347 |
fn type_expression_tandem(&mut self) { |
| 348 |
// For each type T, compute the latest global expression E that T and |
| 349 |
// its predecessors refer to. Given the ordering rules on types and |
| 350 |
// global expressions in valid modules, we can do this with a single |
| 351 |
// forward scan of the type arena. The rules further imply that T can |
| 352 |
// only be referred to by expressions after E. |
| 353 |
let mut max_dep = Vec::with_capacity(self.module.types.len()); |
| 354 |
let mut previous = None; |
| 355 |
for (_handle, ty) in self.module.types.iter() { |
| 356 |
previous = core::cmp::max( |
| 357 |
previous, |
| 358 |
match ty.inner { |
| 359 |
crate::TypeInner::Array { size, .. } |
| 360 |
| crate::TypeInner::BindingArray { size, .. } => match size { |
| 361 |
crate::ArraySize::Constant(_) | crate::ArraySize::Dynamic => None, |
| 362 |
crate::ArraySize::Pending(handle) => self.module.overrides[handle].init, |
| 363 |
}, |
| 364 |
_ => None, |
| 365 |
}, |
| 366 |
); |
| 367 |
max_dep.push(previous); |
| 368 |
} |
| 369 |
|
| 370 |
// Visit types and global expressions from youngest to oldest. |
| 371 |
// |
| 372 |
// The outer loop visits types. Before visiting each type, the inner |
| 373 |
// loop ensures that all global expressions that could possibly refer to |
| 374 |
// it have been visited. And since the inner loop stop at the latest |
| 375 |
// expression that the type could possibly refer to, we know that we |
| 376 |
// have previously visited any types that might refer to each expression |
| 377 |
// we visit. |
| 378 |
// |
| 379 |
// This lets us assume that any type or expression that is *not* marked |
| 380 |
// as used by the time we visit it is genuinely unused, and can be |
| 381 |
// ignored. |
| 382 |
let mut exprs = self.module.global_expressions.iter().rev().peekable(); |
| 383 |
|
| 384 |
for ((ty_handle, ty), dep) in self.module.types.iter().zip(max_dep).rev() { |
| 385 |
while let Some((expr_handle, expr)) = exprs.next_if(|&(h, _)| Some(h) > dep) { |
| 386 |
if self.global_expressions_used.contains(expr_handle) { |
| 387 |
self.as_const_expression().trace_expression(expr); |
| 388 |
} |
| 389 |
} |
| 390 |
if self.types_used.contains(ty_handle) { |
| 391 |
self.as_type().trace_type(ty); |
| 392 |
} |
| 393 |
} |
| 394 |
// Visit any remaining expressions. |
| 395 |
for (expr_handle, expr) in exprs { |
| 396 |
if self.global_expressions_used.contains(expr_handle) { |
| 397 |
self.as_const_expression().trace_expression(expr); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
fn as_type(&mut self) -> types::TypeTracer { |
| 403 |
types::TypeTracer { |
| 404 |
overrides: &self.module.overrides, |
| 405 |
types_used: &mut self.types_used, |
| 406 |
expressions_used: &mut self.global_expressions_used, |
| 407 |
overrides_used: &mut self.overrides_used, |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
fn as_const_expression(&mut self) -> expressions::ExpressionTracer { |
| 412 |
expressions::ExpressionTracer { |
| 413 |
constants: &self.module.constants, |
| 414 |
overrides: &self.module.overrides, |
| 415 |
expressions: &self.module.global_expressions, |
| 416 |
types_used: &mut self.types_used, |
| 417 |
constants_used: &mut self.constants_used, |
| 418 |
expressions_used: &mut self.global_expressions_used, |
| 419 |
overrides_used: &mut self.overrides_used, |
| 420 |
global_expressions_used: None, |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
pub fn as_function<'tracer>( |
| 425 |
&'tracer mut self, |
| 426 |
function: &'tracer crate::Function, |
| 427 |
) -> FunctionTracer<'tracer> { |
| 428 |
FunctionTracer { |
| 429 |
function, |
| 430 |
constants: &self.module.constants, |
| 431 |
overrides: &self.module.overrides, |
| 432 |
types_used: &mut self.types_used, |
| 433 |
constants_used: &mut self.constants_used, |
| 434 |
overrides_used: &mut self.overrides_used, |
| 435 |
global_expressions_used: &mut self.global_expressions_used, |
| 436 |
expressions_used: HandleSet::for_arena(&function.expressions), |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
struct ModuleMap { |
| 442 |
types: HandleMap<crate::Type>, |
| 443 |
constants: HandleMap<crate::Constant>, |
| 444 |
overrides: HandleMap<crate::Override>, |
| 445 |
global_expressions: HandleMap<crate::Expression>, |
| 446 |
} |
| 447 |
|
| 448 |
impl From<ModuleTracer<'_>> for ModuleMap { |
| 449 |
fn from(used: ModuleTracer) -> Self { |
| 450 |
ModuleMap { |
| 451 |
types: HandleMap::from_set(used.types_used), |
| 452 |
constants: HandleMap::from_set(used.constants_used), |
| 453 |
overrides: HandleMap::from_set(used.overrides_used), |
| 454 |
global_expressions: HandleMap::from_set(used.global_expressions_used), |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
impl ModuleMap { |
| 460 |
fn adjust_special_types(&self, special: &mut crate::SpecialTypes) { |
| 461 |
let crate::SpecialTypes { |
| 462 |
ref mut ray_desc, |
| 463 |
ref mut ray_intersection, |
| 464 |
ref mut ray_vertex_return, |
| 465 |
ref mut predeclared_types, |
| 466 |
} = *special; |
| 467 |
|
| 468 |
if let Some(ref mut ray_desc) = *ray_desc { |
| 469 |
self.types.adjust(ray_desc); |
| 470 |
} |
| 471 |
if let Some(ref mut ray_intersection) = *ray_intersection { |
| 472 |
self.types.adjust(ray_intersection); |
| 473 |
} |
| 474 |
|
| 475 |
if let Some(ref mut ray_vertex_return) = *ray_vertex_return { |
| 476 |
self.types.adjust(ray_vertex_return); |
| 477 |
} |
| 478 |
|
| 479 |
for handle in predeclared_types.values_mut() { |
| 480 |
self.types.adjust(handle); |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
struct FunctionMap { |
| 486 |
expressions: HandleMap<crate::Expression>, |
| 487 |
} |
| 488 |
|
| 489 |
impl From<FunctionTracer<'_>> for FunctionMap { |
| 490 |
fn from(used: FunctionTracer) -> Self { |
| 491 |
FunctionMap { |
| 492 |
expressions: HandleMap::from_set(used.expressions_used), |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
#[test] |
| 498 |
fn type_expression_interdependence() { |
| 499 |
let mut module: crate::Module = Default::default(); |
| 500 |
let u32 = module.types.insert( |
| 501 |
crate::Type { |
| 502 |
name: None, |
| 503 |
inner: crate::TypeInner::Scalar(crate::Scalar { |
| 504 |
kind: crate::ScalarKind::Uint, |
| 505 |
width: 4, |
| 506 |
}), |
| 507 |
}, |
| 508 |
crate::Span::default(), |
| 509 |
); |
| 510 |
let expr = module.global_expressions.append( |
| 511 |
crate::Expression::Literal(crate::Literal::U32(0)), |
| 512 |
crate::Span::default(), |
| 513 |
); |
| 514 |
let type_needs_expression = |module: &mut crate::Module, handle| { |
| 515 |
let override_handle = module.overrides.append( |
| 516 |
crate::Override { |
| 517 |
name: None, |
| 518 |
id: None, |
| 519 |
ty: u32, |
| 520 |
init: Some(handle), |
| 521 |
}, |
| 522 |
crate::Span::default(), |
| 523 |
); |
| 524 |
module.types.insert( |
| 525 |
crate::Type { |
| 526 |
name: None, |
| 527 |
inner: crate::TypeInner::Array { |
| 528 |
base: u32, |
| 529 |
size: crate::ArraySize::Pending(override_handle), |
| 530 |
stride: 4, |
| 531 |
}, |
| 532 |
}, |
| 533 |
crate::Span::default(), |
| 534 |
) |
| 535 |
}; |
| 536 |
let expression_needs_type = |module: &mut crate::Module, handle| { |
| 537 |
module |
| 538 |
.global_expressions |
| 539 |
.append(crate::Expression::ZeroValue(handle), crate::Span::default()) |
| 540 |
}; |
| 541 |
let expression_needs_expression = |module: &mut crate::Module, handle| { |
| 542 |
module.global_expressions.append( |
| 543 |
crate::Expression::Load { pointer: handle }, |
| 544 |
crate::Span::default(), |
| 545 |
) |
| 546 |
}; |
| 547 |
let type_needs_type = |module: &mut crate::Module, handle| { |
| 548 |
module.types.insert( |
| 549 |
crate::Type { |
| 550 |
name: None, |
| 551 |
inner: crate::TypeInner::Array { |
| 552 |
base: handle, |
| 553 |
size: crate::ArraySize::Dynamic, |
| 554 |
stride: 0, |
| 555 |
}, |
| 556 |
}, |
| 557 |
crate::Span::default(), |
| 558 |
) |
| 559 |
}; |
| 560 |
let mut type_name_counter = 0; |
| 561 |
let mut type_needed = |module: &mut crate::Module, handle| { |
| 562 |
let name = Some(format!("type{}", type_name_counter)); |
| 563 |
type_name_counter += 1; |
| 564 |
module.types.insert( |
| 565 |
crate::Type { |
| 566 |
name, |
| 567 |
inner: crate::TypeInner::Array { |
| 568 |
base: handle, |
| 569 |
size: crate::ArraySize::Dynamic, |
| 570 |
stride: 0, |
| 571 |
}, |
| 572 |
}, |
| 573 |
crate::Span::default(), |
| 574 |
) |
| 575 |
}; |
| 576 |
let mut override_name_counter = 0; |
| 577 |
let mut expression_needed = |module: &mut crate::Module, handle| { |
| 578 |
let name = Some(format!("override{}", override_name_counter)); |
| 579 |
override_name_counter += 1; |
| 580 |
module.overrides.append( |
| 581 |
crate::Override { |
| 582 |
name, |
| 583 |
id: None, |
| 584 |
ty: u32, |
| 585 |
init: Some(handle), |
| 586 |
}, |
| 587 |
crate::Span::default(), |
| 588 |
) |
| 589 |
}; |
| 590 |
let cmp_modules = |mod0: &crate::Module, mod1: &crate::Module| { |
| 591 |
(mod0.types.iter().collect::<Vec<_>>() == mod1.types.iter().collect::<Vec<_>>()) |
| 592 |
&& (mod0.global_expressions.iter().collect::<Vec<_>>() |
| 593 |
== mod1.global_expressions.iter().collect::<Vec<_>>()) |
| 594 |
}; |
| 595 |
// borrow checker breaks without the tmp variables as of Rust 1.83.0 |
| 596 |
let expr_end = type_needs_expression(&mut module, expr); |
| 597 |
let ty_trace = type_needs_type(&mut module, expr_end); |
| 598 |
let expr_init = expression_needs_type(&mut module, ty_trace); |
| 599 |
expression_needed(&mut module, expr_init); |
| 600 |
let ty_end = expression_needs_type(&mut module, u32); |
| 601 |
let expr_trace = expression_needs_expression(&mut module, ty_end); |
| 602 |
let ty_init = type_needs_expression(&mut module, expr_trace); |
| 603 |
type_needed(&mut module, ty_init); |
| 604 |
let untouched = module.clone(); |
| 605 |
compact(&mut module); |
| 606 |
assert!(cmp_modules(&module, &untouched)); |
| 607 |
let unused_expr = module.global_expressions.append( |
| 608 |
crate::Expression::Literal(crate::Literal::U32(1)), |
| 609 |
crate::Span::default(), |
| 610 |
); |
| 611 |
type_needs_expression(&mut module, unused_expr); |
| 612 |
assert!(!cmp_modules(&module, &untouched)); |
| 613 |
compact(&mut module); |
| 614 |
assert!(cmp_modules(&module, &untouched)); |
| 615 |
} |
| 616 |
|
| 617 |
#[test] |
| 618 |
fn array_length_override() { |
| 619 |
let mut module: crate::Module = Default::default(); |
| 620 |
let ty_bool = module.types.insert( |
| 621 |
crate::Type { |
| 622 |
name: None, |
| 623 |
inner: crate::TypeInner::Scalar(crate::Scalar::BOOL), |
| 624 |
}, |
| 625 |
crate::Span::default(), |
| 626 |
); |
| 627 |
let ty_u32 = module.types.insert( |
| 628 |
crate::Type { |
| 629 |
name: None, |
| 630 |
inner: crate::TypeInner::Scalar(crate::Scalar::U32), |
| 631 |
}, |
| 632 |
crate::Span::default(), |
| 633 |
); |
| 634 |
let one = module.global_expressions.append( |
| 635 |
crate::Expression::Literal(crate::Literal::U32(1)), |
| 636 |
crate::Span::default(), |
| 637 |
); |
| 638 |
let _unused_override = module.overrides.append( |
| 639 |
crate::Override { |
| 640 |
name: None, |
| 641 |
id: Some(40), |
| 642 |
ty: ty_u32, |
| 643 |
init: None, |
| 644 |
}, |
| 645 |
crate::Span::default(), |
| 646 |
); |
| 647 |
let o = module.overrides.append( |
| 648 |
crate::Override { |
| 649 |
name: None, |
| 650 |
id: Some(42), |
| 651 |
ty: ty_u32, |
| 652 |
init: Some(one), |
| 653 |
}, |
| 654 |
crate::Span::default(), |
| 655 |
); |
| 656 |
let _ty_array = module.types.insert( |
| 657 |
crate::Type { |
| 658 |
name: Some("array<bool, o>".to_string()), |
| 659 |
inner: crate::TypeInner::Array { |
| 660 |
base: ty_bool, |
| 661 |
size: crate::ArraySize::Pending(o), |
| 662 |
stride: 4, |
| 663 |
}, |
| 664 |
}, |
| 665 |
crate::Span::default(), |
| 666 |
); |
| 667 |
|
| 668 |
let mut validator = super::valid::Validator::new( |
| 669 |
super::valid::ValidationFlags::all(), |
| 670 |
super::valid::Capabilities::all(), |
| 671 |
); |
| 672 |
|
| 673 |
assert!(validator.validate(&module).is_ok()); |
| 674 |
compact(&mut module); |
| 675 |
assert!(validator.validate(&module).is_ok()); |
| 676 |
} |
| 677 |
|
| 678 |
/// Test mutual references between types and expressions via override |
| 679 |
/// lengths. |
| 680 |
#[test] |
| 681 |
fn array_length_override_mutual() { |
| 682 |
use crate::Expression as Ex; |
| 683 |
use crate::Scalar as Sc; |
| 684 |
use crate::TypeInner as Ti; |
| 685 |
|
| 686 |
let nowhere = crate::Span::default(); |
| 687 |
let mut module = crate::Module::default(); |
| 688 |
let ty_u32 = module.types.insert( |
| 689 |
crate::Type { |
| 690 |
name: None, |
| 691 |
inner: Ti::Scalar(Sc::U32), |
| 692 |
}, |
| 693 |
nowhere, |
| 694 |
); |
| 695 |
|
| 696 |
// This type is only referred to by the override's init |
| 697 |
// expression, so if we visit that too early, this type will be |
| 698 |
// removed incorrectly. |
| 699 |
let ty_i32 = module.types.insert( |
| 700 |
crate::Type { |
| 701 |
name: None, |
| 702 |
inner: Ti::Scalar(Sc::I32), |
| 703 |
}, |
| 704 |
nowhere, |
| 705 |
); |
| 706 |
|
| 707 |
// An override that the other override's init can refer to. |
| 708 |
let first_override = module.overrides.append( |
| 709 |
crate::Override { |
| 710 |
name: None, // so it is not considered used by definition |
| 711 |
id: Some(41), |
| 712 |
ty: ty_i32, |
| 713 |
init: None, |
| 714 |
}, |
| 715 |
nowhere, |
| 716 |
); |
| 717 |
|
| 718 |
// Initializer expression for the override: |
| 719 |
// |
| 720 |
// (first_override + 0) as u32 |
| 721 |
// |
| 722 |
// The `first_override` makes it an override expression; the `0` |
| 723 |
// gets a use of `ty_i32` in there; and the `as` makes it match |
| 724 |
// the type of `second_override` without actually making |
| 725 |
// `second_override` point at `ty_i32` directly. |
| 726 |
let first_override_expr = module |
| 727 |
.global_expressions |
| 728 |
.append(Ex::Override(first_override), nowhere); |
| 729 |
let zero = module |
| 730 |
.global_expressions |
| 731 |
.append(Ex::ZeroValue(ty_i32), nowhere); |
| 732 |
let sum = module.global_expressions.append( |
| 733 |
Ex::Binary { |
| 734 |
op: crate::BinaryOperator::Add, |
| 735 |
left: first_override_expr, |
| 736 |
right: zero, |
| 737 |
}, |
| 738 |
nowhere, |
| 739 |
); |
| 740 |
let init = module.global_expressions.append( |
| 741 |
Ex::As { |
| 742 |
expr: sum, |
| 743 |
kind: crate::ScalarKind::Uint, |
| 744 |
convert: None, |
| 745 |
}, |
| 746 |
nowhere, |
| 747 |
); |
| 748 |
|
| 749 |
// Override that serves as the array's length. |
| 750 |
let second_override = module.overrides.append( |
| 751 |
crate::Override { |
| 752 |
name: None, // so it is not considered used by definition |
| 753 |
id: Some(42), |
| 754 |
ty: ty_u32, |
| 755 |
init: Some(init), |
| 756 |
}, |
| 757 |
nowhere, |
| 758 |
); |
| 759 |
|
| 760 |
// Array type that uses the overload as its length. |
| 761 |
// Since this is named, it is considered used by definition. |
| 762 |
let _ty_array = module.types.insert( |
| 763 |
crate::Type { |
| 764 |
name: Some("delicious_array".to_string()), |
| 765 |
inner: Ti::Array { |
| 766 |
base: ty_u32, |
| 767 |
size: crate::ArraySize::Pending(second_override), |
| 768 |
stride: 4, |
| 769 |
}, |
| 770 |
}, |
| 771 |
nowhere, |
| 772 |
); |
| 773 |
|
| 774 |
let mut validator = super::valid::Validator::new( |
| 775 |
super::valid::ValidationFlags::all(), |
| 776 |
super::valid::Capabilities::all(), |
| 777 |
); |
| 778 |
|
| 779 |
assert!(validator.validate(&module).is_ok()); |
| 780 |
compact(&mut module); |
| 781 |
assert!(validator.validate(&module).is_ok()); |
| 782 |
} |
| 783 |
|
| 784 |
#[test] |
| 785 |
fn array_length_expression() { |
| 786 |
let mut module: crate::Module = Default::default(); |
| 787 |
let ty_u32 = module.types.insert( |
| 788 |
crate::Type { |
| 789 |
name: None, |
| 790 |
inner: crate::TypeInner::Scalar(crate::Scalar::U32), |
| 791 |
}, |
| 792 |
crate::Span::default(), |
| 793 |
); |
| 794 |
let _unused_zero = module.global_expressions.append( |
| 795 |
crate::Expression::Literal(crate::Literal::U32(0)), |
| 796 |
crate::Span::default(), |
| 797 |
); |
| 798 |
let one = module.global_expressions.append( |
| 799 |
crate::Expression::Literal(crate::Literal::U32(1)), |
| 800 |
crate::Span::default(), |
| 801 |
); |
| 802 |
let override_one = module.overrides.append( |
| 803 |
crate::Override { |
| 804 |
name: None, |
| 805 |
id: None, |
| 806 |
ty: ty_u32, |
| 807 |
init: Some(one), |
| 808 |
}, |
| 809 |
crate::Span::default(), |
| 810 |
); |
| 811 |
let _ty_array = module.types.insert( |
| 812 |
crate::Type { |
| 813 |
name: Some("array<u32, 1>".to_string()), |
| 814 |
inner: crate::TypeInner::Array { |
| 815 |
base: ty_u32, |
| 816 |
size: crate::ArraySize::Pending(override_one), |
| 817 |
stride: 4, |
| 818 |
}, |
| 819 |
}, |
| 820 |
crate::Span::default(), |
| 821 |
); |
| 822 |
|
| 823 |
let mut validator = super::valid::Validator::new( |
| 824 |
super::valid::ValidationFlags::all(), |
| 825 |
super::valid::Capabilities::all(), |
| 826 |
); |
| 827 |
|
| 828 |
assert!(validator.validate(&module).is_ok()); |
| 829 |
compact(&mut module); |
| 830 |
assert!(validator.validate(&module).is_ok()); |
| 831 |
} |
| 832 |
|
| 833 |
#[test] |
| 834 |
fn global_expression_override() { |
| 835 |
let mut module: crate::Module = Default::default(); |
| 836 |
let ty_u32 = module.types.insert( |
| 837 |
crate::Type { |
| 838 |
name: None, |
| 839 |
inner: crate::TypeInner::Scalar(crate::Scalar::U32), |
| 840 |
}, |
| 841 |
crate::Span::default(), |
| 842 |
); |
| 843 |
|
| 844 |
// This will only be retained if we trace the initializers |
| 845 |
// of overrides referred to by `Expression::Override` |
| 846 |
// in global expressions. |
| 847 |
let expr1 = module.global_expressions.append( |
| 848 |
crate::Expression::Literal(crate::Literal::U32(1)), |
| 849 |
crate::Span::default(), |
| 850 |
); |
| 851 |
|
| 852 |
// This will only be traced via a global `Expression::Override`. |
| 853 |
let o = module.overrides.append( |
| 854 |
crate::Override { |
| 855 |
name: None, |
| 856 |
id: Some(42), |
| 857 |
ty: ty_u32, |
| 858 |
init: Some(expr1), |
| 859 |
}, |
| 860 |
crate::Span::default(), |
| 861 |
); |
| 862 |
|
| 863 |
// This is retained by _p. |
| 864 |
let expr2 = module |
| 865 |
.global_expressions |
| 866 |
.append(crate::Expression::Override(o), crate::Span::default()); |
| 867 |
|
| 868 |
// Since this is named, it will be retained. |
| 869 |
let _p = module.overrides.append( |
| 870 |
crate::Override { |
| 871 |
name: Some("p".to_string()), |
| 872 |
id: None, |
| 873 |
ty: ty_u32, |
| 874 |
init: Some(expr2), |
| 875 |
}, |
| 876 |
crate::Span::default(), |
| 877 |
); |
| 878 |
|
| 879 |
let mut validator = super::valid::Validator::new( |
| 880 |
super::valid::ValidationFlags::all(), |
| 881 |
super::valid::Capabilities::all(), |
| 882 |
); |
| 883 |
|
| 884 |
assert!(validator.validate(&module).is_ok()); |
| 885 |
compact(&mut module); |
| 886 |
assert!(validator.validate(&module).is_ok()); |
| 887 |
} |
| 888 |
|
| 889 |
#[test] |
| 890 |
fn local_expression_override() { |
| 891 |
let mut module: crate::Module = Default::default(); |
| 892 |
let ty_u32 = module.types.insert( |
| 893 |
crate::Type { |
| 894 |
name: None, |
| 895 |
inner: crate::TypeInner::Scalar(crate::Scalar::U32), |
| 896 |
}, |
| 897 |
crate::Span::default(), |
| 898 |
); |
| 899 |
|
| 900 |
// This will only be retained if we trace the initializers |
| 901 |
// of overrides referred to by `Expression::Override` in a function. |
| 902 |
let expr1 = module.global_expressions.append( |
| 903 |
crate::Expression::Literal(crate::Literal::U32(1)), |
| 904 |
crate::Span::default(), |
| 905 |
); |
| 906 |
|
| 907 |
// This will be removed by compaction. |
| 908 |
let _unused_override = module.overrides.append( |
| 909 |
crate::Override { |
| 910 |
name: None, |
| 911 |
id: Some(41), |
| 912 |
ty: ty_u32, |
| 913 |
init: None, |
| 914 |
}, |
| 915 |
crate::Span::default(), |
| 916 |
); |
| 917 |
|
| 918 |
// This will only be traced via an `Expression::Override` in a function. |
| 919 |
let o = module.overrides.append( |
| 920 |
crate::Override { |
| 921 |
name: None, |
| 922 |
id: Some(42), |
| 923 |
ty: ty_u32, |
| 924 |
init: Some(expr1), |
| 925 |
}, |
| 926 |
crate::Span::default(), |
| 927 |
); |
| 928 |
|
| 929 |
let mut fun = crate::Function { |
| 930 |
result: Some(crate::FunctionResult { |
| 931 |
ty: ty_u32, |
| 932 |
binding: None, |
| 933 |
}), |
| 934 |
..crate::Function::default() |
| 935 |
}; |
| 936 |
|
| 937 |
// This is used by the `Return` statement. |
| 938 |
let o_expr = fun |
| 939 |
.expressions |
| 940 |
.append(crate::Expression::Override(o), crate::Span::default()); |
| 941 |
fun.body.push( |
| 942 |
crate::Statement::Return { |
| 943 |
value: Some(o_expr), |
| 944 |
}, |
| 945 |
crate::Span::default(), |
| 946 |
); |
| 947 |
|
| 948 |
module.functions.append(fun, crate::Span::default()); |
| 949 |
|
| 950 |
let mut validator = super::valid::Validator::new( |
| 951 |
super::valid::ValidationFlags::all(), |
| 952 |
super::valid::Capabilities::all(), |
| 953 |
); |
| 954 |
|
| 955 |
assert!(validator.validate(&module).is_ok()); |
| 956 |
compact(&mut module); |
| 957 |
assert!(validator.validate(&module).is_ok()); |
| 958 |
} |
| 959 |
|
| 960 |
#[test] |
| 961 |
fn unnamed_constant_type() { |
| 962 |
let mut module = crate::Module::default(); |
| 963 |
let nowhere = crate::Span::default(); |
| 964 |
|
| 965 |
// This type is used only by the unnamed constant. |
| 966 |
let ty_u32 = module.types.insert( |
| 967 |
crate::Type { |
| 968 |
name: None, |
| 969 |
inner: crate::TypeInner::Scalar(crate::Scalar::U32), |
| 970 |
}, |
| 971 |
nowhere, |
| 972 |
); |
| 973 |
|
| 974 |
// This type is used by the named constant. |
| 975 |
let ty_vec_u32 = module.types.insert( |
| 976 |
crate::Type { |
| 977 |
name: None, |
| 978 |
inner: crate::TypeInner::Vector { |
| 979 |
size: crate::VectorSize::Bi, |
| 980 |
scalar: crate::Scalar::U32, |
| 981 |
}, |
| 982 |
}, |
| 983 |
nowhere, |
| 984 |
); |
| 985 |
|
| 986 |
let unnamed_init = module |
| 987 |
.global_expressions |
| 988 |
.append(crate::Expression::Literal(crate::Literal::U32(0)), nowhere); |
| 989 |
|
| 990 |
let unnamed_constant = module.constants.append( |
| 991 |
crate::Constant { |
| 992 |
name: None, |
| 993 |
ty: ty_u32, |
| 994 |
init: unnamed_init, |
| 995 |
}, |
| 996 |
nowhere, |
| 997 |
); |
| 998 |
|
| 999 |
// The named constant is initialized using a Splat expression, to |
| 1000 |
// give the named constant a type distinct from the unnamed |
| 1001 |
// constant's. |
| 1002 |
let unnamed_constant_expr = module |
| 1003 |
.global_expressions |
| 1004 |
.append(crate::Expression::Constant(unnamed_constant), nowhere); |
| 1005 |
let named_init = module.global_expressions.append( |
| 1006 |
crate::Expression::Splat { |
| 1007 |
size: crate::VectorSize::Bi, |
| 1008 |
value: unnamed_constant_expr, |
| 1009 |
}, |
| 1010 |
nowhere, |
| 1011 |
); |
| 1012 |
|
| 1013 |
let _named_constant = module.constants.append( |
| 1014 |
crate::Constant { |
| 1015 |
name: Some("totally_named".to_string()), |
| 1016 |
ty: ty_vec_u32, |
| 1017 |
init: named_init, |
| 1018 |
}, |
| 1019 |
nowhere, |
| 1020 |
); |
| 1021 |
|
| 1022 |
let mut validator = super::valid::Validator::new( |
| 1023 |
super::valid::ValidationFlags::all(), |
| 1024 |
super::valid::Capabilities::all(), |
| 1025 |
); |
| 1026 |
|
| 1027 |
assert!(validator.validate(&module).is_ok()); |
| 1028 |
compact(&mut module); |
| 1029 |
assert!(validator.validate(&module).is_ok()); |
| 1030 |
} |
| 1031 |
|
| 1032 |
#[test] |
| 1033 |
fn unnamed_override_type() { |
| 1034 |
let mut module = crate::Module::default(); |
| 1035 |
let nowhere = crate::Span::default(); |
| 1036 |
|
| 1037 |
// This type is used only by the unnamed override. |
| 1038 |
let ty_u32 = module.types.insert( |
| 1039 |
crate::Type { |
| 1040 |
name: None, |
| 1041 |
inner: crate::TypeInner::Scalar(crate::Scalar::U32), |
| 1042 |
}, |
| 1043 |
nowhere, |
| 1044 |
); |
| 1045 |
|
| 1046 |
// This type is used by the named override. |
| 1047 |
let ty_i32 = module.types.insert( |
| 1048 |
crate::Type { |
| 1049 |
name: None, |
| 1050 |
inner: crate::TypeInner::Scalar(crate::Scalar::I32), |
| 1051 |
}, |
| 1052 |
nowhere, |
| 1053 |
); |
| 1054 |
|
| 1055 |
let unnamed_init = module |
| 1056 |
.global_expressions |
| 1057 |
.append(crate::Expression::Literal(crate::Literal::U32(0)), nowhere); |
| 1058 |
|
| 1059 |
let unnamed_override = module.overrides.append( |
| 1060 |
crate::Override { |
| 1061 |
name: None, |
| 1062 |
id: Some(42), |
| 1063 |
ty: ty_u32, |
| 1064 |
init: Some(unnamed_init), |
| 1065 |
}, |
| 1066 |
nowhere, |
| 1067 |
); |
| 1068 |
|
| 1069 |
// The named override is initialized using a Splat expression, to |
| 1070 |
// give the named override a type distinct from the unnamed |
| 1071 |
// override's. |
| 1072 |
let unnamed_override_expr = module |
| 1073 |
.global_expressions |
| 1074 |
.append(crate::Expression::Override(unnamed_override), nowhere); |
| 1075 |
let named_init = module.global_expressions.append( |
| 1076 |
crate::Expression::As { |
| 1077 |
expr: unnamed_override_expr, |
| 1078 |
kind: crate::ScalarKind::Sint, |
| 1079 |
convert: None, |
| 1080 |
}, |
| 1081 |
nowhere, |
| 1082 |
); |
| 1083 |
|
| 1084 |
let _named_override = module.overrides.append( |
| 1085 |
crate::Override { |
| 1086 |
name: Some("totally_named".to_string()), |
| 1087 |
id: None, |
| 1088 |
ty: ty_i32, |
| 1089 |
init: Some(named_init), |
| 1090 |
}, |
| 1091 |
nowhere, |
| 1092 |
); |
| 1093 |
|
| 1094 |
let mut validator = super::valid::Validator::new( |
| 1095 |
super::valid::ValidationFlags::all(), |
| 1096 |
super::valid::Capabilities::all(), |
| 1097 |
); |
| 1098 |
|
| 1099 |
assert!(validator.validate(&module).is_ok()); |
| 1100 |
compact(&mut module); |
| 1101 |
assert!(validator.validate(&module).is_ok()); |
| 1102 |
} |