Unity detects infinite loop where there is none (Compute Shader)

When I try to compile my compute shader, it fails to compile and yields the following error message:

Shader error in ‘AppendTest’: infinite loop detected - loop never exits at kernel CSMain at AppendTest.compute(355) (on d3d11)

Nothing I’m doing here should cause it to detect an infinite loop, as the condition is met after 5 successful iterations. The actual delcaration is simply for (uint i = 0; i < 16; i += 3) but if you want to see the whole loop, here it is:

(I’m still pretty new to compute shaders, so apologies if my code is bad. Just trying to get this working, and then I’ll improve/optimize it. Just putting this so I don’t get unsolicited comments on the quality of my approach!)

    for (uint i = 0; i < 16; i += 3)
    {
        Triangle toAppend;

        uint edge1ID[2] = EdgeMap[edgesToConnect[i]];
        uint edge2ID[2] = EdgeMap[edgesToConnect[i + 1]];
        uint edge3ID[2] = EdgeMap[edgesToConnect[i + 2]];

        toAppend.v1 = vertexInterp(blfPos, edge1ID[0] + blf, edge1ID[1] + blf);
        toAppend.v2 = vertexInterp(blfPos, edge2ID[0] + blf, edge2ID[1] + blf);
        toAppend.v3 = vertexInterp(blfPos, edge3ID[0] + blf, edge3ID[1] + blf);

        //tris.Append(toAppend);  Commented out because it yields a different confusing error, but that's for another thread.
    }

(apologies for flagging this as CSharp code; it won’t let me select HLSL)

Strangely, when I add an if statement which can break the loop, it doesn’t encounter the error anymore. Very confused and haven’t been able to find anything online. Would appreciate some help. Thanks!

    for (uint i = 0; i < 16; i += 3)
    {
        Triangle toAppend;

        // And this if statement makes it compile without any errors.
        if (edgesToConnect[i] == -1)
            break;

        uint edge1ID[2] = EdgeMap[edgesToConnect[i]];
        uint edge2ID[2] = EdgeMap[edgesToConnect[i + 1]];
        uint edge3ID[2] = EdgeMap[edgesToConnect[i + 2]];

        toAppend.v1 = vertexInterp(blfPos, edge1ID[0] + blf, edge1ID[1] + blf);
        toAppend.v2 = vertexInterp(blfPos, edge2ID[0] + blf, edge2ID[1] + blf);
        toAppend.v3 = vertexInterp(blfPos, edge3ID[0] + blf, edge3ID[1] + blf);

        //tris.Append(toAppend);
    }

Hello,
Does the shader compile with for (uint i = 0; i < 16; i++)?

How about looping with a helper variable like

for (uint  j = 0; j <6;  j++)
{
   i=j*3;
   ...

(Not sure though if multiplying will save many more cpu cycles than a if statement.)

I have also encountered similar behavior. If I have the following for loop do more than 4 iterations, the editor (seemingly incorrectly) reports that the loop is infinite:

for (int index = 0; index < 4; index++)
{ 
    lights[index].position = float3(unity_4LightPosX0[index],
        unity_4LightPosY0[index],
        unity_4LightPosZ0[index]);
    lights[index].intensity = unity_LightColor[index].rgb;
    lights[index].size = 1;
}

Unlike OP’s post, inserting a break line does not resolve this error. All I’m doing is using the data from Unity’s lights to initialize the first x elements in a struct array.

bumping, also getting this error. I also solved this by inserting a condition that checks if the iterator is equal to -1, then breaking/returning out of the loop