Miscompile: later `break` overrides an earlier one in an unrolled loop (loop exit structurization)
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 38/100
Research direction
The bug is in lib/Transforms/Scalar/DxilRemoveUnstructuredLoopExits.cpp, in the guarding logic that uses HasSideEffects(bb) to decide which blocks between an exit and the latch get wrapped. Reproduce with the HLSL in the issue: -O3 gives Out is 2 for In=0, while -Od and -opt-disable structurize-loop-exits-for-unroll give 7. Done means the -O3 output matches -Od for In=0 and 5, and the fix does not break existing loop-exit tests.
Written by the indexing model from the issue text.
Description
Description
When dxc unrolls a loop that has two breaks nested inside a conditional, a shader invocation that leaves through
the first break gets the value that belongs to the second one. No diagnostic (silent miscompilation) and the generated DXIL passes validation.
-opt-disable structurize-loop-exits-for-unroll gives correct code, and fxc compiles the same source correctly.
Steps to Reproduce
StructuredBuffer<uint> In : register(t0);
RWStructuredBuffer<uint> Out : register(u0);
[numthreads(64, 1, 1)]
void main(uint3 dtid : SV_DispatchThreadID)
{
uint tid = dtid.x;
uint a = In[tid];
uint u = In[63];
uint c = 4u; // (c & 3u) < 2u is true on the first iteration
uint r = 7u;
[unroll] for (uint i = 0u; i < 3u; ++i) {
[branch] if (a != 14u) {
if (a == 0u) { break; } // exit #1
r = 2u; // not reached when exit #1 is taken
if ((c & 3u) < 2u) { break; } // exit #2
}
c = u;
}
Out[tid] = r;
}
dxc -T cs_6_0 -E main repro.hlsl -Fc out.ll
Expected: Out[tid] is 7 when In[tid] is 0 or 14, and 2 otherwise.
Actual Behavior
Out[tid] is 2 when In[tid] is 0. First unrolled iteration of the -O3 DXIL (%5 is In[tid], comments added):
; <label>:9
%10 = icmp eq i32 %5, 0 ; exit #1 condition
br label %11
; <label>:11
%12 = phi i1 [ true, %9 ], [ false, %0 ] ; "exit #2 taken"
%13 = phi i32 [ 2, %9 ], [ 0, %0 ] ; r at exit #2
%14 = phi i1 [ %10, %9 ], [ false, %0 ] ; "exit #1 taken"
%15 = phi i32 [ 7, %9 ], [ 0, %0 ] ; r at exit #1
%17 = or i1 %12, %14
%18 = select i1 %12, i32 %13, i32 %15 ; exit #2's value wins whenever its flag is set
br i1 %17, label %40, label %19 ; %40 stores %18 to Out[tid]
Exit #2's flag is set for invocations that already took exit #1, and it is tested first.
Run with In = 0, 5, 0, 5, ...; WARP and an NVIDIA GPU give the same results:
| compile | In[tid] == 0 |
In[tid] == 5 |
|---|---|---|
-Od |
7 | 2 |
-O3 (default) |
2 | 2 |
-O3 -opt-disable structurize-loop-exits-for-unroll |
7 | 2 |
fxc /T cs_5_0 |
7 | 2 |
It needs all of the following:
- the loop is unrolled (
[unroll], or no attribute;[loop]is correct); - both
breaks are nested in a conditional (directly in the loop body is correct); - no memory access between them (a buffer read in exit #2's condition is correct);
- both exit conditions are true for the same invocation in the same iteration.
The constants do not matter; run-time values fail the same way. -Od is also wrong when the loop sits in a helper
function. The same function compiles to the same loop logic for ps_6_0, vs_6_0, gs_6_0 and lib_6_3.
Analysis (lib/Transforms/Scalar/DxilRemoveUnstructuredLoopExits.cpp)
When exit #1 is converted to a flag, the blocks between it and the latch are only guarded if HasSideEffects(bb),
which checks mayReadOrWriteMemory(). The block holding exit #2 has no memory access, so it stays unguarded and
invocations whose exit #1 flag is set still run its exit branch. The file header says leaving such code
unconditional "doesn't impact code correctness"; that does not hold when the block is itself an exiting block.
Also guarding a traversed block when L->isLoopExiting(bb) might fix it; I have not tested that.
Environment
- DXC version:
dxcompiler.dll: 1.9(5512-01b62ad4)(1.9.2609.5) - 1.9.2609.5 (01b62ad47); also 1.10.2605.37 and 1.8.2502.11
Found during compiler testing. An AI assistant was used for the reduction, the analysis and for drafting this text;
I reviewed all of it and reproduced the results.
- Dominant language
- C++
- Stars
- 3.7k
- Forks
- 926
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 45
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/DirectXShaderCompiler
-
bug needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/DirectXShaderCompiler#9173 ·
Maintainers usually reply within 2 days
-
bug needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
microsoft/DirectXShaderCompiler#9155 · 4 comments ·
Maintainers usually reply within 2 days
-
bug needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/DirectXShaderCompiler#9135 ·
Maintainers usually reply within 2 days
-
DXC emits DW_LANG_C_plus_plus instead of DW_LANG_HLSL for HLSLPossibly taken @rj123-nv claimed this 18 days ago. Openbug needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
microsoft/DirectXShaderCompiler#9087 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
microsoft/DirectXShaderCompiler#9086 ·
Maintainers usually reply within 2 days
All issues in microsoft/DirectXShaderCompiler
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
table_translator 当一个 Prism 键映射到多个原码时,首个候选未按权重排序Possibly taken A pull request linked to this issue is open or already merged. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
feature:config kind:docs os:windows toolchain:cmake triaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
arch-based bug niri
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
noctalia-dev/noctalia#5552 ·
Maintainers usually reply within 1 day