Why does the shader processor tell me that my loop is not doing anything?

Maybe I’m missing something obvious here, but I have a loop in my shader that the shader processor is warning me about.

while (exit.distance < intersections[i].distance)
{
    intersections[i + 2] = intersections[i];
    i--;
}

In the inspector of this compute shader, I get the following warning:

loop doesn’t seem to do anything, forcing loop to unroll at kernel main

As far as I can tell, that is not true. The loop condition depends on shader input, so I don’t think that the processor is detecting a loop that it thinks will never execute. And I’m clearly modifying array elements in the loop, which I use for the output for the shader. Some debug output directly after the loop verifies to me that the loop is indeed executed and does what it should.

Just as an experiment I tried to add a [loop] attribute, which made the warning message to change:

loop doesn’t seem to do anything, consider removing [loop] infinite loop detected - loop writes no values at kernel main

This isn’t a huge issue, because it’s only a warning and the shader seems to work.

However, a slightly more complicated loop further down in the shader is also flagged with the same error. This time the processor is unable to unroll it and this makes it an error:

force to unroll loop, but unrolling failed at kernel main

Where does the value of “i” come from? That’s important to see what that loop really would end up doing. How is intersections accessed afterwards? Also, what is intersections, anyway? An uniform array, a local variable array, a RWStructuredBuffer, a groupshared array?

The variable i comes from an outer for loop. intersections is a local array of structs.

I think the problem is, that the while loop is not executed for the first iteration of the outer loop. So if the while loop is unrolled, there is going to be one branch that is never executed and I think this may cause the warning, even though other branches would execute just fine. If I explicitly prevent the loop from running in the first iteration by wrapping it with an if, the warning disappears.