Skip to content

Valid shader rejected at -O1..-O3: Access to out-of-bounds memory is disallowed (index created by the optimizer in a dead switch case) #9125

Open

Description

@ubmaxxer

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:

  1. indvars removes the masks. Since i <= 3, i & 7 and i & 3 are both equal to i, and SimplifyIndvar::eliminateIVUser (SimplifyIndVar.cpp) replaces them with i. The code is now switch (i) and A[i].
  2. gvn replaces i with 5 inside case 5 (GVN.cpp, "For switches, propagate the case values into the case destinations"). The code is now A[5]. The first IR dump with the index 5 is the one after gvn.
  3. case 5 is never removed, although i <= 3. EliminateDeadSwitchCases (SimplifyCFG.cpp) only looks at known bits and does not see the loop bound, and CorrelatedValuePropagation::processSwitch returns 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.

Activity

  1. added
    bug
    needs-triage
    on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds-triage

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions