Files
wgpu/naga/tests/out/msl/collatz.msl
Jim Blandy 3fda684eb9 [naga msl-out] Defeat the MSL compiler's infinite loop analysis.
See the comments in the code for details.

This patch emits the definition of the macro only when the first loop
is encountered. This does make that first loop's code look a bit odd:
it would be more natural to define the macro at the top of the
file. (See the modified files in `naga/tests/out/msl`.)

Rejected alternatives:

- We could emit the macro definition unconditionally at the top of the
  file. But this changes every MSL snapshot output file, whereas only
  eight of them actually contain loops.

- We could have the validator flag modules that contain loops. But the
  changes end up being not small, and spread across the validator, so
  this seems disproportionate. If we had other consumers of this
  information, it might make sense.

- We could change the MSL backend to allow text to be generated out of
  order, so that we can decide whether to define the macro after we've
  generated all the function bodies. But at the moment this seems like
  unnecessary complexity, although it might be worth doing in the
  future if we had additional uses for it - say, to conditionally emit
  helper function definitions.

Fixes #4972.
2024-09-18 11:01:51 -04:00

58 lines
1.2 KiB
Plaintext

// language: metal1.0
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct _mslBufferSizes {
uint size0;
};
typedef uint type_1[1];
struct PrimeIndices {
type_1 data;
};
uint collatz_iterations(
uint n_base
) {
uint n = {};
uint i = 0u;
n = n_base;
#define LOOP_IS_REACHABLE if (volatile bool unpredictable_jump_over_loop = true; unpredictable_jump_over_loop)
LOOP_IS_REACHABLE while(true) {
uint _e4 = n;
if (_e4 > 1u) {
} else {
break;
}
{
uint _e7 = n;
if ((_e7 % 2u) == 0u) {
uint _e12 = n;
n = _e12 / 2u;
} else {
uint _e16 = n;
n = (3u * _e16) + 1u;
}
uint _e20 = i;
i = _e20 + 1u;
}
}
uint _e23 = i;
return _e23;
}
struct main_Input {
};
kernel void main_(
metal::uint3 global_id [[thread_position_in_grid]]
, device PrimeIndices& v_indices [[user(fake0)]]
, constant _mslBufferSizes& _buffer_sizes [[user(fake0)]]
) {
uint _e9 = v_indices.data[global_id.x];
uint _e10 = collatz_iterations(_e9);
v_indices.data[global_id.x] = _e10;
return;
}