Repository navigation
Description
Description
dxc fails to compile the valid shader below at -O1, -O2 and -O3 (the default). The source has no out-of-bounds access: every array index is masked to the array size. The optimizer turns A[i & 3u] into A[5] inside a switch case that can never run, and the validator then rejects that.
It compiles at -Od and with -opt-disable gvn. fxc compiles the same source.
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 A[4] = { a, a + 1u, a + 2u, a + 3u };
uint n = (In[0] & 3u) + 1u; // 1..4, so i is 0..3
for (uint i = 0u; i < n; ++i) {
switch (i & 7u) {
case 1u: A[0] = 9u; break;
case 5u: A[i & 3u] = a; break; // never runs; the index would be 1 anyway
}
}
Out[tid] = A[a & 3u];
}dxc -T cs_6_0 -E main repro.hlsl
Expected: the shader compiles.
Actual Behavior
error: validation errors
repro.hlsl:14:18: error: Access to out-of-bounds memory is disallowed.
note: at '%21 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i32 0, i32 5' in block '#3' of function 'main'.
Validation failed.
The loop in the generated DXIL (-Vd -Fc; %4 is A, %6 is a, comments added):
; <label>:17
%18 = phi i32 [ 0, %0 ], [ %23, %22 ] ; i
switch i32 %18, label %22 [ ; was: switch (i & 7)
i32 1, label %19
i32 5, label %20
]
; <label>:19
store i32 9, i32* %7, align 4
br label %22
; <label>:20 ; case 5: never runs, i <= 3
%21 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i32 0, i32 5 ; was: A[i & 3]
store i32 %6, i32* %21, align 4
br label %22| compile | result |
|---|---|
-Od |
compiles |
-O1, -O2, -O3 (default) |
validation error |
-O3 -opt-disable gvn |
compiles |
-O3 -HV 2018 |
validation error |
-T cs_6_6, -T cs_6_8 |
validation error |
fxc /T cs_5_0 (/Od, /O1, /O3) |
compiles |
Changes to the loop (-O3):
| change | result |
|---|---|
| none (as above) | validation error |
switch (i) instead of switch (i & 7u) |
validation error |
remove case 1u (one case only) |
compiles |
if / else if instead of switch |
compiles |
constant bound n = 4u |
compiles |
n = (In[0] & 7u) + 1u (case 5 can run) |
compiles |
case 2u instead of case 5u |
compiles |
read instead of write in the case (A[0] = A[i & 3u]) |
compiles |
Analysis
n is 1..4, so i <= 3 inside the loop. Three things then happen:
indvarsremoves the masks. Sincei <= 3,i & 7andi & 3are both equal toi, andSimplifyIndvar::eliminateIVUser(SimplifyIndVar.cpp) replaces them withi. The code is nowswitch (i)andA[i].gvnreplacesiwith 5 insidecase 5(GVN.cpp, "For switches, propagate the case values into the case destinations"). The code is nowA[5]. The first IR dump with the index 5 is the one aftergvn.case 5is never removed, althoughi <= 3.EliminateDeadSwitchCases(SimplifyCFG.cpp) only looks at known bits and does not see the loop bound, andCorrelatedValuePropagation::processSwitchreturns early because the condition is defined in the same block as the switch.
The validator (DxilValidation.cpp, InstrInBoundsAccess) then reports the constant index 5. It does not check whether the code can run.
Each pass is correct on its own. The problem is that i <= 3 is used to remove the masks but not to remove case 5. Removing dead switch cases with range information, or a late cleanup that deletes code with a constant out-of-range access, might fix it; I have not tested either.
Environment
- DXC version:
dxcompiler.dll: 1.9(5512-01b62ad4)(1.9.2609.5) - 1.9.2609.5 (01b62ad47)
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity