By "variable invalidation issue" I gather you mean "poor performance with continue, because the compiler can't tell that the iteration block is executed on all branches"?
No, I mean failure to unroll shaders with conditional jumps as the the value of `i` will not be propagated forwards beyond the second iteration. Here's a example, using the shader from [here](https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/748#note_66697):
1st iteration --- ```hlsl i = 0; broken = false; if (!broken) { if (!(i < 100)) broken = true; if (!b[i]) broken = true; } ```
After expr folding, copy prop, trivial if simplification (i.e `transform_run_const_passes`): ```hlsl i = 0; broken = false; if (!b[0]) broken = true; ```
2nd iteration --- ```hlsl i = 0; broken = false; if (!b[0]) broken = true; if (!broken) { ++i; if (!(i < 100)) broken = true; if (!b[i]) broken = true; } ```
Simplifying: ```hlsl i = 0; broken = false; if (!b[0]) broken = true; if (!broken) { ++i; if (!b[1]) broken = true; } ```
Things are still fine so far.
3rd iteration --- This is where stuff breaks: ```hlsl i = 0; broken = false; if (!b[0]) broken = true; if (!broken) { ++i; /* this invalidates i in the parent block. */ if (!b[1]) broken = true; } if (!broken) { /* i is invalid here */ ++i; if (!(i < 100)) broken = true; if (!b[i]) broken = true; } ```
Since `i` is invalid, nothing really simplifies and we can't determine statically if the loop breaks before `max_iterations`.
---
How bad is the performance penalty from visiting n² "if" blocks? Have you benchmarked it? I suspect that we should try to find a way to optimize copyprop independently of unrolling, if it's a problem.
Using the shader from [tests/hlsl/switch.shader_test:545](https://gitlab.winehq.org/wine/vkd3d/-/blob/master/tests/hlsl/switch.shader_...), which is probably the worst case in the vkd3d test suite:
267582ff7cbb43a7fee2b354c57cff7495f9267f (this MR): ``` $ time ./vkd3d-compiler -p ps_4_0 -x hlsl -b dxbc-tpf bad.hlsl -o out bad.hlsl:14:1: W5306: Unable to unroll loop, maximum iterations reached (1024). bad.hlsl:9:1: W5306: Unable to unroll loop, maximum iterations reached (1024).
real 0m0.403s user 0m0.387s sys 0m0.019s ```
[302834aa0be75d105e7f17487b6c475812fe8ec0](https://gitlab.winehq.org/vitorhnn/vkd3d/-/commit/302834aa0be75d105e7f17487b...) (Nesting ifs, passes all tests): ``` $ time ./vkd3d-compiler -p ps_4_0 -x hlsl -b dxbc-tpf bad.hlsl -o out
real 4m44.458s user 4m44.075s sys 0m0.018s ```
Interestingly, the native compiler also struggles to unroll this shader's outer loop (but only if it's forced to by the `[unroll]` attribute, otherwise it seems to bail out early)