| 1 |
use alloc::{vec, vec::Vec}; |
| 2 |
|
| 3 |
use super::functions::FunctionTracer; |
| 4 |
use super::FunctionMap; |
| 5 |
use crate::arena::Handle; |
| 6 |
|
| 7 |
impl FunctionTracer<'_> { |
| 8 |
pub fn trace_block(&mut self, block: &[crate::Statement]) { |
| 9 |
let mut worklist: Vec<&[crate::Statement]> = vec![block]; |
| 10 |
while let Some(last) = worklist.pop() { |
| 11 |
for stmt in last { |
| 12 |
use crate::Statement as St; |
| 13 |
match *stmt { |
| 14 |
St::Emit(ref _range) => { |
| 15 |
// If we come across a statement that actually uses an |
| 16 |
// expression in this range, it'll get traced from |
| 17 |
// there. But since evaluating expressions has no |
| 18 |
// effect, we don't need to assume that everything |
| 19 |
// emitted is live. |
| 20 |
} |
| 21 |
St::Block(ref block) => worklist.push(block), |
| 22 |
St::If { |
| 23 |
condition, |
| 24 |
ref accept, |
| 25 |
ref reject, |
| 26 |
} => { |
| 27 |
self.expressions_used.insert(condition); |
| 28 |
worklist.push(accept); |
| 29 |
worklist.push(reject); |
| 30 |
} |
| 31 |
St::Switch { |
| 32 |
selector, |
| 33 |
ref cases, |
| 34 |
} => { |
| 35 |
self.expressions_used.insert(selector); |
| 36 |
for case in cases { |
| 37 |
worklist.push(&case.body); |
| 38 |
} |
| 39 |
} |
| 40 |
St::Loop { |
| 41 |
ref body, |
| 42 |
ref continuing, |
| 43 |
break_if, |
| 44 |
} => { |
| 45 |
if let Some(break_if) = break_if { |
| 46 |
self.expressions_used.insert(break_if); |
| 47 |
} |
| 48 |
worklist.push(body); |
| 49 |
worklist.push(continuing); |
| 50 |
} |
| 51 |
St::Return { value: Some(value) } => { |
| 52 |
self.expressions_used.insert(value); |
| 53 |
} |
| 54 |
St::Store { pointer, value } => { |
| 55 |
self.expressions_used.insert(pointer); |
| 56 |
self.expressions_used.insert(value); |
| 57 |
} |
| 58 |
St::ImageStore { |
| 59 |
image, |
| 60 |
coordinate, |
| 61 |
array_index, |
| 62 |
value, |
| 63 |
} => { |
| 64 |
self.expressions_used.insert(image); |
| 65 |
self.expressions_used.insert(coordinate); |
| 66 |
if let Some(array_index) = array_index { |
| 67 |
self.expressions_used.insert(array_index); |
| 68 |
} |
| 69 |
self.expressions_used.insert(value); |
| 70 |
} |
| 71 |
St::Atomic { |
| 72 |
pointer, |
| 73 |
ref fun, |
| 74 |
value, |
| 75 |
result, |
| 76 |
} => { |
| 77 |
self.expressions_used.insert(pointer); |
| 78 |
self.trace_atomic_function(fun); |
| 79 |
self.expressions_used.insert(value); |
| 80 |
if let Some(result) = result { |
| 81 |
self.expressions_used.insert(result); |
| 82 |
} |
| 83 |
} |
| 84 |
St::ImageAtomic { |
| 85 |
image, |
| 86 |
coordinate, |
| 87 |
array_index, |
| 88 |
fun: _, |
| 89 |
value, |
| 90 |
} => { |
| 91 |
self.expressions_used.insert(image); |
| 92 |
self.expressions_used.insert(coordinate); |
| 93 |
if let Some(array_index) = array_index { |
| 94 |
self.expressions_used.insert(array_index); |
| 95 |
} |
| 96 |
self.expressions_used.insert(value); |
| 97 |
} |
| 98 |
St::WorkGroupUniformLoad { pointer, result } => { |
| 99 |
self.expressions_used.insert(pointer); |
| 100 |
self.expressions_used.insert(result); |
| 101 |
} |
| 102 |
St::Call { |
| 103 |
function: _, |
| 104 |
ref arguments, |
| 105 |
result, |
| 106 |
} => { |
| 107 |
for expr in arguments { |
| 108 |
self.expressions_used.insert(*expr); |
| 109 |
} |
| 110 |
if let Some(result) = result { |
| 111 |
self.expressions_used.insert(result); |
| 112 |
} |
| 113 |
} |
| 114 |
St::RayQuery { query, ref fun } => { |
| 115 |
self.expressions_used.insert(query); |
| 116 |
self.trace_ray_query_function(fun); |
| 117 |
} |
| 118 |
St::SubgroupBallot { result, predicate } => { |
| 119 |
if let Some(predicate) = predicate { |
| 120 |
self.expressions_used.insert(predicate); |
| 121 |
} |
| 122 |
self.expressions_used.insert(result); |
| 123 |
} |
| 124 |
St::SubgroupCollectiveOperation { |
| 125 |
op: _, |
| 126 |
collective_op: _, |
| 127 |
argument, |
| 128 |
result, |
| 129 |
} => { |
| 130 |
self.expressions_used.insert(argument); |
| 131 |
self.expressions_used.insert(result); |
| 132 |
} |
| 133 |
St::SubgroupGather { |
| 134 |
mode, |
| 135 |
argument, |
| 136 |
result, |
| 137 |
} => { |
| 138 |
match mode { |
| 139 |
crate::GatherMode::BroadcastFirst => {} |
| 140 |
crate::GatherMode::Broadcast(index) |
| 141 |
| crate::GatherMode::Shuffle(index) |
| 142 |
| crate::GatherMode::ShuffleDown(index) |
| 143 |
| crate::GatherMode::ShuffleUp(index) |
| 144 |
| crate::GatherMode::ShuffleXor(index) => { |
| 145 |
self.expressions_used.insert(index); |
| 146 |
} |
| 147 |
} |
| 148 |
self.expressions_used.insert(argument); |
| 149 |
self.expressions_used.insert(result); |
| 150 |
} |
| 151 |
|
| 152 |
// Trivial statements. |
| 153 |
St::Break |
| 154 |
| St::Continue |
| 155 |
| St::Kill |
| 156 |
| St::Barrier(_) |
| 157 |
| St::Return { value: None } => {} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
fn trace_atomic_function(&mut self, fun: &crate::AtomicFunction) { |
| 164 |
use crate::AtomicFunction as Af; |
| 165 |
match *fun { |
| 166 |
Af::Exchange { |
| 167 |
compare: Some(expr), |
| 168 |
} => { |
| 169 |
self.expressions_used.insert(expr); |
| 170 |
} |
| 171 |
Af::Exchange { compare: None } |
| 172 |
| Af::Add |
| 173 |
| Af::Subtract |
| 174 |
| Af::And |
| 175 |
| Af::ExclusiveOr |
| 176 |
| Af::InclusiveOr |
| 177 |
| Af::Min |
| 178 |
| Af::Max => {} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
fn trace_ray_query_function(&mut self, fun: &crate::RayQueryFunction) { |
| 183 |
use crate::RayQueryFunction as Qf; |
| 184 |
match *fun { |
| 185 |
Qf::Initialize { |
| 186 |
acceleration_structure, |
| 187 |
descriptor, |
| 188 |
} => { |
| 189 |
self.expressions_used.insert(acceleration_structure); |
| 190 |
self.expressions_used.insert(descriptor); |
| 191 |
} |
| 192 |
Qf::Proceed { result } => { |
| 193 |
self.expressions_used.insert(result); |
| 194 |
} |
| 195 |
Qf::GenerateIntersection { hit_t } => { |
| 196 |
self.expressions_used.insert(hit_t); |
| 197 |
} |
| 198 |
Qf::ConfirmIntersection => {} |
| 199 |
Qf::Terminate => {} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
impl FunctionMap { |
| 205 |
pub fn adjust_body(&self, function: &mut crate::Function) { |
| 206 |
let block = &mut function.body; |
| 207 |
let mut worklist: Vec<&mut [crate::Statement]> = vec![block]; |
| 208 |
let adjust = |handle: &mut Handle<crate::Expression>| { |
| 209 |
self.expressions.adjust(handle); |
| 210 |
}; |
| 211 |
while let Some(last) = worklist.pop() { |
| 212 |
for stmt in last { |
| 213 |
use crate::Statement as St; |
| 214 |
match *stmt { |
| 215 |
St::Emit(ref mut range) => { |
| 216 |
self.expressions.adjust_range(range, &function.expressions); |
| 217 |
} |
| 218 |
St::Block(ref mut block) => worklist.push(block), |
| 219 |
St::If { |
| 220 |
ref mut condition, |
| 221 |
ref mut accept, |
| 222 |
ref mut reject, |
| 223 |
} => { |
| 224 |
adjust(condition); |
| 225 |
worklist.push(accept); |
| 226 |
worklist.push(reject); |
| 227 |
} |
| 228 |
St::Switch { |
| 229 |
ref mut selector, |
| 230 |
ref mut cases, |
| 231 |
} => { |
| 232 |
adjust(selector); |
| 233 |
for case in cases { |
| 234 |
worklist.push(&mut case.body); |
| 235 |
} |
| 236 |
} |
| 237 |
St::Loop { |
| 238 |
ref mut body, |
| 239 |
ref mut continuing, |
| 240 |
ref mut break_if, |
| 241 |
} => { |
| 242 |
if let Some(ref mut break_if) = *break_if { |
| 243 |
adjust(break_if); |
| 244 |
} |
| 245 |
worklist.push(body); |
| 246 |
worklist.push(continuing); |
| 247 |
} |
| 248 |
St::Return { |
| 249 |
value: Some(ref mut value), |
| 250 |
} => adjust(value), |
| 251 |
St::Store { |
| 252 |
ref mut pointer, |
| 253 |
ref mut value, |
| 254 |
} => { |
| 255 |
adjust(pointer); |
| 256 |
adjust(value); |
| 257 |
} |
| 258 |
St::ImageStore { |
| 259 |
ref mut image, |
| 260 |
ref mut coordinate, |
| 261 |
ref mut array_index, |
| 262 |
ref mut value, |
| 263 |
} => { |
| 264 |
adjust(image); |
| 265 |
adjust(coordinate); |
| 266 |
if let Some(ref mut array_index) = *array_index { |
| 267 |
adjust(array_index); |
| 268 |
} |
| 269 |
adjust(value); |
| 270 |
} |
| 271 |
St::Atomic { |
| 272 |
ref mut pointer, |
| 273 |
ref mut fun, |
| 274 |
ref mut value, |
| 275 |
ref mut result, |
| 276 |
} => { |
| 277 |
adjust(pointer); |
| 278 |
self.adjust_atomic_function(fun); |
| 279 |
adjust(value); |
| 280 |
if let Some(ref mut result) = *result { |
| 281 |
adjust(result); |
| 282 |
} |
| 283 |
} |
| 284 |
St::ImageAtomic { |
| 285 |
ref mut image, |
| 286 |
ref mut coordinate, |
| 287 |
ref mut array_index, |
| 288 |
fun: _, |
| 289 |
ref mut value, |
| 290 |
} => { |
| 291 |
adjust(image); |
| 292 |
adjust(coordinate); |
| 293 |
if let Some(ref mut array_index) = *array_index { |
| 294 |
adjust(array_index); |
| 295 |
} |
| 296 |
adjust(value); |
| 297 |
} |
| 298 |
St::WorkGroupUniformLoad { |
| 299 |
ref mut pointer, |
| 300 |
ref mut result, |
| 301 |
} => { |
| 302 |
adjust(pointer); |
| 303 |
adjust(result); |
| 304 |
} |
| 305 |
St::Call { |
| 306 |
function: _, |
| 307 |
ref mut arguments, |
| 308 |
ref mut result, |
| 309 |
} => { |
| 310 |
for expr in arguments { |
| 311 |
adjust(expr); |
| 312 |
} |
| 313 |
if let Some(ref mut result) = *result { |
| 314 |
adjust(result); |
| 315 |
} |
| 316 |
} |
| 317 |
St::RayQuery { |
| 318 |
ref mut query, |
| 319 |
ref mut fun, |
| 320 |
} => { |
| 321 |
adjust(query); |
| 322 |
self.adjust_ray_query_function(fun); |
| 323 |
} |
| 324 |
St::SubgroupBallot { |
| 325 |
ref mut result, |
| 326 |
ref mut predicate, |
| 327 |
} => { |
| 328 |
if let Some(ref mut predicate) = *predicate { |
| 329 |
adjust(predicate); |
| 330 |
} |
| 331 |
adjust(result); |
| 332 |
} |
| 333 |
St::SubgroupCollectiveOperation { |
| 334 |
op: _, |
| 335 |
collective_op: _, |
| 336 |
ref mut argument, |
| 337 |
ref mut result, |
| 338 |
} => { |
| 339 |
adjust(argument); |
| 340 |
adjust(result); |
| 341 |
} |
| 342 |
St::SubgroupGather { |
| 343 |
ref mut mode, |
| 344 |
ref mut argument, |
| 345 |
ref mut result, |
| 346 |
} => { |
| 347 |
match *mode { |
| 348 |
crate::GatherMode::BroadcastFirst => {} |
| 349 |
crate::GatherMode::Broadcast(ref mut index) |
| 350 |
| crate::GatherMode::Shuffle(ref mut index) |
| 351 |
| crate::GatherMode::ShuffleDown(ref mut index) |
| 352 |
| crate::GatherMode::ShuffleUp(ref mut index) |
| 353 |
| crate::GatherMode::ShuffleXor(ref mut index) => adjust(index), |
| 354 |
} |
| 355 |
adjust(argument); |
| 356 |
adjust(result); |
| 357 |
} |
| 358 |
|
| 359 |
// Trivial statements. |
| 360 |
St::Break |
| 361 |
| St::Continue |
| 362 |
| St::Kill |
| 363 |
| St::Barrier(_) |
| 364 |
| St::Return { value: None } => {} |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
fn adjust_atomic_function(&self, fun: &mut crate::AtomicFunction) { |
| 371 |
use crate::AtomicFunction as Af; |
| 372 |
match *fun { |
| 373 |
Af::Exchange { |
| 374 |
compare: Some(ref mut expr), |
| 375 |
} => { |
| 376 |
self.expressions.adjust(expr); |
| 377 |
} |
| 378 |
Af::Exchange { compare: None } |
| 379 |
| Af::Add |
| 380 |
| Af::Subtract |
| 381 |
| Af::And |
| 382 |
| Af::ExclusiveOr |
| 383 |
| Af::InclusiveOr |
| 384 |
| Af::Min |
| 385 |
| Af::Max => {} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
fn adjust_ray_query_function(&self, fun: &mut crate::RayQueryFunction) { |
| 390 |
use crate::RayQueryFunction as Qf; |
| 391 |
match *fun { |
| 392 |
Qf::Initialize { |
| 393 |
ref mut acceleration_structure, |
| 394 |
ref mut descriptor, |
| 395 |
} => { |
| 396 |
self.expressions.adjust(acceleration_structure); |
| 397 |
self.expressions.adjust(descriptor); |
| 398 |
} |
| 399 |
Qf::Proceed { ref mut result } => { |
| 400 |
self.expressions.adjust(result); |
| 401 |
} |
| 402 |
Qf::GenerateIntersection { ref mut hit_t } => { |
| 403 |
self.expressions.adjust(hit_t); |
| 404 |
} |
| 405 |
Qf::ConfirmIntersection => {} |
| 406 |
Qf::Terminate => {} |
| 407 |
} |
| 408 |
} |
| 409 |
} |