Miscompile: later `break` overrides an earlier one in an unrolled loop (loop exit structurization)
维护者通常 2 天内回复
还没有人认领这个 Issue。
评估
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- C++
- 星标
- 3.7k
- 派生
- 926
- 平均合并
- 3 天 22 小时
- 30 天内合并 PR
- 45
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/DirectXShaderCompiler 的其他 Issue
-
bug needs-triage
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/DirectXShaderCompiler#9173 ·
维护者通常 2 天内回复
-
bug needs-triage
难度 2/5 1-3 小时 新手友好度 65/100
microsoft/DirectXShaderCompiler#9155 · 4 条评论 ·
维护者通常 2 天内回复
-
bug needs-triage
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/DirectXShaderCompiler#9135 ·
维护者通常 2 天内回复
-
DXC emits DW_LANG_C_plus_plus instead of DW_LANG_HLSL for HLSL可能已有人在做 @rj123-nv 于 18 天前认领。 未关闭bug needs-triage
难度 2/5 1-3 小时 新手友好度 75/100
microsoft/DirectXShaderCompiler#9087 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
microsoft/DirectXShaderCompiler#9086 ·
维护者通常 2 天内回复
查看 microsoft/DirectXShaderCompiler 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
-
feature:config kind:docs os:windows toolchain:cmake triaged
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
arch-based bug niri
难度 2/5 1-3 小时 新手友好度 68/100
noctalia-dev/noctalia#5552 ·
维护者通常 1 天内回复