gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.72k stars 933 forks source link

[naga msl-out] Avoid UB by making all loops bounded. #6545

Closed jimblandy closed 3 days ago

jimblandy commented 1 week ago

In MSL output, avoid undefined behavior due to unbounded loops by adding an unpredictable, never-actually-taken break to the bottom of each loop body, rather than adding an unpredictable, never-actually-taken branch over each loop.

This will probably have more of a performance impact, because it affects each iteration of the loop, but unlike branching over the loop, which leaves infinite loops (and thus undefined behavior) in the output, this actually ensures that no loop presented to Metal is unbounded, so that there is no undefined behavior present that the optimizer could use to make unwelcome inferences.

Fixes #6528.

JMS55 commented 1 week ago

Is this something we can turn off via wgpu/naga for native platforms?

cwfitzgerald commented 1 week ago

Yes, see https://github.com/gfx-rs/wgpu/pull/6520